
// global variables
var _browserType = navigator.appName;
var _isIEBrowser = _browserType == 'Microsoft Internet Explorer';

// global constants
var _cXmlNodeTypeElement = 1; //xml node type is Element



var _xmlHttpObj = null;

if (!_isIEBrowser)
  addMozillaXmlSupport();

function executeServerRequest(methodName, params) {
  var requestUrl = getObjectPath('svc/AjaxService.asmx') + '/' + methodName + (params == '' ? '' : '?' + params);
  _xmlHttpObj = getXmlHttpObject();
  if (!_xmlHttpObj)
    return;
  _xmlHttpObj.onreadystatechange = getResponseMethod(methodName);
  _xmlHttpObj.open('POST', requestUrl, true);
	//_xmlHttpObj.setRequestHeader('componentEngineRequest', 'true');
	try {
	  _xmlHttpObj.send(''); //must contain '' for mozilla
  }
  catch(e) {
    alert(e)
    return;
  }
}

function getResponseXml() {
  if (_xmlHttpObj.readyState == 4) {//Loaded
    if (_xmlHttpObj.status == 200) {//OK
      if (!_xmlHttpObj.responseXML || !_xmlHttpObj.responseXML.documentElement)
        return null;
        //alert (_xmlHttpObj.responseText)
        //prompt ("1", _xmlHttpObj.responseText)
      return _xmlHttpObj.responseXML.documentElement;
    }
    else {
      //alert (_xmlHttpObj.status + ' : ' + _xmlHttpObj.statusText);
      return null;
    }
  }
  else
    return null;
}

function addMozillaXmlSupport() {
  if (document.implementation.hasFeature("XPath", "3.0")) {
    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;
	  }
	  
	  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;
	  }

	  Element.prototype.selectNodes = function(cXPathString) {
		  if(this.ownerDocument.selectNodes)
			  return this.ownerDocument.selectNodes(cXPathString, this);
		  else
		    throw "For XML Elements Only";
	  }

	  Element.prototype.selectSingleNode = function(cXPathString) {	
		  if(this.ownerDocument.selectSingleNode)
		    return this.ownerDocument.selectSingleNode(cXPathString, this);
		  else
		    throw "For XML Elements Only";
	  }
	  //add the loadXML() method to the Document class
      Document.prototype.loadXML = function(strXML) {
            
        //create a DOMParser
        var objDOMParser = new DOMParser();
            
        //create new document from string
        var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
        
        //make sure to remove all nodes from the document
        while (this.hasChildNodes())
          this.removeChild(this.lastChild);

        //add the nodes from the new document
        for (var i=0; i < objDoc.childNodes.length; i++) {
                    
          //import the node
          var objImportedNode = this.importNode(objDoc.childNodes[i], true);
                    
          //append the child to the current document
          this.appendChild(objImportedNode);
                
        } //End: for
      } //End: function

        Node.prototype.__defineGetter__("xml", _Node_getXML);


  }
}

function _Node_getXML() {
    
    //create a new XMLSerializer
    var objXMLSerializer = new XMLSerializer;
    
    //get the XML string
    var strXML = objXMLSerializer.serializeToString(this);
    
    //return the XML string
    return strXML;
}

function getXmlHttpObject() {
  // code for Mozilla
  if (window.XMLHttpRequest)
    return new XMLHttpRequest();
  // code for IE
  else if (window.ActiveXObject)
    return new ActiveXObject('Microsoft.XmlHttp');
}

 function loadXML(XMLstr) 
{ 
    //load xml file 
    // code for IE 
    if (window.ActiveXObject) 
    { 
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
        xmlDoc.async=false; 
        xmlDoc.loadXML(XMLstr); 
        //alert("xml loaded in Internet Explorer"); 
        return xmlDoc; 
    } 
    // code for Mozilla, etc. 
    else if (document.implementation && document.implementation.createDocument) 
    { 
        xmlDoc= document.implementation.createDocument("","",null); 
        xmlDoc.loadXML(XMLstr); 
        //alert("xml loaded in Mozilla"); 
        return xmlDoc; 
    } 
    else 
    { 
        alert('Your browser cannot handle this script'); 
    } 
}  

