/*
 * xsltTransform
 *
 * Written By: Donald Milton
 * Version 0.5.2
 * Modified: June 23, 2010 02:40pm EST
 * Date: June 18, 2010 02:20am EST
 *
 * This JavaScript is based upon many versions floating
 * around on the net. The biggest problem I had was in
 * understanding the way XML/XSLT files are loaded client
 * side. One problem I encountered is that sometimes the
 * XML file would load successfully according to the 
 * status but the responseXML object would be null and 
 * the responseText wuld have the actual file contents
 * loaded into the string value. This "version" takes
 * that possibility into account and forces the text
 * to be parsed into the proper XML document required 
 * to do the XSLT Transform.
 *
 * Many thanks to all that posted their solutions out 
 * on the web that gave some insight into what I needed
 * to do and to w3schools.com for their wonderful tutorial
 * on XML and XSLT Transformations.
 * 
 * And even though I hate to admit it, the MSDN site was
 * actually helpful in getting the proper way to do XSLT
 * using ActiveX objects. This was the tough one, but now
 * it works!
 *
 */

/*
 * loadXMLDoc loads a xml file from the file located at
 * docName. It does not support cross domain. Although
 * I have not tested it, I believe it will NOT work on a
 * local machine (because of security issues?).
 */
function loadXMLDoc(docName)
{
	// Are we using Internet Exploder? 
	if (window.ActiveXObject)
	{
		// Let's get the right object ready
		xhttp=new ActiveXObject("Msxml2.DOMDocument.3.0");
		// Set the asynchronous mode off
		// This will cause the script to wait until loading is complete.
		xhttp.async = false;
		// Load the requested file (Note: If it was a string, we would use
		// loadXML instead. Might want to keep that in mind for later)
		xhttp.load(docName);
		// Should be nothing else to do here so return it
		return xhttp;
	}
	// Check if we are in a non-MS browser like Mozilla, Safari, Chrome
	else if (window.XMLHttpRequest)
	{
		xhttp=new XMLHttpRequest();
		// Load the requested Doc and do so synchronously
		// I may want to change this into a asynchronsis version later
		// but for now it works!  
		xhttp.open("GET",docName,false);
		xhttp.send("");
		// And now for something we hope you really like!
		// (Hey Rocky I think I need another hat!)
		// At this point everything is fine...or so we think!
		// But we better check to see if the responseXML is really there. 
		if (xhttp.responseXML != null)
		{
			// It seems to be there alright, so return it!
			return xhttp.responseXML;
		}
		// Well it isn't there! So let us now use responseText to parse the
		// XML file we loaded and pass it on!
		else 
		{
			// Take the responseText and parse it into an XML Doc
			return (new DOMParser()).parseFromString(xhttp.responseText, "text/xml") ;
		}
	}
	// Is is another browser type? I have seen many referneces to this. 
	else if (window.createRequest)
	{
		alert("Browser not IE nor Mozilla/Webkit...");
		xhttp=new window.createRequest();
		xhttp.open("GET",docName,false);
		xhttp.send("");
		return xhttp;
	}
	else 
	{
		alert("XMLHttpRequest not supported on this browser (maybe?)!");
		return null;
	} 
}

/*
 * xsltTransform loads the specified xml and xsl files,
 * and does the XSLT transformation placing it in the
 * section named 'target'.
 */
function xsltTransform(xmlSource, xslSource, target)
{
	xmlDoc=loadXMLDoc(xmlSource);
	xslDoc=loadXMLDoc(xslSource);

	// Now that we have our proper XML and XSL files create the document
	// code for Mozilla, Firefox, Safari, Opera, or other W3C compliant browser
	if (document.implementation && document.implementation.createDocument)
	{
		xsltProcessor=new XSLTProcessor();
		try
		{
			xsltProcessor.importStylesheet(xslDoc);
			resultDoc = xsltProcessor.transformToFragment(xmlDoc,document);
			document.getElementById(target).appendChild(resultDoc);
		}
		catch (e)
		{
			resultDoc="Transformation Error!";
			document.getElementById(target).innerHTML=resultDoc;			
			alert(e);
		}
		//document.getElementById(target).replaceChild(resultDoc);
	}
	// code for Exploder
	else if (window.ActiveXObject)
	{
		try
		{
			resultDoc=xmlDoc.transformNode(xslDoc);
		}
		catch (e)
		{
			resultDoc="Transformation Error!"
			alert(e);
		}
		document.getElementById(target).innerHTML=resultDoc;
	}
	else
	{ 
		alert("Sorry! Your browser does not seem to support XSLT.\nTry another browser like FireFox 3.6 or Safari!");
	}
}

