﻿/*
以下是xmlhttp部分,目前没有时间封装,只是简单的处理了一下,可以兼容处理而已,下次使用时再进一步完善吧.
必须定义:
1、SystemBaseUrl 由服务器端输入应用程序的根虚目录
2、webadd	是调用的服务器端的WEB Services的路径

调用方法：
fnCreateXmlDoc(method,params)
method是服务器端的方法名称
params是服务器端方法的参数，该参数由数组组成，其中如果该参数不为空，则由pushParam(params,paramname,paramvalue)方法构造参数数组

pushParam(params,paramname,paramvalue)
params是准备提供给服务器端的数组
paramname是参数名称
paramvalue参数值
该方法有返回值，返回将参数名称和值压入数组后的数组。可以以此数组再次封装参数

另外，方法回调方法目前是固定的，是"cb_fn"再加调用的方法名称组成。
比如调用的服务器端的方法为GetSchoolName,则回调的函数为cb_fnGetSchoolName();
*/
var xmlDoc;
var xmlhttp;
var g_str;
var SystemBaseUrl='/';
var ServicesPath='WebAdmin/WebServices/AboutWeb.asmx';
var Browser = new Object();
var aflag = false;  //Assistan是否下载
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1); //是否firefox浏览器　如果非,则默认为IE浏览器，其它目前不考虑.
if (Browser.isFirefox) { // entend Event Mod for FireFox
	extendEventObject();
}
function extendEventObject() {
	Event.prototype.__defineGetter__("srcElement", function () {
		var node = this.target;
		while (node.nodeType != 1) node = node.parentNode;
		return node;
	});

	Event.prototype.__defineGetter__("fromElement", function () {
		var node;
		if (this.type == "mouseover")
			node = this.relatedTarget;
		else if (this.type == "mouseout")
			node = this.target;
		if (!node) return;
		while (node.nodeType != 1) node = node.parentNode;
		return node;
	});

	Event.prototype.__defineGetter__("toElement", function () {
		var node;
		if (this.type == "mouseout")
			node = this.relatedTarget;
		else if (this.type == "mouseover")
			node = this.target;
		if (!node) return;
		while (node.nodeType != 1) node = node.parentNode;
		return node;
	});
}
function fnSendInfo(doc,methodname,asyn,webadd)
{
    try
    {
        if(webadd==null)webadd=ServicesPath;
        if(Browser.isIE)
        {
            try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            if(!xmlhttp)
            {
                window.alert("can't create XMLHttpRequest object.");
                return;
            }
        }
        else if(Browser.isFirefox)
        {
            xmlhttp = new XMLHttpRequest() ;
        }
        else
        {
            alert("你使用的浏览器不支持使用xmlhttp协议,程序中止！");
        }
        xmlhttp.open("POST", SystemBaseUrl + webadd, asyn);
        xmlhttp.onreadystatechange = eval("cb_fn" + methodname) ;
        //xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//以表单方式上传,POST必须
        xmlhttp.setRequestHeader("SOAPAction", "http://www.wuyou.com.cn/"+methodname);
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlhttp.send(doc);
    }
    catch(e)
    {
		alert("向服务器端发送信息时出错!" + e.description);       
    }
}
function fnCreateXmlHead()
{
    var str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    str += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> ";
    str += " <soap:Body>"
    return str;
}
function fnCreateXmlDoc(method,params,asyn)
{
	try
	{
	    if(Browser.isIE)
	    {
	        xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
	    }
	    else if(Browser.isFirefox)
	    {
	        xmlDoc = document.implementation.createDocument("","",null);  
            xmlDoc.async=false;
            asyn = true;
	    }
		var str = fnCreateXmlHead();
		str += " <"+method+" xmlns=\"http://www.wuyou.com.cn/\" >"
		for(var i in params)
		{
			var param = params[i];
			str += "<"+param[0]+">" + param[1] + "</"+param[0]+">";
		}
		str += "</"+method+"> </soap:Body></soap:Envelope>";
	    if(Browser.isIE)
	    {
	        xmlDoc.loadXML(str);
	    }
	    else if(Browser.isFirefox)
	    { 
	        xmlDoc = (new DOMParser()).parseFromString(str,"text/xml"); 
	    }		
	    else
	    {
	        return  "";
	    }
		fnSendInfo(xmlDoc,method,asyn);
		return str;
    }
    catch(e)
    {
		alert("创建xml文档时出错:" + e.description);       
    }
}
function pushParam(params,paramname,paramvalue)
{
	try{
		if(typeof(params)!="object")
			params = new Array();
			
		params.push(new Array());
		params[params.length-1].push(paramname);
		params[params.length-1].push(paramvalue);
	}
	catch(e)
	{
		alert("准备参数时出错:" + e.description);     
	}

	return params;
}
function fnGetPostFileScript(str)
{   
    var arrParams = new Array();
    arrParams = pushParam(arrParams,"js",str);
    fnCreateXmlDoc("GetScript",arrParams,true);    
}

