/*
Author: Robert Hashemian
http://www.hashemian.com/

You can use this code in any manner so long as the author's
name, Web address and this disclaimer is kept intact.
********************************************************
Usage Sample:

<script language="JavaScript">
TargetDate = "12/31/2020 5:00 AM";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";
</script>
<script language="JavaScript" src="http://scripts.hashemian.com/js/countdown.js"></script>
*/

function calcage(secs, num1, num2) {
  return ((Math.floor(secs/num1))%num2);
}

function CountBack(secs) {
  if (secs < 0) {
    //document.getElementById("cntdwn").innerHTML = FinishMessage;
    return;
  }
  days =  calcage(secs,86400,100000);
  hours = calcage(secs,3600,24);
  min = calcage(secs,60,60);
  sec = calcage(secs,1,60);
  
  document.getElementById("day1").innerHTML = Math.floor(((days / 100) % 10));
  document.getElementById("day2").innerHTML = Math.floor(((days / 10) % 10));
  document.getElementById("day3").innerHTML = Math.floor((days % 10));
  document.getElementById("hour1").innerHTML = Math.floor(((hours / 10) % 10));
  document.getElementById("hour2").innerHTML = Math.floor((hours % 10));
  document.getElementById("min1").innerHTML = Math.floor(((min / 10) % 10));
  document.getElementById("min2").innerHTML = Math.floor((min % 10));
  document.getElementById("sec1").innerHTML = Math.floor(((sec / 10) % 10));
  document.getElementById("sec2").innerHTML = Math.floor((sec % 10));

  if (CountActive)
    setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}


if (typeof(BackColor)=="undefined")
  BackColor = "white";
if (typeof(ForeColor)=="undefined")
  ForeColor= "black";
if (typeof(TargetDate)=="undefined")
  TargetDate = "12/31/2020 5:00 AM";
if (typeof(DisplayFormat)=="undefined")
  DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
if (typeof(CountActive)=="undefined")
  CountActive = true;
if (typeof(FinishMessage)=="undefined")
  FinishMessage = "";
if (typeof(CountStepper)!="number")
  CountStepper = -1;

CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
  CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
  ddiff = new Date(dnow-dthen);
else
  ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
