/******************************************************************************
* Filename:    form.js                                                        *
* Description: This Javascript takes care of form validation. If there is a   *
*              form on a page and it has at least one field with the          *
*              className containing "required", then these functions will     *
*              attach to that form's "onSubmit" function. Every page should   *
*              link to this script.                                           *
* Author:      Michael A. Smith and Dan Crane                                 *
* Modified:    2006-11-27                                                     *
******************************************************************************/
/** form validation: Takes an array of fields to validate */
function validateFields(fields) {
  // Assume valid at first
  var isValid = true;
  // Loop backwards through the fields so the first invalid field gets the focus.
  for (var i = fields.length - 1; i >= 0; i--) {
    // 'field' is the field we are dealing with during this iteration
    var field = fields[i];
    // If the field isn't required, we don't validate it.
    if(field.className.indexOf("required") == -1) continue;
    // Remove the class name "invalid" from prior validation attempts
    field.className = field.className.replace("invalid", "");
    // Check dropdown boxes
    if (field.nodeName.indexOf("select") != -1) {
      if (!checkSelectField(field)) {
        makeInvalid(field);
        isValid = false;
      }
      continue;
    }
    // At this point we can be sure it's some kind of text field
    // Check that the field at least has some value
    if (!checkTextField(field)) {
      makeInvalid(field);
      isValid = false;
      continue;
    }
		// Check email values
    if (field.className.indexOf("email") != -1) {
      
      if (!checkEmailField(field)) {
        isValid = true;
        //return;
        //makeInvalid(field);
        //isValid = false;
      }
      
      continue;
    }
    
  } // end for
  
  return isValid;
} // end function

/** Marks a field invalid */
function makeInvalid(field) {
  if (field.className.indexOf("invalid") == -1) field.className += " invalid";
  field.focus();  
}
/** Checks a select field; returns true if the selected index is greater than 
    zero. */
function checkSelectField(field) {
	return (field.selectedIndex > 0);
}

/** Checks a plain text field; Returns true if there is non-whitespace content
*/
function checkTextField(field) {
  if (!field.value) return false;
  
  var value = field.value;
  
  value = value.replace(" ", "");
  if (!value) return false;
  return true;
}

/** Checks an email field; Returns true if the email is of the format 
    name@domain.tld */
function checkEmailField(field) {
  
  if (!checkTextField(field)) return false;
  
  var value = field.value;
  
  //check for '.'
  var indexOfDot = value.lastIndexOf(".");
  
  //check for '@'
  var indexOfAt = value.lastIndexOf("@");
  
  //the length of the name portion
  var lenName = indexOfAt;
  
  //the length of the domain portion
  /* algorithm note: it's possible for this to be positive even if the email is
     malformed, but the lenName will still be -1, so the function will return
     the correct value. */
  var lenDomain = indexOfDot - indexOfAt - 1;
  
  //the length of the tld portion
  var lenTLD = value.length - indexOfDot - 1;
  
  //returns true if there is at least one character in each portion
  return ((lenName > 0) && (lenDomain > 0) && (lenTLD > 0));
}

/** Validates a form's textarea, select, and input fields. */
function validateForm() {
  var isValid = true;
  
  var form = this;
  
  // 3 lines required to avoid shortcut boolean operations    
  isValid = validateFields(form.getElementsByTagName("textarea"));
  isValid = validateFields(form.getElementsByTagName("select")) && isValid;
  isValid = validateFields(form.getElementsByTagName("input")) && isValid;
  
  var alertMsg = 'Please fill in all required fields.';
  
  if (!isValid) alert(alertMsg);
  return isValid;
}

/** attaches the validateForm() function to the onSubmit event */
function validateForms() {
  if(!document.getElementsByTagName) return;
  
  var forms = document.getElementsByTagName("form");
  
  for (var i = 0; i < forms.length; i++){
    var form = forms[i];
    //form.onsubmit = validateForm;
  }
}
