﻿function AjaxThread() {
    this.Request = false;
    this.Url = "/kajax.ashx";
    this.Action = "";
    this.Method="";
    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() {
    this.Url += "?method=" + this.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) {
            cd.alert(e.responseText);
            return;
        }
        try {
            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.Method=data.cmdName;
    ajax.Data="web=cc720";
    for(var i=0;i<data.cmdParm.length;i++){
        ajax.Data+="&"+data.cmdParm[i][0]+"="+escape(JSON.stringify(data.cmdParm[i][1]));    
    }
    ajax.Send();
}

///<summary>JsonCmd对象</summary>
///<field name="cmdName">命令名称</field>
///<field name="Parms">参数数组集合</field>
function JsonCmd() {
    this.cmdName = "";
    this.cmdParm = [];
}
