/****************************[   Program Information   ]***********************
*
*	System:		 Live Football Stats
*	File:		 showstats.js
*	Authors:	 Deborah L Johnson
*	Date:		 August 2005
*	Description: Javascript for creating a XSL processor and using it to
*				 transform the xml files based on the user's request (which
*				 is done by selecting a menu link)  First time through
*				 it's defaulted to the summary screen.
*
*****************************[   Change Information   ]************************
*
*	Programmer:
*	Date:
*	Description:
*
*******************************************************************************/
var xmlFile = "/athletics/mediastats/livefb.xml";
var xslFile = "livefb.xsl";
var xmlDoc;
var selection;
var timeout;

/* IE & Firefox use different methods of creating an XSL Processor.
   First we try to create a XSL Processor the IE way and if that fails
   we try it the Firefox way otherwise we log a XSL error.
*/
try {
	var xsltStylesheet = new ActiveXObject("MSXML2.FreeThreadedDOMDocument.4.0");
	xsltStylesheet.async = false;
	xsltStylesheet.load(xslFile);
	
	template = new ActiveXObject("MSXML2.XSLTemplate.4.0");
	template.stylesheet = xsltStylesheet;
	processor = template.createProcessor();
}
catch(e) {
	try{
		var xsltProcessor = new XSLTProcessor();
		var myXMLHTTPRequest = new XMLHttpRequest();
		myXMLHTTPRequest.open("GET", xslFile, false);
		myXMLHTTPRequest.send(null);
		xslStylesheet = myXMLHTTPRequest.responseXML;
		xsltProcessor.importStylesheet(xslStylesheet);		
	}
	catch(e) {
		alert("Unable to create a XSL processor.");
	}
}

/***************************[   Function Information   ]***********************
*
*	Fuction: football
*	Description: This function is called by the body onload trigger.
*	Selection is defaulted to 'summary' the first time through but after that
*	it is controlled by the user selecting a menu link.  This function
*	controls the refreshing of the screen.
*
*******************************************************************************/
function football(selection)
{
	if (refresh(selection))
	{
		window.clearInterval(timeout);
		timeout = window.setInterval('refresh("' + selection + '")',10000);
	}
}

/***************************[   Function Information   ]***********************
*
*	Fuction: refresh
*	Description: This function is called by the football function.
*	It loads the XML files everytime and then uses the already defined
*	XSL Processor to display the html output.
*
*	IE & Firefox use different methods of transforming XML data by using
*	a XSL Processor.  First we try to transform the data the IE way and 
*	if that fails then we try it the Firefox way otherwise we log an error.
*
*
*******************************************************************************/
function refresh(selection)
{
	try {

		var source = new ActiveXObject("MSXML2.DOMDocument.4.0");
		source.async = false;
		source.validateOnParse = false;
		source.load(xmlFile);
		if (source.parseError.errorCode == 0)
		{
			processor.input = source;
			processor.addParameter('selection',selection);
			processor.transform();
			target.innerHTML = processor.output;
		}
		return true;
	}
	catch(e) {
		try{
			myXMLHTTPRequest.open("GET", xmlFile, false);
			myXMLHTTPRequest.send(null);
			xmlDoc = myXMLHTTPRequest.responseXML;
			if(myXMLHTTPRequest.responseText.indexOf('/fbgame')>0)
			{
   				xsltProcessor.setParameter(null,'selection',selection);
				fragment = xsltProcessor.transformToFragment(xmlDoc, document);  
				document.getElementById("target").innerHTML = "";
				document.getElementById("target").appendChild(fragment);
			}
			return true;
		}
		catch(e) {
			return true;
		}
	}
}
