/*
	XML browser compatibility layer.
	
	These functions & objects provide a uniform interface to xml document & xml http 
	functions.
	
	Usage:				
						********************************************
						******	XML TRANSPORT OBJECT (xmlhttp) *****
						********************************************
		
			xt = XmlHttp.create();
			
			RETURNS:
			
				xt - new xml http transport object. this is the standard object,
						all methods should be the same on both browsers.
					
						*********************************
						******	XML DOCUMENT OBJECT *****
						*********************************
						
		To create a compatible Xml Document object, you must use the compatible 
		object XmlDoc as follows:
		
			myXmlDoc = new XmlDoc();
			
		RETURNS:
		
			 an xml document.
			
		
		CLASS PROPERTIES:
		
			XmlDoc.isNetscape	-	returns true if on netscape browser.
			
		PROPERTIES:
			
			.contentDocument	-	the underlying platform specific xmlDocument.
		
		METHODS:
		
			.appendChild(element)
					Appends child element to the document.
			
					element = an element
				
		
			.createElement(name)
			
					name - string containing name of element.
				
				Returns: element with tag=string
					
			.createElementNS(namespace, name)
			
					namespace - string containing namespace urn
					
					name - string containing name of element.
					
				Returns: new element with specified name in specified namespace
				
			.createTextNode(TagName)
			
					TagName - a node name
					
			.getElementsByTagName(TagName)
			
					TagName - a tag spec.
					
				Returns: all elements in documement who's tag matches TagName
				
			.loadXML(string)
					
					sets document to xml representation of string.
					
			.xml	-	returns a text representation of the xml content of the
						document.
			
				Returns: a new element tagged as TagName.
			
						*******************************
						******	OBJECT EXTENSIONS *****
						*******************************
						
			Some Moz native objects were extended to provide compatible methods
			with Microsoft objects.
			
			Object Element:
			
			.text	-	returns the value of a node as a string value. If the node
						has childred, it returns the value of all the children 
						concatenated together.
						
***************************************************************************************/

												// Are we Running Netscape ?
												
XmlDoc.isNetscape=document.implementation && document.implementation.createDocument;

												// XmlHttp factory
function XmlHttp() {}

XmlHttp.create = function () {
   try {
      if (window.XMLHttpRequest) {
         var req = new XMLHttpRequest();
         
         // some older versions of Moz did not support the readyState property
         // and the onreadystate event so we patch it!
         if (req.readyState == null) {
            req.readyState = 1;
            req.addEventListener("load", function () {
               req.readyState = 4;
               if (typeof req.onreadystatechange == "function")
                  req.onreadystatechange();
            }, false);
         }
         
         return req;
      }
	 /*********************************************************************************************************************
	  ** Added 3/6/2008 - REM																							**
	  **																													**
	  ** Apparently the getControlPrefix function uses old objects and was causing us all kinds of XML issues in PT-61	**
	  ** The functions below are an attempt to fix it.  For a reference look at the blog below.							**
	  **																													**
	  ** http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx		**
	  **																													**	
	  *********************************************************************************************************************/
      if (window.ActiveXObject) {
         //return new ActiveXObject(getControlPrefix() + ".XmlHttp");
         return new ActiveXObject(getXmlHttpProgID());
      }
   }
   catch (ex) {}
   // fell through
   throw new Error("Your browser does not support XmlHttp objects");
};

													// XmlDoc, replacement for xml 
													//	class.
function XmlDoc(aDoc) {
	this.contentDocument = aDoc || XmlDocument.create();
	
	this.appendChild = function(anElement) {
	
		this.contentDocument.appendChild(anElement);
	}
	
	this.loadXML = function(aString) {
		if (XmlDoc.isNetscape) {
			// parse the string to a new doc   
			var doc2 = (new DOMParser()).parseFromString(aString, "text/xml");
			      
			// remove all initial children
			while (this.contentDocument.hasChildNodes())
				this.contentDocument.removeChild(this.contentDocument.lastChild);
			         
			// insert and import nodes
			for (var i = 0; i < doc2.childNodes.length; i++) {
				this.contentDocument.appendChild(this.contentDocument.importNode(doc2.childNodes[i], true));
			}
		} else {
			this.contentDocument.loadXML(aString);
		}
	}
	
	this.createElement = function(sName) {
		return this.contentDocument.createElement(sName);
	}
	
	this.createElementNS = function(sNamespace, sName) {
		if (XmlDoc.isNetscape) 
			return this.contentDocument.createElementNS(sNamespace, sName);
		else
			return this.contentDocument.createNode(1, sName, sNamespace);
	}
	
	this.getElementsByTagName = function(sTagName) {
		return this.contentDocument.getElementsByTagName(sTagName);
	}
	
	this.createTextNode = function(sText) {
		return this.contentDocument.createTextNode(sText);
	}
	this.xml = function () {
		if (XmlDoc.isNetscape)
			return (new XMLSerializer()).serializeToString(this.contentDocument);
		else
			return this.contentDocument.xml;
	}
}
														// Add property methods
