// JavaScript Document

function validateEmailAddr(emailAddrElem) {
  // For our purposes a "valid" email address consists of a username followed
  // by an @ sign followed by a domain name.
  //
  // The @ sign is required, as is some top-level domain. Strictly speaking,
  // this may not conform to the technical spec, as I believe that an IP
  // address may be used as the domain name and in some instances the domain
  // name may be omitted altogether. However, practically speaking, this
  // should not be a problem.
  //
  // The regular expression used here is partly derived from one in the book 
  // "JavaScript & DHTML Cookbook", by Danny Goodman, pp. 196. I believe 
  // that expression is not quite correct and modified the domain name portion. 
  // The username portion was completely replaced by a much looser check.  
  // EF 10-2006
  //
  // The username may not contain the @ sign nor any whitespace characters.
  // Other than those restrictions, the username may be any sequence of at
  // least one character.
  //
  // In the domain name the only characters allowed are English letters, digits, 
  // underscores, hyphens, and periods. The domain name must start with a letter, 
  // digit, or underscore. Consecutive periods are not allowed.
  //
  // The domain must end with an apparent top-level domain, although there is no 
  // check against a list of actually valid top-level domains. The top-level domain 
  // may consist only of English letters and must be at least two characters and 
  // no more than seven characters.
  
  // Returns 0 if the email address is valid,
  //         1 if the email address is "blank" or not provided,
  //         2 if the email address is otherwise considered invalid.

  var re_non_blank = /\S/;  // Any one non-whitespace character.
  var re_valid_addr = /^[^@\s]+@\w[\w-]*(\.[\w-]+)*\.[a-zA-Z]{2,7}$/;

  var str = emailAddrElem.value;
  
  var isBlank = (str.length == 0 || str == 'your email address' || !str.match(re_non_blank))
  
  if (!isBlank && str.match(re_valid_addr))
    return 0;
  else if (isBlank)
    return 1;
  else
    return 2;
}

function validateComment(commentElem) {
  // If the comment is empty, is equal to the special filler value, or
  // consists only of whitespace characters such as spaces and newlines,
  // then it is invalid.
  
  // Returns true if valid, false otherwise.

  var str = commentElem.value;
  var re_non_blank = /\S/;  // Any one non-whitespace character.

  if (str.length == 0 || str == 'your question or comment' || !str.match(re_non_blank))
    return false;
  else
    return true;
}


function validateContactForm(formElem) {
  //var validComment = validateComment(formElem.comment);  // Returns true or false.
  var validName = validateComment(formElem.name);  // Returns true or false.
  var validCompany = validateComment(formElem.company);
  var validAddress = validateComment(formElem.address);
  var validCity = validateComment(formElem.city);
  var validState = validateComment(formElem.state);
  var validZip = validateComment(formElem.zip);
  var validPhone = validateComment(formElem.phone);
  var validEmail = validateEmailAddr(formElem.email);    // Returns 0, 1 or 2.

  if (!validName)
    window.alert('Please type in your name')
  if (!validCompany)
    window.alert('Please type in your company name')
  if (!validAddress)
    window.alert('Please type in your street address')
  if (!validCity)
    window.alert('Please type in the name of your city')
  if (!validState)
    window.alert('Please type in the name of your state')
  if (!validZip)
    window.alert('Please type in your zip code')
  if (!validPhone)
    window.alert('Please type in your business phone number')
  
  if (validEmail == 1)
    window.alert('Please enter an email address');
  else if (validEmail == 2)
    window.alert('Please check the email address. It appears to be invalid');
   
 

  return (validEmail == 0 && validName);
}