
//<![CDATA[
/*
	----- BEGIN COPYRIGHT NOTICE -----
	
	Copyright 2007, Harold Consulting, Inc.
	Author: Ben Harold <benharold@mac.com>
	Original Site: "Countdown to Kickoff - Super Bowl XLI" at www.haroldinc.com in late January 2007
	History: I originally created a "Countodwn to Kickoff" page for Super Bowl XLI, which the
	Indianapolis Colst won on February 4th, 2007. This countdown/countup script was a natural
	progression.
	
	How to use this script:

	This script calculates the difference in time between right now and another point in time which
	we designate as theMainEvent. It returns the difference in time as either a countdown or
	countup timer.
	
		year - The year in which theMainEven happens
		month - The month in which theMainEvent happens
		day - The day in which theMainEvent happens
		hour - The hour in which theMainEvent happens
		minute - The minute in which theMainEvent happens
		ampm - Must be either 'am' or 'pm'

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	<http://www.gnu.org/licenses/>.

	----- END COPYRIGHT NOTICE -----
*/
var numberOpenTag = '<span class="countdownNumbers">';
var numberCloseTag = '<\/span>';
var textOpenTag = '<span class="countdownText">';
var textCloseTag = '<\/span>';
var timer;
function updateCounter(year,month,day,hour,minute,ampm) {
// Converts an hour of the day and either 'am' or 'pm' to military time and returns the hour
	function hourToMilitaryTime(hour,ampm) {
		hour = parseInt(hour);
		if (ampm == 'am') {
			if (hour == 12)
				hour = 0;
		}
		else if (ampm == 'pm') {
			if (hour != 12)
				hour += 12;
		}
		return hour;
	}
	// This function is used to format the number of minutes. If the minute value is 0 - 9, this
	// function will return a string corresponding to the minute value of 00 - 09.
	function minuteValue(minute) {
		if (String(minute).length == 1)
			return '0' + minute;
		else
			return minute;
	}
	// Make sure the countdown message is accurate

//	document.getElementById('date').innerHTML = day + "/" + month + "/" + year + "&nbsp;" + hour + ":" + minuteValue(minute) + "&nbsp;" + ampm;
	document.getElementById('date').innerHTML = day + "/" + month + "/" + year ;

	// Define the universal time code date for theMainEvent
	
	var theMainEvent = Date.UTC(year,(month - 1),day,(hourToMilitaryTime(hour,ampm)),minute);
	// Create a new date object (called rightNow) with the current date and time
	var rightNow = new Date();
	// Define variables based on the current date and time
	var todaysYear = rightNow.getFullYear();
	var todaysMonth = rightNow.getMonth();
	var todaysDate = rightNow.getDate();
	var todaysHours = rightNow.getHours();
	var todaysMinutes = rightNow.getMinutes();
	var todaysSeconds = rightNow.getSeconds();
	// The maximum number of years we wish to count down from
	var maxYears = Math.abs(year - todaysYear);
	// Define the universal time code date for rightNow
	var todaysUTCDate = Date.UTC(todaysYear,todaysMonth,todaysDate,todaysHours,todaysMinutes,todaysSeconds);
	if (theMainEvent > todaysUTCDate)
		countdown(theMainEvent,todaysUTCDate);
	else if (theMainEvent < todaysUTCDate)
		countup(theMainEvent,todaysUTCDate);
	else if (theMainEvent == todaysUTCDate) {
		alert('Blast Off!');
		countup(theMainEvent,todaysUTCDate);
	}
	function countdown(theMainEvent,todaysUTCDate) {
		document.getElementById('word').innerHTML = 'al ';
		// JavaScript deals with time in milliseconds
		var milliseconds = theMainEvent - todaysUTCDate;
		var seconds = milliseconds / 1000;
		var minutes = seconds / 60;
		var hours = minutes / 60;
		var days = hours / 24;
		var years = days / 365;
		for (var x = 0; x <= maxYears; x++)
			if (years >= x)
				var actualYears = x;
		var daysToSubtract = (365 * actualYears);
		for (var x = 0; x <= 364; x++)
			if ((days - daysToSubtract) >= x)
				var actualDays = x;
		var hoursToSubtract = (24 * actualDays) + (365 * 24 * actualYears);
		for (var x = 0; x <= 23; x++)
			if ((hours - hoursToSubtract) >= x)
				var actualHours = x;
		var minutesToSubtract = (60 * actualHours) + (24 * 60 * actualDays) + (365 * 24 * 60 * actualYears);
		for (var x = 0; x <= 59; x++)
			if ((minutes - minutesToSubtract) >= x)
				var actualMinutes = x;
		var secondsToSubtract = (60 * actualMinutes) + (60 * 60 * actualHours) + (24 * 60 * 60 * actualDays) + (365 * 24 * 60 * 60 * actualYears);
		var actualSeconds = seconds - secondsToSubtract;
		// This code is the same in both the countdown and countup function
		var countdownString = '';

		if (actualYears > 1)
			countdownString = textOpenTag + '<b>rimangono</b><br>' + textCloseTag + numberOpenTag + '<b>' + actualYears + '</b> ' + numberCloseTag + textOpenTag + 'anni ' + textCloseTag;
		else if (actualYears == 1)
			countdownString = textOpenTag + '<b>rimane</b><br>' + textCloseTag + numberOpenTag + '<b>' + actualYears + '</b> ' + numberCloseTag + textOpenTag + 'anno ' + textCloseTag;
		
		if (actualDays > 1 && actualYears < 1)
			countdownString = countdownString + textOpenTag + '<b>rimangono</b><br>' + textCloseTag;
		else if (actualDays == 1 && actualYears < 1)
			countdownString = countdownString + textOpenTag + '<b>rimane</b><br>' + textCloseTag;
		
		if (actualDays < 1 && actualYears < 1 && actualHours > 1)
			countdownString = countdownString + textOpenTag + '<b>rimangono</b><br>' + textCloseTag;
		else if (actualDays < 1 && actualYears < 1 && actualHours < 2)
			countdownString = countdownString + textOpenTag + '<b>rimane</b><br>' + textCloseTag;

		if (actualDays > 1)
			countdownString = countdownString + numberOpenTag + '<b>' + actualDays + '</b>' + numberCloseTag + textOpenTag + ' gg<br>' + textCloseTag;
		else if (actualDays == 1)
			countdownString = countdownString + numberOpenTag + '<b>' + actualDays + '</b>' + numberCloseTag + textOpenTag + ' giorno<br>' + textCloseTag;
		
		if (actualHours > 1)
			countdownString = countdownString + numberOpenTag + '<b>' + actualHours + '</b>' + numberCloseTag;
		else if (actualHours == 1)
			countdownString = countdownString + numberOpenTag + '<b>' + actualHours + '</b>' + numberCloseTag;
	
		if (actualMinutes > 1)
			countdownString = countdownString + numberOpenTag + '<FONT SIZE=-2 POINT-SIZE=8>h</font> <b>' + actualMinutes + '</b>' + numberCloseTag;
		else if (actualMinutes == 1)
			countdownString = countdownString + numberOpenTag + '<FONT SIZE=-2 POINT-SIZE=8>h</font> <b>' + actualMinutes + '</b>' + numberCloseTag;
	
		if (actualSeconds > 1)
			countdownString = countdownString + numberOpenTag + '<FONT SIZE=-2 POINT-SIZE=8>m</font> <b>' + actualSeconds + '</b>' + numberCloseTag;
		else if (actualSeconds == 1)
			countdownString = countdownString + numberOpenTag + '<FONT SIZE=-2 POINT-SIZE=8>m</font> <b>' + actualSeconds + '</b>' + numberCloseTag;
	
		document.getElementById('countdownBody').innerHTML = countdownString;
		timer = setTimeout("updateCounter("+year+","+month+","+day+","+hour+","+minute+",'"+ampm+"')",1000);
	}

	function countup(theMainEvent,todaysUTCDate) {
		document.getElementById('word').innerHTML = '&nbsp;Promessa scaduta&nbsp;<br>il ';
		var milliseconds = todaysUTCDate - theMainEvent;
		var seconds = milliseconds / 1000;
		var minutes = seconds / 60;
		var hours = minutes / 60;
		var days = hours / 24;
		var years = days / 365;
		for (var x = 0; x <= maxYears; x++)
			if (years >= x)
				var actualYears = x;
		var daysToSubtract = (365 * actualYears);
		for (var x = 0; x <= 364; x++)
			if ((days - daysToSubtract) >= x)
				var actualDays = x;
		var hoursToSubtract = (24 * actualDays) + (365 * 24 * actualYears);
		for (var x = 0; x <= 23; x++)
			if ((hours - hoursToSubtract) >= x)
				var actualHours = x;
		var minutesToSubtract = (60 * actualHours) + (24 * 60 * actualDays) + (365 * 24 * 60 * actualYears);
		for (var x = 0; x <= 59; x++)
			if ((minutes - minutesToSubtract) >= x)
				var actualMinutes = x;
		var secondsToSubtract = (60 * actualMinutes) + (60 * 60 * actualHours) + (24 * 60 * 60 * actualDays) + (365 * 24 * 60 * 60 * actualYears);
		var actualSeconds = seconds - secondsToSubtract;
		// This code is the same in both the countdown and countup functions
		var countupString = '';
		
		if (actualYears > 1) {
			countupString = textOpenTag + 'sono trascorsi<br>' + textCloseTag + numberOpenTag + actualYears + numberCloseTag + textOpenTag + 'anni ' + textCloseTag;
		}
		else if (actualYears == 1) {
			countupString = textOpenTag + '&egrave; trascorso<br>' + textCloseTag + numberOpenTag + actualYears + numberCloseTag + textOpenTag + 'anno ' + textCloseTag;
		}

		if (actualDays > 1 && actualYears < 1)
			countupString = countupString + textOpenTag + '<b>sono trascorsi</b><br>' + textCloseTag;
		else if (actualDays == 1 && actualYears < 1)
			countupString = countupString + textOpenTag + '<b>&egrave; trascorso</b><br>' + textCloseTag;
		
		if (actualDays > 1)
			countupString = countupString +numberOpenTag + '<FONT SIZE=+1 POINT-SIZE=12 color=#920000><b>' + actualDays + '</b></FONT>' + numberCloseTag + textOpenTag + 'gg ' + textCloseTag;
		else if (actualDays == 1)
			countupString = countupString + numberOpenTag + '<FONT SIZE=+1 POINT-SIZE=12 color=#920000><b>' + actualDays + '</b></FONT>' + numberCloseTag + textOpenTag + ' giorno<br>' + textCloseTag;
		
		if (actualHours > 1)
			countupString = countupString + numberOpenTag + '<FONT SIZE=+1 POINT-SIZE=12 color=#920000><b>' + actualHours + '</b></FONT>' + numberCloseTag;
		else if (actualHours == 1)
			countupString = countupString + numberOpenTag + '<FONT SIZE=+1 POINT-SIZE=12 color=#920000><b>' + actualHours + '</b></FONT>' + numberCloseTag;
	
		if (actualMinutes > 1)
			countupString = countupString + numberOpenTag + '<FONT SIZE=-2 POINT-SIZE=8>h</font> <FONT SIZE=+1 POINT-SIZE=12 color=#920000><b>' + actualMinutes + '</b></FONT>' + numberCloseTag;
		else if (actualMinutes == 1)
			countupString = countupString + numberOpenTag + '<FONT SIZE=-2 POINT-SIZE=8>h</font> <FONT SIZE=+1 POINT-SIZE=12 color=#920000><b>' + actualMinutes + '</b></FONT>' + numberCloseTag;
	
		if (actualSeconds > 1)
			countupString = countupString + numberOpenTag + '<FONT SIZE=-2 POINT-SIZE=8>m</font> <FONT SIZE=+1 POINT-SIZE=12 color=#920000><b>' + actualSeconds + '</b></FONT>' + numberCloseTag;
		else if (actualSeconds == 1)
			countupString = countupString + numberOpenTag + '<FONT SIZE=-2 POINT-SIZE=8>m</font> <FONT SIZE=+1 POINT-SIZE=12 color=#920000><b>' + actualSeconds + '</b></FONT>' + numberCloseTag;
		document.getElementById('countdownBody').innerHTML = countupString;
		timer = setTimeout("updateCounter("+year+","+month+","+day+","+hour+","+minute+",'"+ampm+"')",1000);
	}
}

function changeCount(form) {
	clearTimeout(timer);
	var month = form.month.value;
	var day = form.day.value;
	var year = form.year.value;
	var hour = form.hour.value;
	var minute = form.minute.value;
	var ampm = form.ampm.value;
	updateCounter(year,month,day,hour,minute,ampm);
}
// ]]>
