// JavaScript Document for validating the contact form (#mailingListForm)

function addMailingListFormValidation()
{
	//adds validation to any form called #mailingListForm
	if (document.getElementById("mailingListForm"))
	{
		document.getElementById("mailingListForm").onsubmit=function(){return(validateMailingListForm());};
	}
}

function validateMailingListForm()
{
	//Must run removeErrorMessages() a couple times to get rid off all the messages for some reason...
	removeErrorMessages();
	removeErrorMessages();
	removeErrorMessages();
	removeErrorMessages();

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

	//Check if all the required fields are filled in
	//****************************************************************************************************

	if (form.mailingName.value === "")
	{
		addErrorMessage(form.mailingName,"Please add your name.");
		isError = true;
	}
	
	if ((form.mailingEmail.value === "") || (!isEmail(form.mailingEmail.value)))
	{
		addErrorMessage(form.mailingEmail,"This email is not in the proper format (name@provider.com)");
		isError = true;
	}


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

	// if there are any errors, then don't send the info...
	if (isError === true) 
	{
		document.getElementById("errorMessageSectionMailingList").innerHTML = '<p class="message-box error">There were some problems in with the info you provided. Please review 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(addMailingListFormValidation); // run addValidation() onLoad 
