// ---------------------------------------------------------------------
// Countdown timer written by Naich - Copyright (c) 2001 Naich.
// This code is covered by the Creative Commons licence as shown
// on the main page of the site.
// ---------------------------------------------------------------------

function showCountDown (endTime, endMessage)
{
	
if (document.forms.timer.timerPause.checked == true) return;

today = new Date ();
msPerSecond = 1000; // Number of milliseconds per second
secondsLeft = (endTime.getTime () - today.getTime ()) / msPerSecond;
secondsLeft = Math.round (secondsLeft); // Round things up

delete today;
delete endTime;

secondsLeftT = secondsLeft;

secondsPerWeek = 60 * 60 * 24 * 7;
if (secondsLeft >= secondsPerWeek) {
	weeksLeft = secondsLeft / secondsPerWeek;
	weeksLeft = Math.round (weeksLeft - 0.5);
	secondsLeft = secondsLeft - (weeksLeft * secondsPerWeek);
	}
else weeksLeft = 0;

secondsPerDay = 60 * 60 * 24;
if (secondsLeft >= secondsPerDay) {
	daysLeft = secondsLeft / secondsPerDay;
	daysLeft = Math.round (daysLeft - 0.5);
	secondsLeft = secondsLeft - (daysLeft * secondsPerDay);
        }
else daysLeft = 0;

secondsPerHour = 60 * 60;
if (secondsLeft >= secondsPerHour) {
	hoursLeft = secondsLeft / secondsPerHour;
	hoursLeft = Math.round (hoursLeft - 0.5);
	secondsLeft = secondsLeft - (hoursLeft * secondsPerHour);
	}
else hoursLeft = 0;

secondsPerMinute = 60;
if (secondsLeft >= secondsPerMinute) {
	minutesLeft = secondsLeft / secondsPerMinute;
	minutesLeft = Math.round (minutesLeft - 0.5);
	secondsLeft = secondsLeft - (minutesLeft * secondsPerMinute);
	}
else minutesLeft = 0;

var theTimeW = ((weeksLeft == 0) ? "" : weeksLeft + " Week");
theTimeW += ((weeksLeft > 1) ? "s" : "");
var theTimeD = ((daysLeft == 0) ? "" : daysLeft + " Day");
theTimeD += ((daysLeft > 1) ? "s" : "");
var theTimeH = ((hoursLeft == 0) ? "" : hoursLeft + " Hour");
theTimeH += ((hoursLeft > 1) ? "s" : "");
var theTimeM = ((minutesLeft == 0) ? "" : minutesLeft + " Minute");
theTimeM += ((minutesLeft > 1) ? "s" : "");
var theTimeS = ((secondsLeft == 0) ? "" : secondsLeft + " Second");
theTimeS += ((secondsLeft > 1) ? "s" : "");

if (theTimeW != "") var gap1 = ", ";
else var gap1 = "";
if (theTimeD != "") var gap2 = ", ";
else var gap2 = "";
if (theTimeH != "") var gap3 = ", ";
else var gap3 = "";
if (theTimeM != "") var gap4 = ", ";
else var gap4 = "";

if (theTimeS != "") {
	if (gap4 != "") gap4 = " and ";
	else if (gap3 != "") gap3 = " and ";
	else if (gap2 != "") gap2 = " and ";
	else if (gap1 != "") gap1 = " and ";
	}
else if (theTimeM != "") {
	gap4 = "";
	if (gap3 != "") gap3 = " and ";
	else if (gap2 != "") gap2 = " and ";
	else if (gap1 != "") gap1 = " and ";
	}
else if (theTimeH != "") {
	gap4 = "";
	gap3 = "";
	if (gap2 != "") gap2 = " and ";
	else if (gap1 != "") gap1 = " and ";
	}
else if (theTimeD != "") {
	gap4 = "";
	gap3 = "";
	gap2 = "";
	if (gap1 != "") gap1 = " and ";
	}
else {
	gap4 = "";
	gap3 = "";
	gap2 = "";
	gap1 = "";
	}

theTime = " " + theTimeW + gap1 + theTimeD + gap2 + theTimeH + gap3 + theTimeM + gap4 + theTimeS + ".";

if (document.forms.timer.timerType.checked == true) 
	{
	secondsLeftS = secondsLeftT.toString ();
	theTime = " " + secondsLeftT;

	if (secondsLeftS.length > 3) {
		theTime = "";
		for (i = secondsLeftS.length - 3; i > 0; i -= 3) {
			theTime = secondsLeftS.substr (i, 3) + theTime;
			theTime = "," + theTime;
			}
		theTime = secondsLeftS.substr (0, i + 3) + theTime;
		theTime = " " + theTime;
		}

	theTime += " Seconds.";
	delete secondsLeftS;
	}

if (secondsLeftT > 0) document.forms.timer.timerDisplay.value = theTime;
else document.forms.timer.timerDisplay.value = endMessage;
}

