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/amateur.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);
    }

    checkCut(xmlDoc);
    getPar(xmlDoc);
    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 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 (showCut == 'true') {
            if (finalCut == 'true') {
                if (i == madeCut - 1) {
                    if (madeCut == 1) {
                        tbl = tbl + "<tr class='bold'>";
                        tbl = tbl + "<td align='center' valign='middle' colspan='11' height='30'>No Amateurs Made the Cut</td>";
                        tbl = tbl + "</tr>";
                    }
                    tbl = tbl + "<tr bgcolor='#A8ADBE' height='22'>";
                    tbl = tbl + "<td align='right' valign='top'>";
                    tbl = tbl + "<img src='/images/scoring/img/cutL.gif'/>";
                    tbl = tbl + "</td>";
                    tbl = tbl + "<td colspan='2' background='/images/scoring/img/cutr.gif' align='center'>";
                    tbl = tbl + "</td>";
                    tbl = tbl + "<td colspan='2' align='center' class='csumbold'>Failed to Qualify</td>";
                    tbl = tbl + "<td colspan='6' background='/images/scoring/img/cutr.gif' align='center'>";
                    tbl = tbl + "</td>";
                    tbl = tbl + "</tr>";
                }
            }
            if (finalCut == 'false') {
                if (i == underCutLine - 1) {
                    tbl = tbl + "<tr bgcolor='#A8ADBE' height='22'>";
                    tbl = tbl + "<td align='right' valign='top'>";
                    tbl = tbl + "<img src='/images/scoring/img/cutL.gif'/>";
                    tbl = tbl + "</td>";
                    tbl = tbl + "<td colspan='2' background='/images/scoring/img/cutr.gif' align='center'>";
                    tbl = tbl + "</td>";
                    tbl = tbl + "<td colspan='2' align='center' class='csumbold'>Projected Cut</td>";
                    tbl = tbl + "<td colspan='6' background='/images/scoring/img/cutr.gif' align='center'>";
                    tbl = tbl + "</td>";
                    tbl = tbl + "</tr>";
                }
            }
        }        
        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='10%'>";   
        tbl = tbl + x[i].getAttribute('position');
        tbl = tbl + "</td>";
        
        tbl = tbl + "<td align='left' id='playerlinks' width='30%'>";
        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'>";
        tbl = tbl + "<img src='/images/scoring/flag/";
        tbl = tbl + x[i].getAttribute('country');
        tbl = tbl + ".gif'>";
        tbl = tbl + "</td>";

        tbl = tbl + "<td width='8%' class='bolddefault'>";
        if (parseInt(x[i].getAttribute('totaltopar')) < 0) {
            tbl = tbl + "<font color='#FF0033'>";
            tbl = tbl + x[i].getAttribute('totaltopar');
            tbl = tbl + "</font>";
        } else {
            tbl = tbl + x[i].getAttribute('totaltopar');
        }
        tbl = tbl + "</td>";

        tbl = tbl + "<td width='9%'>";
        tbl = tbl + x[i].getAttribute('thru');
        tbl = tbl + "</td>";

        tbl = tbl + "<td width='8%'>";

        if (parseInt(x[i].getAttribute('today')) < 0) {
            tbl = tbl + "<font color='#FF0033'>";
            tbl = tbl + x[i].getAttribute('today');
            tbl = tbl + "</font>";
        } else {
            tbl = tbl + x[i].getAttribute('today');
        }
        tbl = tbl + "</td>";

        tbl = tbl + "<td width='5%'>";
        if (x[i].getAttribute('R1') != null) {
            if (parseInt(x[i].getAttribute('R1')) < parseInt(coursePar)) {
                tbl = tbl + "<font color='#FF0033'>";
                tbl = tbl + x[i].getAttribute('R1');
                tbl = tbl + "</font>";
            } else {
                tbl = tbl + x[i].getAttribute('R1');
            }
        }
        tbl = tbl + "</td>";

        tbl = tbl + "<td width='5%'>";
        if (x[i].getAttribute('R2') != null) {
            if (parseInt(x[i].getAttribute('R2')) < parseInt(coursePar)) {
                tbl = tbl + "<font color='#FF0033'>";
                tbl = tbl + x[i].getAttribute('R2');
                tbl = tbl + "</font>";
            } else {
                tbl = tbl + x[i].getAttribute('R2');
            }
        }
        tbl = tbl + "</td>";

        tbl = tbl + "<td width='5%'>";
        if (x[i].getAttribute('R3') != null) {
            if (parseInt(x[i].getAttribute('R3')) < parseInt(coursePar)) {
                tbl = tbl + "<font color='#FF0033'>";
                tbl = tbl + x[i].getAttribute('R3');
                tbl = tbl + "</font>";
            } else {
                tbl = tbl + x[i].getAttribute('R3');
            }
        }
        tbl = tbl + "</td>";

        tbl = tbl + "<td width='5%'>";
        if (x[i].getAttribute('R4') != null) {
            if (parseInt(x[i].getAttribute('R4')) < parseInt(coursePar)) {
                tbl = tbl + "<font color='#FF0033'>";
                tbl = tbl + x[i].getAttribute('R4');
                tbl = tbl + "</font>";
            } else {
                tbl = tbl + x[i].getAttribute('R4');
            }
        }
        tbl = tbl + "</td>";

        tbl = tbl + "<td width='5%'>";
        if (x[i].getAttribute('strokes') != null) {
            if (parseInt(x[i].getAttribute('strokes')) < (parseInt(coursePar)*parseInt(x[i].getAttribute('roundsCompleted')))) {
                tbl = tbl + "<font color='#FF0033'>";
                tbl = tbl + x[i].getAttribute('strokes');
                tbl = tbl + "</font>";
            } else {
                tbl = tbl + x[i].getAttribute('strokes');
            }
        }
        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 checkCut(mDoc){
    var x=mDoc.getElementsByTagName('cut');
    showCut = x[0].getAttribute('showCut');
    finalCut = x[0].getAttribute('finalCut');
    cutLine = x[0].getAttribute('cutLine');
    underCutLine = x[0].getAttribute('underCutLine');
    madeCut = x[0].getAttribute('madeCut');
}

function getPar(mDoc) {
    var x=mDoc.getElementsByTagName('course');
    coursePar = x[0].getAttribute('par');
}

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();
}





