$(document).ready(function() {

	//any field that is required for form submission needs a "required" class in its label tag
	//so far this just validates that something is filled out, no email validation
	
	//when the user tries to submit form, this hijax it to check to see if required fields are filled out
	//if so, (numErrors == 0 ) then it allows it to be sent to the server (return true implied)
	//if not (numErrors != 0) then it alerts the user and prevents form submission (return false)
	
	var $requiredFieldLabels = $('.required');					//get an array of all labels with required class	

	// hijack form submission.  note: user might try to submit the form several times if they forget one after second or third try
	$('form').submit(function() {
		
		var alertMessage = "Please fill out the following fields: \n\n";	
		var numErrors = 0;													

		// iterate through each required field labels
		$requiredFieldLabels.each(function(index) {	
			var $requiredFieldLabel = $(this);					//making what we are iterating over (this) explicit
			var $labelText = $($requiredFieldLabel).text();		//get label text for displaying in alert message
			
			var $requiredFieldValue = $($requiredFieldLabel).next().val();	//get value of input or select field to check later if it is empty
			
			$($requiredFieldLabel).removeClass('error');		//class="error" is added dynamically by this script later
																//this will remove previous error classes if they are trying to submit several times
																//otherwise when they fill in a previous error and miss another, then go back the 
																//one they filled in will still be red

			// if the required field is empty... 
			if ( $requiredFieldValue == '' ) {
				$($requiredFieldLabel).addClass('error');		//add class="error", in css this turns label text red
				alertMessage += $labelText+"\n";				//add label text to alert message so they know what they missed
				numErrors += 1;									//add to the number of errors
			}
		});

		// if the number of errors is greater than zero...
		if ( numErrors != 0 ) {
			alert(alertMessage);								//alert user with which fields are missing
			return false;										//prevent form from being submitted
		}


	});

});

