/*
	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;
    this.CountStepper = -1;
    this.Seconds           = 0;
    this.IntervalId     = 0;
}

function cd_Calcage(Seconds, num1, num2)
{
    s = ((Math.floor(Seconds/num1))%num2).toString();
    if (s.length < 2) s = "0" + s;
    return s;
}

function cd_CountBack()
{
    var Seconds = this.Seconds;
    this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(Seconds,86400,100000));
    this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(Seconds,3600,24));
    this.DisplayStr = this.DisplayStr.replace(/%%HH%%/g,		this.Calcage(Seconds,3600,10000000));
    this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(Seconds,60,60));
    this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(Seconds,1,60));
    
    document.getElementById(this.Div).innerHTML = this.DisplayStr;
    this.Seconds += this.CountStepper;
    if (!this.CountActive) clearInterval(this.IntervalId);
}

function cd_Setup()
{
    if (!this.Seconds) 
    {
        var dthen	= new Date(this.TargetDate);
        var dnow	= new Date();
        var ddiff		= new Date(dthen-dnow);
        var gsecs		= Math.floor(ddiff.valueOf()/1000);
        this.Seconds = Math.max(gsecs, 0);
    }
    this.IntervalId = setInterval(this.obj +".CountBack()", 1000);
}
