// If the length of the element's string is 0 then display helper message
function isEmpty(elem, alertelem,msg){
	if(elem.value.length == 0){
		elem.focus(); // set the focus to this input
		alertelem.style.display='block';
		alertelem.innerHTML=msg;
		return false;
	}
	alertelem.style.display='none';
	return true;
}

function emailValidator(elem, alertelem,msg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		alertelem.style.display='none';
		return true;
	}else{
		alertelem.style.display='block';
		alertelem.innerHTML=msg;
		elem.focus();
		return false;
	}
}

function checkForm() {

	//Reference to fields
	var Email = document.getElementById('vEmail');
	var Phone = document.getElementById('vPhone');
	var Subject = document.getElementById('vSubject');
	var Message = document.getElementById('vMessage');
	var Heard = document.getElementById('vHeard');
	var alertBox = document.getElementById('alertBox');

	//Check the input
	if(isEmpty(Email,alertBox,"*Please type in your email"))
	{
		if(emailValidator(Email,alertBox,"*Please check your email"))
		{
			if(isEmpty(Phone,alertBox,"*Please type in your phone number"))
			{
				if(isEmpty(Subject,alertBox,"*Please type in your subject"))
				{
					if(isEmpty(Message,alertBox,"*Please type in your message"))
					{
						if(isEmpty(Heard,alertBox,"*Please select one option"))
						{
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}
