// JavaScript Document
// this form validation is copyright aimeemaree 
// author aimee@aimeemaree.com

function formValidator(){
	
	// collect field data and save it to a variable to use throughout the functions
	var bridegroomname = document.getElementById('bridegroomname');
    var lastname = document.getElementById('lastname');
	var mobile = document.getElementById('mobile');
	
	var suburb = document.getElementById('suburb');
    var state = document.getElementById('state');
	var postcode = document.getElementById('postcode');
	var country = document.getElementById('country');
	
	var email = document.getElementById('email');
	var preferred = document.getElementById('preferred');
    var guests = document.getElementById('guests');
	
	// Check each input in the order that it appears in the form!
		if(isBlank(bridegroomname, "You must enter a valid First name for the Bride and Groom")){
			if(isBlank(lastname, "You must enter a valid Last name")){
				if(isBlank(mobile, "You must enter a valid mobile phone number")){
					if(isBlank(suburb, "You must enter the Suburb of your residential address)")){
						if(isNotBlank(state, "You must enter the State of your residential address")){
							if(isBlank(postcode, "You must enter the Post Code of your residential address")){
								if(isBlank(country, "You must enter the Country of your residential address")){
									if(emailValidation(email, "The E-mail address you entered is not valid, you must enter a valid E-mail address")){
										if(isNotBlank(preferred, "You must enter a preffered date for your wedding")){
											if(isNumeric(guests, "You must enter a number of your expected guests")){
									
								return true;
											}}}}}}}}}}
	return false;
	
}



// function to ensure that the email address entered is in correct format
function emailValidation(field, helperMsg){
	var emailValid = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(field.value.match(emailValid)){
		return true;
	}else{
		alert(helperMsg);
		field.focus();
		return false;
	}
}


// function to ensure that the field is not left blank
function isBlank(field, helperMsg){
	if(field.value.length < 4){
		alert(helperMsg);
		field.focus(); // set the focus to this input
		return false;
	}
	return true;
}

// function to ensure that the field is not left blank
function isNotBlank(field, helperMsg){
	if(field.value.length < 2){
		alert(helperMsg);
		field.focus(); // set the focus to this input
		return false;
	}
	return true;
}

// function to ensure that all data entries is a numeric character
function isNumeric(field, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(field.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		field.focus(); // set the focus to this input
		return false;
	}
}