// Preload the appropriate images

function preload() {
  if (!document.images) return;
  var ar = new Array();
  var arguments = preload.arguments;
  for (var i = 0; i < arguments.length; i++) {
    ar = new Image();
    ar.src = arguments;
  }
}


//-------------------------------------------
// Create an XML document object parser using Javascript
// running in Internet Explorer 5.0 or later create
//-------------------------------------------

var xmlDoc=null;
var isIE= false;
var parsingCompleted=true;
var updateInterval=60000;

stra = window.location.search.substr(1).split("&"); 
height_str = stra[0].split("=");
length_str = stra[1].split("=");

var strliveid = height_str[1];
var recordid  = length_str[1];

var strDataSource= "http://www.meridix.com/mbp/football/" + recordid + ".xml";

//------------------------------------------------------
// The beginUpdates() function starts by checking which
// browser we're using and trying to get an XML document object to work with
//------------------------------------------------------

function beginUpdates()
{

	if (document.all) {
		isIE=true;
	}

	//determine if this is a standards-compliant browser like Mozilla
   	if (document.implementation && document.implementation.createDocument) {

      	//create the DOM Document
      	xmlDoc = document.implementation.createDocument("","", null);

   	} else if (isIE) {
		//create the DOM Document - IE specific
		xmlDoc = new ActiveXObject("microsoft.xmldom");
   	}

	if (xmlDoc==null) {
		//no supported way of loading the xml document - send user to error page.
		//alert("Your browser does not support XML processing, please upgrade.");

		window.location = "pleaseupgrade.htm"
	}
	else
	{
		//created initial xml doc ok -proceed with loading the xml document for the first time
		loadDoc();
	}

}



//------------------------------------------------------
// The loadDoc() function attaches a callback for handling the loaded xml document, then loads the document itself
// once loading is complete the callback function UpdateValues will be called and the page updated
//
// The XMLDOM parser reloads the same XML file. Because a server will provide this file, the web tags will be reevaluated to provide the latest values.
//------------------------------------------------------


function loadDoc()
{
	if (parsingCompleted==false) return; //skip trying to load the xml document if for some reason we didn't manage to complete parsing last time

	//setup a callback function to invoke (UpdateValues) after the xml document has been successfully loaded
	if (isIE) {
		xmlDoc.async = false;
		xmlDoc.onreadystatechange=UpdateValues; //callback on state change
	}
	else {
		//Mozilla/Standards Compliant callback
		xmlDoc.addEventListener("load", UpdateValues, false);
	}

	//now load the actual xml document
	xmlDoc.load(strDataSource);

}

//------------------------------------------------------
// The UpdateValues() retrieves data from the XML parser and updates the text of SPAN tags in the html web page.
//
// To notify the user of an update, the current time
// is retrieved using GetTime() and copied to the status bar.
//
// page will display
// 'ON' for '100 1' and 'OFF' for '0 0'.
//------------------------------------------------------

