/********************************************************
 * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
 * CodeHouse(TM) is a registered trademark.
 *
 * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
 * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
 * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
 * 
 * CodeHouse.com JavaScript Library Module: Load XML Document Method
 *
 * You can obtain this script at http://www.codehouse.com
 ********************************************************/

var BrowserType;

function CJL_loadXmlDocument(xmlFile, cbFun)

{
/*
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  BrowserType="FF";
  }
else // Internet Explorer 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  BrowserType="IE";
  }
xhttp.open("GET",xmlFile,false);
xhttp.send("");
xmlDoc=xhttp.responseXML;
cbFun(xmlDoc);
return true;
*/
   var xdoc;

   if( window.ActiveXObject && /Win/.test(navigator.userAgent) )
   {
      xdoc = new ActiveXObject("Microsoft.XMLDOM");

      xdoc.async = false;
     myIdentifier=Math.round(Math.random()*10000);
     xdoc.load(xmlFile + "?uniq=" + myIdentifier);
	  BrowserType="IE";

      cbFun(xdoc);

      return true;
   }
   else if( document.implementation && document.implementation.createDocument )
   {
     xdoc = document.implementation.createDocument("", "", null);
     myIdentifier=Math.round(Math.random()*10000);
     xdoc.load(xmlFile + "?uniq=" + myIdentifier);
	  BrowserType="FF";

      xdoc.onload = function(evt)
      {
      cbFun(xdoc);
      }

      return true;
   }
   else
   {
      return false;
   }
}

function myTroutCallBack(xdoc)
{
	try {
		xstuff = xdoc.documentElement;
		} catch(err) {
			txt="Error loading the trout table.\n\n";
			  txt+="Error description: " + err.description + "\n\n";
			  txt+="Click OK to continue.\n\n";
//			  alert(txt);
		}
	try {
		loadStandings(xstuff);
		} catch(err) {
			txt="Error loading the trout standings table.\n\n";
			  txt+="Error description: " + err.description + "\n\n";
			  txt+="Click OK to continue.\n\n";
//			  alert(txt);
		}
	try {
		dynamictable("TroutTable");
		} catch(err) {
			txt="Error creating the trout display table.\n\n";
			  txt+="Error description: " + err.description + "\n\n";
			  txt+="Click OK to continue.\n\n";
			  alert(txt);
		}
}

function myWhitefishCallBack(xdoc)
{
	try {
		xstuff = xdoc.documentElement;
		} catch(err) {
			txt="Error loading the whitefish table.\n\n";
			  txt+="Error description: " + err.description + "\n\n";
			  txt+="Click OK to continue.\n\n";
//			  alert(txt);
		}
	try {
		loadStandings(xstuff);
		} catch(err) {
			txt="Error loading the whitefish standings table.\n\n";
			  txt+="Error description: " + err.description + "\n\n";
			  txt+="Click OK to continue.\n\n";
//			  alert(txt);
		}
	try {
		dynamictable("WhitefishTable");
		} catch(err) {
			txt="Error creating the whitefish display table.\n\n";
			  txt+="Error description: " + err.description + "\n\n";
			  txt+="Click OK to continue.\n\n";
			  alert(txt);
		}
}

var Weight = new Array();
var ParticipantNumber = new Array();
var FirstName = new Array();
var LastName = new Array();
var City = new Array();
var Lake = new Array();
var FishDate = new Array();
var FishTime = new Array();
var Province = new Array();

var participantIndex;
var dataIndex;

function loadStandings(tree)
{
	ParticipantNumber = new Array();
	Weight = new Array();
	FirstName = new Array();
	LastName = new Array();
	City = new Array();
	Lake = new Array();
	FishDate = new Array();
	FishTime = new Array();
	Province = new Array();

	dataIndex = "" ;

	participantIndex = -1;

    if(tree.hasChildNodes())
    {
        var nodes=tree.childNodes.length;
        for(var i=0; i<tree.childNodes.length; i++)
            outputStandingsLevel2(tree.childNodes[i]);
    }
}

// Level 2 is where we see the report heading and the participant entries

