 var phoneNumberDelimiters = "()- ";
 var digits = "0123456789";
 // characters which are allowed in US phone numbers
 var validUSPhoneChars = digits + phoneNumberDelimiters;
 // U.S. phone numbers have 10 digits.
 // They are formatted as 123 456 7890 or (123) 456-7890.
 var digitsInUSPhoneNumber = 10;
 var defaultEmptyOK = false;
function checkForm () {
  var validate = "";
  var errorMessage = "";
  var phoneNumber = document.contactForm.Phone.value;
  var emailAddress = document.contactForm.email.value;
  var name = document.contactForm.realname;
    errorMessage += checkFields('document.contactForm.realname','Name is required.');
	errorMessage += checkFields('document.contactForm.Phone','Phone number is required.');
	 var normalizedPhone = stripCharsInBag(phoneNumber, phoneNumberDelimiters);
	  if (normalizedPhone != "" && !isUSPhoneNumber(normalizedPhone)) {
         errorMessage += "Phone number must be ten digits long.\n";
      } else if (isUSPhoneNumber(normalizedPhone)){ 
	      document.contactForm.Phone.value = reformatUSPhone(normalizedPhone);
	  }
      if (emailAddress.length) {
	      errorMessage += checkEmail('document.contactForm.email','Email address format is incorrect.');
      } 

  if (errorMessage != "")
  {
    alert("The following form field(s) were incomplete or incorrect:\n\n" + errorMessage + "\n\n Please complete or correct the form and submit again.");
  }
  else
  {
        document.contactForm.submit();
  }  
}
function checkFields(fieldName,msg) {
  var msg_addition = "";
   temp = eval(fieldName);
   if (temp.value.length == "0") {
   msg_addition = msg +"\n";
  }
  return(msg_addition);
}  
function checkEmail(fieldName,msg)
{         
  var msg_addition = ""
  ctrl = eval(fieldName);
  period = ".";
  //Need east one letter before @
  if (ctrl.value == "" || ctrl.value.indexOf ('@', 0) < 1) error = 1;
  else
  {
    //A period must come after the @
    test = ctrl.value.indexOf('.', ctrl.value.indexOf ('@', 0))
    if (test != -1)
    {
      error = 0;
    }
    else
    {
      error=1;
    }
  }
  if (error == 1)
  {
    msg_addition = msg+"\n";
  }
  else
  {
    new_length = ctrl.value.length - test  //The length of email address - the number of characters from @ to a period.
    if (new_length == 4 || new_length == 3 || (new_length >= 5 && ctrl.value.indexOf ('.', (test+1)) != -1))
    {
       msg_addition = "";
    }
    else
     msg_addition = msg+"\n";
  }
  return(msg_addition)
} 
/* Following Code Provied Netscape Developers Edge */

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function reformat (s)

{   var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
  }
    return resultString;
}
//Verify that we are getting a us phone number..
function isUSPhoneNumber (s){   
if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

//Reformat it to look nice..
function reformatUSPhone (USPhone) {   
return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}
