/*
	Author:		Robert Hashemian (http://www.hashemian.com/)
	Modified by:	Munsifali Rashid (http://www.munit.co.uk/)
*/

  function countdown(obj)

{

	this.obj		= obj;
	this.Div		= "clock";
	this.BackColor		= "white";
	this.ForeColor		= "black";
	this.TargetDate		= "12/31/2020 5:00 AM";
	
	// add another property here, the from date
	this.CreateDate = new Date();
	
	this.DisplayFormat	= "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
	this.CountActive	= true; 
 
	
	
	this.DisplayStr;

	this.Calcage		= cd_Calcage;
	this.CountBack		= cd_CountBack;
	this.Setup		= cd_Setup;
}

function cd_Calcage(secs, num1, num2)
{
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2) s = "0" + s;
  return (s);
}
function cd_CountBack(secs)
{
  this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
  this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
  this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
  this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));


	var dEta= new Date(this.TargetDate);
  var dCreate	= new Date(this.CreateDate);
  appDiff = new Date(dEta-dCreate); 
  dsecs = Math.floor(appDiff.valueOf()/1000);
  
 // CHECKING IF CREATE DATE HAS < 48 HOURS THAN ETA DATE, TO CHANGE THE MESSAGE , EDIT HERE 
  if (dsecs < 172800 ) {
  	document.getElementById(this.Div).innerHTML = this.DisplayStr + "<font color='red'><b> ( < 48 hrs )</b></font>";
  	
  	// code below is an alternative if page does not handle innerHTML
  	//document.getElementById(this.Div).firstChild.nodeValue = this.DisplayStr + "( < 48 hrs )" ;
  	
  	
  } else {
  	
  	document.getElementById(this.Div).innerHTML = this.DisplayStr;
  	
  	// code below is an alternative if page does not handle innerHTML
  	//document.getElementById(this.Div).firstChild.nodeValue =  this.DisplayStr;
  	
  	
  }
  
  if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
}

function cd_Setup()
{
	var dthen	= new Date(this.TargetDate);
  var dnow	= new Date();
	ddiff		= new Date(dthen-dnow);
	gsecs		= Math.floor(ddiff.valueOf()/1000);
	
	// CHECKING IF ETA DATE HAS PASSED CURRENT DATE AND TIME , TO CHANGE THE MESSAGE , EDIT THE MESSAGE IN HERE 
	if (gsecs <= 0 ) {
		
			document.getElementById(this.Div).innerHTML = "<font color='red'><b> ARRIVED </b></font>" ; 	
			// code below is an alternative if page does not handle innerHTML
			// redFont = document.createElement("font");
			// redFont.setAttribute("color", "red");
			// redFont.appendChild(document.createTextNode("TIME'S UP"));  
			// document.getElementById(this.Div).appendChild(redFont) ;   
			
	} else {
		  // code below is an alternative if page does not handle innerHTML
		  //document.getElementById(this.Div).appendChild(document.createTextNode("&nbsp;")) ;  
 			this.CountBack(gsecs);
  }	
	
	
}