function outputStandingsLevel2(tree)
{
//        alert("Level 2 Node Tag:" + tree.tagName)
	if(tree.hasChildNodes())
	{
        if (tree.tagName == "participant")
        {
			participantIndex += 1;
			dataIndex = ""; 
		}
        var nodes=tree.childNodes.length;
        for(var i=0; i<tree.childNodes.length; i++)
            outputStandingsLevel3(tree.childNodes[i]);
	}
}

function outputStandingsLevel3(tree)
{
	if(tree.hasChildNodes())
	{
        dataIndex = tree.tagName;
        var nodes=tree.childNodes.length;
        for(var i=0; i<tree.childNodes.length; i++)
        {
            if (dataIndex == "weight")
            {
            	if (BrowserType == "IE") {
            		Weight[participantIndex] = [tree.childNodes[i].text,participantIndex];
            	} else {
            		Weight[participantIndex] = [tree.childNodes[i].textContent,participantIndex];
				}
            } else if (dataIndex == "participant_number")
            {
            	if (BrowserType == "IE") {
            		ParticipantNumber[participantIndex] = tree.childNodes[i].text;
            	} else {
            		ParticipantNumber[participantIndex] = tree.childNodes[i].textContent;
            	}
            } else if (dataIndex == "first-name")
            {
            	if (BrowserType == "IE") {
            		FirstName[participantIndex] = tree.childNodes[i].text;
            	} else {
            		FirstName[participantIndex] = tree.childNodes[i].textContent;
            	}
            } else if (dataIndex == "last-name")
            {
            	if (BrowserType == "IE") {
            		LastName[participantIndex] = tree.childNodes[i].text;
            	} else {
            		LastName[participantIndex] = tree.childNodes[i].textContent;
            	}
            } else if (dataIndex == "city")
            {
            	if (BrowserType == "IE") {
            		City[participantIndex] = tree.childNodes[i].text;
            	} else {
            		City[participantIndex] = tree.childNodes[i].textContent;
            	}
            } else if (dataIndex == "date")
            {
            	if (BrowserType == "IE") {
            		FishDate[participantIndex] = tree.childNodes[i].text;
            	} else {
            		FishDate[participantIndex] = tree.childNodes[i].textContent;
            	}
            } else if (dataIndex == "time")
            {
            	if (BrowserType == "IE") {
            		FishTime[participantIndex] = tree.childNodes[i].text;
            	} else {
            		FishTime[participantIndex] = tree.childNodes[i].textContent;
            	}
            } else if (dataIndex == "lake")
            {
            	if (BrowserType == "IE") {
            		Lake[participantIndex] = tree.childNodes[i].text;
             	} else {
            		Lake[participantIndex] = tree.childNodes[i].textContent;
            	}
           	} else if (dataIndex == "province")
           	{
            	if (BrowserType == "IE") {
            		Province[participantIndex] = tree.childNodes[i].text;
            	} else {
            		Province[participantIndex] = tree.childNodes[i].textContent;
            	}
			}		
		}
	}
}

