﻿var Float = true;
var XMLhttp = null;
AjaxPro.prototype.AjaxShowDiv = "";
AjaxPro.prototype.AjaxReturnType = true;
AjaxPro.prototype.AjaxCommand = null;

function AjaxPro() {
    if (XMLhttp == null) {
        XMLhttp = this.Init();
    }

}
AjaxPro.prototype.Float = true;
AjaxPro.prototype.$ = function (id) { return document.getElementById(id); }
AjaxPro.prototype.Init = function () {
    var xmlhttp;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        }
        catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
AjaxPro.prototype.Load = function (method, HttpUrl, value) {
    if (value == null) { }
    XMLhttp.open(method, HttpUrl, Float);
    XMLhttp.onreadystatechange = this.Process;
    var https = null;
    if (method == "Post") {
        XMLhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        https = value;
    }
    XMLhttp.send(https);
}
AjaxPro.prototype.Process = function () {
    if (XMLhttp.readyState == 4) {
        if (XMLhttp.status == 200) {
            var ReturnValue = XMLhttp.responseText;

            if (!Ajax.AjaxReturnType) {
                var ShowDiv = Ajax.$(Ajax.AjaxShowDiv);
                ShowDiv.innerHTML = ReturnValue

            } else {
                eval(Ajax.AjaxCommand(ReturnValue));
            }

        } else {
            if (!Ajax.AjaxReturnType) {
                var ReturnValue = "HTTP 错误，状态码：" + XMLhttp.status;
                var ShowDiv = Ajax.$(Ajax.AjaxShowDiv);
                ShowDiv.innerHTML = ReturnValue;
            } else {
                var ShowDiv = Ajax.$(Ajax.AjaxShowDiv);
                ShowDiv.innerHTML = XMLhttp.status;
            }
        }
    }

}
AjaxPro.prototype.Get = function (HttpUrl) {
    if (HttpUrl.indexOf("?") == -1) {
        this.Load("Get", HttpUrl + "?rndmath=" + Math.floor(Math.random() * 10));
    } else {
        this.Load("Get", HttpUrl + "&rndmath=" + Math.floor(Math.random() * 10));
    }
}

AjaxPro.prototype.Post = function (HttpUrl, value) {
    this.Load("Post", HttpUrl, value);
    return 1;
}
var Ajax = new AjaxPro();
