// JavaScript Document
//VALIDATE FORM FUNCTIONS ON THE CONTACT US PAGE
function validateForm(contactus) {
  if (isNotEmpty(contactus.message)) {
    if (isNotEmpty(contactus.email)) {
	  if (isEMailAddr(contactus.email)) {
	    return true;
	  }
	}
  }
  return false;
}

//validates that the field value string ahs one or more characters in it
function isNotEmpty(elem) {
  var str = elem.value;
  if(str == null || str.length == 0) {
    alert("You must complete BOTH boxes before the message can be sent");
	return false;
  }
  else {
    return true;
  }
}
  
//Validates that the entry is formatted as an email address
function isEMailAddr(elem) {
  var str = elem.value;
  str = str.toLowerCase();
  if (str.indexOf("@") > 1) {
    var addr = str.substring(0, str.indexOf("@"));
	var domain = str.substring(str.indexOf("@") + 1, str.length);
	  //at least one top level domain required
	  if (domain.indexOf(".") == -1) {
	    alert("Please confirm the domain portion of the email address.");
	    return false;
	  }
	  //parse address portion first, character by character
	  for(var i = 0; i < addr.length; i++) {
	    oneChar = addr.charAt(i).charCodeAt(0);
	      //dot or hyphen not allowed in first position; dot in last
	      if ((i == 0 && (oneChar == 45 || oneChar == 46)) ||
	       (i == addr.length - 1 && oneChar == 46)) {
		   alert("Please confirm the username portion of the email address");
		   return false;
	      }
	        //Acceptable characters (- . _ 0-9 a-z)
	        if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
	        (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
		      continue;
	        } 
			else {
	          alert("Please confirm the username portion of the email address");
		      return false;
		    }
       }
	   for (i = 0; i < domain.length; i++) {
	     oneChar = domain.charAt(i).charCodeAt(0);
		   if ((i == 0 && (oneCahr == 45 || oneChar == 46)) ||
		   ((i == domain.length - 1 || i == domain.length - 2)  && oneChar == 46)) {
		     alert("Please confirm the domain portion of the email address");
		     return false;
		   }
		   if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
		      (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
			    continue;
		   } 
		   else {
		     alert("Please confirm the domain portion of the email address");
		     return false;
		   }
	    }
	    return true;
     }
     alert("The email address may not be formatted correctly. Please confirm that it is correct");
     return false;
		  
} 
  