// JavaScript Document


// Return true if string only contains whitespace characters
function isBlank(s) {
 for(var i=0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != " ") && (c != "\n") && (c != "")) return false;
 }
 return true;
}


// Check whether string s is empty.

function isEmpty(s) {
   return ((s == null) || (s.length == 0))
}


// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c) {
   return ((c >= "0") && (c <= "9"))
}


function isLetter (c) {
   return ( ((c >= "a") && (c <= "z")) || ((c >="A") && (c <= "Z")) )
}


function isEmail (s) {
	var emailPattern = /^(.+)@(.+)$/;   // basic pattern check:  user@domain  format
	var knownDomsPattern = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|tv)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";   // do NOT allow those characters in address
	var validChars = "\[^\\s" + specialChars + "\]";   // do not allow chars in username or domainname
	var ipDomainPattern = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;   // IP address domains
	var atom = validChars + '+';   // an atom - i.e. a series of non-special characters
	var quotedUser = "(\"[^\"]*\")";  //  quoted user name
	var word = "(" + atom + "|" + quotedUser + ")";   // one word in the typical user name -- atom or qtd str
	var userPattern = new RegExp("^" + word + "(\\." + word + ")*$");  // user pattern
	var domainPattern = new RegExp("^" + atom + "(\\." + atom +")*$");  // domain pattern
	
	var matchArray = s.match(emailPattern);
	
	if (matchArray == null) {
		return false;
	}
	var user = matchArray[1];
	var domain = matchArray[2];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i) > 127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
			return false;
		}
	}
	
	// check for valid "user"
	
	if (user.match(userPattern) == null) {
		return false;
	}
	var atomPattern = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	
	// is domain name valid?
	for (i=0; i<len; i++) {
		if (domArr[i].search(atomPattern) == -1) {
			return false;
		}
	}	
	
	// does domain match TLDs or look like country code?
	if (domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPattern)==-1) {
		return false;
	}
	
	// is host name preceding domain?
	if (len < 2) {
		return false;
	}
	
	return true;
}




// form verification
function verifyForm(f) {
  var msg;
  var empty_fields = "";
  var errors = "";

  // Check any text element that has a "required" field defined.
  // If field is empty, make a list.
  for(var i=0; i < f.length; i++) {
    var e = f.elements[i];
    if (e.required) {
	// Check if field is empty
	 if ((e.type == "text" || e.type == "textarea") && (isEmpty(e.value) || isBlank(e.value))) {
	 	var spaceName = e.name;
	 	spaceName = spaceName.replace(/_/g, " ");
	 	spaceName = spaceName.toLowerCase();
	 	empty_fields += "\n     " + spaceName;
	 	continue;
	 }
	 if (e.type == "select-one" && e.value == " ") {
	 	var spaceName = e.name;
	 	spaceName = spaceName.replace(/_/g, " ");
	 	spaceName = spaceName.toLowerCase();
	 	empty_fields += "\n     " + spaceName;
	 	continue;
	 }
	 if (e.name == "email" && !isEmail(e.value)) {
		 errors += "- Invalid email address entered."
		 continue;
	 }
    }
  }



  // If there are errors, display error message and return false
  // to prevent the form from being submitted.
  // If there are NO errors, return true.

  if (empty_fields || errors) {

  msg  = "_________________________________________________\n\n"
  msg += "The form was not submitted because of the following error(s).\n";
  msg += "Please correct the error(s) and re-submit.\n";
  msg += "_________________________________________________\n\n";

  if (empty_fields) {
    msg += "- The following required field(s) are empty:"
	   + empty_fields + "\n";
    if (errors) msg += "\n";
  }
  msg += errors;
  alert(msg);
 } else {

    // Submit Form
    document.contactForm.submit();
 }

}
