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 url="/scoring/dyn/putts.xml";

    try {
        if (window.ActiveXObject) {
            var errorHappendHere = "Check Browser and security settings";
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async=false;
            xmlDoc.load(url);
        }
        else if(window.XMLHttpRequest) {
            var errorHappendHere = "Error handling XMLHttpRequest request";
            var d = new XMLHttpRequest();
            d.open("GET", url, false);
            d.send(null);
            xmlDoc=d.responseXML;
        } else {
            var errorHappendHere = "Error.";
            xmlDoc = document.implementation.createDocument("","",null);
            xmlDoc.async=false;
        xmlDoc.load(url);
        }
    }
    catch(e)	{
        alert(errorHappendHere);
    }

	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='/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('pph1') != null) {
			tbl = tbl + x[i].getAttribute('pph1');
		}
		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('pph2') != null) {
			tbl = tbl + x[i].getAttribute('pph2');
		}
		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('pph3') != null) {
			tbl = tbl + x[i].getAttribute('pph3');
		}
		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('pph4') != null) {
			tbl = tbl + x[i].getAttribute('pph4');
		}
		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('pph_ave') != null) {
			tbl = tbl + x[i].getAttribute('pph_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();
}