function dynamictable(tableName)
{
var j = new Array();
var table1;
var tmpRow;
var tmpDateTime;
var rowShade=false;

try
{
table1=document.createElement("table");
table1.id="tab1";
table1.border=0;
var tmpCell = null;
tmpRow=table1.insertRow(0);
tmpCell=tmpRow.insertCell(0);
tmpCell.innerHTML = "";
tmpCell.width="8px";
tmpCell=tmpRow.insertCell(1);
tmpCell.innerHTML = "Weight";
tmpCell.align="center";
tmpCell=tmpRow.insertCell(2);
tmpCell.innerHTML = "Name";
tmpCell.width="175px";
tmpCell=tmpRow.insertCell(3);
tmpCell.innerHTML = "Caught";
tmpCell.width="150px";
Weight.sort(mySorting);

	for(var n=0; n<participantIndex+1; n++) {
		j=Weight[n];
		i=j[1];
		tmpRow=table1.insertRow((n*3)+1);
		tmpRow.height="10px";
		tmpCell=tmpRow.insertCell(0);
		
		tmpRow=table1.insertRow((n*3)+2);
		if (rowShade) {
			tmpRow.style.color="#FFFF00";
		} else {
			tmpRow.style.color="#FFFFFF";
		}
		tmpCell=tmpRow.insertCell(0);
		tmpCell.innerHTML  = n+1;
		tmpCell=tmpRow.insertCell(1);
		tmpCell.innerHTML  = j[0];
		tmpCell.align="center";
		tmpCell=tmpRow.insertCell(2);
		tmpCell.innerHTML  = "#"+ParticipantNumber[i]+" - "+FirstName[i]+" "+LastName[i];
		tmpCell=tmpRow.insertCell(3);
		tmpDateTime=FishDate[i];
		try
		{
		    if (typeof(FishTime[i]) !== "undefined" && FishTime[i] != null)
		    {
				tmpDateTime = tmpDateTime+" at "+FishTime[i];
			}
		} catch(err) {
		}
			
		tmpCell.innerHTML  = tmpDateTime;
		
		tmpRow=table1.insertRow((n*3)+3);
		if (rowShade) {
			tmpRow.style.color="#FFFF00";
		} else {
			tmpRow.style.color="#FFFFFF";
		}
		tmpCell=tmpRow.insertCell(0);
		tmpCell=tmpRow.insertCell(1);
		tmpCell=tmpRow.insertCell(2);
		tmpCell.innerHTML  = City[i]+", "+Province[i];
		try
		{
		    if (typeof(Lake[i]) !== "undefined" && Lake[i] != null && Lake[i] !== "N/A")
		    {
				tmpCell=tmpRow.insertCell(3);
				tmpCell.innerHTML=Lake[i];
			}
		} catch(err) {
		}
		
		rowShade=!rowShade;
	}		
	try
		{
		
		document.getElementById(tableName).appendChild(table1);
		} catch(err) {
			txt="Error loading the "+tableName+" table onto the web page.\n\n";
			  txt+="Error description: " + err.description + "\n\n";
			  txt+="Click OK to continue.\n\n";
			  alert(txt);
		}
	} catch(err) {
		txt="Error making a table.\n\n";
		  txt+="Error description: " + err.description + "\n\n";
		  txt+="Click OK to continue.\n\n";
		  alert(txt);
	}

} 
function mySorting(a,b) {
a = a[0];
b = b[0];
return b - a;
}

function RedoMe()
{
	try {
		var P2;
		P2=document.getElementById("TroutTable");
		P2.removeChild(P2.lastChild);
		P2=document.getElementById("WhitefishTable");
		P2.removeChild(P2.lastChild);
	} catch(err) {
		txt="Error removing the old tables.\n\n";
		txt+="Error description: " + err.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
	}
	LoadStandings();
}
function LoadStandings()
{
	var sofDerbyDay1 = new Date(gstrDerbyDay1Date + "," + gstrDerbyDay1StartTime); // Load derby day 1 start date and time
    var eofDerbyDay2 = new Date(gstrDerbyDay2Date + "," + gstrDerbyDay2EndWeighinTime); // Load derby day 2 end date and time
	var today = new Date();
	
	if (today < sofDerbyDay1) {
		var msg="Once the derby starts, the current standings will be shown, similar to that seen below.<br />During the derby, this page will automatically reload every ten minutes.<br />&nbsp;";
		CJL_loadXmlDocument("standings/demo-trout.xml", myTroutCallBack);
		CJL_loadXmlDocument("standings/demo-whitefish.xml", myWhitefishCallBack);
	
	    document.getElementById("StandingsComing").innerHTML = msg ;
	    document.getElementById("StandingsDateStamp").innerHTML = "" ;
	} else {
	
		CJL_loadXmlDocument("standings/trout.xml", myTroutCallBack);
		CJL_loadXmlDocument("standings/whitefish.xml", myWhitefishCallBack);
		
		var msg="Results as of " + today.toDateString() + " " + today.toLocaleTimeString()
		if (today < eofDerbyDay2) {
			msg = msg + " (page will automatically refresh every ten minutes)";
			setTimeout("RedoMe()", 1000*60*10);
		}
    	document.getElementById("StandingsDateStamp").innerHTML = msg ;
    	document.getElementById("StandingsComing").innerHTML = "" ;
    }
}


