// JavaScript Document
var RefreshRate = 2000;
var ContestantCount = 0;
var ContestantPath = '/Contestants/Contestants2009/Contestant%20Bios/'; // include ending slash!!!

function StartScripting() { 
	 MonitorContestantCount();
	 for (c=0; c<=27; c++) {
		UpdateStatusImages(c);
	}
}

// CHECKS FOR CHANGE IN CONTESTANT COUNT AND THEN UPDATES PAGE
function MonitorContestantCount() {
	var h = GetHTTPRequest(true);
	 h.onreadystatechange = function() { CompareContestantCount(h); };
     h.open('GET', '?PeriodicCheck=1', true);
     h.send(null);
     setTimeout("MonitorContestantCount()",RefreshRate);
}

function CompareContestantCount(h) {
	if (h.readyState == 4) {
		if (h.status == 200) {
	 		var NewCount = -1;
			var XMLDoc =  h.responseXML;
			var NewCountNode = XMLDoc.getElementsByTagName("RemainingContestants").item(0);
			RefreshRate = XMLDoc.getElementsByTagName("RefreshRate").item(0).firstChild.data;
			NewCount = NewCountNode.firstChild.data;
			if (NewCount != ContestantCount) { 
				ContestantCount = NewCount;
				for (c=0; c<=27; c++) {
					UpdateStatusImages(c);
				}
			}
		} 
	}
}

function UpdateStatusImages(c) {
		if (document.getElementById('statusImage' + c)) {
			var h = GetHTTPRequest();
	 		h.onreadystatechange = function() { SetStatusImage(h, c); };
     		h.open('GET', '?GetTimeOnTruck=' + c, true);
     		h.send(null);
		}
}

function SetStatusImage(h, c) {
	if (h.readyState == 4) {
		if (h.status == 200) {
			var Status = h.responseText;
			var x = document.getElementById('statusImage' + c);
			if ((Status) && (x)) { 
				x.src = "http://img.stuckonatruck.com/images/redX.gif";
				if (Status == 'Winner') { x.src = "/images/winner.gif"; }
			} else {
				x.src = "http://img.stuckonatruck.com/images/blank.gif";
			}
		} 
	}
}



function GetHTTPRequest(isXML) {
	 var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
				if (isXML) {            
				if (httpRequest.overrideMimeType) {
               httpRequest.overrideMimeType('text/xml');
            } }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
         }
        if (!httpRequest) {
//            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
		return httpRequest;
}


///  GET CONTESTANT INFO  - GetContestants(c) & ShowContestantInfo(h)
function GetContestant(name, id) {
		var h = GetHTTPRequest();
        h.onreadystatechange = function() { ShowContestantInfo(h); };
		var today=new Date();
		var qs = today.getTime();
        h.open('GET', ContestantPath + name + '.php?ContestantID=' + id + '&time=' + qs, true);
        h.send(null);
}


function ShowContestantInfo(h) {
	if (h.readyState == 4) {
		if (h.status == 200) {
			y = document.getElementById('ContestantInfo');
			y.innerHTML = "";
			var x = document.createElement('div');
			x.innerHTML = h.responseText;
			y.appendChild(x);
		} else { alert("Error retrieving contestant bio.\r\n" + h.status); }
	}
}