function UpdateValues()
{

	if (isIE && xmlDoc.readyState != "4") return;  //IE Specific check - return from callback if not ready to parse data

	if (xmlDoc==null) {
		alert("could not load xml document");
		return;
	}
	if (xmlDoc.documentElement==null) {
		alert("could not load xml document -error in document");
		return;
	}
	var rootElem = xmlDoc.documentElement;

	//example of copying values from the XML parser to the HTML tags

	var liveid;
        var event_name;
        var event_date;
        var br_type;
        var venue;
        var home_name;
        var home_record;
        var home_score;
	var away_name;
        var away_record;
        var away_score;
        var period;
        var time;
        var home_timeout;
        var away_timeout;
        var down;
        var ytg;
        var yard_line;
        var play;
	var away_q1;
	var away_q2;
	var away_q3;
	var away_q4;
	var home_q1;
	var home_q2;
	var home_q3;
	var home_q4;
	var possession;


	if (isIE) {

		liveid = rootElem.childNodes.item(0).text;
                event_name = rootElem.childNodes.item(1).text;
                event_date = rootElem.childNodes.item(2).text;
                br_type = rootElem.childNodes.item(3).text;
                venue = rootElem.childNodes.item(4).text;
		home_name = rootElem.childNodes.item(5).text;
		home_record = rootElem.childNodes.item(6).text;
		home_score = rootElem.childNodes.item(7).text;
		away_name = rootElem.childNodes.item(8).text;
		away_record = rootElem.childNodes.item(9).text;
		away_score = rootElem.childNodes.item(10).text;
		period = rootElem.childNodes.item(11).text;
		time = rootElem.childNodes.item(12).text;
		home_timeout = rootElem.childNodes.item(13).text;
		away_timeout = rootElem.childNodes.item(14).text;
		down = rootElem.childNodes.item(15).text;
		ytg = rootElem.childNodes.item(16).text;
		yard_line = rootElem.childNodes.item(17).text;
		play = rootElem.childNodes.item(18).text;
		away_q1 = rootElem.childNodes.item(19).text;
		away_q2 = rootElem.childNodes.item(20).text;
		away_q3 = rootElem.childNodes.item(21).text;
		away_q4 = rootElem.childNodes.item(22).text;
		home_q1 = rootElem.childNodes.item(23).text;
		home_q2 = rootElem.childNodes.item(24).text;
		home_q3 = rootElem.childNodes.item(25).text;
		home_q4 = rootElem.childNodes.item(26).text;
		possession = rootElem.childNodes.item(27).text;
		
	}
	else {

		var variableNodes = xmlDoc.getElementsByTagName("liveid");
		if (variableNodes[0].firstChild) liveid=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("event_name");
		if (variableNodes[0].firstChild) event_name=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("event_date");
		if (variableNodes[0].firstChild) event_date=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("br_type");
		if (variableNodes[0].firstChild) br_type=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("venue");
		if (variableNodes[0].firstChild) venue=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("home_name");
		if (variableNodes[0].firstChild) home_name=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("home_record");
		if (variableNodes[0].firstChild) home_record=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("home_score");
		if (variableNodes[0].firstChild) home_score=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("away_name");
		if (variableNodes[0].firstChild) away_name=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("away_record");
		if (variableNodes[0].firstChild) away_record=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("away_score");
		if (variableNodes[0].firstChild) away_score=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("period");
		if (variableNodes[0].firstChild) period=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("time");
		if (variableNodes[0].firstChild) time=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("home_timeout");
		if (variableNodes[0].firstChild) home_timeout=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("away_timeout");
		if (variableNodes[0].firstChild) away_timeout=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("down");
		if (variableNodes[0].firstChild) down=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("ytg");
		if (variableNodes[0].firstChild) ytg=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("yardline");
		if (variableNodes[0].firstChild) yard_line=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("play");
		if (variableNodes[0].firstChild) play=variableNodes[0].firstChild.nodeValue;


		variableNodes = xmlDoc.getElementsByTagName("away_q1");
		if (variableNodes[0].firstChild) away_q1=variableNodes[0].firstChild.nodeValue;


		variableNodes = xmlDoc.getElementsByTagName("away_q2");
		if (variableNodes[0].firstChild) away_q2=variableNodes[0].firstChild.nodeValue;


		variableNodes = xmlDoc.getElementsByTagName("away_q3");
		if (variableNodes[0].firstChild) away_q3=variableNodes[0].firstChild.nodeValue;


		variableNodes = xmlDoc.getElementsByTagName("away_q4");
		if (variableNodes[0].firstChild) away_q4=variableNodes[0].firstChild.nodeValue;


		variableNodes = xmlDoc.getElementsByTagName("home_q1");
		if (variableNodes[0].firstChild) home_q1=variableNodes[0].firstChild.nodeValue;


		variableNodes = xmlDoc.getElementsByTagName("home_q2");
		if (variableNodes[0].firstChild) home_q2=variableNodes[0].firstChild.nodeValue;


		variableNodes = xmlDoc.getElementsByTagName("home_q3");
		if (variableNodes[0].firstChild) home_q3=variableNodes[0].firstChild.nodeValue;


		variableNodes = xmlDoc.getElementsByTagName("home_q4");
		if (variableNodes[0].firstChild) home_q4=variableNodes[0].firstChild.nodeValue;

		variableNodes = xmlDoc.getElementsByTagName("possession");
		if (variableNodes[0].firstChild) possession=variableNodes[0].firstChild.nodeValue;


	}


	document.getElementById("liveid").innerHTML=liveid;
	document.getElementById("event_name").innerHTML=event_name;
	document.getElementById("event_date").innerHTML=event_date;
	document.getElementById("br_type").innerHTML=br_type;
	document.getElementById("venue").innerHTML=venue;
	document.getElementById("home_name").innerHTML=home_name;
	document.getElementById("home_record").innerHTML=home_record;
	document.getElementById("home_score").innerHTML=home_score;
	document.getElementById("away_name").innerHTML=away_name;
	document.getElementById("away_record").innerHTML=away_record;
	document.getElementById("away_score").innerHTML=away_score;
	document.getElementById("period").innerHTML=period;
	document.getElementById("time").innerHTML=time;
	document.getElementById("home_timeout").innerHTML=home_timeout;
	document.getElementById("away_timeout").innerHTML=away_timeout;
	document.getElementById("down").innerHTML=down;
	document.getElementById("ytg").innerHTML=ytg;
	document.getElementById("yard_line").innerHTML=yard_line;
	document.getElementById("play").innerHTML=play;
	document.getElementById("away_q1").innerHTML=away_q1;
	document.getElementById("away_q2").innerHTML=away_q2;
	document.getElementById("away_q3").innerHTML=away_q3;
	document.getElementById("away_q4").innerHTML=away_q4;
	document.getElementById("home_q1").innerHTML=home_q1;
	document.getElementById("home_q2").innerHTML=home_q2;
	document.getElementById("home_q3").innerHTML=home_q3;
	document.getElementById("home_q4").innerHTML=home_q4;
	document.getElementById("possession").innerHTML=possession;


	//example of doing additional data process. The following
	//3 lines of code may not be necessary.
	var mySNVT_switch= home_score;

	if (mySNVT_switch=='100 1') document.getElementById("home_score").innerHTML='ON';
	else if (mySNVT_switch=='0 0') document.getElementById("home_score").innerHTML='OFF';

	//update the status bar

	window.status = "MES Last Update: " + GetTime();

	parsingCompleted=true;

	//after a completed update, get ready to load the document again in 2 secs
	window.setTimeout("loadDoc()",updateInterval)
}


//------------------------------------------------------
// The GetTime() function retrieves the current date and
// time. The time is then formatted using the IfZero function
// to prefix a 0 for single digit numbers.
//------------------------------------------------------

function GetTime()
{
	var dt = new Date();
	curTime = (IfZero(dt.getHours()) + ":" +
	IfZero(dt.getMinutes()) + ":" + IfZero(dt.getSeconds()));
	return curTime;
}

//------------------------------------------------------
// The IfZero() function prefixes a 0 to a number if it is a
// single digit.
//
// This will guarantee that the hour, second, and minute
// fields are always formatted to hh:mm:ss
//------------------------------------------------------

function IfZero(num)
{
	return ((num <= 9) ? ("0" + num) : num);
}

