/*
Default Setup:

<script language="JavaScript">
TargetDate = "01/01/2050 12:00:00 AM";
BackgroundColor = "#FFFFFF";
TextColor = "#F60400";
CounterGo = true;
CounterStep = -1;
LeadZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
EndMessage = "It is finally here!";
</script>
<script language="JavaScript" src="http://www.mattrauch.com/scripts/countdown.js"></script>
*/

function calctime(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadZero && s.length < 2)
    s = "0" + s;
  return "<b>" + s + "</b>";
}

function CountDown(secs) {
  if (secs < 0) {
    document.getElementById("cntdwn").innerHTML = EndMessage;
    return;
  }
  DisplayStr = DisplayFormat.replace(/%%D%%/g, calctime(secs,86400,100000));
  DisplayStr = DisplayStr.replace(/%%H%%/g, calctime(secs,3600,24));
  DisplayStr = DisplayStr.replace(/%%M%%/g, calctime(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g, calctime(secs,1,60));

  document.getElementById("cntdwn").innerHTML = DisplayStr;
  if (CounterGo)
    setTimeout("CountDown(" + (secs+CounterStep) + ")", SetTimeOutPeriod);
}

function putspan(BackgroundColor, textColor) {
 document.write("<span id='cntdwn' style='background-color:" + BackgroundColor + 
                "; color:" + TextColor + "'></span>");
}

if (typeof(BackgroundColor)=="undefined")
  BackgroundColor = "#FFFFFF";
if (typeof(TextColor)=="undefined")
  TextColor= "#F60400";
if (typeof(TargetDate)=="undefined")
  TargetDate = "01/01/2050 4:59:59 PM";
if (typeof(DisplayFormat)=="undefined")
  DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
if (typeof(CounterGo)=="undefined")
  CounterGo = true;
if (typeof(EndMessage)=="undefined")
  EndMessage = "";
if (typeof(CounterStep)!="number")
  CounterStep = -1;
if (typeof(LeadZero)=="undefined")
  LeadZero = true;

CounterStep = Math.ceil(CounterStep);
if (CounterStep == 0)
  CounterGo = false;
var SetTimeOutPeriod = (Math.abs(CounterStep)-1)*1000 + 990;
putspan(BackgroundColor, TextColor);
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CounterStep>0)
  ddiff = new Date(dnow-dthen);
else
  ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountDown(gsecs);
