﻿    var Ajax = {
                    xmlhttp:function(){ 
		                try{ 
			                    return new ActiveXObject('Msxml2.XMLHTTP'); //新版本的 Internet Explorer
		                   }catch(e){ 
				                      try{ 
					                       return new ActiveXObject('Microsoft.XMLHTTP');//较老版本的 Internet Explorer 
				                         }catch(e){ 
					                               return new XMLHttpRequest(); //在Firefox下初始化XMLHttpRequest对象
				                                  } 
                                    } 
                    } 
                }; 

        Ajax.Request = function(){ 
		          if (arguments.length<2) return; 
			      var _p = {asynchronous:true,method:"GET",parameters:""};            //default option 
			      for (var key in arguments[1]){                                      //custom option overwrite default option 
				        _p[key] = arguments[1][key]; 
			      } 
			      var _x = Ajax.xmlhttp();                                            //xml obj 
			      var _url = arguments[0];                                            //str 
			      if(_p["parameters"].length>0) _p["parameters"] += '&_='; 
			      if(_p["method"].toUpperCase()=="GET")_url += (_url.match(/\?/) ? '&' : '?') + _p["parameters"]; 
			      _x.open(_p["method"],_url,_p["asynchronous"]); 
			      _x.onreadystatechange = function(){ 
			  	       if (_x.readyState==4){ 
					   if(_x.status==200){ 
					                         _p["onComplete"]?_p["onComplete"](_x):""; 
					                     }else{ 
						                        _p["onError"]?_p["onError"](_x):""; 
					                           } 
			  	 } 
			 } 
			if(_p["method"].toUpperCase()=="POST")_x.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
			_x.send(_p["method"].toUpperCase()=="POST" ? _p["parameters"] : null); 
      }; 
      
      

//加载XML文件的通用函数(for IE and Firefox)

var xmlDocObj;
 function loadXML(xmlfile){
    //load xml file
    // code for IE
    if (window.ActiveXObject)
    {
        xmlDocObj=new ActiveXObject("Microsoft.XMLDOM");
        xmlDocObj.async=false;
        xmlDocObj.load(xmlfile);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
        xmlDocObj=document.implementation.createDocument("","",null);
        xmlDocObj.load(xmlfile);    
    }
    else
    {
        alert('Your browser cannot handle this script');
    }
}

//Javascript 在 Firefox 下读取XML文档的功能代码
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";
                }
            }
        }
}
