/*
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.
********************************************************
*/

function calcage(secs, num1, num2) {
  return ((Math.floor(secs/num1))%num2);
}

function CountBack(secs) {
	
	if ($('count_down')=="undefined")
		return;
		
	if (secs <= 0)
	{
		CurIndex++;
		var dthen = new Date(TargetDates[CurIndex][0]);
		var dnow = new Date();
		if(CountStepper>0)
		  ddiff = new Date(dnow-dthen);
		else
		  ddiff = new Date(dthen-dnow);

		secs = Math.floor(ddiff.valueOf()/1000);
	}
	var tmp = calcage(secs,86400,100000);
	var hours = (tmp*24) + calcage(secs,3600,24);
	DisplayStr = DisplayFormat.replace(/%%H%%/g, "<b>"+hours+"</b>");
	DisplayStr = DisplayStr.replace(/%%M%%/g, "<b>"+calcage(secs,60,60)+"</b>");
	DisplayStr = DisplayStr.replace(/%%S%%/g, "<b>"+calcage(secs,1,60)+"</b>");
	
	document.getElementById("count_down").innerHTML = DisplayStr+"<b>"+TargetDates[CurIndex][1]+"</b>";
	if ($('est_arrive'))
		$('est_arrive').innerHTML = "<b>"+TargetDates[CurIndex][1]+"</b>";
	if (CountActive)
		setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}

var SetTimeOutPeriod;
var CurIndex = 0;

if (typeof(BackColor)=="undefined")
  BackColor = "white";
if (typeof(ForeColor)=="undefined")
  ForeColor= "black";
if (typeof(DisplayFormat)=="undefined")
  DisplayFormat = " Order within %%H%% Hours, %%M%% Minutes, and get it ";
if (typeof(CountActive)=="undefined")
  CountActive = true;
if (typeof(FinishMessage)=="undefined")
  FinishMessage = "";
if (typeof(CountStepper)!="number")
  CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
  LeadingZero = false;

function showCountdown()
{
	if (document.getElementById('count_down') == null)
	{
		var tbl = $('RHead');
		var tbl_body = tbl.getElementsBySelector("tbody");
		
		var node = document.createElement("tr");
		var data = document.createElement("td");
		var div = document.createElement("div");
		div.setAttribute("id", "count_down");
		div.innerHTML = "&nbsp;";
		data.style.verticalAlign = "middle";
		data.appendChild(div);
		node.appendChild(data);
		tbl_body[0].appendChild(node);
	}
	
	init_countdown();
}
		
function init_countdown() {
	CountStepper = Math.ceil(CountStepper);
	if (CountStepper == 0)
	  CountActive = false;
	SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
	var dthen = new Date(TargetDates[CurIndex][0]);
	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);
}