function formValidator(){
	// Make quick references to our fields
	var firstname = document.getElementById('name');
	var surname = document.getElementById('last_name');
	var email = document.getElementById('email');
	var address1 = document.getElementById('address1');
	var postcode = document.getElementById('postcode');
	var phone_number = document.getElementById('phone_number');
	var participants = document.getElementById('participants');
	var nonparticipants = document.getElementById('nonparticipants');
	var coursedate = document.getElementById('course_date');

	
	// Check each input in the order that it appears in the form!
	if( notEmpty(firstname, "Please enter a name")){
		if(emailValidator(email, "Please enter a valid email address")){
							return true;
		}
	}
	
	
	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		if (elem.value != "Name") {
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		elem.style.backgroundColor = 'yellow';
		return false;
		}
	}
	elem.style.backgroundColor = 'white';
	return true;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		elem.style.backgroundColor = 'white';
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		elem.style.backgroundColor = 'yellow';
		return false;
	}
}

function isOneNumeric(elem1, elem2, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if (elem1.value.match(numericExpression)||elem2.value.match(numericExpression)){
		elem1.style.backgroundColor = 'white';
		return true;
	}else{
		alert(helperMsg);
		elem1.focus();
		elem1.style.backgroundColor = 'yellow';
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		elem.style.backgroundColor = 'white';
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		elem.style.backgroundColor = 'yellow';
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9 a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		elem.style.backgroundColor = 'white';
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		elem.style.backgroundColor = 'yellow';
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		elem.style.backgroundColor = 'white';
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		elem.style.backgroundColor = 'yellow';
		return false;
	}
}


function minlengthRestriction(elem, field, min){
	var uInput = elem.value;
	if(uInput.length >= min){
		elem.style.backgroundColor = 'white';
		return true;
	}else{
		alert("Please enter at least " +min+ " characters for the "+field);
		elem.focus();
		elem.style.backgroundColor = 'yellow';
		return false;
	}
}


function isphoneNumber(elem, helperMsg){
	var uInput = elem.value;
	if(uInput.length >= 8 && uInput.length <= 12){
		elem.style.backgroundColor = 'white';
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		elem.style.backgroundColor = 'yellow';
		return false;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		elem.style.backgroundColor = 'white';
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		elem.style.backgroundColor = 'yellow';
		return false;
	}
}

