/*
	Author:		Robert Hashemian (http://www.hashemian.com/)
	Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
	
	
	NOTE: would have been better to implement with 'setInterval'
*/


function countdown(obj)
{
	this.obj		= obj;
	this.Div		= "clock";
	this.BackColor		= "white";
	this.ForeColor		= "black";
	this.TargetDate		= "12/31/2020 5:00 AM";
	this.DisplayFormat	= "%%H%%:%%M%%:%%S%%";
	this.CountActive	= true;
	
	this.DisplayStr;

	this.Calcage		= cd_Calcage;
	this.CountBack		= cd_CountBack;
	this.Setup = cd_Setup;
	this.timeoutIds = new Array();
}

function cd_Calcage(secs, num1, num2)
{
   
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) s = "0" + s;
  return (s);
}
function cd_CountBack(secs)
{
  this.timeoutIds.pop();
  this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
  this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
  this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
  this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));

    document.getElementById(this.Div).innerHTML = this.DisplayStr;
    if(secs <= 0){
        global_currentTest.timeElapsed();
        return;
    }
    if (this.CountActive) {
        var id = setTimeout(this.obj + ".CountBack(" + (secs - 1) + ")", 990);
        this.timeoutIds.unshift(id);
    }
}
function cd_Setup(gsecs)
{

    //clear all pending timeouts (in the case of a "Reset" opposed to an Initial "Set")
    while(this.timeoutIds.length > 0) {
        var id = this.timeoutIds.pop();
        clearTimeout(id);
    }
	if(isNaN(gsecs)){
	    alert("Warning : timer recieved NaN value.(countdown.js)")
	    return;
	}
	this.CountBack(gsecs);
}

