var BackCounter = {

	htmlField        : 'back-counter',
	CountActive      : true,
	TargetDate       : false,
	DisplayFormat    : "Deadline for submitting Applications is <b>22 September 2009</b> within the first Call for Proposals.<br/><strong>%%H%%</strong> <b>hours</b> <strong>%%M%%</strong> <b>minutes</b> remained for submitting <b><u>Applications by hand-delivery!!</u></b>",
	FinishMessage    : '',
	CountStepper     : -1,
	LeadingZero      : false,
	CountActive		 : true,

	setBackCounter : function (TargetDate)
	{
		BackCounter.TargetDate   = TargetDate;
		BackCounter.CountStepper = Math.ceil(BackCounter.CountStepper);

		if (BackCounter.CountStepper == 0)
		{
			BackCounter.CountActive = false;
		}

		var dthen = new Date(BackCounter.TargetDate);
		var dnow  = new Date();

		if ( BackCounter.CountStepper > 0 )
		{
			  ddiff = new Date(dnow-dthen);
		}
		else 
		{
			  ddiff = new Date(dthen-dnow);
		}

		gsecs = Math.floor(ddiff.valueOf()/1000);

		BackCounter.countBack(gsecs);
	},

	calcage : function (secs, num1, num2) 
	{
		 s = ((Math.floor(secs/num1))%num2).toString();

		if (BackCounter.LeadingZero && s.length < 2)
		{
			s = "0" + s;
		}

		return s;
	},

	countBack : function (secs) 
	{
		if (secs < 0) 
		{
			$('#'+BackCounter.htmlField).html(BackCounter.FinishMessage);
			return;
		}

		DisplayStr = BackCounter.DisplayFormat.replace(/%%D%%/g, BackCounter.calcage(secs,86400,100000));
		DisplayStr = DisplayStr.replace(/%%H%%/g, BackCounter.calcage(secs,3600,24));
		DisplayStr = DisplayStr.replace(/%%M%%/g, BackCounter.calcage(secs,60,60));
		DisplayStr = DisplayStr.replace(/%%S%%/g, BackCounter.calcage(secs,1,60));

		$('#'+BackCounter.htmlField).html(DisplayStr);

		if (BackCounter.CountActive)
		{
			setTimeout("BackCounter.countBack(" + (secs+BackCounter.CountStepper) + ")", 1000);
		}
	}
};
