/*
	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";
	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) {
	if (secs < 0) {
		document.getElementById(this.Div).innerHTML = this.FinishMessage;
		return;
	}

	numdays=this.Calcage(secs,86400,100000);
	numhours=this.Calcage(secs,3600,24);
	numminutes=this.Calcage(secs,60,60);
	numseconds=this.Calcage(secs,1,60);
	if (numdays > 0) {
		this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g, numdays);
		if (numdays == 1) {
			this.DisplayStr = this.DisplayStr.replace(/days/, "day");
		}
	}
	else {
		this.DisplayStr = this.DisplayFormat.replace(/%%D%%/, "");
		this.DisplayStr = this.DisplayStr.replace(/days,/, "");
	}
	if (numhours > 0) {
		this.DisplayStr = this.DisplayStr.replace(/%%H%%/g, numhours);
		if (numhours == 1) {
			this.DisplayStr = this.DisplayStr.replace(/hours/, "hour");
		}
	}
	else {
		this.DisplayStr = this.DisplayStr.replace(/%%H%%/, "");
		this.DisplayStr = this.DisplayStr.replace(/hours,/, "");
	}
	if (numminutes > 0) {
		this.DisplayStr = this.DisplayStr.replace(/%%M%%/g, numminutes);
		if (numminutes == 1) {
			this.DisplayStr = this.DisplayStr.replace(/minutes/, "minute");
		}
	}
	else {
		this.DisplayStr = this.DisplayStr.replace(/%%M%%/, "");
		this.DisplayStr = this.DisplayStr.replace(/minutes,/, "");
	}
	if (numseconds > 0) {
		this.DisplayStr = this.DisplayStr.replace(/%%S%%/g, numseconds);
		if (numseconds == 1) {
			this.DisplayStr = this.DisplayStr.replace(/seconds/, "second");
		}
	}
	else {
		this.DisplayStr = this.DisplayStr.replace(/%%S%%/, "");
		this.DisplayStr = this.DisplayStr.replace(/seconds/, "");
	}

	document.getElementById(this.Div).innerHTML = 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);
	this.CountBack(gsecs);
}