//based on prototype's ajax class
//to be used with prototype.lite, moofx.mad4milk.net.

ScriptFragment='(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)';
function extractScripts(htmlText) {
    var matchAll = new RegExp(ScriptFragment, 'img');
    var matchOne = new RegExp(ScriptFragment, 'im');
    var temp1= htmlText.match(matchAll);
    if (!temp1) {
    	temp1= new Array();
    }
    var res=new Array();
    var j=0;
    for (var i=0;i<temp1.length;i++)
    	res.unshift(eval(temp1[i].match(matchOne)[1]));
    return res;
}
function evalScripts(htmlText) {
    var temp1=extractScripts(htmlText);
}
ajax = Class.create();
ajax.prototype = {
    initialize: function(url, options){
        this.transport = this.getTransport();
        this.postBody = options.postBody || '';
        this.evalScripts = options.evalScripts || '';
        this.method = options.method || 'post';
        this.onComplete = options.onComplete || null;
        this.update = $(options.update) || null;
        this.request(url);
    },

    request: function(url){
        this.transport.open(this.method, url, true);
        this.transport.onreadystatechange = this.onStateChange.bind(this);
        if (this.method == 'post') {
            this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
        }
//		var response = this.transport.responseText;
//		response = stripScripts(response);
        this.transport.send(this.postBody);
    },

    onStateChange: function(){
        if (this.transport.readyState == 4 && this.transport.status == 200) {
            if (this.onComplete)
                setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
            if (this.update)
                setTimeout(function(){
                	this.update.innerHTML = this.transport.responseText;
                }.bind(this), 10);
            this.transport.onreadystatechange = function(){};
        }
    },

    getTransport: function() {
        if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
        else if (window.XMLHttpRequest) return new XMLHttpRequest();
        else return false;
    }
};
myajax = Class.create();
myajax.prototype = {
    initialize: function(url, options){
        this.transport = this.getTransport();
        this.postBody = options.postBody || '';
        this.evalScripts = options.evalScripts || '';
        this.method = options.method || 'post';
        this.onComplete = options.onComplete || null;
        this.update = $(options.update) || null;
        this.request(url);
    },

    request: function(url){
        this.transport.open(this.method, url, true);
        this.transport.onreadystatechange = this.onStateChange.bind(this);
        if (this.method == 'post') {
            this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
        }
//		var response = this.transport.responseText;
//		response = stripScripts(response);
        this.transport.send(this.postBody);
    },

    onStateChange: function(){
        if (this.transport.readyState == 4 && this.transport.status == 200) {
            if (this.onComplete)
                setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
            if (this.update)
                setTimeout(function(){
                	this.update.innerHTML = this.transport.responseText;
                	if (this.evalScripts) {
                		evalScripts(this.transport.responseText);
                	}
                }.bind(this), 10);
            this.transport.onreadystatechange = function(){};
        }
    },

    getTransport: function() {
        if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
        else if (window.XMLHttpRequest) return new XMLHttpRequest();
        else return false;
    }
};