var hMsg ="" //Global variable, this catches error messages (Empty String)
var countErr = 0 //ignore this, was just some testing
function validateForm(){

// Required validation fields
var firstname = document.getElementById('firstname');
var lastname = document.getElementById('lastname');
var address = document.getElementById('address');
var city = document.getElementById('city');
var state = document.getElementById('state');
var zipcode = document.getElementById('zipcode');
var email = document.getElementById('email');
var phone = document.getElementById('phone');
var phoneMsg = "Please provide your phone number";
var emailMsg = "Please provide a valid email address";


// These will now be validated regardless of completion, as all functions are called one by one
isAddress(firstname, "Please enter your first name");
isAddress(lastname, "Please enter your last name");
isAddress(address, "Please enter your address");
isAddress(city, "Please enter your city");
isAddress(state, "Please enter your state");
isNumeric(zipcode, "Please enter a valid numeric zipcode");
emailValidator(email, emailMsg);
isAddress(phone, phoneMsg);

if (hMsg.length>0){
alert(hMsg);
hMsg = ""; //this is the variable set at the start of the script containing all error strings, now set back to empty, in case submit button is clicked more than once
return false;
}
else{
hMsg = "";
}

}
function isEmpty(elem, helperMsg){
if(elem.value.length == 0){
hMsg+=(helperMsg + '\n') //add error string to hMsg
elem.focus(); // set the focus to this input
return true;
}
}
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
hMsg+=(helperMsg + '\n') //add error string to hMsg
elem.focus();
}
}
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
hMsg+=(helperMsg + '\n') //add error string to hMsg
elem.focus();
}
}
function isAddress(elem, helperMsg){
if(elem.value.length > 0){
return true;
}else{
hMsg+=(helperMsg + '\n') //add error string to hMsg
elem.focus();
}
}
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{
hMsg+=(helperMsg + '\n') //add error string to hMsg
elem.focus();
}
}
