var targetDate = false;

function UpdateCountdown()
{
	if(!document.getElementById('countdown'))
		return;
	if (!targetDate)
	{
		var targetDateString = document.getElementById('countdown').innerHTML;
		// format of start date should be "July 01, 2010"
		targetDate = new Date(targetDateString);
	}
	var now = new Date();
	var passed = 1;
	if(now > targetDate)
		passed = -1;
	var seconds = passed*(targetDate.getSeconds() - now.getSeconds());
	var minutes = passed*(targetDate.getMinutes() - now.getMinutes());
	var hours = passed*(targetDate.getHours() - now.getHours());
	var days = passed*(targetDate.getDate() - now.getDate());
	var months = passed*(targetDate.getMonth() - now.getMonth());
	var years = passed*(targetDate.getFullYear() - now.getFullYear());
	if (seconds<0)
	{
		seconds = 60 + seconds;
		minutes--;
	}
	if (minutes<0)
	{
		minutes = 60 + minutes;
		hours--;
	}
	if (hours<0)
	{
		hours = 24 + hours;
		days--;
	}
	if (days<0)
	{
		days = daysInMonth(now) + days;
		months--;
	}
	if (months<0)
	{
		months = 12 + months;
		years--;
	}
	var countdownString = "";
	if (years>0)
	{
		if(years==1)
			countdownString += years+" year, ";
		else
			countdownString += years+" years, ";
	}
	if (countdownString.length || months>0)
	{
		if(months==1)
			countdownString += months+" month, ";
		else
			countdownString += months+" months, ";
	}
	if (countdownString.length || days>0)
	{
		if(days==1)
			countdownString += days+" day, ";
		else
			countdownString += days+" days, ";
	}
	if (countdownString.length || hours>0)
		countdownString += hours+" hr, ";
	if (countdownString.length || minutes>0)
		countdownString += minutes+" min, ";
	countdownString += seconds+" sec";
	if(passed<0)
		countdownString += " ago";
	document.getElementById("countdown").innerHTML = countdownString;
	setTimeout("UpdateCountdown();", 1000);
}

function daysInMonth(date)
{
	// http://javascript.about.com/library/bllday.htm
	month = date.getMonth();	// January = 0
	year = date.getFullYear();
	var m = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (month != 1) return m[month];	// not February
	if (year%4 != 0) return m[1];
	if (year%100 == 0 && year%400 != 0) return m[1];
	return m[1] + 1;	//leap year
}
