/** 
	Contact Form JavaScript Validation (form_validation.js)
	Validates data inputted into contact form fields before being sent to server
	Version 0.6
	Coded by James White
*/

/**
	Function: CheckEmail
	This function is for checking the email field is valid
	It does this by checking for a @ symbol as well as . symobol at the correct positions
*/

function CheckEmail(email) 
{
	var atpos=email.value.indexOf("@"); // Create atpos (@ position) variable to check for @ symbol in field entry
	var dotpos=email.value.lastIndexOf("."); // Create dotpos (. position) variable to check for . after the @ symbol

		// Use the variables created in a positional basis
		if (atpos <1 || (dotpos-atpos) <2 )
		{
			var enquiry_email_invalid = document.getElementById('enquiry_email');
			enquiry_email_invalid.style.background = '#FF0';
			alert("Please enter a valid email address");
			enquiry_email_invalid.style.background = '#eaeaea';
			return false;	
		}
}

/**
	Function: ValidateForm
	This function is the main function to validate the form
	It is called using the onsubmit event on the HTML form tag.
	It contains all the validate checks JavaScript will run through
*/

function ValidateForm(thisform)
{
	
	// Check that name field is not blank
	if (document.getElementById('enquiry_name').value === '')
	{
		var enquiry_name = document.getElementById('enquiry_name');
		enquiry_name.style.background = '#FF2429';
		alert('Please enter your name');
		enquiry_name.style.background = '#eaeaea';
		return false;
	}
	
	// Check that email field is not blank
	if (document.getElementById('enquiry_email').value === '')
	{
		var enquiry_email = document.getElementById('enquiry_email');
		enquiry_email.style.background = '#FF2429';
		alert('Please enter your email');
		enquiry_email.style.background = '#eaeaea';
		return false;
	}
	
	if(CheckEmail(thisform.emailaddress)===false)
	{
			thisform.emailaddress.focus();
			return false;
	}
	
	// Check that enquiry field is not blank
	if (document.getElementById('enquiry_comments').value === '')
	{
		var enquiry_comments = document.getElementById('enquiry_comments');
		enquiry_comments.style.background = '#FF2429';
		alert('Please enter your enquiry');
		enquiry_comments.style.background = '#eaeaea';
		return false;
	}

	// Check that reCAPTCHA field is not blank
	if (document.getElementById('recaptcha_response_field').value === '')
	{
		var recaptcha_field = document.getElementById('recaptcha_response_field');
		recaptcha_field.style.background = '#FF2429';
		alert('Please enter the two words that appear in the reCAPTCHA box on the right');
		recaptcha_field.style.background = '#eaeaea';
		return false;
	}

	// If all validation statements are passed, use contact.php to send the form
	return true;
}