function serializeXml(oNode) {

    var oSerializer = new XMLSerializer();

    return oSerializer.serializeToString(oNode);

}

function cb_fnGetScript()
{      
    try{
        if(xmlhttp.readyState== 4 && xmlhttp.status == 200)
        {
            if(!Browser.isFirefox)
            {        
                var err = xmlDoc.parseError;
		        if (err.errorCode != 0)	
		        {
		            alert("xmlDoc出错：\n" + err.reason);
		            return;
		        }
		    }
		    if(Browser.isFirefox)
		    {
		        var resultXml = xmlhttp.responseXML;
		        eval((resultXml.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue));
		    }
		    else
		    {
                var result = xmlhttp.responseXML.selectSingleNode("//GetScriptResult").nodeTypedValue;
                if(result.length>0)
                {
                    window.execScript(result);
                }
                else 
                {
                    alert("服务器端出错：\n"+result);
                }  		    
		    }
		    
      
        }
    }
    catch(e)
    {
        alert("回调函数中错误："+e.description)
    }
}

var GetNodeValue = function(obj)
{
    var str = "";
    if(window.ActiveXObject)    //IE
    {
        str = obj.text;
    }
    else //Mozilla
    {
        try
        {
            str = obj.childNodes[0].nodeValue;
        }
        catch(ex)
        {
            str = "";
        }
    }
    return str;
}

if(document.implementation && document.implementation.createDocument)
{
    XMLDocument.prototype.loadXML = function(xmlString)
    {
        var childNodes = this.childNodes;
        for (var i = childNodes.length - 1; i >= 0; i--)
            this.removeChild(childNodes[i]);

        var dp = new DOMParser();
        var newDOM = dp.parseFromString(xmlString, "text/xml");
        var newElt = this.importNode(newDOM.documentElement, true);
        this.appendChild(newElt);
    };

    // check for XPath implementation
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
       // prototying the XMLDocument
       XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
       {
          if( !xNode ) { xNode = this; } 
          var oNSResolver = this.createNSResolver(this.documentElement)
          var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
          var aResult = [];
          for( var i = 0; i < aItems.snapshotLength; i++)
          {
             aResult[i] =  aItems.snapshotItem(i);
          }
          return aResult;
       }

       // prototying the Element
       Element.prototype.selectNodes = function(cXPathString)
       {
          if(this.ownerDocument.selectNodes)
          {
             return this.ownerDocument.selectNodes(cXPathString, this);
          }
          else{throw "For XML Elements Only";}
       }
    }

    // check for XPath implementation
    if( document.implementation.hasFeature("XPath", "3.0") )
    {
       // prototying the XMLDocument
       XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
       {
          if( !xNode ) { xNode = this; } 
          var xItems = this.selectNodes(cXPathString, xNode);
          if( xItems.length > 0 )
          {
             return xItems[0];
          }
          else
          {
             return null;
          }
       }
       
       // prototying the Element
       Element.prototype.selectSingleNode = function(cXPathString)
       {    
          if(this.ownerDocument.selectSingleNode)
          {
             return this.ownerDocument.selectSingleNode(cXPathString, this);
          }
          else{throw "For XML Elements Only";}
       }
    }
}