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

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 calday(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  return "<b>" + s + "</b>";
}

function calhour(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,divid) {
  if (secs < 0) {
    document.getElementById(divid).innerHTML = '<p id="timehome">' + FinishMessage + '</p>';
    if (divid=='time27home') {
		document.getElementById('button').innerHTML = '';
		bids(0,'finish');
		}
		else {
		pid=divid.replace('time','');
		pid=pid.replace('home','');
  	winnerhome(pid,'finish');
	  }
    return;
  }
  DisplayStr = DisplayFormat.replace(/%%D%%/g, calday(secs,86400,100000));
  DisplayStr = DisplayStr.replace(/%%H%%/g, calhour(secs,3600,24));
  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
	document.getElementById(divid).innerHTML = '<p id="timehome">' + DisplayStr + '</p>';
	i=i+1;
	
	if (i==j) {
	i=0;
	if (divid=='time27home') {
	bids(0,'update');
	}
	else {
	pid=divid.replace('time','');
		pid=pid.replace('home','');
  	winnerhome(pid,'update');
	}
	}
	if (CountActive)
    setTimeout("CountBack(" + (secs+CountStepper) + ",'" + divid + "')", 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;
if (typeof(LeadingZero)=="undefined")
  LeadingZero = true;


CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
  CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
if (TargetDate1!='') {
var dthen1 = new Date(TargetDate1);
var dthen2 = new Date(TargetDate2);
var dthen3 = new Date(TargetDate3);
var dthen4 = new Date(TargetDate4);
var dthen5 = new Date(TargetDate5);
var dthen6 = new Date(TargetDate6);
var dthen7 = new Date(TargetDate7);
var dthen8 = new Date(TargetDate8);
var dthen9 = new Date(TargetDate9);
var dthen10 = new Date(TargetDate10);
var dthen11 = new Date(TargetDate11);
var dthen12 = new Date(TargetDate12);
var dthen13 = new Date(TargetDate13);
var dthen14 = new Date(TargetDate14);
var dthen15 = new Date(TargetDate15);
var dthen16 = new Date(TargetDate16);
var dthen17 = new Date(TargetDate17);
var dthen18 = new Date(TargetDate18);
var dthen19 = new Date(TargetDate19);
var dthen20 = new Date(TargetDate20);
var dthen21 = new Date(TargetDate21);
var dthen22 = new Date(TargetDate22);
var dthen23 = new Date(TargetDate23);
var dthen24 = new Date(TargetDate24);
var dthen25 = new Date(TargetDate25);
var dthen26 = new Date(TargetDate26);
}
else {var dthen27 = new Date(TargetDate27);}
var dnow = new Date();
if(CountStepper>0) {
  ddiff = new Date(dnow-dthen);
  }
else {
  if (TargetDate1!='') {
  ddiff1 = new Date(dthen1-dnow);
	ddiff2 = new Date(dthen2-dnow);
  ddiff3 = new Date(dthen3-dnow);
  ddiff4 = new Date(dthen4-dnow);
  ddiff5 = new Date(dthen5-dnow);
	ddiff6 = new Date(dthen6-dnow);
  ddiff7 = new Date(dthen7-dnow);
  ddiff8 = new Date(dthen8-dnow);
  ddiff9 = new Date(dthen9-dnow);
	ddiff10 = new Date(dthen10-dnow);
  ddiff11 = new Date(dthen11-dnow);
  ddiff12 = new Date(dthen12-dnow);
  ddiff13 = new Date(dthen13-dnow);
	ddiff14 = new Date(dthen14-dnow);
  ddiff15 = new Date(dthen15-dnow);
  ddiff16 = new Date(dthen16-dnow);
  ddiff17 = new Date(dthen17-dnow);
  ddiff18 = new Date(dthen18-dnow);
  ddiff19 = new Date(dthen19-dnow);
	ddiff20 = new Date(dthen20-dnow);
  ddiff21 = new Date(dthen21-dnow);
  ddiff22 = new Date(dthen22-dnow);
  ddiff23 = new Date(dthen23-dnow);
	ddiff24 = new Date(dthen24-dnow);
  ddiff25 = new Date(dthen25-dnow);
  ddiff26 = new Date(dthen26-dnow);
  }
  else {
    ddiff27 = new Date(dthen27-dnow);
    }
  }
if (TargetDate1!='') {
gsecs1 = Math.floor(ddiff1.valueOf()/1000);
gsecs2 = Math.floor(ddiff2.valueOf()/1000);
gsecs3 = Math.floor(ddiff3.valueOf()/1000);
gsecs4 = Math.floor(ddiff4.valueOf()/1000);
gsecs5 = Math.floor(ddiff5.valueOf()/1000);
gsecs6 = Math.floor(ddiff6.valueOf()/1000);
gsecs7 = Math.floor(ddiff7.valueOf()/1000);
gsecs8 = Math.floor(ddiff8.valueOf()/1000);
gsecs9 = Math.floor(ddiff9.valueOf()/1000);
gsecs10 = Math.floor(ddiff10.valueOf()/1000);
gsecs11 = Math.floor(ddiff11.valueOf()/1000);
gsecs12 = Math.floor(ddiff12.valueOf()/1000);
gsecs13 = Math.floor(ddiff13.valueOf()/1000);
gsecs14 = Math.floor(ddiff14.valueOf()/1000);
gsecs15 = Math.floor(ddiff15.valueOf()/1000);
gsecs16 = Math.floor(ddiff16.valueOf()/1000);
gsecs17 = Math.floor(ddiff17.valueOf()/1000);
gsecs18 = Math.floor(ddiff18.valueOf()/1000);
gsecs19 = Math.floor(ddiff19.valueOf()/1000);
gsecs20 = Math.floor(ddiff20.valueOf()/1000);
gsecs21 = Math.floor(ddiff21.valueOf()/1000);
gsecs22 = Math.floor(ddiff22.valueOf()/1000);
gsecs23 = Math.floor(ddiff23.valueOf()/1000);
gsecs24 = Math.floor(ddiff24.valueOf()/1000);
gsecs25 = Math.floor(ddiff25.valueOf()/1000);
gsecs26 = Math.floor(ddiff26.valueOf()/1000);
}
else {
gsecs27 = Math.floor(ddiff27.valueOf()/1000);
}

if (TargetDate1!='') {
CountBack(gsecs1,'time1home');
CountBack(gsecs2,'time2home');
CountBack(gsecs3,'time3home');
CountBack(gsecs4,'time4home');
CountBack(gsecs5,'time5home');
CountBack(gsecs6,'time6home');
CountBack(gsecs7,'time7home');
CountBack(gsecs8,'time8home');
CountBack(gsecs9,'time9home');
CountBack(gsecs10,'time10home');
CountBack(gsecs11,'time11home');
CountBack(gsecs12,'time12home');
CountBack(gsecs13,'time13home');
CountBack(gsecs14,'time14home');
CountBack(gsecs15,'time15home');
CountBack(gsecs16,'time16home');
CountBack(gsecs17,'time17home');
CountBack(gsecs18,'time18home');
CountBack(gsecs19,'time19home');
CountBack(gsecs20,'time20home');
CountBack(gsecs21,'time21home');
CountBack(gsecs22,'time22home');
CountBack(gsecs23,'time23home');
CountBack(gsecs24,'time24home');
CountBack(gsecs25,'time25home');
CountBack(gsecs26,'time26home');
}
else {
CountBack(gsecs27,'time27home');
}

