/*
Author: Shawn Patel, Saber Robotics Programming Team Member
Saber Robotics (FIRST Team #2506), Franklin, Wisconsin

You can use this code in any manner so long as the text above remains intact.
*/

function makeClock(spaces_before,caption,days_id,hours_id,minutes_id,seconds_id,Month,Day,Hour)
{
	for (i = 1; i <= spaces_before; i = i + 1)
	{
		document.write('<br>');
	}
	document.write(
	   '<table align="center" class="clock" border="1" cellpadding="0" cellspacing="0" width="145px"><caption>'
	  +caption+'</caption><tr><td width="25%" align="center" class="clock"><span class="clock-digit" id="'+days_id
	  +'">Please</span></td><td width="25%" align="center" class="clock"><span class="clock-digit" id="'+hours_id
	  +'">Enable</span></td><td width="25%" align="center" class="clock"><span class="clock-digit" id="'+minutes_id
	  +'">Java</span></td><td width="25%" align="center" class="clock"><span class="clock-digit" id="'+seconds_id
	  +'">script!</span></td></tr><tr><td align="center" class="clock">D</td><td align="center" class="clock">H</td>'
	  +'<td align="center" class="clock">M</td><td align="center" class="clock">S</td></tr></table>'
	);
	//displayTimer("ship_d","ship_h","ship_m","ship_s",Month - 1,Day,Hour);
}

function clockCount()
{
	displayTimer('ship_d','ship_h','ship_m','ship_s',1,23,15);
	displayTimer('wisc_d','wisc_h','wisc_m','wisc_s',2,11,15);
	displayTimer('ohio_d','ohio_h','ohio_m','ohio_s',2,25,15);
	setTimeout('clockCount();',1000);
}

function displayTimer(d_id,h_id,m_id,s_id,month,day,hour) //month, day, and hour is for TARGET time
{
	var fromDate = new Date();	//Current time/date
	var days = 0;	//These variables are for how much time left
	var hours = 0;
	var mins = 0;
	var secs = 0;
	var timeout = 0;	//Error trapping variable
	
	//Calculating Days	
	if (month > fromDate.getMonth())	//If not in the target month, add days to the target days to compensate
	{
		if (fromDate.getMonth() == 0)
			day = day + 31 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 1)
			day = day + 28 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 2)
			day = day + 31 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 3)
			day = day + 30 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 4)
			day = day + 31 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 5)
			day = day + 30 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 6)
			day = day + 31 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 7)
			day = day + 31 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 8)
			day = day + 30 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 9)
			day = day + 31 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 10)
			day = day + 30 * (month - fromDate.getMonth());
		if (fromDate.getMonth() == 11)
			day = day + 31 * (month - fromDate.getMonth());
	}
	else if (month < fromDate.getMonth())	//Error trap to check if past target month
		timeout = 1;	//No checker needed for current month since nothing should happen
	days = day - fromDate.getDate();	//Calculates amount of days
	if (days < 0)	//Error trap to check if past days
		timeout = 1;
	
	//Calculating Hours
	if (hour < fromDate.getHours()) //Like subtraction in math, if it needs more hours to be positi
	{
		days = days - 1;
		hour = hour + 24;
	}
	hours = hour - fromDate.getHours() - 1;
	
	//Calculating Minutes
	mins = 60 - fromDate.getMinutes() - 1;
	
	//Calculating Seconds
	secs = 60 - fromDate.getSeconds();
	if (secs == 60)
	{
		secs = 0;
		mins = mins + 1;
	}
		
	if (timeout == 0)	//Only outputs if CURRENT time is before TARGET
	{
		document.getElementById(d_id).innerHTML = days;
		document.getElementById(h_id).innerHTML = hours;
		document.getElementById(m_id).innerHTML = mins;
		document.getElementById(s_id).innerHTML = secs;
		
	}
	else	//Only outputs if CURRENT time is past TARGET
	{
		document.getElementById(d_id).innerHTML = "The"
		document.getElementById(h_id).innerHTML = "time"
		document.getElementById(m_id).innerHTML = "has"
		document.getElementById(s_id).innerHTML = "come!"
	}
	
}
