/**
 * NSX Internal Volunteer Form JavaScript.
 *
 * This file contains the validation functions for the volunteer form for the
 * NSX Internal application.
 *
 * @author Daniel J. Summers <daniel@djs-consulting.com>
 * @version $Revision: 5 $
 * @since 1
 * @package NSXapp
 * @subpackage View
 */

/**
 * Function to run on page load.
 */
function setCursorOnFirstName() {
	getElement("txtFirstName").focus();
}

/**
 * Checking the "translate" or "licensed contactor work" buttons make the
 * comments field required.  This changes the class as needed.
 *
 * @return False to prevent page submission.
 */
function checkCommentsRequired() {
	if ((isChecked("chkRequest72")) || (isChecked("chkRequest78"))) {
		getElement("lblComments").className = "required";
	}
	else {
		getElement("lblComments").className = "";
	}
	return false;
}

/**
 * Checking the "Xtreme Team" checkbox hides the X-Week schedule.
 *
 * @return False to prevent page submission.
 */
function checkXWeek() {
	if (isChecked("chkXtremeTeam")) {
		getElement("divXWeek").style.display = "none";
	}
	else {
		getElement("divXWeek").style.display = "block";
	}
	// Check or clear all the boxes based on this box.
	for (i = 1; i <= 6; i++) {
		getElement("chkXWeekDay" + i).checked = isChecked("chkXtremeTeam");
		getElement("chkXWeekEve" + i).checked = isChecked("chkXtremeTeam");
	}
	return false;
}

/**
 * Validate Form.
 *
 * This function validates the input data on the form, to ensure that is
 * complete.
 *
 * @return False if there's an error, true otherwise.
 */
function validateForm() {
	
	// First name must be input.
	if (!validateString("txtFirstName", "You must enter your first name.")) {
		return false;
	}
	// Last name must be input.
	if (!validateString("txtLastName", "You must enter your last name.")) {
		return false;
	}
	// Age Group must be input.
	if (!validateOptionGroup("volAgeGroupId", "You must select an age group.", "lblAgeGroup", "required")) {
		return false;
	}
	// Contact information must be input.  (This uses the common contact
	// validation routine in contact.js.)
	if (!validateContact()) {
		return false;
	}
	// One of the five "ways to serve" must be checked.
	if ((!isChecked("chkGoTeam")) && (!isChecked("chkXtremeTeam"))
			&& (!isChecked("chkSupport")) && (!isChecked("chkIndividual"))
			&& (!isChecked("chkWithFamily"))) {
		alert("One of the ways to serve must be selected.");
		getElement("lblWaysToServe").className = "required errorField";
		return false;
	}
	else {
		getElement("lblWaysToServe").className = "required";
	}
	// At least one checkbox on the two schedules must be checked.
	if (!validateSchedule()) {
		return false;
	}
	// At least one box under the "How You Can Help" section must be checked.
	if (!validateOptionGroup("volRequest[]", "You must select at least one way you can help.\nSurely you can pray?  :)", "", "")) {
		return false;
	}
	// For "licensed contractor work" or "translate", comments are required.
	if ((isChecked("chkRequest72")) || (isChecked("chkRequest78"))) {
		if (!validateString("txtComments", "You checked either \"Translate\" or \"Provide licensed contractor work\" but did not provide comments.")) {
			return false;
		}
	}
	// The "consent" box must be checked.
	if (!isChecked("chkConsent")) {
		alert("You must check the \"Consent\" box.");
		getElement("lblConsent").className = "required errorField";
		getElement("chkConsent").focus();
		return false;
	}
	else {
		getElement("lblConsent").className = "required";
	}
	// It's good to go!
	return true;
}

/**
 * Validate the volunteer schedule.
 * (If the "Xtreme Team" checkbox is checked, this entry is not required.)
 *
 * @return True if a box is checked, false if not.
 */
function validateSchedule() {
	
	if (isChecked("chkXtremeTeam")) {
		getElement("lblPre").className = getElement("lblXWeek").className = "required";
		return true;
	}
	
	for (x = 1 ; x < 7; x++) {
		if ((isChecked("chkPreDay" + x)) || (isChecked("chkPreEve" + x))
				|| (isChecked("chkXWeekDay" + x)) || (isChecked("chkXWeekEve" + x))) {
			getElement("lblPre").className = getElement("lblXWeek").className = "required";
			return true;
		}
	}
	
	alert("You have not selected a time when you are available to volunteer.");
	getElement("lblPre").className = getElement("lblXWeek").className = "required errorField";
	return false;
}