// JavaScript Document

/*Checks if the mandatory input fields were left blank*/
function verifyInputFields(){
var error = new String();

if(policyRead(document.resForm.policyRead)){
	error = "\n\n You have not verified that you have read the Sunset Inn Motel's policies. Please read the policy and ensure that you understand.";
}
if(document.resForm.email.value.length == 0){
	error = "\n\n Please enter your email address and try again. ";
}
if(document.resForm.phone.value.length == 0){
	error = "\n\n Please enter your telephone number and try again. ";
}
if(document.resForm.postalZipCode.value.length == 0){
	error = "\n\n Please enter a postal or zip code and try again. ";
}
if(document.resForm.provState.value.length == 0){
	error = "\n\n Please enter your province or state and try again. ";
}
if(document.resForm.city.value.length == 0){
	error = "\n\n Please enter your city and try again. ";
}
if(document.resForm.address.value.length == 0){
	error = "\n\n You have not entered your address. Please try again. ";
}
if(document.resForm.lastName.value.length == 0){
	error = "\n\n Last name has not been entered. Please try again. ";
}
if(document.resForm.firstName.value.length == 0){
	error = "\n\n First name has not been entered. Please try again. ";
}

if(document.resForm.roomType.value.length == 0){
	error = "\n\n Please Select the type of room you would like to reserve and try again. ";
}

if(error.length > 2){
	alert(error);
	return false;
}
document.resForm.submit();
return true;
}

function policyRead(checkBox){
	if(checkBox.checked){ 
		return false; 
	}
	return true;
}

/*Sets the arrival date to the current date and the departure date to the next date (tomorrow).*/
function setDefaultDates(){
	var date = new Date();
	var day = date.getDate();
	var month = date.getMonth();
	var year = date.getFullYear();

	var departDate = new Date();
	departDate.setDate(date.getDate()+1);

	document.resForm.arrivalMonth.selectedIndex = month;
	document.resForm.arrivalDay.selectedIndex = day-1;
	
	for(var i=0; i<document.resForm.arrivalYear.length; i++){
		if(document.resForm.arrivalYear[i].value == year){
			document.resForm.arrivalYear.selectedIndex = i;
		}
	}
	//sets the default Departure Date
	document.resForm.departMonth.selectedIndex = departDate.getMonth();
	document.resForm.departDay.selectedIndex = departDate.getDate()-1;
	
	for(var i=0; i<document.resForm.departYear.length; i++){
		if(document.resForm.departYear[i].value == departDate.getFullYear()){
			document.resForm.departYear.selectedIndex = i;
		}
	}
	
	//reset the number of days back to 01.
	document.resForm.numberOfDays.selectedIndex = 0;
}

/* When the date changes by the user, it will automatically change the number of days to the correct number.
   eg. Arriving on Jan 26, 2006 and checking out on Jan 29, 2006, the number of days will then change to 3. */
function updateNumOfNights(){
	
	var arrivalDate = new Date(document.resForm.arrivalYear.value, document.resForm.arrivalMonth.selectedIndex, document.resForm.arrivalDay.value);
	var departure = new Date(document.resForm.departYear.value, document.resForm.departMonth.selectedIndex, document.resForm.departDay.value);
	//Set 1 day in milliseconds
	var oneDay=1000*60*60*24

	//Calculate difference between the arrival and departure dates, and convert to days
	var dateDifference = Math.ceil((departure.getTime()-arrivalDate.getTime())/(oneDay));
	document.resForm.numberOfDays.selectedIndex = (dateDifference-1);
}


/* Changes the departure date based on the arrival date and the number of days staying. 
   Eg. Arriving on Jan 20, 2006 and staying for 5 days, means the departure date is Jan 25, 2006. */
function updateDepartDate(){
	
	var arrivalDate = new Date(document.resForm.arrivalYear.value, document.resForm.arrivalMonth.selectedIndex, document.resForm.arrivalDay.value);
	var numberDays = (document.resForm.numberOfDays.selectedIndex + 1);
	var departDate = new Date();
	departDate.setDate(departDate.getDate() + numberDays);
	
	//sets the Departure Date to the right date based on changing the arrival date and the number of days. 
	document.resForm.departMonth.selectedIndex = departDate.getMonth();
	document.resForm.departDay.selectedIndex = departDate.getDate()-1;
	
	for(var i=0; i<document.resForm.departYear.length; i++){
		if(document.resForm.departYear[i].value == departDate.getFullYear()){
			document.resForm.departYear.selectedIndex = i;
		}
	}
	
}



