
//----------------------------------------------------------------------------------------------------------------------------------
//
//		Routines to make XMLDocument Creation consistant across browsers
//

/*
// Create the loadXML method and xml getter for Mozilla
if (window.DOMParser &&	window.XMLSerializer && window.Node && Node.prototype && Node.prototype.__defineGetter__)
{
	// XMLDocument did not extend the Document interface in some versions of Mozilla. Extend both!
	//XMLDocument.prototype.loadXML =
	Document.prototype.loadXML = function (s)
	{
        	this.readyState = 1;

		// parse the string to a new doc
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");

		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);

		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++)
		{
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}

		this.readyState = 4;
	};

	// xml getter
	// This serializes the DOM tree to an XML String
	// Usage: var sXml = oNode.xml
	Document.prototype.__defineGetter__("xml",
		function ()	{
					return (new XMLSerializer()).serializeToString(this, "text/xml");
					});
}


var createXMLDocument = function()
{
	var xdoc = null;
	if (document.implementation && document.implementation.createDocument)
	{// Mozilla (ie. Firefox) based XML document
		xdoc = document.implementation.createDocument("", "", null);
		if (!xdoc) throw "Mozilla Browser is not providing XML document support.";

		// some versions of Moz do not support the readyState property
		// and the onreadystate event so we patch it!
		if (xdoc.readyState == null) {
			xdoc.readyState = 1;
			var self = this;
			xdoc.addEventListener("load", function ()
					{
					self.xdoc.readyState = 4;
					if (typeof xdoc.onreadystatechange == "function")
						self.xdoc.onreadystatechange();
					}, false);
			}
		//xdoc.onload= function () {return (parent.xdoc.readyState == 4 ? true:false);};
		return xdoc;
	}
	else if (window.ActiveXObject)
	{// Internet Explorer
		xdoc =  Try.these(
							function() { return new ActiveXObject('MSXML4.DOMDocument')   },
							function() { return new ActiveXObject('MSXML3.DomDocument') },
							function() { return new ActiveXObject('MSXML2.DomDocument') },
							function() { return new ActiveXObject('MSXML.DomDocument')  },
							function() { return new ActiveXObject('Microsoft.XmlDom')}
						  ) || null;
		if (!xdoc) throw "IE Browser is not providing XML document support.";
		Self = this;
		xdoc.onreadystatechange = function () {return (Self.xdoc.readyState == 4 ? true:false); };
		xdoc.async="false";
		return xdoc;
	}

	return xdoc;
}


function loadXMLString(txt)
{
	var xmlDoc;
	try
	{
		xmlDoc = new createXMLDocument();
		xmlDoc.loadXML(txt);
	}
	catch (e)
	{
	}
	return(xmlDoc);
}
*/




// Given an XML string it returns an XML document
function loadXMLString(xmlString)
{
	var xmlDoc;

	if (window.DOMParser)
	{	// Mozilla and Netscape browsers
		parser=new DOMParser();
		xmlDoc=parser.parseFromString(xmlString,"text/xml");
	}
	else if (window.ActiveXObject) // Internet Explorer
	{
		xmlDoc =  Try.these(
			function() { return new ActiveXObject('MSXML6.DOMDocument')   },
			function() { return new ActiveXObject('MSXML4.DOMDocument')   },
			function() { return new ActiveXObject('MSXML3.DomDocument') },
			function() { return new ActiveXObject('MSXML2.DomDocument') },
			function() { return new ActiveXObject('MSXML.DomDocument')  },
			function() { return new ActiveXObject('Microsoft.XmlDom')}
			) || null;
		if (!xmlDoc) throw "IE Browser is not providing XML document support.";
		xmlDoc.async="false";
		xmlDoc.loadXML(xmlString);
	}
	return(xmlDoc);
}


/**
 * Create a new Document object. If no arguments are specified,
 * the document will be empty. If a root tag is specified, the document
 * will contain that single root tag. If the root tag has a namespace
 * prefix, the second argument must specify the URL that identifies the
 * namespace.
 */
if (XML)
{
	XML.newDocument = function(rootTagName, namespaceURL) {
	  if (!rootTagName) rootTagName = "";
	  if (!namespaceURL) namespaceURL = "";
	  if (document.implementation && document.implementation.createDocument) {
		// This is the W3C standard way to do it
		return document.implementation.createDocument(namespaceURL, rootTagName, null);
	  }
	  else { // This is the IE way to do it
		// Create an empty document as an ActiveX object
		// If there is no root element, this is all we have to do
		var doc = new ActiveXObject("MSXML2.DOMDocument");
		// If there is a root tag, initialize the document
		if (rootTagName) {
		  // Look for a namespace prefix
		  var prefix = "";
		  var tagname = rootTagName;
		  var p = rootTagName.indexOf(':');
		  if (p != -1) {
			prefix = rootTagName.substring(0, p);
			tagname = rootTagName.substring(p+1);
		  }
		  // If we have a namespace, we must have a namespace prefix
		  // If we don't have a namespace, we discard any prefix
		  if (namespaceURL) {
			if (!prefix) prefix = "a0"; // What Firefox uses
		  }
		  else prefix = "";
		  // Create the root element (with optional namespace) as a
		  // string of text
		  var text = "<" + (prefix?(prefix+":"):"") +  tagname +
			  (namespaceURL
			   ?(" xmlns:" + prefix + '="' + namespaceURL +'"')
			   :"") +
			  "/>";
		  // And parse that text into the empty document
		  doc.loadXML(text);
		}
		return doc;
	  }
	};
}
