// check for XPath implementation
if( document.implementation && 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";}
   }
} else if (window.Document) {
  if(!Document.prototype.selectNodes) {
    Document.prototype.selectNodes = function(xpath, xnode) {
      if(!xnode) xnode = this.documentElement;
      re = /^([^\/]*)((\/)(.*))?$/;
      var match = re.exec(xpath);
      if(match[1]=="") {
        return this.selectNodes(match[4], xnode.documentElement);
      } else {
        if(xnode.nodeName==match[1]) {
          if(match[3]=="/") {
            var a = new Array();
            for(var i=0;i<xnode.childNodes.length;i++) {
              var s = this.selectNodes(match[4], xnode.childNodes[i]);
              for(var i2=0;i2<s.length;i2++) a[a.length] = s[i2];
            }
            return a;
          } else {
            var arr = new Array();
            arr[0] = xnode;
            return arr;
          }
        } else {
          return new Array();
        }
      }
    }
  
    Element.prototype.getAttribute = function(name) {
      if(this.attributes[name]) return this.attributes[name].nodeValue;
      else return null;
    }
  
    Element.prototype.selectNodes = function(cXPathString) {
      if(this.document.selectNodes) {
        xnode = this;
        var a = new Array();
        for(var i=0;i<xnode.childNodes.length;i++) {
          var s = this.document.selectNodes.call(this.document,cXPathString, xnode.childNodes[i]);
          for(var i2=0;i2<s.length;i2++) a[a.length] = s[i2];
        }
        return a;
      } else{throw "For XML Elements Only";}
    }
  }
}

// check for XPath implementation
if( document.implementation && 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";}
   }
} else if (window.Document) {
  if(!Document.prototype.selectSingleNode) {
    Document.prototype.selectSingleNode = function(xpath, xnode) {
      if(!xnode) xnode = this.documentElement;
      re = /^([^\/]*)((\/)(.*))?$/;
      var match = re.exec(xpath);
      if(match[1]=="") {
        return this.selectSingleNode(match[4], xnode.documentElement);
      } else {
        if(xnode.nodeName==match[1]) {
          if(match[3]=="/") {
            for(var i=0;i<xnode.childNodes.length;i++) {
              var s = this.selectSingleNode(match[4], xnode.childNodes[i]);
              if(s) return s;
            }
            return null;
          } else {
            return xnode;
          }
        } else {
          return null;
        }
      }
    }
  
    Element.prototype.selectSingleNode = function(cXPathString) {
      if(this.document.selectNodes) {
        xnode = this;
        for(var i=0;i<xnode.childNodes.length;i++) {
          var s = this.selectSingleNode.call(this.document,cXPathString, xnode.childNodes[i]);
          if(s) return s;
        }
        return null;
      } else{throw "For XML Elements Only";}
    }
  }
}

function LoadXML(Url, data, option) {
  try { 
    var req = new XMLHttpRequest(); 
  } 
  catch (error) { 
    try { 
      var req = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    catch (error) {
      return false; 
    } 
  }
  if (option != 'allowCache') {
    if (Url.indexOf('?') == -1)
      Url += '?now=' + Math.floor(Math.random()*100000);
    else
      Url += '&now=' + Math.floor(Math.random()*100000);
  }
  
  if(typeof(data) != 'undefined' && data != null) {
    req.open("POST", Url, false);
    req.send(data);
  } else {
    req.open("GET", Url, false);
    req.send(null);
  }
  return req
}

function LoadXMLAsync(Url, func, obj, data) {
  if(!Sima.apply) {
    alert("Sima.js required");
    return;
  }
  try { 
    var req = new XMLHttpRequest(); 
  } 
  catch (error) { 
    try { 
      var req = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    catch (error) {
      return false; 
    } 
  }
  if (Url.indexOf('?') == -1)
    Url += '?now=' + Math.floor(Math.random()*100000);
  else
    Url += '&now=' + Math.floor(Math.random()*100000);
  if(typeof(data) != 'undefined') {
    req.open("POST", Url, true);
  } else {
    req.open("GET", Url, true);
  }

  req.onreadystatechange = function() {
    if(req.readyState == 4){
      if (req.status == 200) {
        var contenttype = req.getResponseHeader('Content-Type');
        if (contenttype.indexOf('xml')>-1) {
           Sima.apply(func, Array(req.responseXML, req.status), obj);
        } else {
           Sima.apply(func, Array(req.responseText, req.status), obj);
        }
      } else {
        Sima.apply(func, Array('Error: '+req.status, req.status), obj);
      }
    }
  };
  if(typeof(data) != 'undefined') {
    req.send(data);
  } else {
    req.send(null);
  }
}

function getXMLNodeText(xmlNode)   
{   
    if(!xmlNode) return '';   
    if(typeof(xmlNode.textContent) != "undefined") return xmlNode.textContent;   
    return xmlNode.firstChild.nodeValue;   
}  

