

function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero[timerId] && s.length < 2)
    s = "0" + s;
  return s;
}

function CountBack(secs, timerId, SetTimeOutPeriod) {

  if (secs < 0) {
    document.getElementById("cntdwn" + timerId).innerHTML = FinishMessage[timerId];
    return;
  }

  DisplayStr = DisplayFormat[timerId].replace(/%%D%%/g, calcage(secs,86400,100000));
  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

  document.getElementById("cntdwn"+timerId).innerHTML = DisplayStr;
  if (CountActive[timerId])
    setTimeout("CountBack(" + (secs+CountStepper[timerId]) + ", "+timerId+", "+SetTimeOutPeriod+")", SetTimeOutPeriod);
}

function putspan(timerId) {
 document.write('<div class="timer" id="cntdwn'+timerId+'"></div>');
}

function startIt(timerId)
{
	CountStepper[timerId] = Math.ceil(CountStepper[timerId]);
	if (CountStepper[timerId] == 0)
	  CountActive[timerId] = false;
	var SetTimeOutPeriod = (Math.abs(CountStepper[timerId])-1)*1000 + 990;
	putspan(timerId);
	var dthen = new Date(TargetDate[timerId]);
	var dnow = new Date(NowDate[timerId]);
	if(CountStepper[timerId]>0)
	  ddiff = new Date(dnow-dthen);
	else
	  ddiff = new Date(dthen-dnow);
	gsecs = Math.floor(ddiff.valueOf()/1000);
	CountBack(gsecs, timerId, SetTimeOutPeriod);
}

