// Validate IT Resources Contact Form

//This first set of functions defines the checks and error messages for individual fields.
//The appropriate checks for the form are called below in the second set of functions.

//check name field
function checkName(passedForm) {
	if (passedForm.firstname.value == "") {
		alert("Please enter your first name.");
		passedForm.firstname.focus();
		return false
	}
	if (passedForm.lastname.value == "") {
		alert("Please enter your last name.");
		passedForm.lastname.focus();
		return false
	}
	return true
}

//check email field
function checkEmail(passedForm) {
	emailEntered = passedForm.email.value
	invalidChars = " /:,;"
	
	if (emailEntered == "") { //check that something was entered
		alert("Please enter a valid email address.")
		passedForm.email.focus()
		return false
	}
	for (i=0; i<invalidChars.length; i++) { //check for invalid characters
		badChar = invalidChars.charAt(i)
		if (emailEntered.indexOf(badChar,0) > -1) {
			alert("Please enter a valid email address.")
			passedForm.email.focus()
			passedForm.email.select()
			return false
		}
	}
	atPosition = emailEntered.indexOf("@",1) //check for @ sign
	if (atPosition == -1) {
		alert("Please enter a valid email address.")
		passedForm.email.focus()
		passedForm.email.select()
		return false
	}
	if (emailEntered.indexOf("@",atPosition+1) > -1) { //check for no more than one @ sign
		alert("Please enter a valid email address.")
		passedForm.email.focus()
		passedForm.email.select()
		return false
	}
	periodPosition = emailEntered.indexOf(".",atPosition) //check for period after @ sign
	if (periodPosition == -1){
		alert("Please enter a valid email address.")
		passedForm.email.focus()
		passedForm.email.select()
		return false
	}
	if (periodPosition+3 > emailEntered.length) { //check for at least two characters after period
		alert("Please enter a valid email address.")
		passedForm.email.focus()
		passedForm.email.select()
		return false
	}
	return true
}

//check status field
function checkStatus(passedForm) {
	if (passedForm.status.value == "no selection") {
		alert("Please select your UCLA affiliation (student, faculty, staff) from the pulldown menu.");
		passedForm.status.focus();
		return false
	}
	return true
}

//check comment field
function checkComment(passedForm) {
	if (passedForm.comment.value == "") {
		alert("Please enter your comments.");
		passedForm.comment.focus();
		return false
	}
	return true
}


//This set of functions calls the appropriate checks for the form.

// validation for IT Resources Contact Form
function validateContact(passedForm) {
	if (!checkName(passedForm)) {
		return false
	}
	if (!checkEmail(passedForm)) {
		return false
	}
	if (!checkStatus(passedForm)) {
		return false
	}
	if (!checkComment(passedForm)) {
		return false
	}
	return true
}