// This code will check the form values to make sure that they have
// something entered in.
// ***********************************************************************************
// This file requires functions in reserve_code.js which means that will need to be
// included in the html file with this file.
// ***********************************************************************************

function checkform(theform)
{
	if (getSelectedRadioValue(theform.semester) == "")
	{
		alert("Please select a semester.");
		return false;
	}
			
	// Check the Am Times, Return false if the check failed
	if (!checkAmTimes(theform))
	{
		return false;
	}
	// Check the Pm Times, Return false if the check failed
	if (!checkPmTimes(theform))
	{
		return false;
	}
			
	if (theform.name.value == "")
	{
		alert("Please enter a name.");
		return false;
	}
	if (theform.address.value == "")
	{
		alert("Please enter an address.");
		return false;
	}
	if (theform.city.value == "")
	{
		alert("Please enter a city.");
		return false;
	}
	if (theform.state.value == "")
	{
		alert("Please enter a state.");
		return false;
	}
	if (theform.zip.value == "")
	{
		alert("Please enter a zip code.");
		return false;
	}
	if (theform.phone.value == "")
	{
		alert("Please enter a phone number.");
		return false;
	}
	if (theform.email.value == "")
	{
		alert("Please enter an e-mail address.");
		return false;
	}
	if (theform.studentID.value == "")
	{
		alert("Please enter a student ID.");
		return false;
	}
		return true;
}

// This will go through all of the Am depature days a check
// that a location and time have been selected for each day
// that is checked.
function checkAmTimes(theform)
{
	var chk_am_index = 0;
	var sel_am_stop = 0;
	var sel_am_time = 0;
	var i = 0;
	
	for (i = 1; i <= 5; i++)
	{
		chk_am_index = find_element_index(theform, "chkAm" + i);
		sel_am_stop = find_element_index(theform, "selAmFrom" + i);
		sel_am_time = find_element_index(theform, "selAmTime" + i);
		
		if (theform.elements[chk_am_index].checked)
		{
			if (theform.elements[sel_am_stop].value == "-1")
			{
				alert("Please Select a Departure Location for " + getDayText(i));
				return false;
			}
			else
			{
				if (theform.elements[sel_am_time].value == "-1")
				{
					alert("Please Select a Departure Time for " + getDayText(i));
					return false;
				}
			}
		}
	}
	
	return true;
}

// This will go through all of the Pm depature days a check
// that a location and time have been selected for each day
// that is checked.
function checkPmTimes(theform)
{
	var chk_pm_index = 0;
	var sel_pm_stop = 0;
	var sel_pm_time = 0;
	var i = 0;
	
	for (i = 1; i <= 5; i++)
	{
		chk_pm_index = find_element_index(theform, "chkPm" + i);
		sel_pm_stop = find_element_index(theform, "selPmDest" + i);
		sel_pm_time = find_element_index(theform, "selPmTime" + i);
		
		if (theform.elements[chk_pm_index].checked)
		{
			if (theform.elements[sel_pm_stop].value == "-1")
			{
				alert("Please Select a Destination Location for " + getDayText(i));
				return false;
			}
			else
			{
				if (theform.elements[sel_pm_time].value == "-1")
				{
					alert("Please Select a Return Time for " + getDayText(i));
					return false;
				}
			}
		}
	}
	
	return true;
}

// This will return the text for the name of the day referenced
// by the passed in index.
function getDayText(index)
{
	switch (index)
	{
		case 1:
			return "Monday";
			break;
		case 2:
			return "Tuesday";
			break;
		case 3:
			return "Wednesday";
			break;
		case 4:
			return "Thursday";
			break;
		case 5:
			return "Friday";
	}
}