﻿// JScript 文件
/**************************** XML ***************************/
function XmlDoc(s_xml) {
    this.xmldoc=null;
    if (window.ActiveXObject) {
        this.xmldoc=new ActiveXObject("Msxml2.DOMDocument");
        this.xmldoc.loadXML(s_xml);
    }
    else {
        var DOM = new DOMParser(); 
        this.xmldoc = DOM.parseFromString(s_xml, 'text/xml'); 
    }
    
    this.selectSingleNode=function (s_xmlpath) {
        return new fn_XmlNode(this.xmldoc,s_xmlpath);
    }
    this.selectNodes=function (s_xmlpath) {
        return new fn_XmlNodes(this.xmldoc,s_xmlpath);
    }
}

function fn_XmlNode(xmldoc,s_xmlpath) {
    this.text=null;
    this.attributes=null;
    this.nodeName=null;
    if (window.ActiveXObject) {
        if (xmldoc.selectSingleNode(s_xmlpath)==null) return;
        this.text=xmldoc.selectSingleNode(s_xmlpath).text;
        this.attributes=xmldoc.selectSingleNode(s_xmlpath).attributes;
        this.nodeName=xmldoc.selectSingleNode(s_xmlpath).nodeName;
    }
    else {
        var oEvaluator = new XPathEvaluator();
        var oResult = oEvaluator.evaluate(s_xmlpath,xmldoc,null, XPathResult.FIRST_ORDERED_NODE_TYPE,null);
        if (oResult.singleNodeValue==null) return;
        
        this.text=oResult.singleNodeValue.textContent;
        this.attributes=new fn_NodeAttributs(oResult.singleNodeValue);
        this.nodeName=oResult.singleNodeValue.nodeName;
    }
}

function fn_XmlNodes(xmldoc,s_xmlpath) {
    var array=null;
    if (array==null) {
        if (window.ActiveXObject) {
            array=xmldoc.selectNodes(s_xmlpath);
            return array;
        }
        else {
            array=new Array();
            var oEvaluator = new XPathEvaluator();
            var oIt = oEvaluator.evaluate(s_xmlpath,xmldoc,null, XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
            if (oIt!=null) {
                var oEle = oIt.iterateNext();
                while (oEle!=null) {
                    var node=new fn_EleNode(oEle);
                    array.push(node);
                    oEle = oIt.iterateNext();
                }
            }
            return array;
        }
    }
}

function fn_EleNode(ele) {
    this.text=null;
    this.attributes=null;
    this.nodeName=null;
    if (ele==null) return;
    
    this.text=ele.textContent;
    this.attributes=new fn_NodeAttributs(ele);
    this.nodeName=ele.nodeName;
}

function fn_NodeAttributs(ele) {
    var array=null;
    if (array==null) {
        array=new Array();
        var str = (new XMLSerializer()).serializeToString(ele);
        var ns=str.indexOf("<");
        var ne=str.indexOf(">",ns+1);
        str=str.substring(ns,ne);
        var matchs=str.match(/'([^'"]*)'|"([^'"]*)"/gi);
        
        var sval=String("");
        if (matchs!=null) {
            for (var ni=0;ni<matchs.length;ni++) {
                sval=matchs[ni];
                array.push(new fn_NodeAttribute(matchs[ni].replace(/'|"/gi,"")));
            }
        }
    }
    return array;
}

function fn_NodeAttribute(val) {
    this.nodeValue=val;
}
/**************************** XML ***************************/


/****************************AJAX***************************/
function AJAX(func,method,url,async,user,password,data,efunc)
{
    var req=new XMLHttpRequest();
    if(req)
    {
        req.onreadystatechange=function()
        {
            if(req.readyState==4&&req.status==200)
            {
                if(func)func(req);
            }
            else if(req.readyState==4&&req.readyState>200)
            {
                if(efunc)efunc(req);
            }
            if(req.readyState==4)
            {
                req.onreadystatechange=emptyFunc;
            }
        };
        method=method.toUpperCase();
        req.open(method,url,async,user,password);
        if(method=='POST')
        {
            req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        }
        if(data)
        {
            req.send(data);
        }
        else
        {
            req.send(null);
        }
    }
    return req;
}

if(window.ActiveXObject&&!window.XMLHttpRequest)
{
    window.XMLHttpRequest=function()
    {
        var msxmls=new Array('Msxml2.XMLHTTP','Microsoft.XMLHTTP');
        for(var i=0;i<msxmls.length;i++)
        {
            try
            {
                return new ActiveXObject(msxmls[i]);
            }
            catch(e)
            {
            }
        }
        return null;
    };
}
function emptyFunc()
{
}
/****************************AJAX***************************/
