/*
 * $Header: /usr/local/cvsroot/vorkdk/_inc/countdown.js,v 1.5 2005/08/16 15:14:07 krut Exp $
 *
 * Javascript countdown function
 *
 */

function countdown (ctimeTarget, countdownId, format)
{
	output = '';
	runtime = new Date ();
	ctimeNow = Math.round ((runtime.getTime ()) / 1000);
	
	timeLeft = ctimeTarget - ctimeNow;

	if (timeLeft < 0)
	{
		timeLeft = 0;
	}
	
	daysLeft = Math.floor (timeLeft / (60 * 60 * 24));
	timeLeft %= (60 * 60 * 24);
	
	hoursLeft = Math.floor (timeLeft / (60 * 60));
	timeLeft %= (60 * 60);

	minutesLeft = Math.floor (timeLeft / 60);
	timeLeft %= 60;
	
	secondsLeft = timeLeft;
	
	if (daysLeft > 1)
	{
		output += daysLeft + ' dage ';
	} else if (daysLeft == 1)
	{
		output += daysLeft + ' dag ';
	}
	
	if (format == 2)
	{
		output += "<br />\n";
	}

	if (hoursLeft > 1 || (hoursLeft == 0 && daysLeft > 0))
	{
		output += hoursLeft + ' timer ';
	} else if (hoursLeft == 1)
	{
		output += hoursLeft + ' time ';
	}

	if (format == 2)
	{
		output += "<br />\n";
	}
	
	if (minutesLeft > 1 || (minutesLeft == 0 && (daysLeft > 0 || hoursLeft > 0)))
	{
		output += minutesLeft + ' minutter ';
	} else if (minutesLeft == 1)
	{
		output += minutesLeft + ' minut ';
	}

	if (format == 2)
	{
		output += "<br />\n";
	}

	if (secondsLeft > 1 || (secondsLeft == 0 && (daysLeft > 0 || hoursLeft > 0 || minutesLeft > 0)))
	{
		output += secondsLeft + ' sekunder ';
	} else if (secondsLeft == 1)
	{
		output += secondsLeft + ' sekund ';
	}

	document.getElementById (countdownId).innerHTML = output;

	//Recursive call, keeps the clock ticking.
	setTimeout('countdown (' + ctimeTarget + ', \'' + countdownId + '\', ' + format + ');', 1000);
}
