//WebUtils Ajax

function AjaxBase(method, async) {
    this.__async = async;
    this.__method = method;
    this.__objXmlHttp = null;
    this.__callBackFunction = null;
    this.__params = "";
    
    try {
        this.__objXmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        try {
            this.__objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                this.__objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {}
        }
    }
    
    if(!this.__objXmlHttp) {
        alert("Ajax Object could not be created.");
    }
}

AjaxBase.prototype.SetCallBackFunction = function(value) {
    this.__callBackFunction = value;
}

AjaxBase.prototype.PrepareCallBackHandler = function() {
    if(this.__callBackFunction != null && this.__callBackFunction != undefined) {
        var _this = this;
        this.__objXmlHttp.onreadystatechange = function() { _this.Handle_CallBack(); };
    }
}

AjaxBase.prototype.Open = function() {
    this.__objXmlHttp.open(this.__method, "WebUtilsAjax/AjaxProcessor.ashx", this.__async);
}

AjaxBase.prototype.SetRequestHeader = function(key, value) {
    this.__objXmlHttp.setRequestHeader(key, value);
}

AjaxBase.prototype.SetParamsString = function(value) {
    this.__params = value;
}

AjaxBase.prototype.Send = function() {
    this.__objXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.__objXmlHttp.setRequestHeader("Content-length", this.__params.length);
    this.__objXmlHttp.setRequestHeader("Connection", "close");
    this.__objXmlHttp.send(this.__params);
}

AjaxBase.prototype.Handle_CallBack = function() {
    if(this.__objXmlHttp.readyState == 4 || this.__objXmlHttp.readyState == "complete") {
        if(this.__objXmlHttp.status == 200) {
            var responseType = this.__objXmlHttp.getResponseHeader("Content-Type");
            var objAjaxResult = new AjaxResult();
            switch(responseType) {
                case "text/xml":
                    if(this.__objXmlHttp.responseXML.xml) {
                        objAjaxResult.SetValue(this.__objXmlHttp.responseXML.xml);
                    }
                    else {
                        var serializer = new XMLSerializer();
                        objAjaxResult.SetValue(serializer.serializeToString(this.__objXmlHttp.responseXML));
                        serializer = null;
                    }
                    break;
                default:
                    objAjaxResult.SetValue(this.__objXmlHttp.responseText);
                    break;
            }
            this.__callBackFunction.call(this, objAjaxResult);
        }
        else {
            var status = "An error occurred while retrieving data from the server.\r\n"
                + "The servers' response was: "
                + this.__objXmlHttp.status + " - " + this.__objXmlHttp.statusText + ".";
            
            alert(status);
        }
    }
}