//the requests
reqs = new Array();

//single request
function AjaxRequest(req, func, id) {
    this.req = req;
    this.func = func;
    this.id = id;
}


function execRequests() {
    for (var i = 0; i < reqs.length; i++) {        
        if (reqs[i].req.readyState == 4 && (reqs[i].req.status == 200 || reqs[i].req.status == 304)) {
            reqs[i].func(reqs[i].req.responseText, reqs[i].id);
            reqs[i].req = null;
            reqs.splice(i,1);                            
        }
    }
}

//ajax constructor
function Ajax() {
}

Ajax.prototype.createRequest = function() {
    try{
    // Opera 8.0+, Firefox, Safari
    return new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer Browsers
        try{
            return ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                // Something went wrong
                alert("Your browser does not support Ajax. Get FireFox! www.mozilla.com");
                return false;
            }
        }
    }    
}


Ajax.prototype._rndsid = function(url) {
    if (url.indexOf('?') == -1) return url + "?___ajaxsid=" + Math.random();
    return url + "&___ajaxsid=" + Math.random();
}

Ajax.prototype.doreq = function(method, url, para, func, id, timer) {
    var request = this.createRequest();
    
    if (!request) return false;

    request.onreadystatechange = execRequests;

    request.open(method, this._rndsid(url), true);
    if (method == "POST") {
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.setRequestHeader("Content-length", para.length);
        request.setRequestHeader("Connection", "close");
        request.send(para);
    } else {
        request.send(null);
    }
    reqs.push(new AjaxRequest(request, func, id));
    if (timer) {
        var _this = this;
        setTimeout(function(){_this.doreq(method, url, para, func, id, timer);}, timer);    
    }

}

Ajax.prototype.get = function(url, func, id, timer) {
    this.doreq("GET", url, "", func, id, timer);
    
}

Ajax.prototype.post = function(url, para, func, id, timer) {
    this.doreq("POST", url, para, func, id, timer);
}

//fill
Ajax.prototype.doFill = function(res, id) {
    document.getElementById(id).innerHTML = res;
    var js = new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 'img');
    var matches = res.match(js);
    if (matches)
        for (i in matches)
            eval(matches[i].replace(/<script.*?>/g, "").replace(/<\/script>/g, ""));    
}

Ajax.prototype.getFill = function(url, id, timer) {
    this.get(url, this.doFill, id, timer);    
}

Ajax.prototype.postFill = function(url, para, id, timer) {
    this.post(url, para, this.doFill, id, timer);
}

//eval
Ajax.prototype.doEval = function(res) {
    eval(res);
}

Ajax.prototype.getEval = function(url) {
    this.get(url, this.doEval);
}

Ajax.prototype.postEval = function(url, para) {
    this.post(url, para, this.doEval);
}

ajax = new Ajax();


