function AjaxParameter(name, value) {
    this.name = name;
    this.value = value;
}
function AjaxRequest(name, url, onErrorFunction, onSuccessFunction, action) {
    this.name = name;
    this.url = url;
    this.onErrorFunction = onErrorFunction;
    this.onSuccessFunction = onSuccessFunction;
    this.isComplete = false;
    this.generateBoundary = function() {
        var t = "qwertyuiopasdfghjklzxcvbnm1234567890";
        var ret = "----";
        for (var i = 0; i < 10; i++) {
            var c = Math.round(Math.random() * t.length);
            ret += t.charAt(c);
        }
        ;
        ret += "---";
        return ret;
    };
    this.boundary = this.generateBoundary();
    this.isErrorResponse = false;
    this.action = action;
    this.sendParameters = new Array();
    this.addSendParameters = function(name, value) {
        this.sendParameters[this.sendParameters.length] = new AjaxParameter(name, value);
    };
    this.createSendText = function() {
        var ret = "";
        for (var i = 0; i < this.sendParameters.length; i++) {
            ret += encodeURIComponent(this.sendParameters[i].name) + "=";
            ret += encodeURIComponent(this.sendParameters[i].value) + "&";
        }
        return ret;
    };
    if (this.url.indexOf("?") == -1) {
        this.url += "?action=" + this.action;
    } else {
        this.url += "&action=" + this.action;
    }
    ;
    this.url += "&boundary=" + this.boundary;
    this.url += "&rnd=" + Math.random();
    this.init = function() {
        var st = this.createSendText();
        ajaxTransport.send(st);
    };
    this.parameters = new Array();
    this.getParameter = function(name) {
        for (var i = 0; i < this.parameters.length; i++) {
            if (this.parameters[i].name == name) {
                return this.parameters[i].value;
            }
        }
        return null;
    };

    this.getParameterValues = function(name) {
        var ret = null;
        for (var i = 0; i < this.parameters.length; i++) {
            if (this.parameters[i].name == name) {
                if (ret == null){
                    ret = new Array();
                }
                ret[ret.length] = this.parameters[i].value;
            }
        }
        return ret;
    };
    this.getParameterNames = function(){
        var ret = new Array();
        for (var i = 0; i < this.parameters.length; i++) {
            ret[ret.length] = this.parameters[i].name;
        }
        return ret;
    }
    this.complete = function() {
        this.isComplete = true;
        var t = ajaxTransport.responseText;
        if (t == null) {
            return;
        }
        if (t.trim().replace("\n", "") == "ERROR") {
            this.isErrorResponse = true;
        }
        var tt = t.split(this.boundary + "\n");
        for (var i = 0; i < tt.length; i++) {
            if (tt[i].isEmpty()) {
                continue;
            }
            var name = tt[i].substring(0, tt[i].indexOf("\n"));
            var value = tt[i].substring(name.length + 1, tt[i].length - 1);
            this.parameters[this.parameters.length] = new AjaxParameter(name, value);
        }
    };
    this.isError = function(){
        return this.getParameter("error")!=null;
    };
    this.showError = function(){
        alert(this.getParameter("errorMessage"));
        if (document.location.hostname.indexOf("xplorex.dev")!=-1 && this.getParameter("debugError")!=null){
            alert(this.getParameter("debugError"));
        }
    };
}
function AjaxObject() {
    this.browser = {IE:!!(window.attachEvent && !window.opera),Opera:!!window.opera,WebKit:navigator.userAgent.indexOf('AppleWebKit/') > -1,Gecko:navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,MobileSafari:!!navigator.userAgent.match(/Apple.*Mobile.*Safari/)}
    this.request = null;
    this.createTransport = function() {
        var xmlHttp;
        try {
            xmlHttp = new XMLHttpRequest();
        } catch(e) {
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
            }
        }
        return xmlHttp;
    };
    this.addRequest = function(req) {
        if (this.request != null && !this.request.isComplete) {
            //alert("Please wait");
            return false;
        }
        if (ajaxTransport == null) {
            ajaxTransport = this.createTransport();
        }
        ajaxTransport.open("POST", req.url, true);
        ajaxTransport.onreadystatechange = function() {
            if (ajaxTransport.readyState == 4) {
                var req = ajax.request;
                if (req != null) {
                    if (ajaxTransport.status) {
                        if (ajaxTransport.status != 200) {
                            req.isErrorResponse = true;
                        }
                    }
                    if (!req.isErrorResponse) {
                        req.complete();
                    }
                    if (!req.isErrorResponse) {
                        if (req.onSuccessFunction != null) {
                            req.onSuccessFunction();
                        }
                    } else {
                        if (req.onErrorFunction != null) {
                            req.onErrorFunction();
                        }
                    }
                }

                ajax.deleteRequest(req.name);

            }
        };
        ajaxTransport.setRequestHeader("nameRequest", req.name);
        ajaxTransport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        this.request = req;
        return true;
    };
    this.deleteRequest = function(reqName) {
        this.request = null;
    };
    this.sendConnectGET = function(url,readyFunction){
        var transport = this.createTransport();
        transport.open("POST", url,true);
        transport.onreadystatechange = readyFunction;
        transport.send();
    }
}
var ajax = new AjaxObject();
var ajaxTransport = null;
