//Javascript Include File to Validate the Contact Us form.
//Updated 3/16/2002 by Steve Valenzuela
//  -Incorporated Jay's work into the include file and rewrote some of the
//     code for formatting.
//  -Added all error messages to a single alert at the time of submission.
//Updated 3/19/2002 by Gabe Netty
//  -Added a return value to the function
//	-Removed call to submit()

function validateContact()
{
  var err = 0;
  
  var reqFields = new Array('First_Name','Last_Name','email');
  var fieldNames = new Array('First Name','Last Name','E-mail');
  
  var errorArray = new Array();
  var categoryError;
  var messageError;
  var emailError;
  
  //Create the array of blank field errors
  for (i=0; i<document.contact.elements.length; i++) {
	for (j=0; j<reqFields.length; j++) {
	  if (document.contact.elements[i].name == reqFields[j]) {
	    if (document.contact.elements[i].value == "") {
		  errorArray[errorArray.length] = fieldNames[j];
		  err = 1;
		}
	  }
	}
  }
  
  /*Check the value of the category drop down list
  if (document.contact.category[document.contact.category.selectedIndex].value == "0"){
    categoryError = 1;
	err = 1;
  }*/
  
  //Validate the email entered by the user
  var emailStr = document.contact.email.value;
  var emailPattern;
  var matchArray;
	
  if (emailStr!=""){
	//emailPattern = /^(.+)@(.+)$/;
	emailPattern = /[A-Za-z0-9_\.]+@[A-Za-z0-9_]+\.[A-Za-z0-9_]+/;
	matchArray = emailStr.match(emailPattern)
      
	if (matchArray==null) {
  	  emailError = 1;
	  err = 1;
    }
  }
  
  //Validate the Message area...ensure that it is not left blank
  var messageStr = document.contact.message.value;
  
  if (messageStr == ""){
    messageError = 1;
	err = 1;
  }
	
  //Check for the existance of errors and format for output to the user
  //prior to form submission	
  if (err == 1) {
    errorMessage = "___________________________________________________________\n\n";
	errorMessage+= "The form was not submitted because of the following error(s).\n";
	errorMessage+= "Please correct these error(s) and re-submit.\n";
	errorMessage+= "___________________________________________________________\n\n";
	
	if (errorArray.length > 0) {
	  errorMessage+= "-The following required text field(s) are empty:\n";
		
		for (i=0; i<errorArray.length; i++) {
		  errorMessage+="     "+errorArray[i]+"\n";
		}
	}
		
    if (emailError == 1){		
	  errorMessage+="\n-A valid E-mail address is required for submission.\n";
	}

	/*if (categoryError == 1) {
	  errorMessage+="\n-Please select a Topic of Inquiry or select 'Others' if you are unsure.\n";
	}*/

	if (messageError == 1){
	  errorMessage+="\n-A message describing the nature of your inquiry is required.\n";
	}
	
	alert(errorMessage);
	return false;
  }
	
  else if (err == 0){
    return true;
//    document.contact.submit();
  }
}  



