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("/2007/scoring/dyn/amateur.xml");

    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='/2007/images/scoring/img/cutL.gif'/>";
                    tbl = tbl + "</td>";
                    tbl = tbl + "<td colspan='2' background='/2007/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='/2007/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='/2007/images/scoring/img/cutL.gif'/>";
                    tbl = tbl + "</td>";
                    tbl = tbl + "<td colspan='2' background='/2007/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='/2007/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='/2007/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') + '&nbsp;&nbsp;&nbsp;&nbsp;(' + x[0].getAttribute('GMT') + ')';
}

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();
}





