var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var nextYear = currentYear + 1;
var currentMonth = currentDate.getMonth();
var currentDay = currentDate.getDate();
var currentHour = currentDate.getHours();
var setTheDate = "yes";

function set_date() { // sets the month, day and hour fields to now
	if (setTheDate == "yes") {
		if (parseInt(currentHour) == 0) { // i.e., it's midnight
			currentHour = 12;
			document.apptForm.AmPm.selectedIndex = 0; // i.e., AM
		} else if (parseInt(currentHour) < 12) { // i.e., it's morning
			document.apptForm.AmPm.selectedIndex = 0; // i.e., AM
		} else if (parseInt(currentHour) == 12) { // i.e., it's noon
			document.apptForm.AmPm.selectedIndex = 1; // i.e., PM
		} else if (parseInt(currentHour) > 12) { // i.e., it's afternoon
			currentHour = currentHour - 12;
			document.apptForm.AmPm.selectedIndex = 1;
		}
		document.apptForm.year.value = currentYear;
		document.apptForm.Month.selectedIndex = parseInt(currentMonth + 1);
		document.apptForm.Day.selectedIndex = parseInt(currentDay);
		document.apptForm.Hour.selectedIndex = parseInt(currentHour - 1);
		document.getElementById('displayYear').innerHTML = currentYear;
		setTheDate = "no"; //once date has been set, don't do it again
	}
return false;
}

function form_validator(theForm) {
	if(theForm.FirstName.value == "") {
		 alert("Please enter your first name.");
		 theForm.FirstName.focus();
		 return false;
	}
	
	if(theForm.LastName.value == "") {
		 alert("Please enter your last name.");
		 theForm.LastName.focus();
		 return false;
	}
	// Check selected confirmation method and 
	// confirm that the correct fields have been filled out
	var confType;
	for (var i=0; i < theForm.Confirm.length; i++) {
		if (theForm.Confirm[i].checked) {
		  confType = theForm.Confirm[i].value;
		}
		if (((confType == "Phone") || (confType == "Phone and Email")) && (theForm.Phone.value == "")) {
			alert("Please enter your phone number so we may confirm your appointment.");
			theForm.Phone.focus();
			return false;
		} else if (((confType == "Email") || (confType == "Phone and Email")) && (theForm.email.value == "")) {
 			alert("Please enter your email address so we may confirm your appointment.");
			theForm.email.focus();
			return false;
		}
	}

	// check email address if filled out
	if ((theForm.email.value != "") && (!emailvalid(theForm.email.value))) {
		 alert("Please make sure your email address is entered correctly.");
		 theForm.email.focus();
		 return false;
	}
	// make sure at least one service has been selected
	if (theForm.FirstService.selectedIndex == 0) {
		 alert("Please select the service for which you are requesting this appointment.");
		 theForm.FirstService.focus();
		 return false;
	}

	// make sure appointment date has been selected
	if (theForm.Month.selectedIndex == 0) {
		 alert("Please select the month for your requested appointment date.");
		 theForm.Month.focus();
		 return false;
	}
	if (theForm.Day.selectedIndex == 0) {
		 alert("Please select the day for your requested appointment date.");
		 theForm.Day.focus();
		 return false;
	}

return true;
}

function set_year() {
	// make sure appt date isn't in the past
	if ((document.apptForm.Month.selectedIndex == 0) || (document.apptForm.Day.selectedIndex == 0)){ //either month or day not selected
		document.getElementById('displayYear').innerHTML = currentYear;
	} else if (document.apptForm.Month.selectedIndex <= currentMonth) { //if prev. month, set to next year
		 document.getElementById('displayYear').innerHTML = nextYear;
	} else if ((document.apptForm.Month.selectedIndex == (currentMonth +1)) && (document.apptForm.Day.selectedIndex < currentDay)) { //if this month but prev. day, set to next year
		 document.getElementById('displayYear').innerHTML = nextYear;
	} else {
		 document.getElementById('displayYear').innerHTML = currentYear;
	}
return false;
}

function emailvalid(email) {
	emailreg=/[\w\.\-]{2,}\@[\w\-]{2,}\.[a-z]{2,3}/i;
	return(emailreg.exec(email));
}
