// JavaScript Document


function check(thisform)
	{
		with(this)
		var agree = confirm("Do you wish to clear the form and start over?");
		if (agree)
		{thisform.clear();}
			else {return false}
			}



function confirmSubmit(func1, func2, thisform){




if (func1 & func2 == true){

return true;
}

else {return false;}
}





/*This function uses other functions to guarantee that the user cannot schedule a campus tour sooner than one week of requesting it. It receives values from the form fields titled RequestedTourDateDay and RequestedTourDateMonth and uses JavaScripts built in calendar abilities to ensure that at least seven days exist between the current time and the date the user is requesting a visit. It is called by the confirmSubmit() function and returns to it either true or false depending on whether or not the user has inputted a valid date to request a campus visit.
*/
function guaranteeWeek(inputMonth, inputDay){

/*Get current date */
var today = new Date();
var month = today.getMonth() + 1;
var day = today.getDate();

/*Create date object(?) using input values the user will specify in the form*/
var useDate = new Date();
useDate.setFullYear(2009, inputMonth, inputDay);
var useDay = useDate.getDate();
var useMonth = useDate.getMonth();


/*Ensure that the user does not select a day of the month that does not exist. For example, a user who requests a date of Feb. 31 should be informed that there are only 28 days in Feb. and should not be allowed to continue until the error is fixed. Also check to make sure the user only schedules on a weekday or the second saturday of each month.
*/
if( ensureDateExists(inputMonth, inputDay) == false)
	{return false;}

if (mondayThruFriday(inputMonth, inputDay) == false)
	{
	alert("Please remember that group tours are offered only Monday thru Friday and the second Saturday of each month."); 
	   return false;
	}


/*Make sure user cannot receive a "valid date" note if they have not entered a date and click submit anyway
*/
	if (inputMonth == "" | inputMonth == null)
	{alert("Please select a date for your requested visit."); return false;}
	
	if (inputDay == "" | inputDay == null)
	{alert("Please select a date for your requested visit."); return false;}


/*Use the getAddress() function to find the "address"of the desired dates in the array created by the getAddress() function and return them to the ensureWeekSpan() function which will guarantee at least seven days exist between the day the user submits the request and the day the user desires to have a campus tour.
*/
var addressOfWantedDate = getAddress(useMonth, useDay, addMonths());
var addressOfCurrentDate = getAddress(month, day, addMonths());


	
	if (getAddress(useMonth, useDay, addMonths()) == false)
	{alert("Please select a date"); return false;}
	
	if	(ensureWeekSpan(addressOfCurrentDate, addressOfWantedDate) == true)
		{return true;}
	
	if (ensureWeekSpan(addressOfCurrentDate, addressOfWantedDate) == false)
		{
		alert("Please allow us at least one week between now "
				+	"and the date of your requested visit.");
		 return false;}
//close guaranteeWeek()
}






/*This function is called by a button next to the drop down fields where a user can request a date for a visit. This function is basically the guaranteeWeek() function but will display the message "You have selected a valid date" if the user has selected a valid date.
*/
function dateIsValid(inputMonth, inputDay){

var today = new Date();
var month = today.getMonth() + 1;
var day = today.getDate();

var useDate = new Date();
useDate.setFullYear(2009, inputMonth, inputDay);
var useDay = useDate.getDate();
var useMonth = useDate.getMonth();


/*Ensure that the user does not select a day of the month that does not exist. For example, a user who requests a date of Feb. 31 should be informed that there are only 28 days in Feb. and should not be allowed to continue until the error is fixed. Also disallow scheduling on weekends except the second saturday of the month.
*/
if( ensureDateExists(inputMonth, inputDay) == false)
	{return false;}
	
if (mondayThruFriday(inputMonth, inputDay) == false)
	{
	alert("Please remember that tours are offered only Monday thru Friday and the second Saturday of each month."); 
	   return false;
	}




/*Make sure user cannot receive a "valid date" note if they have not entered a date but click submit anyway
*/
	if (inputMonth == "" | inputMonth == null)
	{alert("Please select a date for your requested visit."); return false;}
	
	if (inputDay == "" | inputDay == null)
	{alert("Please select a date for your requested visit."); return false;}
	
	
	
	/*Use the getAddress() function to find the "address"of the desired dates in the array created by the getAddress() function and return them to the ensureWeekSpan() function which will guarantee at least seven days exist between the day the user submits the request and the day the user desires to have a campus tour.
*/
var addressOfWantedDate = getAddress(useMonth, useDay, addMonths());
var addressOfCurrentDate = getAddress(month, day, addMonths());

	if (getAddress(useMonth, useDay, addMonths()) == false)
	{alert("Please select a date"); return false;}

	if	(ensureWeekSpan(addressOfCurrentDate, addressOfWantedDate) == true)
		{alert("You have selected a valid date.");return true;}
	
	if (ensureWeekSpan(addressOfCurrentDate, addressOfWantedDate) == false)
		{
		alert("Please allow us at least one week between now "
				+	"and the date of your requested visit.");
		 return false;}
//close dateIsValid()
}





