function isNumeric(x) {
if (!x) return 0;
// I use this function like this: if (isNumeric(myVar)) { } 
// regular expression that validates a value is numeric 
var RegExp = /^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$/; // Note: this WILL allow a number that ends in a decimal: -452.
// compare the argument to the RegEx 
// the 'match' function returns 0 if the value didn't match 
var result = x.match(RegExp);
return result;
}

function getTimeStamp(xmlDoc) {
    if( xmlDoc.documentElement && xmlDoc.documentElement.tagName && xmlDoc.documentElement.tagName.toUpperCase() == 'HTML' ) {
        setTimeout('alert(\'For no apparent reason, your browser has turned the clean XML into HTML based garbage.\\nScript aborted.\');',50); return; }
    if (newTimeStamp != null) oldTimeStamp = newTimeStamp;
    var y = xmlDoc.getElementsByTagName('updated');
    newTimeStamp = y[0].childNodes[0].nodeValue;
}

function createTable(xmlDoc) {
    savXmlDoc = xmlDoc;
    if( xmlDoc.documentElement && xmlDoc.documentElement.tagName && xmlDoc.documentElement.tagName.toUpperCase() == 'HTML' ) {
        setTimeout('alert(\'For no apparent reason, your browser has turned the clean XML into HTML based garbage.\\nScript aborted.\');',50); return; }

    var newTab = document.createElement('TABLE');
    var y = xmlDoc.getElementsByTagName(tagnam + 'Hdr');
    var x = xmlDoc.getElementsByTagName(tagnam);
    newTab.setAttribute('id',tagnam);
    newTab.setAttribute('cellPadding',2);
    newTab.setAttribute('cellSpacing',0);
    newTab.setAttribute('border',1);
    newTab.setAttribute('borderColor',"#999999");
    var tmp = document.createElement('THEAD');
    newTab.appendChild(tmp);
    var hdr = document.createElement('TR');
    var row = document.createElement('TR');
    var k = 0;
    for (j=0;j<x[0].childNodes.length;j++)
    {
        if (x[0].childNodes[j].nodeType != 1) continue;
        k = k+1;
        var container = document.createElement('TH');
        var theData = document.createTextNode(x[0].childNodes[j].nodeName);
        if (bTrunc && k>3) {
            var iDataLen = theData.nodeValue.length - 1;
            theData.nodeValue = theData.substringData(1, iDataLen); 
        }
        container.appendChild(theData);
        row.appendChild(container);
    }
    var container = document.createElement('TH');
    container.setAttribute('colSpan',k);
    var theData = document.createTextNode(y[0].childNodes[0].nodeValue);
    container.appendChild(theData);
    hdr.appendChild(container);
    tmp.appendChild(hdr);
    tmp.appendChild(row);
    var tmp = document.createElement('TBODY');
    newTab.appendChild(tmp);
    for (i=0;i<x.length;i++)
    {
        var row = document.createElement('TR');
        for (j=0;j<x[i].childNodes.length;j++)
        {
            if (x[i].childNodes[j].nodeType != 1) continue;
            var container = document.createElement('TD');
            if (x[i].childNodes[j].nodeName.indexOf('Score') >= 0) {
                container.className = "style5";
            } else {
                container.className = "style4";
            }
            if (!x[i].childNodes[j].firstChild) {
                var sText = " ";
            }
            else {
                var sText = x[i].childNodes[j].firstChild.nodeValue;
            }
            if (isNumeric(sText)) {
                container.setAttribute('align',"right");
            }
            else {
                if (sText.length == 1) container.setAttribute('align',"center");
            }
            var theData = document.createTextNode(sText);
            container.appendChild(theData);
            row.appendChild(container);
        }
        tmp.appendChild(row);
    }
    if (oldTab) document.getElementById('writeroot').replaceChild(newTab, oldTab);
    if (!oldTab) document.getElementById('writeroot').appendChild(newTab);
    oldTab = newTab;
}
function buildTable(sTag,bFlag) {
    bTrunc = bFlag;
    if (savRefTO != null) {
        clearTimeout(savRefTO);
        savRefTO = null;
    }
    importXML('HKGtim.xml','getTimeStamp');
    if (!oldTimeStamp || newTimeStamp != oldTimeStamp) {
        tagnam = sTag;
        importXML('HKGrsl.xml','createTable');
    }
    else {
        if (tagnam != sTag) {
            tagnam = sTag;
            createTable(savXmlDoc);
        }
    }
    savRefTO = setTimeout("buildTable(tagnam,bTrunc)",refTime);
}
function buildNews(sTag,bFlag) {
    bTrunc = bFlag;
    if (savRefTO != null) {
        clearTimeout(savRefTO);
        savRefTO = null;
    }
    importXML('HKGtim.xml','getTimeStamp');
    if (!oldTimeStamp || newTimeStamp != oldTimeStamp) {
        tagnam = sTag;
        importXML('Game1FIJKEN14.02.04.xml','createTable');
    }
    else {
        if (tagnam != sTag) {
            tagnam = sTag;
            createTable(savXmlDoc);
        }
    }
    savRefTO = setTimeout("buildNews(tagnam,bTrunc)",refTime);
}
function DJ_swap() {
    var bFound = false;
    if (document.styleSheets[0].rules) {
        var oRules = document.styleSheets[0].rules
    }
    else {
        var oRules = document.styleSheets[0].cssRules
    }
    for (i=0;i<oRules.length;i++) {
        var oStyle = oRules[i].style;
        var sStext = oRules[i].selectorText;
        if (bFound || (!oStyle.fontFamily && sStext == '#Q1Time')) bFound = true;
        if (!bFound) continue;
        var sCSS = oStyle.cssText;
        var ip1 = sCSS.toLowerCase().indexOf("left:");
        if (ip1==-1) continue;
        var sTest = sCSS.substring(ip1-1, ip1);
        if (sTest=="-") {
            ip1 = sCSS.toLowerCase().indexOf("left:", ip1+1);
        }
        if (ip1==-1) continue;
        ip1+=6;
        var ip2 = sCSS.indexOf("px", ip1+1);
        if (ip2==-1) continue;
        var iLeft = Number(sCSS.substring(ip1, ip2));
        var ip3 = sCSS.toLowerCase().indexOf("width:");
        if (ip3==-1) continue;
        ip3+=7;
        var sRemain = sCSS.substring(ip3);
        var ip4 = sRemain.indexOf("px");
        if (ip4==-1 || ip4>4) continue;
        var iWidth = Number(sRemain.substring(0, ip4));
        iLeft = 570 - iLeft - iWidth;
        sCSS = sCSS.substring(0, ip1) + iLeft + sCSS.substring(ip2);
        if (sStext.substr(0,4)=="#con") {
            sCSS = sCSS.replace(/border-right|border-left|BORDER-RIGHT|BORDER-LEFT/g, function(thematch) {if (thematch.toUpperCase()=="BORDER-RIGHT") return "border-left"; else return "border-right"});
        }
        else if (sStext.substr(2)=="footer") {
            sCSS = sCSS.replace(/right;|left;/, function(thematch) {if (thematch=="right;") return "left;"; else return "right;"});
        }
        oRules[i].style.cssText = sCSS;
    }
}
