// Function to validate zip codes
function validateZip(zip) {
	var regex = /^\d{5}(-\d{4})?$/;
	if (zip.search(regex) == -1) {
		return false;
	} else {
		return true;
	}
}

// Function to validate phone numbers
function validatePhone(phone) {
	var regex = /^\(?(\d{3})\)?[ -.](\d{3})[ -.](\d{4})$/;
	if (phone.search(regex) == -1) {
		return false;
	} else {
		return true;
	}
}

// Function to validate email addresses
function validateEmail(email) {
	var regex = /^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/;
	if (email.search(regex) == -1) {
		return false;
	} else {
		return true;
	}
}

// Function to validate the volunteer form
function validateVolunteerForm() {
	var instructions			= document.getElementById("instructions");
	var errorZip				= document.getElementById("errorZip");
	var errorPhone				= document.getElementById("errorPhone");
	var errorEmail				= document.getElementById("errorEmail");
	var formFields				= new Array();
	formFields["name"]			= [document.getElementById("labelName"),document.getElementById("name")];
	formFields["address"]		= [document.getElementById("labelAddress"),document.getElementById("address")];
	formFields["city"]			= [document.getElementById("labelCity"),document.getElementById("city")];
	formFields["state"]			= [document.getElementById("labelState"),document.getElementById("state")];
	formFields["zip"]			= [document.getElementById("labelZip"),document.getElementById("zip")];
	formFields["phone"]			= [document.getElementById("labelPhone"),document.getElementById("phone")];
	formFields["email"]			= [document.getElementById("labelEmail"),document.getElementById("email")];
	formFields["timeToCall"]	= [document.getElementById("labelTimeToCall"),document.getElementById("timeToCall")];
	formFields["textarea1"]		= [document.getElementById("labelTextarea1"),document.getElementById("textarea1")];
	formFields["textarea2"]		= [document.getElementById("labelTextarea2"),document.getElementById("textarea2")];
	formFields["textarea3"]		= [document.getElementById("labelTextarea3"),document.getElementById("textarea3")];
	formFields["textarea4"]		= [document.getElementById("labelTextarea4"),document.getElementById("textarea4")];
	formFields["textarea5"]		= [document.getElementById("labelTextarea5"),document.getElementById("textarea5")];
	formFields["textarea6"]		= [document.getElementById("labelTextarea6"),document.getElementById("textarea6")];
	var labelInterests			= document.getElementById("labelInterests");
	var interests				= document.getElementsByName("interests[]");
	var formValidated			= true;
	var interestChecked			= false;

	// Did the user enter any values in the form fields?
	for (field in formFields) {
		if (formFields[field][1].value == "") {
			formValidated = false;
			formFields[field][0].style.color="red"; // Style the label
			formFields[field][1].style.borderColor="red"; // Style the field
		} else {
			formFields[field][0].style.color="black"; // Style the label
			formFields[field][1].style.borderColor="#cccccc"; // Style the field
		}
	}

	// Did the user check any special interests?
	for (var i=0; i<interests.length; i++) {
		if (interests[i].checked) {
			interestChecked = true;
		}
	}
	if (interestChecked) {
		labelInterests.style.color="black"; // Style the label
	} else {
		formValidated = false;
		labelInterests.style.color="red"; // Style the label
	}

	// Style the instructions
	if (formValidated) {
		instructions.style.color="black";
	} else {
		instructions.style.color="red";
	}

	// Did the user enter a valid zip code?
	var zipValidates = validateZip(formFields["zip"][1].value);
	if (zipValidates) {
		errorZip.style.display="none";
		formFields["zip"][0].style.color="black"; // Style the label
		formFields["zip"][1].style.borderColor="#cccccc"; // Style the field
	} else {
		formValidated = false;
		if (formFields["zip"][1].value == "") {
			errorZip.style.display="none";
		} else {
			errorZip.style.display="block";
			formFields["zip"][0].style.color="red"; // Style the label
			formFields["zip"][1].style.borderColor="red"; // Style the field
		}
	}

	// Did the user enter a valid phone number?
	var phoneValidates = validatePhone(formFields["phone"][1].value);
	if (phoneValidates) {
		errorPhone.style.display="none";
		formFields["phone"][0].style.color="black"; // Style the label
		formFields["phone"][1].style.borderColor="#cccccc"; // Style the field
	} else {
		formValidated = false;
		if (formFields["phone"][1].value == "") {
			errorPhone.style.display="none";
		} else {
			errorPhone.style.display="block";
			formFields["phone"][0].style.color="red"; // Style the label
			formFields["phone"][1].style.borderColor="red"; // Style the field
		}
	}

	// Did the user enter a valid email address?
	var emailValidates = validateEmail(formFields["email"][1].value);
	if (emailValidates) {
		errorEmail.style.display="none";
		formFields["email"][0].style.color="black"; // Style the label
		formFields["email"][1].style.borderColor="#cccccc"; // Style the field
	} else {
		formValidated = false;
		if (formFields["email"][1].value == "") {
			errorEmail.style.display="none";
		} else {
			errorEmail.style.display="block";
			formFields["email"][0].style.color="red"; // Style the label
			formFields["email"][1].style.borderColor="red"; // Style the field
		}
	}

	if (formValidated == false) {
		window.scrollTo(0,0)
		return false;
	} else {
		return true;
	}

}
