
//----------------------------------------------------------------------------------------------------------------------------------
//
//		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);
}

