function formValidator()
{
  // Make quick references to our fields
  var name = document.getElementById('name');
  var message = document.getElementById('message');
  var email = document.getElementById('email');
	
  // Check each input in the order that it appears in the form!
  if(notEmpty(name, "Please enter your name"))
  {
    if(isAlphabet(name, "Please enter only letters for your name"))
    {
      if(notEmpty(message, "Please enter a message"))
      {
        if(lengthRestriction(message, 1, 2000, "Message can be no longer than 2000 characters"))
        {
          if(notEmpty(email, "Please enter your email address"))
          {
	    if(emailValidator(email, "Please enter a valid email address"))
            {
	      return true;
	    }
	  }
	}
      }
    }
  }	
  return false;	
}

function notEmpty(elem, helperMsg)
{
  if(elem.value.length == 0)
  {
    alert(helperMsg);
    elem.focus()
    // set the focus to this input
    return false;
  }
  return true;
}

// If the element's string matches the regular expression it is all letters
function isAlphabet(elem, helperMsg)
{
  var alphaExp = /^[a-zA-Z\s\.]+$/;
  if(elem.value.match(alphaExp))
  {
    return true;
  }
  else
  {
    alert(helperMsg);
    elem.focus();
    return false;
  }
}


function lengthRestriction(elem, min, max, helperMsg)
{
  var uInput = elem.value;
  if(uInput.length >= min && uInput.length <= max)
  {
    return true;
  }
  else
  {
    alert(helperMsg+" " +min+ " and " +max+ " characters");
    elem.focus();
    return false;
  }
}

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;
  }
}
