// JavaScript Document
/*
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 countdownTimer(elementID,target,bgcolor,forecolor,active,step,zero,format,message){
	
	this.TargetDate = target;
	this.BackColor = bgcolor;
	this.ForeColor = forecolor;
	this.CountActive = active;
	this.CountStepper = step;
	this.LeadingZero = zero;
	this.DisplayFormat = format;
	this.FinishMessage = message;
	this.elemID = elementID;

	
	this.calcage = function(secs, num1, num2) {
	  s = ((Math.floor(secs/num1))%num2).toString();
	  if (this.LeadingZero && s.length < 2)
		s = "0" + s;
	  return "<span class=\"bold\">" + s + "</span>";
	}
	
	this.CountBack = function(secs) {
	  if (secs < 0) {
		document.getElementById(this.elemID).innerHTML = this.FinishMessage;
		return;
	  }
	  DisplayStr = this.DisplayFormat.replace(/%%D%%/g, this.calcage(secs,86400,100000));
	  DisplayStr = DisplayStr.replace(/%%H%%/g, this.calcage(secs,3600,24));
	 DisplayStr = DisplayStr.replace(/%%M%%/g, this.calcage(secs,60,60));
	  DisplayStr = DisplayStr.replace(/%%S%%/g, this.calcage(secs,1,60));
	
	  document.getElementById(this.elemID).innerHTML = DisplayStr;
	  if (this.CountActive)
		setTimeout(this.elemID+".CountBack(" + (secs+this.CountStepper) + ")", this.SetTimeOutPeriod);
	}
	
	this.putspan = function(backcolor, forecolor) {
	 document.write("<span id='"+this.elemID+"' style='background-color:" + backcolor + 
					"; color:" + forecolor + "'></span>");
	}
	
	if (typeof(this.BackColor)=="undefined")
	  this.BackColor = "white";
	if (typeof(this.ForeColor)=="undefined")
	  this.ForeColor= "black";
	if (typeof(this.TargetDate)=="undefined")
	  this.TargetDate = "12/31/2020 5:00 AM";
	if (typeof(this.DisplayFormat)=="undefined")
	  this.DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
	if (typeof(this.CountActive)=="undefined")
	  this.CountActive = true;
	if (typeof(this.FinishMessage)=="undefined")
	  this.FinishMessage = "";
	if (typeof(this.CountStepper)!="number")
	  this.CountStepper = -1;
	if (typeof(this.LeadingZero)=="undefined")
	  this.LeadingZero = true;
	
	
	this.CountStepper = Math.ceil(this.CountStepper);
	if (this.CountStepper == 0)
	  this.CountActive = false;
	this.SetTimeOutPeriod = (Math.abs(this.CountStepper)-1)*1000 + 990;
	this.putspan(this.BackColor, this.ForeColor);
	this.dthen = new Date(this.TargetDate);
	this.dnow = new Date();
	if(this.CountStepper>0)
	  this.ddiff = new Date(this.dnow-this.dthen);
	else
	  this.ddiff = new Date(this.dthen-this.dnow);
	this.gsecs = Math.floor(this.ddiff.valueOf()/1000);
	this.CountBack(this.gsecs);
}
