﻿function AjaxThread() {
    this.Request = false;
    this.Url = "/kajax.ashx";
    this.Action = "";
    this.Data = "";
    this.ErrorMethod = "网络故障，请刷新整个页面";
    this.Init();
}

AjaxThread.prototype.Init = function() {
    try { this.Request = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
        try { this.Request = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e2) { this.Request = false; }
    }
    if (!this.Request && typeof XMLHttpRequest != 'undefined')
    { this.Request = new XMLHttpRequest(); }
}

AjaxThread.prototype.Send = function() {
    var method = "NoName";
    try {
        method = "";
        var arySplit = unescape(this.Data).split("cmdName");
        for (var i = 1; i < arySplit.length; i++) {
            var regExp = /(.*)cmdName[\\]{0,1}\"\:[\\]{0,1}\"([^\\\"|^\"]*)[\\]{0,1}\"(.*)/g;
            method += i == 1 ? "" : "_";
            //alert(unescape(this.Data) + "\n\n" + arySplit[i] + "\n" + ("cmdName" + arySplit[i]).replace(regExp, "$2"))
            method += ("cmdName" + arySplit[i]).replace(regExp, "$2");
        }
    } catch (e) { method = "NoName"; }

    this.Url += "?method=" + method + "&randomStr=" + Math.random();
    var e = this.Request;
    var obj = this;
    var errMethod = this.ErrorMethod;
    this.Request.open("POST", this.Url, true);
    this.Request.onreadystatechange = function() {
        if (e.readyState != 4) { return; }
        if (e.status != 200) {
            //            alert("网络故障，请刷新整个页面");
            //            document.write(e.responseText);
            //            alert(e.responseText);
            return;
        }
        try {
            //alert(e.responseText);
            eval(e.responseText);
            e = null;
            obj = null;
        }
        catch (err) {
            e = null;
            obj = null;
        }
    }
    
    this.Request.setRequestHeader("content-length", this.Data.length);
    this.Request.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    this.Request.send(this.Data);
}

function SendToServer(data, errorMethod) {
    var ajax = new AjaxThread();
    ajax.Data = "data=" + escape(data);
    ajax.Send();
}

///<summary>JsonCmd对象</summary>
///<field name="cmdName">命令名称</field>
///<field name="Parms">参数数组集合</field>
function JsonCmd() {
    this.cmdName = "";
    this.cmdParm = [];
}

JsonCmd.prototype = {
    cmdName: "",
    cmdParm: [],
    toString: function() {
        ///<summary>JSON序列化</summary>
        return JSON.stringify(this);
    }
}
