/*
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 = "02/15/2010 6:00 PM";
BackColor = "palegreen";
ForeColor = "navy";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
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>

# JavaScript countdown/Count-up timer/clock/ticker to/from the target date and time of your choice. 

# You can omit most parameters. Default values will be used in that case. CountActive is used to enable to disable counting. If you only want to show a fixed static time-remaining message, set it to false. It'll save on browser resources.

# DisplayFormat allows you to format the countdown/Count-up display to your liking. For example, instead of the default English, you can use terms from your own language, or make any other desired adjustments, like omitting the Seconds segment.

# Use FinishMessage to display a desired message (or nothing, i.e. ""), when countdown reaches zero. Obviously never displayed when counting up.

# CountStepper specifies the step value or period (in seconds) for the counter. Use positive number for counting up, negative number for counting down. Value is rounded up to next integer. When specifying positive (count up), be sure to specify past TargetDate, otherwise only the finish message is displayed.

# Single digits are displayed with leading zeros, unless LeadingZero is set to false.

*/



// TargetDate = "02/15/2010 6:00 PM";
BackColor = "white";
ForeColor = "black";
CountActive = true;
CountStepper = -15;  // refresh seconds
LeadingZero = false;
DisplayFormat = "%%D%% Days, %%H%% Hrs, %%M%% Min";

// EndString = "<strong style='font-size:16px;'>&nbsp; 'till Our Family Day Special - <a href='/news/' style='font-size:16px;font-weight:bold;'>Stay Tuned!</a></strong>"
FinishMessage = "<strong style='font-size:16px;'><a style='font-size:16px;font-weight:bold;' href='newsletters_online/feb_10/'>The Sale Ends February 15th! Savings to save others!</a></strong>";


EndString = "<strong style='font-size:16px;'>&nbsp;'til Our " + NameOfSale + " - <a href='/newsletters_online/feb_10/' style='font-size:16px;font-weight:bold;'>Family Day Wknd Special!</a></strong><br><br>"

FinishMessage = LeadingImage + "<strong style='font-size:16px;'><a style='font-size:16px;font-weight:bold;' href='newsletters_online/feb_10/'>Our "+ NameOfSale +" Ends This " + EndDate + "!</a></strong><br><br>";

if (typeof(SaleDuration)=="undefined") {SaleDuration =1}

ClearMessageTime = SaleDuration * 24 * 60 * 60 * -1;


function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero && s.length < 2)
    s = "0" + s;
  return "<b>" + s + "</b>";
}

function CountBack(secs) {

  if (secs < ClearMessageTime) {
    document.getElementById("cntdwn").innerHTML = "";
    return;
  }

  if (secs < 0) {
    document.getElementById("cntdwn").innerHTML = FinishMessage;
    return;
  }
  DisplayStr = DisplayFormat.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").innerHTML = LeadingImage + DisplayStr + EndString;
  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/2050 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;
if (typeof(LeadingZero)=="undefined")
  LeadingZero = true;


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);
