﻿// JScript File

var _countDowncontainer=0;
var _currentSeconds=0;
var _secondContainer=0;

function ActivateCountDown(strContainerID, initialValue, strSecondContainerID) {
_countDowncontainer = document.getElementById(strContainerID);
_secondContainer = document.getElementById(strSecondContainerID);

if (!_countDowncontainer) {
alert("count down error: container does not exist: "+strContainerID+
"\nmake sure html element with this ID exists");
return;
}

SetCountdownText(strContainerID, initialValue, strSecondContainerID);
window.setTimeout("CountDownTick('"+strContainerID+"','"+strSecondContainerID+"')", 1000);
}

function CountDownTick(strContainerID, strSecondContainerID) {
_secondContainer = document.getElementById(strSecondContainerID);
//alert(strSecondContainerID);
var intSeconds=_secondContainer.value;

if (intSeconds <= 0) {
alert("your time has expired!");
return;
}

SetCountdownText(strContainerID, intSeconds-1, strSecondContainerID);
window.setTimeout("CountDownTick('"+strContainerID+"','"+strSecondContainerID+"')", 1000);
}

function SetCountdownText(strContainerID, seconds, strSecondContainerID) {
//alert(strContainerID);
//store:
_countDowncontainer = document.getElementById(strContainerID);
_secondContainer = document.getElementById(strSecondContainerID);
_currentSeconds = seconds;
_secondContainer.value = seconds;

//get minutes:
var minutes=parseInt(seconds/60);

//shrink:
seconds = (seconds%60);

//get hours:
var hours=parseInt(minutes/60);

//shrink:
minutes = (minutes%60);

//build text:
var strText = AddZero(hours) + ":" + AddZero(minutes) + ":" + AddZero(seconds);

//apply:
_countDowncontainer.innerHTML = strText;

}

function AddZero(num) {
return ((num >= 0)&&(num < 10))?"0"+num:num+"";
}