if (typeof(hideText) == "undefined")
    hideText = false;

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

function updateTimeout() { 
  if (secondsToUpdate == 1) {
    window.location.reload(false);
  } else {
    str = formatSeconds();
    // process class="countdown" countdowns
    var countDowns = getElementsByClassName(document, "span", "countdown");  
    for (i in countDowns) countDowns[i].innerHTML = str;
    // process id="updateTime" countdown (deprecated)
    var oldCountDown = document.getElementById("updateTime");
    if (oldCountDown) oldCountDown.innerHTML = str;
    secondsToUpdate--;
    setTimeout(updateTimeout, 1000); 
  }
} 

function formatSeconds() {
    minutes = Math.floor((secondsToUpdate / 60) % 60);
    hours = Math.floor(secondsToUpdate / 3600);
    if (minutes == 0 && hours == 0) {
        result = "" + secondsToUpdate;
        if (!hideText) result += " seconds";
        return result;
    } else {
        seconds = secondsToUpdate % 60;
        hoursStr = "" + hours;
        minutesStr = "" + minutes;
        secondsStr = "" + seconds;
        if (seconds < 10)
            secondsStr = "0" + secondsStr;
        if (minutes < 10 && hours > 0)
            minutesStr = "0" + minutesStr;
        result = minutesStr + ":" + secondsStr;
        if (!hideText) result += " minutes";
        if(hours > 0) {
            result = hoursStr + ":" + minutesStr + ":" + secondsStr;
            if (!hideText) result += " hours";
        }
        return result;
    }
}

window.onload = updateTimeout;
