var sUrlBase = '/WebServices/';
var sNamespace = 'http://www.ce.net.cn';

/* httpxml */
function XmlHttpRequest(){
    var oXmlHttp = null;
	if(window.XMLHttpRequest){
	    oXmlHttp = new XMLHttpRequest();
	}else{
	    try{
	        oXmlHttp = new ActiveXObject('MSXML2.XMLHTTP.4.0');
	    }catch(e){
	        try{
	            oXmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
	        }catch(e){
	            try{
	                oXmlHttp = new ActiveXObject('MSXML2.XMLHTTP.2.6');
	            }catch(e){
	                try{
	                    oXmlHttp = new ActiveXObject('MSXML2.XMLHTTP');
	                }catch(e){
	                    try{
	                        oXmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	                    }catch(e){}
	                }
	            }
	        }
	    }
	}
    if(oXmlHttp == null){
        alert('The browser does not surport XMLHTTP.');
    }
    return oXmlHttp;
}

function HttpUrlCommand(url, sync, request){
    var oXmlHttp = XmlHttpRequest();
    var issync = !RemoteIsNull(sync);
    var doc = RemoteIsNull(request) ? '' : request;
    
    oXmlHttp.open('POST', url +'&token='+ token(), issync);
    oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oXmlHttp.send(doc);
    
    if(issync){
	    oXmlHttp.onreadystatechange = readyStateChanged;
	}else{
	    return oXmlHttp.responseText;
	}
	
    function token(){
	    var hex = new Array('0','1','2','3','4','5','6','7','8', '9','a','b','c','d','e','f');
	    var t = '';
	    for (var i = 0; i<32; i++){
		    t += hex[Math.floor(Math.random() * 16)];
	    }
	    return t.toUpperCase();
    }
    
    function readyStateChanged(){
        if (oXmlHttp.readyState == 4)
        {
            if (oXmlHttp.status != 0)
            {
                var result = oXmlHttp.responseText;
                sync(result);

                oXmlHttp.abort();
            }
        }
    }
}

/* no edit below */
function RemoteCommand(sObject, sCommand)
{
    this.Command = sCommand;
    this.GetParameter = getParameter;

    var sXmlDocumentStart = "";
    var sXmlDocumentEnd = "";
    var oXmlHttp = XmlHttpRequest();
    var aParameters = new Array();
    var sUrl = sUrlBase + sObject + ".asmx";

    sXmlDocumentStart += "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
    sXmlDocumentStart += "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
    sXmlDocumentStart += "<soap:Body>"
    sXmlDocumentStart += "<" + sCommand + " xmlns=\"" + sNamespace + "/\">"

    sXmlDocumentEnd += "</" + sCommand + ">";
    sXmlDocumentEnd += "</soap:Body>";
    sXmlDocumentEnd += "</soap:Envelope>";

    this.Execute = execute;
    this.SetParameter = setParameter;
    this.Abort = abort;

    function getParameter(sName)
    {
        var i = aParameters[sName];
        if (!RemoteIsNull(i))
        {
            return aParameters[i];
        }
        return null;
    }

    function setParameter(sName, sValue)
    {
        var oParameter = (RemoteIsNull(sValue))? sName : new CommandParameter(sName, sValue);
        if (!RemoteIsNull(aParameters[oParameter.Name]))
        {
            aParameters[aParameters[oParameter.Name]] = oParameter;
        }
        else
        {
            aParameters[oParameter.Name] = aParameters.length;
            aParameters.push(oParameter);
        }
    }

    function abort()
    {
        oXmlHttp.abort();
    }

    function execute(funAsyncCallbackFunction)
    {
        var sXmlDocument = sXmlDocumentStart;

        if (oXmlHttp.readyState > 0 && oXmlHttp.readyState < 4)
        {
            abort();
        }

        for (var i = 0; i < aParameters.length; i++)
        {
            sXmlDocument += aParameters[i].GetXml();
        }
        sXmlDocument += sXmlDocumentEnd;
        var async = !RemoteIsNull(funAsyncCallbackFunction);
        if (async)
        {
            var oAsyncResultHandler = new AsyncResultHandler(this, oXmlHttp, funAsyncCallbackFunction);
            oXmlHttp.onreadystatechange = oAsyncResultHandler.ReadyStateChanged;
        }

        oXmlHttp.open("POST", sUrl, async);
        oXmlHttp.setRequestHeader("SOAPAction", sNamespace + "/" + sCommand);
        oXmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        oXmlHttp.setRequestHeader("Content-Length", sXmlDocument.length);
        oXmlHttp.send(sXmlDocument);
        
        if (!async)
        {
            return new RemoteCommandResult(this, oXmlHttp);
        }
    }
}