if (XmlDoc.isNetscape) {
														// Element.text
	// 3/9/2005 - REM
	// The unlying code in FireFox 1.0.3 as well as the versions of Mozilla and Netscape
	// have a bug that prevents the normal way of this from working.  Basically it was a 
	// security enhancement that broke this and the next versions will have it fixed but
	// in the mean time we had to work around it.
	// http://developer-test.mozilla.org/docs/Working_around_the_Firefox_1.0.3_DHTML_regression
	/*
	Element.prototype.__defineGetter__("text", function() {
		var p=this.firstChild;
		var s=p.nodeValue;
		while (p != this.lastChild) {
			s += p.value;
			p = p.nextSibling;
		} 
		return s;
		});
	*/

	// This was the way to get around the problem mentioned above
	var elementProto = Element.prototype;
	elementProto.__proto__ = {
		__proto__: elementProto.__proto__
	};
	var elementGrandProto = elementProto.__proto__;
	elementGrandProto.__defineGetter__('text',
	  function () { 
		var p=this.firstChild;
		var s=p.nodeValue;
		while (p != this.lastChild) {
			s += p.value;
			p = p.nextSibling;
		} 
		return s;
	  }
	);
														// XmlDoc.xml
}

														// XmlDocument factory
														// This isn't inteded to be used
														// because it returns a platform
														// specific Document. It is called
														// by XmlDoc.
function XmlDocument() {}

XmlDocument.create = function () {
   try {
      // DOM2
      if (document.implementation && document.implementation.createDocument) {
         var doc = document.implementation.createDocument("", "", null);
			
         // some versions of Moz do not support the readyState property
         // and the onreadystate event so we patch it!
         if (doc.readyState == null) {
            doc.readyState = 1;
            doc.addEventListener("load", function () {
               doc.readyState = 4;
               if (typeof doc.onreadystatechange == "function")
                  doc.onreadystatechange();
            }, false);
         }
			
         return doc;
      }
	 /*********************************************************************************************************************
	  ** Added 3/6/2008 - REM																							**
	  **																													**
	  ** Apparently the getControlPrefix function uses old objects and was causing us all kinds of XML issues in PT-61	**
	  ** The functions below are an attempt to fix it.  For a reference look at the blog below.							**
	  **																													**
	  ** http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx		**
	  **																													**	
	  *********************************************************************************************************************/
      if (window.ActiveXObject) {
         //return new ActiveXObject(getControlPrefix() + ".XmlDom");
         return new ActiveXObject(getXmlDomProgID());
	  }
   }
   catch (ex) {}
   throw new Error("Your browser does not support XmlDocument objects");
};

function getControlPrefix() {
	//define found flag
	var bFound = false;

   if (getControlPrefix.prefix)
      return getControlPrefix.prefix;
   
   var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
   var o, o2;
   for (var i = 0; i < prefixes.length; i++) {
      try {
         // try to create the objects
         o = new ActiveXObject(prefixes[i] + ".XmlHttp");
         o2 = new ActiveXObject(prefixes[i] + ".XmlDom");
		 bFound = true;
         return getControlPrefix.prefix = prefixes[i];
		
      }
      catch (ex) {};
   }
   
   if (!bFound) 
	    throw new Error("Could not find an installed XML parser");
}

/*********************************************************************************************************************
 ** Added 3/6/2008 - REM																							**
 **																													**
 ** Apparently the getControlPrefix function uses old objects and was causing us all kinds of XML issues in PT-61	**
 ** The functions below are an attempt to fix it.  For a reference look at the blog below.							**
 **																													**
 ** http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx		**
 **																													**	
 *********************************************************************************************************************/
function getXmlDomProgID() {
	//define found flag
	var bFound = false;

   if (getXmlDomProgID.progID)
      return getXmlDomProgID.progID;
   
   var progIDs = ["Msxml2.DomDocument.3.0"];
   var o;
   for (var i = 0; i < progIDs.length; i++) {
      try {
         // try to create the objects
         o = new ActiveXObject(progIDs[i]);
		 bFound = true;
         return getXmlDomProgID.progID = progIDs[i];
      }
      catch (ex) {};
   }
   
   if (!bFound) 
	    throw new Error("Could not find an installed XML DomDocument object");
}


/*********************************************************************************************************************
 ** Added 3/6/2008 - REM																							**
 **																													**
 ** Apparently the getControlPrefix function uses old objects and was causing us all kinds of XML issues in PT-61	**
 ** The functions below are an attempt to fix it.  For a reference look at the blog below.							**
 **																													**
 ** http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx		**
 **																													**	
 *********************************************************************************************************************/
function getXmlHttpProgID() {
	//define found flag
	var bFound = false;

   if (getXmlHttpProgID.progID)
      return getXmlHttpProgID.progID;
   
   var progIDs = ["Msxml2.XMLHTTP.3.0"];
   var o;
   for (var i = 0; i < progIDs.length; i++) {
      try {
         // try to create the objects
         o = new ActiveXObject(progIDs[i]);
		 bFound = true;
         return getXmlHttpProgID.progID = progIDs[i];
      }
      catch (ex) {};
   }
   
   if (!bFound) 
	    throw new Error("Could not find an installed XML HTTP object");
}

