
// used to find the Automation server name
function getDomDocumentServerName() {
	//if (getDomDocumentPrefix.prefix)
	//	return getDomDocumentPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var sufixes = [".4.0", ".3.0", ".2.0", ""];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			for (var j = 0; j < sufixes.length; j++) {
				try{
					o = new ActiveXObject(prefixes[i] + ".DomDocument" +sufixes[j]);
					return getDomDocumentServerName.prefix = prefixes[i] + ".DomDocument" +sufixes[j];
				}
				catch (ex) {};
			}
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

function getXmlHttpPrefix() {
	if (getXmlHttpPrefix.prefix)
		return getXmlHttpPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	
	throw new Error("Could not find an installed XML parser");
}

//////////////////////////
// Start the Real stuff //
//////////////////////////


// XmlHttp factory
function XmlHttp() {}

XmlHttp.create = function () {
	try {
		if (window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
			// some versions of Moz do 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;
		}
		if (window.ActiveXObject) {
			return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (ex) {}
	// fell through
	//throw new Error("Your browser does not support XmlHttp objects");
};

// XmlDocument factory
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;
		}
		if (window.ActiveXObject) {
			return new ActiveXObject( getDomDocumentServerName() );
		}
	}
	catch (ex) {}
	//throw new Error("Your browser does not support XmlDocument objects");
};

XmlDocument.transform = function ( xmlDoc, xsltDoc, obj ) {
	try
	{
		// DOM2
		if( document.implementation && document.implementation.createDocument ) {
			var xslt = new XSLTProcessor();
			xslt.importStylesheet( xsltDoc );
			var resultDoc = xslt.transformToFragment(xmlDoc,document);
			var xmls = new XMLSerializer();
			obj.innerHTML = '';
			obj.appendChild( resultDoc );
		}
		if( window.ActiveXObject ) {
			obj.innerHTML = xmlDoc.transformNode( xsltDoc );
		}
	}
	catch (ex){
		//throw new Error("Your browser does not support XmlDocument objects");
	}
};

// 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) {
		// 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));
		}
	};
	
	
	/*
	 * xml getter
	 *
	 * This serializes the DOM tree to an XML String
	 *
	 * Usage: var sXml = oNode.xml
	 *
	 */
	// XMLDocument did not extend the Document interface in some versions
	// of Mozilla. Extend both!
	/*
	XMLDocument.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
	*/
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}

	function insertContent( xmlUrl, xsltUrl, objName, loadingText, time ) {
		var xmlDoc;
		var xsltDoc;
		var obj = document.getElementById( objName );	

		//display warning( xml is loading )
		if( obj && loadingText && loadingText != "" )
			obj.innerHTML = loadingText;

		var xmlHttp = XmlHttp.create();
		xmlHttp.open( "GET", xmlUrl, true );
		xmlHttp.onreadystatechange = function () {
			if( xmlHttp.readyState == 4 ) {
				xmlDoc = XmlDocument.create();
				xmlDoc.loadXML( xmlHttp.responseXML.xml );
				xsltDoc = XmlDocument.create();
				xsltDoc.load( xsltUrl );
				xsltDoc.onreadystatechange = function () {
					if( xmlHttp.readyState == 4 ) {
							XmlDocument.transform( xmlDoc, xsltDoc, obj );
							initMenus( obj );
							//update the content
							if( time && time != 0 ) 
								window.setTimeout( "insertContent( '"+xmlUrl+"','" + xsltUrl + "','" + objName + "','" + loadingText + "'," + time + ")",time * 1000 ); 
					}
				}
			}
		}
		xmlHttp.send( null );
	}

/*
	Expandable Listmenu Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
*/

function initMenus( obj ) {
	if (!document.getElementsByTagName) return;
	
	var aMenus = obj.getElementsByTagName("LI");
	for( var i = 0; i < aMenus.length; i++ ) {
		var mclass = aMenus[i].className;
		if( typeof(mclass) != 'undefined' && mclass.indexOf("treenode") > -1 ) {
			var submenu = aMenus[i].childNodes;
			for (var j = 0; j < submenu.length; j++) {
				if (submenu[j].tagName == "A") {
					
					submenu[j].onclick = function() {
						var node = this.nextSibling;
											
						while (1) {
							if (node != null) {
								if (node.tagName == "UL") {
									var d = (node.style.display == "none")
									node.style.display = (d) ? "block" : "none";
									this.className = (d) ? "treeopen" : "treeclosed";
									return false;
								}
								node = node.nextSibling;
							} else {
								return false;
							}
						}
						return false;
					}
					
					submenu[j].className = (mclass.indexOf("open") > -1) ? "treeopen" : "treeclosed";
				}
				
				if (submenu[j].tagName == "UL")
					submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none";
			}
		}
	}
}