function AsyncResultHandler(oRemoteCommand, oXmlHttp, funCallback)
{
    this.ReadyStateChanged = readyStateChanged;
    function readyStateChanged()
    {
        if (oXmlHttp.readyState == 4)
        {
            if (oXmlHttp.status != 0)
            {
                var result = new RemoteCommandResult(oRemoteCommand, oXmlHttp);
                funCallback(result);

                oXmlHttp.abort();
            }
        }
    }
}

function CommandParameter(sName, sValue)
{
    this.Name = sName;
    this.Value = HtmlEncode(sValue.toString());
    this.GetXml = getParameterXml;

    function getParameterXml()
    {
        var sXml = "<" + this.Name + ">";
        if (RemoteIsArray(this.Value))
        {
            var sType = this.Value.type;
            if (RemoteIsNull(sType) && this.Value.length > 0)
            {
                sType = "object";
            }
            for (var i = 0; i < this.Value.length; i++)
            {
                sXml += "<" + sType + ">" + this.Value[i] + "</" + sType + ">";
            }
        }
        else
        {
            sXml += this.Value;
        }
        
        sXml += "</" + this.Name + ">";
        return sXml;
    }
    
    function HtmlEncode(text)
    {
	    text = text.replace(/&/g, "&amp;");
	    text = text.replace(/"/g, "&quot;");
	    text = text.replace(/</g, "&lt;");
	    text = text.replace(/>/g, "&gt;");
	    text = text.replace(/'/g, "&#39;");
	    return text ;
    }
}

function RemoteCommandResult(oRemoteCommand, oXmlHttp)
{
    var sCommand = oRemoteCommand.Command;
    var oText = oXmlHttp.responseText;
	var Re = new RegExp('\<'+ sCommand +'Result\>([\\s\\S]+?)\<\/'+ sCommand +'Result\>','m');
	
	var val = null;
	if(Re.test(oText))
	{
	    val = RegExp.$1;
	}
	this.ReturnValue = fromString(val);
	
    function fromString(sValue)
    {
        if(sValue == null){
            return null;
        }
        if (sValue.toLowerCase() == "true")
        {
            return true;
        }
        else if (sValue.toLowerCase() == "false")
        {
            return false;
        }

        var intValue = parseInt(sValue, 10);
        if (!isNaN(intValue) && intValue.toString() == sValue)
        {
            return intValue;
        }

        var floatValue = parseFloat(sValue);
        if (!isNaN(floatValue) && floatValue.toString() == sValue)
        {
            return floatValue;
        }

        var dateValue = new Date(sValue);
        if (!isNaN(dateValue))
        {
            return dateValue;
        }

        return HtmlDecode(sValue);
    }
    
    function HtmlDecode(s)
    {
        if (typeof(s) != "string")
        {
            return s;
        }
        s = s.replace(/\\n/g, '\n');        
        s = s.replace(/&amp;/g, '&');
        s = s.replace(/&lt;/g, '<');
        s = s.replace(/&gt;/g, '>');
        return s.replace(/&quot;/g, '"');
    }
}
function RemoteIsNull(o)
{
	return ("undefined" == typeof(o) || "unknown" == typeof(o) || null == o)
}
function RemoteIsArray(item)
{
	return item != null && ((item instanceof Array) || (typeof item.splice) == "function");
}

