﻿// 定义新类型
var URL_PROXY = "";

if (window.ActiveXObject && !window.XMLHttpRequest) {
    window.XMLHttpRequest = function() {
    var MSXML = ['Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP'];
    for (var i = 0; i < MSXML.length; i++) {
        try {
            return new ActiveXObject(MSXML[i]);
        } catch (e) {}
    }
    return null;
    };
}

function _XMLHttpRequest(){
    this.xhr = new XMLHttpRequest();
    this.isReady = function (){ return this.xhr.readyState == 4};
    this.state = this.xhr.readyState;
}

// 新类型方法 open
_XMLHttpRequest.prototype.open = function(method, url){
    if(url.substring(0,7)=="http://"){
        url = URL_PROXY + url;
    }
    this.xhr.open(method, url, true);
    this.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
};

// 新类型方法 send
_XMLHttpRequest.prototype.send = function (data) {
    this.xhr.onreadystatechange = wrapCallback(this);
    return this.xhr.send(data);
};

// 
wrapCallback = function (_this) {
    return function () {
        var result = 
        {
            result : { value : -2, desc : null },
            status : { code :  0, desc : "Unknow" },
            success : false,
            value : -2,
            desc : null,
            requestType : "?",
            actionName : "?",
            querryName : "?",
            data : null,
            dataLength : 0
        };
        _this.readyState = _this.xhr.readyState;
        if (_this.readyState == 4) {
            _this.status=_this.xhr.status;
            _this.responseXML = _this.xhr.responseXML;
            _this.responseText = _this.xhr.responseText;
            _this.result = _this.getResult();
            _this.engine.result = _this.result;
            _this.engine.success = _this.result.success;
            result = _this.result;
        }
        _this.onreadystatechange(result);
    }
};

_XMLHttpRequest.prototype.selectNode = function(xpath)
{
    if(this.xhr.responseXML && this.xhr.responseXML.documentElement)
        return this.xhr.responseXML.documentElement.selectSingleNode(xpath);
    return null;
};

_XMLHttpRequest.prototype.getNodeText = function(xpath)
{
    if(this.xhr.responseXML != null)
    {
       var node = this.selectNode(xpath);
       if(node)
       {
           return node.nodeValue;
       }
    }
    return null;
};

_XMLHttpRequest.prototype.getNodeXml = function(xpath)
{
    if(this.xhr.responseXML)
    {
        var node = this.selectNode(xpath);
        if(node)
        {
            return node.xml;
        }
    }
    return null;
};

_XMLHttpRequest.prototype.getNodeAttribute = function(xpath)
{
    if(this.xhr.responseXML != null)
    {
        var node = this.selectNode(xpath);
        if(node && node.nodeType == 2)
            return node.value;
    }
    return null;
};

_XMLHttpRequest.prototype.getValue = function(xpath)
{
    if(this.xhr.responseXML != null)
    {
        var node = this.selectNode(xpath);
        if(node)
        {
            switch(node.nodeType)
            {
                case 1:  // Element
                    return node.xml;
                case 2:  // Attribute
                    return node.value;
                case 3:  // Text
                case 4:  // CData
                    return node.nodeValue;
                default:
                    return node.xml;
            }
        }
        return null;
    }
};

_XMLHttpRequest.prototype.getResult = function()
{
    var isAction = this.selectNode("/response/@action");
    var action;
    var querry;
    if(isAction)
        action = this.getNodeAttribute("/response/@action");
    else
        querry = this.getNodeAttribute("/response/@querry"); 
    var resultValue = this.getNodeText("/response/result/text()");
    var resultDesc = this.getNodeText("/response/desc/text()");
    var stateCode = this.getNodeText("/response/state/code/text()");
    var stateDesc = this.getNodeText("/response/state/desc/text()");
    var _result = { value : resultValue, desc : resultDesc};
    var _status = { code : stateCode, desc : stateDesc};
    var xmlResult = { 
        result : _result,
        status : _status,
        success : (resultValue && resultValue != 0 && resultValue != -1) ? true : false
    };
    xmlResult.value = resultValue;
    xmlResult.desc = resultDesc;
    if(isAction)
    {
        xmlResult.requestType = "action";
        xmlResult.actionName = action;
    }
    else
    {
        xmlResult.requestType = "querry";
        xmlResult.querryName = querry;
    }
    xmlResult.data = this.selectNode("/response/data");
    if(xmlResult.data && xmlResult.data.childNodes)
    {
        xmlResult.dataLength = xmlResult.data.childNodes.length;
    }
    else
    {
        xmlResult.dataLength = 0;
    }
    return xmlResult;
};

_XMLHttpRequest.prototype.getNamedXmlNode = function(xpath)
{
    
    var _name = this.getNodeAttribute(xpath + "/@name");
    var _value = this.getNodeAttribute(xpath + "/@value");
    var nv = { name : _name, value : _value};

    return nv;
};

