function isValidDate(dateStr) {
var datePat = /^(\d{4})(-)(\d{1,2})\2(\d{1,2})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[3]; // parse date into variables
day = matchArray[4];
year = matchArray[1];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}

function fixElement(element, message) {
	alert(message);
	element.focus();
}

function checkEmail(str){
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var passed = false;
	
	if (filter.test(str)) {
		passed = true;
	}
	
	return passed;
}

function isFormReady(theform) {
	var passed = false;

	switch (theform.name) {
	
	// basic request information form
	case "basic_info":
		if (theform.name_first.value == "") {
			fixElement(theform.name_first, "Please include your first name.");
			break;
		}
		if (theform.name_last.value == "") {
			fixElement(theform.name_last, "Please include your last name.");
			break;
		}
		if (theform.street_addr.value == "") {
			fixElement(theform.street_addr, "Please include your street address.");
			break;
		}
		if (theform.zip.value == "") {
			fixElement(theform.zip, "Please include your zip code.");
			break;
		}
		if ((theform.email.value == "") || (checkEmail(theform.email.value) === false)) {
			fixElement(theform.email, "Please include a valid email address.");
			break;
		}
		else {
			passed = true;
		}
	break;

	// rental book it form
	case "book_it":
	case "request_info":
		if (theform.name_first.value == "") {
			fixElement(theform.name_first, "Please include your first name.");
			break;
		}
		if (theform.name_last.value == "") {
			fixElement(theform.name_last, "Please include your last name.");
			break;
		}
		if (theform.street_addr.value == "") {
			fixElement(theform.street_addr, "Please include your street address.");
			break;
		}
		if (theform.zip.value == "") {
			fixElement(theform.zip, "Please include your zip code.");
			break;
		}
		if (theform.check_in.value == "") {
			fixElement(theform.check_in, "Please include your requested check in date.");
			break;
		}
		if (theform.check_out.value == "") {
			fixElement(theform.check_out, "Please include your requested check out date.");
			break;
		}
		else {
			passed = true;
		}
	break;

	// email a friend form
	case "email_friend":
		if ((theform.email_from.value == "") || (checkEmail(theform.email_from.value) === false)) {
			fixElement(theform.email_from, "Please include a valid email address.");
			break;
		}
		if ((theform.email_to.value == "") || (checkEmail(theform.email_to.value) === false)) {
			fixElement(theform.email_to, "Please enter a valid address for the email to be sent to.");
			break;
		}
		else {
			passed = true;
		}
	break;

	// event admin form
	case "admin_events":
		if (theform.event_title.value == "") {
			fixElement(theform.event_title, "You must provide a title for this event.");
			break;
		}
		if (!isValidDate(theform.event_date_start.value)) {
			break;
		}
		if (theform.event_desc.value == "") {
			fixElement(theform.event_desc, "You must provide a description for this event.");
			break;
		}
		else {
			passed = true;
		}
	break;

	// news admin form
	case "admin_news":
		if (theform.news_title.value == "") {
			fixElement(theform.news_title, "You must provide a title for this news.");
			break;
		}
		if (theform.news_body.value == "") {
			fixElement(theform.news_body, "You must provide some news for this news.");
			break;
		}
		else {
			passed = true;
		}
	break;

	}
	return passed;
}