// Start the timer to update the count
setInterval("showCountdown()",100);
//if (window.attachEvent) {window.attachEvent('onload', setInterval("showCountdown()",100));}
//else if (window.addEventListener) {window.addEventListener('load', setInterval("showCountdown()",100), false);}
//else {document.addEventListener('load', setInterval("showCountdown()",100), false);} 

function showCountdown(){
	// Set today to the current date and time
	var today = new Date();

	// Store the future events in Date objects
	var Date1 = new Date("September 22, 2012 08:00:00");
	var Date2 = new Date("September 22, 2012 10:00:00");
	var Date3 = new Date("September 22, 2012 11:30:00");
	var Date4 = new Date("January 1, 2011 12:00:00");

	// Show the current date and time
 	document.eventform.thisDay.value = showDateTime(today);

	changeYear(today, Date1);
	changeYear(today, Date2);
	changeYear(today, Date3);
	changeYear(today, Date4);
	
 	document.eventform.count1.value = countdown(today, Date1);
 	document.eventform.count2.value = countdown(today, Date2); 	
 	document.eventform.count3.value = countdown(today, Date3); 	
 	document.eventform.count4.value = countdown(today, Date4); 	
}

//    Returns the date in a text string formatted as:
//    mm/dd/yyyy at hh:mm:ss am
function showDateTime(time) {
	date = time.getDate();
	month = time.getMonth()+1;
	year = time.getFullYear();

	second = time.getSeconds();
	minute = time.getMinutes();
	hour = time.getHours();

	ampm = (hour < 12) ? " a.m." : " p.m.";
	hour = (hour > 12) ? hour - 12 : hour;
	hour = (hour == 0) ? 12 : hour;

	minute = minute < 10 ? "0"+minute : minute;
	second = second < 10 ? "0"+second : second;

	// Return formatted string
	return month+"/"+date +"/"+year+" at "+hour+":"+minute+":"+second+ampm;
}

/*  changeYear(today, holiday)
      Changes the year value of the holiday object to point to the
      next year if it has already occurred in the present year
*/
function changeYear(today, holiday) {
   year=today.getFullYear();
   holiday.setFullYear(year);
   year = (holiday < today) ? year+1 : year;
   holiday.setFullYear(year);
}

/* countdown(stop, start)
      Displays the time between the stop and start date objects in the
      text format:
      dd days, hh hrs, mm mins, ss secs
*/
function countdown(stop, start) {
   time = start-stop;
   days = time/(1000*60*60*24);
   hours = (days - Math.floor(days))*24;
   minutes = (hours - Math.floor(hours))*60;
   seconds = (minutes - Math.floor(minutes))*60;
   return parseInt(days)+" days, "+parseInt(hours)+" hrs, "+parseInt(minutes)+" mins, "+parseInt(seconds)+" secs";
}
