
/* ---------------------------- Class KmwAxaj ------------------------- */

function KmwAxaj(sUrl, sType) {
    this.request_url = sUrl;
    
    this.request_type = "xml";
    if (sType) {
        this.request_type = (sType == "xml") ? "xml" : "txt";
    }
    
    this.xmlHttp = this.__createXmlHttp();
    if (!this.xmlHttp) {
        alert ( '网络连接出错, 请稍后重试!' );
        return;
    }
}

KmwAxaj.prototype.__createXmlHttp = function() {
    if (typeof(XMLHttpRequest) == "function") {
        try {
            return new XMLHttpRequest ();
        } catch(e) {} 
    }
    try {
        return new ActiveXObject ("Microsoft.XMLHTTP");
    } catch(e) {}
    try {
        return new ActiveXObject ("Msxml2.XMLHTTP");
    } catch(e) {}

    return null;
}

KmwAxaj.prototype.__createParams = function(vParams) {
    if (typeof(vParams) == "string") {
        return vParams;
    } else {
        var pairs = new Array();
        var len = vParams.length;
        for (var key in vParams) {
            pairs.push(key + "=" + encodeURIComponent(vParams[key]));
        }
        return pairs.join("&");
    }
}

KmwAxaj.prototype.abort = function () {
    if (this.xmlHttp.readyState == 0) {
        return;
    }
    this.xmlHttp.abort();
}

KmwAxaj.prototype.get = function (vParams, fCallback, vArgs) {
    var reqstr = this.request_url;
    var tmpstr = this.__createParams(vParams);
    if (tmpstr.length > 0) {
        if (reqstr.indexOf("?") > 0) {
            reqstr += "&" + this.__createParams(vParams);
        } else {
            reqstr += "?" + this.__createParams(vParams);
        }
    }
    
    var objXmlHttp = this.xmlHttp;
    var sType = this.request_type;
    
    if (fCallback) {
        objXmlHttp.onreadystatechange = function () {
            KmwAxaj.waitResponse(objXmlHttp, sType, fCallback, vArgs);
        };
        objXmlHttp.open("GET", reqstr, true);
        objXmlHttp.send(null);
    } else {
        //objXmlHttp.onreadystatechange = null;
        objXmlHttp.open("GET", reqstr, false);
        objXmlHttp.send(null);

        if (this.request_type == "xml") {
            return objXmlHttp.responseXML;
        } else {
            return objXmlHttp.responseText;
        }
    }
}

KmwAxaj.prototype.post = function (vParams, fCallback, vArgs) {
    var objXmlHttp = this.xmlHttp;
    var sType = this.request_type;

    if (fCallback) {
        objXmlHttp.onreadystatechange = function () {
            KmwAxaj.waitResponse(objXmlHttp, sType, fCallback, vArgs);
        };
        objXmlHttp.open("POST", this.request_url, true);
        objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        objXmlHttp.send(this.__createParams(vParams));
    } else {
        //objXmlHttp.onreadystatechange = null;
        objXmlHttp.open("POST", this.request_url, false);
        objXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        objXmlHttp.send(this.__createParams(vParams));

        if (this.request_type == "xml") {
            return objXmlHttp.responseXML;
        } else {
            return objXmlHttp.responseText;
        }
    }
}

KmwAxaj.waitResponse = function (vXmlHttp, sType, fCallback, vArgs) {
    if (vXmlHttp.readyState == 4) {
        if (vXmlHttp.status == 200 ) {
            if (typeof(fCallback) == "function") {
                if (sType == "xml") {
                    fCallback(vXmlHttp.responseXML, vArgs);
                } else {
                    fCallback(vXmlHttp.responseText, vArgs);
                }
            }
        }
    }
}


/* ---------------------------- Class KmwLoader ------------------------- */

function KmwLoader() {
}

KmwLoader.__cache = null;

KmwLoader.load = function(sType, sName) {
    var sKey = sType + "/" + sName;
    if (KmwLoader.__cache == null) {
        KmwLoader.__cache = new Object();
        KmwLoader.__cache["js/kmw_axaj"] = true;
    }
    if (typeof(KmwLoader.__cache[sKey]) != "undefined") {
        return;
    }
    var axaj = new KmwAxaj(app_root + sType + "/" + sName + "." + sType, "txt");
    var jsstr = axaj.get("");

    var oScript = document.createElement("script");
    oScript.setAttribute("id", sName);
    oScript.setAttribute("type", "text/javascript");
    oScript.setAttribute("language", "javascript");
    oScript.setAttribute("defer", true);
    oScript.setAttribute("text", jsstr);

    var head = document.getElementsByTagName("head")[0];
    head.appendChild(oScript);

    KmwLoader.__cache[sKey] = true;
}

function import_js(sName) {
    KmwLoader.load("js", sName);
}

function import_css(sName) {
    KmwLoader.load("css", sName);
}


// check for XPath implementation
if (browser == "firefox" && 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";}
   }

   // 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";
     }
   }
}
