function formValidator(){
    // Make quick references to our fields
    var name = document.getElementById('name');
    var agency = document.getElementById('agency');
    var email = document.getElementById('email');
    

    if (isEmpty(name, "Name required")) {
       if (isEmpty(agency, "Agency required")) {
          if (isEmpty(email, "Email required")) {
             if(emailValidator(email, "Please enter valid email address")) {
                  return true; 
             }
          }
       }
    }
    return false;
}

// If the length of the element's string is 0 then display alert message
function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

// if element contains invalid email address then display alert message
function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

// If the element's string matches the regular expression it is all letters else displays alert message
function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

// If the element's string matches the regular expression it is all numbers else displays alert message
function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


// Checks if element meets minmum and maximum size requirement else displays alert message
function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

// If the element's string matches the regular expression it is numbers and letters else displays alert message
function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
