var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
var isMoz = document.implementation && document.implementation.createDocument && navigator.userAgent.toLowerCase().indexOf("msie") === -1;

// XML ============================================================================================
//Possible prefixes ActiveX strings for DOM DOcument
var ARR_ACTIVEX = ["Msxml5.DOMDocument", "MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XmlDom"];

//When the proper prefix is found, store it here
var STR_ACTIVEX = "";

//-----------------------------------------------------------------
// IE Initialization
//-----------------------------------------------------------------
if (isIE) 
{
  var bFound = false;
  
  for (var i=0; i < ARR_ACTIVEX.length && !bFound; i++) 
  {
    try 
    {
      //try to create the object, it will cause an error if it doesn't work (NCZ, 1/30/02)
      var objXML = new ActiveXObject(ARR_ACTIVEX[i]);
      
      STR_ACTIVEX = ARR_ACTIVEX[i];
      bFound = true                
    } 
    catch (objException) { } 
  } 

  if (!bFound) throw "No DOM DOcument found on your computer."
}

//-----------------------------------------------------------------
// Mozilla Initialization
//-----------------------------------------------------------------
if (isMoz && !isIE) 
{ 
  //add the loadXML() method to the Document class
  Document.prototype.loadXML = function(strXML) 
  {
    //change the readystate
    changeReadyState(this, 1);

    //create a DOMParser
    var objDOMParser = new DOMParser();
    
    //create new document from string
    var objDoc = objDOMParser.parseFromString(strXML, "text/xml");
    
    //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);
    } 
    
    //we can't fire the onload event, so we fake it
    handleOnLoad(this);
  } 
    
  //add the getter for the .xml attribute
  Node.prototype.__defineGetter__("xml", _Node_getXML);
  
  //add the readystate attribute for a Document
  Document.prototype.readyState = "0";
  
  //save a reference to the original load() method
  Document.prototype.__load__ = Document.prototype.load;

  //create our own load() method
  Document.prototype.load = _Document_load;
  
  //add the onreadystatechange attribute
  Document.prototype.onreadystatechange = null;
  
  //add the parseError attribute
  Document.prototype.parseError = 0;
}

function jsXML() { }

jsXML.createDOMDocument = function(strNamespaceURI, strRootTagName) 
{
  //variable for the created DOM Document
  var objDOM = null;
  
  //determine if this is a standards-compliant browser like Mozilla
  if (isMoz) 
  {
    //create the DOM Document the standards way
    objDOM = document.implementation.createDocument(strNamespaceURI, strRootTagName, null);    
		objDOM.async = false;
		
    //add the event listener for the load event
    objDOM.addEventListener("load", _Document_onload, false);
  } 
  else if (isIE) 
  {
    //create the DOM Document the IE way
    objDOM = new ActiveXObject(STR_ACTIVEX);
    //log(objDOM);
		objDOM.async = false;
		
    //if there is a root tag name, we need to preload the DOM
    if (strRootTagName) 
    {
      //If there is both a namespace and root tag name, then 
      //create an artifical namespace reference and load the XML.  
      if (strNamespaceURI) { objDOM.loadXML("<a0:" + strRootTagName + " xmlns:a0=\"" + strNamespaceURI + "\" />"); } 
      else { objDOM.loadXML("<" + strRootTagName + "/>"); }
    }
  }
  
  //return the object
  return objDOM;
}

jsXML.transFormNode = function(objXml, objXsl)
{
	var strOut = "";
	
	 //determine if this is a standards-compliant browser like Mozilla
  if (isMoz) 
  {
		// create an instance of XSLTProcessor
		var processor = new XSLTProcessor();

		// make the stylesheet reusable by importing it in the XSLTProcessor
		processor.importStylesheet(objXsl);
		
		var objOutput = processor.transformToDocument(objXml);
		strOut = objOutput.getXML();
  }
  else if (isIE) 
  {
		strOut = objXml.transformNode(objXsl);
  }
  return strOut;
}

function _Node_getXML() 
{
  //create a new XMLSerializer
  var objXMLSerializer = new XMLSerializer;
  var strXML = objXMLSerializer.serializeToString(this);
  return strXML;
}

function _Document_load(strURL) 
{
  this.parseError = 0;
  changeReadyState(this, 1);
  alert('here');
  //watch for errors
  try {  this.__load__(strURL); } 
  catch (objException) 
  {
    this.parseError = -9999999;
    changeReadyState(this, 4);
  } 
}

function _Document_onload() 
{
  //handle the onload event
  handleOnLoad(this);
}

function handleOnLoad(objDOMDocument) 
{
  //check for a parsing error
  if (!objDOMDocument.documentElement || objDOMDocument.documentElement.tagName == "parsererror")
      objDOMDocument.parseError = -9999999;

  //change the readyState
  changeReadyState(objDOMDocument, 4);
}

function changeReadyState(objDOMDocument, iReadyState) 
{
  //change the readyState
  objDOMDocument.readyState = iReadyState;
  
  //if there is an onreadystatechange event handler, run it
  if (objDOMDocument.onreadystatechange != null && typeof objDOMDocument.onreadystatechange == "function")
      objDOMDocument.onreadystatechange();
}

function xmlDocFactory(bolAsync)
{
  try
  {
    if(typeof(bolAsync) == "undefined")
      bolAsync = false;
    
    var xmlData = new ActiveXObject("MSXML2.DOMDocument");
    with(xmlData)
    {
      validateOnParse = false;
      resolveExternals = false;
      preserveWhiteSpace = false;
      async = bolAsync;
    }
    
    return xmlData;
  } catch(e) {}
}

// returns the next highest value from a collection of nodes (XML autonumber) 
function getNewMaxAttribute(colNodes, strAttribute)
{
	try
	{
		var intLen = colNodes.length;
		var intNewMax = 0;
		var intTemp = 0;
		
		for (var i=0; i	< intLen; i++)
		{
			intTemp = parseInt(colNodes.item(i).getAttribute(strAttribute));
			if (intTemp > intNewMax) intNewMax = intTemp;
		}
		
		return intNewMax + 1;
	}
	catch(e) {log(e, 'getNewMaxAttribute');}
}

function xmlEscape(strIn)
{
	var strOut = strIn.replace(/[&]/g, '&amp;');
	strOut = strOut.replace(/[>]/g, '&gt;');
	strOut = strOut.replace(/[<]/g, '&lt;');
	strOut = strOut.replace(/["]/g, '&quot;');
	strOut = strOut.replace(/[']/g, '&apos;');
	
	return strOut
}

function xmlUnEscape(strIn)
{
	var strOut = strIn.replace(/\&amp;/g, '&');
	strOut = strOut.replace(/\&gt;/g, '>');
	strOut = strOut.replace(/\&lt;/g, '<');
	strOut = strOut.replace(/\&quot;/g, '"');
	strOut = strOut.replace(/\&apos;/g, "'");
	
	return strOut
}

// used to overcome ASP.net unescaping html in xml on post
function xmlEscapePost(strIn)
{
	var strOut = xmlUnEscape(strIn);

	strOut = strOut.replace(/[&]/g, '~amp~');
	strOut = strOut.replace(/[>]/g, '~gt~');
	strOut = strOut.replace(/[<]/g, '~lt~');
	strOut = strOut.replace(/["]/g, '~quot~');
	strOut = strOut.replace(/[']/g, '~apos~');
	
	return strOut
}
