﻿function GetXmlHttp()
{
    //return window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    var xmlRequest = null;
    try
    {
        xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e1)
    {
        try
        {
            xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e2)
        {
            xmlRequest = null;
        }
    }

    if ( !xmlRequest && typeof XMLHttpRequest != "undefined" )
    {
        xmlRequest = new XMLHttpRequest();
    }

    return xmlRequest;
}

function postRequest(url, args)
{
    var xmlDom = GetXmlHttp();
    if (!xmlDom)
    {
        return false;
    }
    
    xmlDom.open("POST", url, false);
    xmlDom.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlDom.send(args);
    
    var result = xmlDom.responseXML;
    if (!result.documentElement && xmlDom.responseStream)
    {
        result.load(xmlDom.responseStream);
    }
    return (result.getElementsByTagName("boolean")[0].firstChild.data == "true");

}

function CheckValidateCode(path, code)
{
    var sMethod = "CheckValidateCode";  // 请求的函数
    var para = new parameter();         // 定义参数
    para.add("code", code);             // 添加参数
    
    var sReuqestParam = getRequest_Soap12(sMethod, para);
    return (soapRequest(path + "webservice/wsmain.asmx", sReuqestParam, sMethod) == "true");
    
    //return postRequest(path + "WebService/WSMain.asmx/CheckValidateCode", "code=" + code);
}

function CheckUserPassword(path, pwd)
{
    var sMethod = "CheckUserPassword";  // 请求的函数
    var para = new parameter();         // 定义参数
    para.add("password", pwd);          // 添加参数
    
    var sReuqestParam = getRequest_Soap12(sMethod, para);
    return (soapRequest(path + "webservice/wsmain.asmx", sReuqestParam, sMethod) == "true");

    //return postRequest(path + "WebService/WSMain.asmx/CheckUserPassword", "password=" + pwd);
}

function UserExists(path, codeorname)
{
    var sMethod = "UserExists";         // 请求的函数
    var para = new parameter();         // 定义参数
    para.add("codeorname", codeorname); // 添加参数
    
    var sReuqestParam = getRequest_Soap12(sMethod, para);
    return (soapRequest(path + "webservice/wsmain.asmx", sReuqestParam, sMethod) == "true");

    //return postRequest(path + "WebService/WSMain.asmx/UserExists", "codeorname=" + codeorname);
}

function CheckUserName(path, username, userid)
{
    var sMethod = "CheckUserName";      // 请求的函数
    var para = new parameter();         // 定义参数
    para.add("username", username);     // 添加参数
    para.add("userid", userid);         // 添加参数
    
    var sReuqestParam = getRequest_Soap12(sMethod, para);
    return (soapRequest(path + "webservice/wsmain.asmx", sReuqestParam, sMethod) == "true");
    
    //return postRequest(path + "WebService/WSMain.asmx/CheckUserName", "username=" + username + "&userid=" + userid);
}

//-------------------------------------------------------------
// WEBService下Soap请求
// sWebServiceName: web Service名
// sMethod：Web Service请求时调用的函数名称
//-------------------------------------------------------------
function soapRequest(sWebServiceName, sRequestParam, sMethod)
{
    var xmlDom = GetXmlHttp();
    if (!xmlDom)
    {
        return false;
    }
    //SOAP1.1
//    xmlDom.open("POST", sWebServiceName, false);
//    xmlDom.setRequestHeader("Content-Type","text/xml; charset=utf-8");
//    xmlDom.setRequestHeader("SOAPAction", "http://tempuri.org/" + sMethod);
    
    //SOAP1.2
    xmlDom.open("POST", sWebServiceName, false);
    xmlDom.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");
    
    xmlDom.send(sRequestParam);
    
    var sResponse = xmlDom.responseText;
    var sResult = getWebServiceResponseValue(sResponse, sMethod)
    return sResult;
}

//------------------------------------------------------------------
// 取得Web Service 返回值
// sResponse：Web Service返回数据（字符串）
// sMethod：Web Service请求时调用的函数名称
// 返回Web Service的结果集，可以是任意类型的数据，如XML、JSON等
// 注意：这个方法有漏洞
//------------------------------------------------------------------
function getWebServiceResponseValue(sResponse, sMethod)
{
    var pos1 = sResponse.indexOf("<" + sMethod + "Result>");
    var pos2 = sResponse.indexOf("</" + sMethod + "Result>");
    var index = pos1 + sMethod.length + 2 + 6;
    var result = sResponse.substr(index,pos2 - index);
    return result;
}

//------------------------------------------------------
// 生成SOAP1.2请求参数串
// sMethod: 调用的web Service方法
// Para：parameter类型参数，用于传送WEBService方法下的参数列表
// 返回值：SOAP1.2的Web Service请求串
//-----------------------------------------------------
function getRequest_Soap12(sMethod, para)
{  
    var sRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        sRequest += "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
        sRequest += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ";
        sRequest += "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n";
        sRequest += "<soap12:Body> \n";
        sRequest += "<" + sMethod + " xmlns=\"http://tempuri.org/\">\n";
        sRequest += para.toXml() + "\n";
        sRequest += "</" + sMethod + ">\n";
        sRequest += "</soap12:Body>\n";
        sRequest += "</soap12:Envelope>";
    return sRequest;  
}
//-----------------------------------------------------
// 生成SOAP1.1请求参数串
// sMethod: 调用的web Service方法
// Para：parameter类型参数，用于传送WEBService方法下的参数列表
// 返回值：SOAP1.1的Web Service请求串
//-----------------------------------------------------
function getRequest_Soap11(sMethod, para)
{
    var sRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        sRequest += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
        sRequest += "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ";
        sRequest += "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "\n";
        sRequest += "<soap:Body>";
        sRequest += "<" + sMethod + " xmlns=\"http://tempuri.org/\">\n";
        sRequest += para.toXml() + "\n";
        sRequest += "</" + sMethod + ">\n";
        sRequest += "</soap:Body>\n";
        sRequest += "</soap:Envelope>";
    return sRequest;
}

//-----------------------------------------
// add方法：用于添加参数
// toXML方法：将POST参数转化为XML格式字符串
// toPost方法：将POST参数转化为Post格式字符串
//-----------------------------------------
var parameter = function()
{
    var parameterList = new Array();
    // 添加POST参数
    this.add = function(name,value)
    {
        parameterList[name] = value;
        return this;
    }
    // 将POST参数转换成XML格式字符串(<param1>value1</param1> <param2>value2</param2> ...)
    this.toXml = function()
    {
        var xml = "";
        for(var p in parameterList)
        {
            if(typeof(parameterList[p]) != "function")
                xml += "<" + p + ">" + parameterList[p].toString().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</" + p + ">";
        }
        return xml;   
    }
    // 将POST参数转换成POST格式字符串(Param1=Value1&Param2=Value2&...)
    this.toPost = function()
    {
        var postStr = "";
        for (var p in parameterList)
        {
            if (typeof(parameterList[p]) != "function")
                postStr += p + "=" + parameterList[p] + "&";
        }
        postStr = postStr.substr(0,postStr.length - 1);
        return postStr;
    }
}