var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
var showCut;
var finalCut;
var cutLine;
var underCutLine;
var madeCut;
var coursePar;
var roundsCompleted;

function loadXML() {

	var xmlDoc;
	// code for IE
	if (window.ActiveXObject) {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument) {
		xmlDoc=document.implementation.createDocument("","",null);
	} else {
		alert('Your browser cannot handle this script');
	}
	
	xmlDoc.async=false;
	
	var dtmNow = new Date();
	var strMS = dtmNow.valueOf;
	xmlDoc.load("/2008/scoring/dyn/gir.xml");

	document.getElementById("eventName").innerHTML=showName(xmlDoc);
	document.getElementById("rNo").innerHTML=showRound(xmlDoc);
	document.getElementById("updateTime").innerHTML=showTime(xmlDoc);
	document.getElementById("leaderBoard").innerHTML=showTable(xmlDoc);
}

function showTable(mDoc) {

	var priorAve;
	var x=mDoc.getElementsByTagName('player');
	var tbl="<table width='910' cellpadding='2' cellspacing='0' border='0' align='center' class='mainTbl'>";
	for (i=0;i<x.length;i++) {
		if (i%2==0) {
			tbl = tbl + "<tr height='22' align='center' class='oddplayername'>";    
		} else {
			tbl = tbl + "<tr height='22' align='center' class='evenplayername'>";
		}

		tbl = tbl + "<td width='7%'>";   
		if (i==0) {
			tbl = tbl + (i+1);
			priorAve = x[i].getAttribute('average');
		} else if (x[i].getAttribute('average') != priorAve) {
			tbl = tbl + (i+1);
			priorAve = x[i].getAttribute('average');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='7%'>";
		tbl = tbl + x[i].getAttribute('position');
		tbl = tbl + "</td>";
		
		tbl = tbl + "<td align='left' id='playerlinks'>";
		tbl = tbl + "<a href='card/card.html?id=";
		tbl = tbl + x[i].getAttribute('idint');
		tbl = tbl + "'>";
		tbl = tbl + x[i].getAttribute('name');
		tbl = tbl + "</a>";
		tbl = tbl + "</td>";
		
		tbl = tbl + "<td align='left' width='7%'>";
		tbl = tbl + "<img src='/2008/images/scoring/flag/";
		tbl = tbl + x[i].getAttribute('country');
		tbl = tbl + ".gif'>";
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('rd1') != null) {
			tbl = tbl + x[i].getAttribute('rd1');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('pct_rd1') != null) {
			tbl = tbl + x[i].getAttribute('pct_rd1');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('rd2') != null) {
			tbl = tbl + x[i].getAttribute('rd2');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('pct_rd2') != null) {
			tbl = tbl + x[i].getAttribute('pct_rd2');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('rd3') != null) {
			tbl = tbl + x[i].getAttribute('rd3');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('pct_rd3') != null) {
			tbl = tbl + x[i].getAttribute('pct_rd3');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('rd4') != null) {
			tbl = tbl + x[i].getAttribute('rd4');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('pct_rd4') != null) {
			tbl = tbl + x[i].getAttribute('pct_rd4');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='5%'>";
		if (x[i].getAttribute('average') != null) {
			tbl = tbl + x[i].getAttribute('average');
		}
		tbl = tbl + "</td>";

		tbl = tbl + "<td width='4%'>";
		if (x[i].getAttribute('pct_ave') != null) {
			tbl = tbl + x[i].getAttribute('pct_ave');
		}
		tbl = tbl + "</td>";
		
		tbl = tbl + "</tr>";
	}
	tbl = tbl + "</table>";
	return tbl;
}

function showName(mDoc){
	var x=mDoc.getElementsByTagName('tournament');
	document.title = x[0].getAttribute('name');
	return x[0].getAttribute('name');
}

function showRound(mDoc){
	var x=mDoc.getElementsByTagName('tournament');
	return x[0].getAttribute('currentround');
}

function showTime(mDoc){
	var x=mDoc.getElementsByTagName('event');
	return x[0].getAttribute('localtime');
}

function InitializeTimer(){
	// Set the length of the timer, in seconds
	secs = 60
	StartTheTimer()
}

function StopTheClock(){
	if (timerRunning)
		clearTimeout(timerID)
	timerRunning = false
}

function StartTheTimer(){
	if (secs==0) {
		StopTheClock();
		loadXML();
		InitializeTimer();
	} else {
		self.status = secs
		secs = secs - 1
		timerRunning = true
		timerID = self.setTimeout("StartTheTimer()", delay)
	}
}

function InitializePage(){
	loadXML();
	InitializeTimer();
}





