// ****  Time Zone Count Down Javascript  **** //
/*
Visit http://rainbow.arch.scriptmania.com/scripts/
 for this script and many more
*/

////////// CONFIGURE THE COUNTDOWN SCRIPT HERE //////////////////
var t = Array(0, 0, 0);

var lab = Array('retcd', 'paycd', 'vaccd');

function restart_countdown()
{
	clearTimeout(t[0]);
	clearTimeout(t[1]);
	clearTimeout(t[2]);
	
	start_countdown();
}

function start_countdown() 
{
//	alert(retDate);
	
	displayTZCountDown(setTZCountDown(retDate),lab[0],0);
	displayTZCountDown(setTZCountDown(payDate),lab[1],1);
 	displayTZCountDown(setTZCountDown(vacDate),lab[2],2);
//	alert('in start');
}

// **    The start function can be changed if required   **
window.onload = start_countdown;

////////// DO NOT EDIT PAST THIS LINE //////////////////

function setTZCountDown(toDate) 
{
/*
	var toDate = new Date();
	
	toDate.setYear(xyear);
	toDate.setMonth(xmonth);
	toDate.setDate(xday);
	
	toDate.setHours(xhour);
	toDate.setMinutes(0);
	toDate.setSeconds(0);
*/	
	var fromDate = new Date();	//now
	
	var diffDate = new Date(0);
	
	diffDate.setMilliseconds(toDate - fromDate);
	
	return Math.floor(diffDate.valueOf()/1000);	//...number of seconds...
}

var sSubst;

function displayDigit(digit)
{
	if (digit < 0)
	{
		sSubst += "<IMG src='led/led_blank.png'>"; 
			
	}
	else
	{
		sSubst += "<IMG src='led/led_" + digit + ".png'>"; 
	}
}

function displayNumber(val, totdigits)
{
	var i, j;
	var tempval = "" + val;
	var prezeros = totdigits - tempval.length;
	
	j = 0;	//prezeros;

	for (i=0; i<totdigits; i++)
	{
		//displayDigit(prezeros);

		if (i < prezeros)
		{
			displayDigit(0);
		}
		else
		{
			displayDigit(tempval.substring(j, (j+1)));
			j++;
		}
	}
}

function displayTZCountDown(countdown_a,divname,t_index) 
{
	var countdown = countdown_a;
	var dtNow = new Date();
	
	if (countdown < 0)
	{
		countdown = 0;
	}
	
	// countdown passed in the form of seconds left...
	// total seconds and roundoff seconds after minute
	var SECONDS_PER_MINUTE = 60;
	var SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60; 
	var SECONDS_PER_DAY = SECONDS_PER_HOUR * 24; 
	var SECONDS_PER_YEAR = SECONDS_PER_DAY * 365;
	
	var years = Math.floor(countdown / SECONDS_PER_YEAR);
	countdown -= (years * SECONDS_PER_YEAR);
	
	var days = Math.floor(countdown / SECONDS_PER_DAY);
	countdown -= (days * SECONDS_PER_DAY);
	
	var hours = Math.floor(countdown / SECONDS_PER_HOUR);
	countdown -= (hours * SECONDS_PER_HOUR);
	
	var minutes = Math.floor(countdown / SECONDS_PER_MINUTE);
	countdown -= (minutes * SECONDS_PER_MINUTE);
	 
	var seconds = countdown;
	
//	document.getElementById(divname).innerHTML =  years + " year" + (years == 1 ? '' : 's') + ', ' +
//		days + " day" + (days == 1 ? '' : 's') + ', ' +hours+ ':' +minutes+ ':'+secs;
		
//	alert(countdown_a +'' + years + ' year' + (years == 1 ? '' : 's') + ', ' +
//		days + ' day' + (days == 1 ? '' : 's') + ', ' +hours+ ':' +minutes+ ':'+seconds);

	sSubst = "";
		
	displayNumber(years, 2);
	displayDigit(-1);
	displayNumber(days, 3);
	displayDigit(-1);
	displayNumber(hours, 2);
	displayDigit(-1);
	displayNumber(minutes, 2);
	displayDigit(-1);
	displayNumber(seconds, 2);

	document.getElementById(divname).innerHTML = sSubst;
	
//	alert(sSubst);
	
	t[t_index] = setTimeout('displayTZCountDown('+(countdown_a-1)+',\''+divname+'\','+t_index+');',1000);	//999);
}