/*This function ensures that user user can only select weekdays or the second saturday of each month. It basically  disallows scheduling on dates entered into the array weekEnds. */
function mondayThruFriday(month, day){

/*Number of days in each month of the year 2009*/
		var daysInJan = 31;
		var daysInFeb = 28;
		var daysInMar = 31;
		var daysInApr = 30;
		var daysInMay = 31;
		var daysInJun = 30;
		var daysInJul = 31;
		var daysInAug = 31;
		var daysInSep = 30;
		var daysInOct = 31;
		var daysInNov = 30;
		var daysInDec = 31;

/*create index value (iOffset) to locate user-specified months in the array returned by addMonths().*/
var toJan = 0;
var toFeb = daysInJan;
var toMar = daysInJan + daysInFeb;
var toApr = daysInJan + daysInFeb + daysInMar;
var toMay = daysInJan + daysInFeb + daysInMar + daysInApr;
var toJun = daysInJan + daysInFeb + daysInMar + daysInApr + daysInMay;



/*weekends in January are array addresses [3,4, 10,11, 17,18, 24,25] The Second saturday of each month is omitted to allow for users to schedule weekend tours on the second saturday of each month */


/*Invalid days of January*/
invalidDays = new Array(3,4,11,17,18,24,25);
if (month == 1) 
	{
		for (var i=0; i <= 7; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	/*Invalid days of February*/
invalidDays = new Array(1,7,8,15,21,22,28);
if (month == 2) 
	{
		for (var i=0; i <= 7; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	
	/*Invalid days of March*/
invalidDays = new Array(1,7,8,15,21,22,28,29);
if (month == 3) 
	{
		for (var i=0; i <= 8; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	
	/*Invalid days of April*/
invalidDays = new Array(4,5,12,18,19,25,26);
if (month == 4) 
	{
		for (var i=0; i <= 7; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	/*Invalid days of May*/
invalidDays = new Array(2,3,10,16,17,23,24,30,31);
if (month == 5) 
	{
		for (var i=0; i <= 9; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	/*Invalid days of June*/
invalidDays = new Array(6,7,14,20,21,27,28);
if (month == 6) 
	{
		for (var i=0; i <= 7; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	/*Invalid days of July*/
invalidDays = new Array(4,5,12,18,19,25,26);
if (month == 7) 
	{
		for (var i=0; i <= 7; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}



/*Invalid days of August*/
invalidDays = new Array(1,2,9,15,16,22,23,29,30);
if (month == 8) 
	{
		for (var i=0; i <= 9; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	/*Invalid days of September*/
invalidDays = new Array(5,6,13,19,20,26,27);
if (month == 9) 
	{
		for (var i=0; i <= 7; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	/*Invalid days of October*/
invalidDays = new Array(3,4,11,17,18,24,25,31);
if (month == 10) 
	{
		for (var i=0; i <= 8; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	/*Invalid days of November*/
invalidDays = new Array(1,7,8,15,21,22,28,29);
if (month == 11) 
	{
		for (var i=0; i <= 8; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}
	
	
	/*Invalid days of December*/
invalidDays = new Array(5,6,13,19,20,26,27);
if (month == 12) 
	{
		for (var i=0; i <= 7; i++)
			{
				if (day == invalidDays[i])
					{
					return false;
					}
			}	
	}

return true;
//end mondayThruFriday()
}



/*Ensure that the user does not select a day of the month that does not exist. For example, a user who requests a date of Feb. 31 should be informed that there are only 28 days in Feb. and should not be allowed to continue until the error is fixed.
*/
function ensureDateExists(month, day){
	
/*Number of days in each month of the year 2009*/
		var daysInJan = 31;
		var daysInFeb = 28;
		var daysInMar = 31;
		var daysInApr = 30;
		var daysInMay = 31;
		var daysInJun = 30;
		var daysInJul = 31;
		var daysInAug = 31;
		var daysInSep = 30;
		var daysInOct = 31;
		var daysInNov = 30;
		var daysInDec = 31;


/*Only worry about the days with < 31 days because the drop down menu has 31 days*/

//validate February
if (month == 2 & (day > daysInFeb) )
	{
		alert("[Requested Tour Date] There are only " + daysInFeb + " days in February.");
		return false;
	}
	
//validate April
if (month == 4 & (day > daysInApr) )
	{
		alert("[Requested Tour Date] There are only " + daysInApr + " days in April.");
		return false;
	}
	
	//validate June
if (month == 6 & (day > daysInJun) )
	{
		alert("[Requested Tour Date] There are only " + daysInJun + " days in June.");
		return false;
	}
	
	//validate September
if (month == 9 & (day > daysInSep) )
	{
		alert("[Requested Tour Date] There are only " + daysInSep + " days in September.");
		return false;
	}
	
	//validate April
if (month == 11 & (day > daysInNov) )
	{
		alert("[Requested Tour Date] There are only " + daysInNov + " days in November.");
		return false;
	}
	
else {return true;}



//end ensureDateExists()
}



/*This function creates an array containing each day of the year. It is given the number of days in each month for the year 2009 and puts each of them into an array containing 366 (number of days in the year with a 0 thrown in the beginning) elements. The elements of the array represent the days of the year and if displayed would look like this;
[0,1,2,3,4,5,....31,(end January begin February),32,33,34...,365]. 
Day 32 would be February 1st, day 33 would be Feb. 2 and so on. The reason for this is I couldn't create two Date() objects and subtract them from each other to ensure at least week in between them. This array allows for taking the desired date and subtracting from it the current date. A difference less than 7(days) will be an invalid date. Basically, the method allows the program to compare two dates that are in different months. This seems to commonly be a problem for people.
This array can be extended into the future when necessary and once leap years are taken into account will probably work indefinitely.
*/
function addMonths(){

/*Number of days in each month of the year 2009*/
		var daysInJan = 31;
		var daysInFeb = 28;
		var daysInMar = 31;
		var daysInApr = 30;
		var daysInMay = 31;
		var daysInJun = 30;
		var daysInJul = 31;
		var daysInAug = 31;
		var daysInSep = 30;
		var daysInOct = 31;
		var daysInNov = 30;
		var daysInDec = 31;
		
	

/*Set incremental value to consecutively add 1 to each new element of array.*/
	var step = 1;
	
	/*Create array containing enough elements for each day of the year*/
	array = new Array(1 + daysInJan + daysInFeb + daysInMar + daysInApr
		                      + daysInMay + daysInJun + daysInJul + daysInAug
		                      + daysInSep + daysInOct + daysInNov + daysInDec);
							  


//create days for January
		for (var i=0; i<daysInJan;i++)
		{
			array[i] = step;
			step++;
		}	

		//reset incremental value
		step = 1;
		//create days for February
		for (var i=(1 + daysInJan); i<=(daysInJan + daysInFeb); i++)
		{
			array[i] = step;
			step++;
		}

		//create days for March
		//reset incremental value
		step = 1;
		//create days for March\
		for (var i=(1 + daysInJan + daysInFeb);i<=(daysInJan + daysInFeb
				+ daysInMar);i++)
		{
			array[i] = step;
			step++;
		}
		
		//reset incremental value
		step = 1;
		//determine value of i (iOffset) which accounts for previous months
		var iOffset = daysInJan + daysInFeb + daysInMar;
		//create days for April
		for (var i=(1 + iOffset);i<=(iOffset + daysInApr);i++)
		{
			array[i] = step;
			step++;
		}
	
		//reset incremental value
		step = 1;
		//determine value of i
		iOffset = daysInJan + daysInFeb + daysInMar + daysInApr;
		
		//create days for May
		for (var i=(1 + iOffset);i<=(iOffset + daysInMay);i++)
		{
			array[i] = step;
			step++;
		}
		
		//reset incremental value
		step = 1;
		//determine value of i
		iOffset = daysInJan + daysInFeb + daysInMar + daysInApr + daysInMay;
		
		//create days for June
		for (var i=(1 + iOffset);i<=(iOffset + daysInJun);i++)
		{
			array[i] = step;
			step++;
		}
		
		//reset incremental value
		step = 1;
		//determine value of i
		iOffset = daysInJan + daysInFeb + daysInMar + daysInApr + daysInMay  
						+ daysInJun;
		
		//create days for July
		for (var i=(1 + iOffset);i<=(iOffset + daysInJul);i++)
		{
			array[i] = step;
			step++;
		}
		
		//reset incremental value
		step = 1;
		//determine value of i
		iOffset = daysInJan + daysInFeb + daysInMar + daysInApr + daysInMay  
						+ daysInJun + daysInJul;
		
		//create days for August
		for (var i=(1 + iOffset);i<=(iOffset + daysInAug);i++)
		{
			array[i] = step;
			step++;
		}
		
		//reset incremental value
		step = 1;
		//determine value of i
		iOffset = daysInJan + daysInFeb + daysInMar + daysInApr + daysInMay  
						+ daysInJun + daysInJul + daysInAug;
		
		//create days for September
		for (var i=(1 + iOffset);i<=(iOffset + daysInSep);i++)
		{
			array[i] = step;
			step++;
		}
		
		//reset incremental value
		step = 1;
		//determine value of i
		iOffset = daysInJan + daysInFeb + daysInMar + daysInApr + daysInMay  
						+ daysInJun + daysInJul + daysInAug + daysInSep;
		
		//create days for October
		for (var i=(1 + iOffset);i<=(iOffset + daysInOct);i++)
		{
			array[i] = step;
			step++;
		}
		
		//reset incremental value
		step = 1;
		//determine value of i
		iOffset = daysInJan + daysInFeb + daysInMar + daysInApr + daysInMay  
						+ daysInJun + daysInJul + daysInAug + daysInSep 
						+ daysInOct;
		
		//create days for November
		for (var i=(1 + iOffset);i<=(iOffset + daysInNov);i++)
		{
			array[i] = step;
			step++;
		}
		
		//reset incremental value
		step = 1;
		//determine value of i
		iOffset = daysInJan + daysInFeb + daysInMar + daysInApr + daysInMay  
						+ daysInJun + daysInJul + daysInAug + daysInSep 
						+ daysInOct + daysInNov;
		
		//create days for December
		for (var i=(1 + iOffset);i<=(iOffset + daysInDec);i++)
		{
			array[i] = step;
			step++;
		}
		
		return array;
		
	//end addMonths() function
}

/*Method to accept three arguments, the desired month, day, and the array containing
	 * each day of the year created by the function addMonths(). It will accept a string for the 
	 * month such as "February" or "March" and since there are 31 days in January it would change
	 * Ioffset to 31 to obtain days in February and then add the days the user selected and 		     * return an array address for that date in

	 * february.
	 */	
function getAddress(month, day, array){
	
	
	
	
	/*Establish the number of days in each month*/
		var daysInJan = 31;
		var daysInFeb = 28;
		var daysInMar = 31;
		var daysInApr = 30;
		var daysInMay = 31;
		var daysInJun = 30;
		var daysInJul = 31;
		var daysInAug = 31;
		var daysInSep = 30;
		var daysInOct = 31;
		var daysInNov = 30;
		var daysInDec = 31;
		

		/*Create offsets for each month*/
		var janOffset = 0;
		var febOffset = daysInJan;
		var marOffset = febOffset + daysInFeb;
		var aprOffset = marOffset + daysInMar;
		var mayOffset = aprOffset + daysInApr;
		var junOffset = mayOffset + daysInMay;
		var julOffset = junOffset + daysInJun;
		var augOffset = julOffset + daysInJul;
		var sepOffset = augOffset + daysInAug;
		var octOffset = sepOffset + daysInSep;
		var novOffset = octOffset + daysInOct;
		var decOffset = novOffset + daysInNov;
		//these variables are valid
		
		 
		
		
		
		//get date from user input of month and day
		if (month == 1 || month == 01)
			{
			//subtract 1 from day to account for 0 as first element in array.
				var iOffset = 0;
				var address = iOffset + day;
				
				return address;
				
			}
			
		if (month == 2 || month == 02)
		{
			var iOffset = febOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 3)
		{
			var iOffset = marOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 4)
		{
			var iOffset = aprOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 5)
		{
			var iOffset = mayOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 6)
		{
			var iOffset = junOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 7)
		{
			var iOffset = julOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 8)
		{
			var iOffset = augOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 9)
		{
			var iOffset = sepOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 10)
		{
			var iOffset = octOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 11)
		{
			var iOffset = novOffset;
			var address = iOffset + day;
			
			return address;
		}
		
		if (month == 12)
		{
			var iOffset = decOffset;
			var address = iOffset + day;
			
			return address;
		}
	
	//close function
}

/*This function receives the "addresses" of the date the user submits his/her request on and the date the user wishes to receive a campus tour. It will take the difference of the two addresses and if the difference is less than 7 (for seven days) it will return false. Other wise it returns true for a valid date.
*/
function ensureWeekSpan(currentDateAddress, wantedDateAddress)
	{
		var number = wantedDateAddress - currentDateAddress;
		
		if ( (wantedDateAddress - currentDateAddress) < 7)
			{return false;}
		
		else {return true;}
		
	}



/*inform the user of required fields not filled in while validating form*/
function validate_required(field)
{
with (field)
{
if (value==null||value=="")
  {return false;}
else {return true}
}
}

/*inform the user of required fields that were not filled in while validating form*/
function validate_form(thisform)
{
with (thisform)
{

var requiredEntered = 0;

/* Full Name */
if (validate_required(FullName)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var FullNameStr = "Full Name, ";
  }
  
  if (validate_required(FullName)==true)
  {
  var FullNameStr = "";
  }
  /* End Full Name */
  
  
  /* Email */
if (validate_required(Email)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var EmailStr = "Email, ";
  }
  
  if (validate_required(Email)==true)
  {
  var EmailStr = "";
  }
  /* End Email */
  
 
  /* Cell Phone */
if (validate_required(CellPhone)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var CellPhoneStr = "Cell Phone, ";
  }
  
  if (validate_required(CellPhone)==true)
  {
  var CellPhoneStr = "";
  }
  /* End Cell Phone */
  
  
  
  /* Original Date of Campus Visit */
 		/* Month */
if (validate_required(OriginalTourDateMonth)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var OriginalTourDateMonthStr = "Original Date of Campus Visit (Month), ";
  }
  
  if (validate_required(OriginalTourDateMonth)==true)
  {
  var OriginalTourDateMonthStr = "";
  }
  		/* End Month */
  
  		/* Day */
  if (validate_required(OriginalTourDateDay)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var OriginalTourDateDayStr = "Original Date of Campus Visit (Day), ";
  }
  
  if (validate_required(OriginalTourDateDay)==true)
  {
  var OriginalTourDateDayStr = "";
  }
  		/* End Day */
  
  	/* Year */
  if (validate_required(OriginalTourDateYear)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var OriginalTourDateYearStr = "Original Date of Campus Visit (Year), ";
  }
  
  if (validate_required(OriginalTourDateYear)==true)
  {
  var OriginalTourDateYearStr = "";
  }
  		/* End Year */
  /* End Original Date of Campus Visit */
  
  
  
  
  
  /* Requested Date of Campus Visit */
 		/* Month */
if (validate_required(RequestedTourDateMonth)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var RequestedTourDateMonthStr = "Requested Date of Campus Visit (Month), ";
  }
  
  if (validate_required(RequestedTourDateMonth)==true)
  {
  var RequestedTourDateMonthStr = "";
  }
  		/* End Month */
  
  		/* Day */
  if (validate_required(RequestedTourDateDay)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var RequestedTourDateDayStr = "Requested Date of Campus Visit (Day), ";
  }
  
  if (validate_required(RequestedTourDateDay)==true)
  {
  var RequestedTourDateDayStr = "";
  }
  		/* End Day */
  
  	/* Year */
  if (validate_required(RequestedTourDateYear)==false)
  {
  var requiredEntered = requiredEntered + 1;
  var RequestedTourDateYearStr = "Requested Date of Campus Visit (Year), ";
  }
  
  if (validate_required(RequestedTourDateYear)==true)
  {
  var RequestedTourDateYearStr = "";
  }
  		/* End Year */
  /* End Requested Date of Campus Visit */
  
  
  /* Disclaimer (check box at bottom) */ 	
 if (document.groupform.Disclaimer.checked == true)
 {
  var DisclaimerStr = "";
 }
 
  if (document.groupform.Disclaimer.checked == false)
 {
 var requiredEntered = requiredEntered + 1;
  var DisclaimerStr = "Please check the box just above the 'Submit My Request' button after reading the text next to it.";
 }
 
 if (requiredEntered == 0)
 {

 return true;
 }
 
 if (requiredEntered != 0)
 {
 alert("Please fill in the following fields: " + FullNameStr + EmailStr + CellPhoneStr 
 			+ RequestedTourDateMonthStr
			+ RequestedTourDateDayStr + RequestedTourDateYearStr +OriginalTourDateMonthStr
			  + OriginalTourDateDayStr +   OriginalTourDateYearStr     +     DisclaimerStr);
 return false;
 }
 
 

 
 
 
 //end with (thisform)
 }
 //end validate_form()
 }

