// Validation of reservation form on reservations.php page (form generated by PHP in reservations_script.php).
// Form needs id="reservationForm"

function addReservationValidation()
{
	//adds validation to form #reservationForm
	if (document.getElementById("reservationForm"))
	{
		var form = document.getElementById("reservationForm");
		form.onsubmit=function(){return(validateReservationForm());};
	}
}




// Validation of every field in the form.
function validateReservationForm()
{
	//Must run removeErrorMessages() a couple times to get rid off all the messages for some reason...
	removeErrorMessages();
	removeErrorMessages();
	removeErrorMessages();
	removeErrorMessages();

	var form = document.getElementById("reservationForm");
	var isError = false

	//A bit of clean up. Add any fields here to be cleaned up of weird punctuation or white space. Run through cleanOutJunk()
	form.resMessage.value = cleanOutJunk(form.resMessage.value); // without this the form can be submitted with a blank message (mainly if the user accidentally adds one space)

	//Check if all the required fields are filled in
	//****************************************************************************************************
		
	if (form.resName.value === "")
	{
		addErrorMessage(form.resName,"Please add your name.");
		isError = true;
	}


	if ((form.resEmail.value === "") && (form.resPhone.value === ""))
	{	
		addErrorMessage(form.resEmail,"Please enter an email address (or phone number).");
		addErrorMessage(form.resPhone,"Please enter a phone number (or email address).");
		isError = true;
	}	
	if ((form.resEmail.value) && (!isEmail(form.resEmail.value)) )
	{
		addErrorMessage(form.resEmail,"This email is not in the proper format (name@provider.com)");
		isError = true;
	}
	if ((form.resPhone.value) && (!checkInternationalPhone(form.resPhone.value)) )
	{
		addErrorMessage(form.resPhone,"Please enter a valid phone number.");
		isError = true;
	}


	if (form.resDateMonth.value === "month")
	{
		addErrorMessage(form.resDateMonth,"Select a month from the left drop down.");
		isError = true;
	}
	if (form.resDateDay.value === "day")
	{
		addErrorMessage(form.resDateDay,"Select a day from the right drop down.");
		isError = true;
	}
	
	if ((form.resNumber.value === "") || (!isInteger(form.resNumber.value)) || (form.resNumber.value <= 0))
	{
		addErrorMessage(form.resNumber,"Please let us know how many people will be fishing.");
		isError = true;
	}
	
	if ((form.resFloatWadeWade.checked !== true) && (form.resFloatWadeFloat.checked !== true))
	{
		addErrorMessage(form.resFloatWadeFloat,"Please let us know if you would like to go on a float trip or go wading.");
		isError = true;
	}
	
	

	

	//End checking for filled in fields
	//****************************************************************************************************

	// if there are any errors, then don't send the info...
	if (isError === true) 
	{
		document.getElementById("errorMessageSection").innerHTML = '<p class="validationError">Some info is missing or incorrect. Please <a href="#resName">go back through the form</a> and fix all the fields with red text below them.</p>';
		return false;
	}
	
	// form is valid! Send info on to the PHP!
	form.validated.value = "true";
	return true;
}

addLoadEvent(addReservationValidation); // run addNewCompanyValidation() onLoad 