/*****/
/*  Validate Contact Form
/*****/
/*  This file first checks that the required fields are indeed set, then 
/*  continues to verfiy that all data provided meets our criteria.
/*****/
/* Created By: Justin R. Schwimmer / 2009                
/*****/

// IMPORTANT: Include base validator file
document.write("<script src='js/validators.js' type='text/javascript'></script>");

function ValidateContactForm() 
{    
    // Declare error vars
    var ErrMsg = "";
    var ErrCnt = 0;

    // Obtain the names of the required fields, if any
    var aRequiredFieldNames = document.contactForm.elements['required[]'];
    var intArrayLength = aRequiredFieldNames.length;
    var strCamelCaseFieldName;
    
    // Verify that all required fields are non-blank                           
    for(x=0; x < intArrayLength; x++) {
      strCamelCaseFieldName = aRequiredFieldNames[x].value;
      // Check that the required fields are not blank 
      if (IsEmpty(document.contactForm.elements[strCamelCaseFieldName].value)) {
          ErrMsg += "  => " + strCamelCaseFieldName.replace(/([A-Z])/g, " $1").toLowerCase() + " is a required field.\n";
          ErrCnt += 1;    
      }
    }
      
    // If all required fields are set, proceed to validate data
    if (ErrCnt == 0){    
      // Validate first name
      if (document.contactForm.firstName && ContainsChars(document.contactForm.firstName.value)){
            ErrMsg += "  => First name can only contain letters.";
            ErrCnt += 1;
      }
      
      // Validate last name
      if (document.contactForm.lastName && ContainsChars(document.contactForm.lastName.value)){
            ErrMsg += "  => Last name can only contain letters.";
            ErrCnt += 1;
      }
      
      // Validate address
      if (document.contactForm.address && IsStreetAddress(document.contactForm.address.value)) {
          ErrMsg += "  => Address field can only contain numbers, letters, and hypens.\n";
          ErrCnt += 1;        
      }
      
      // Validate city
      if (document.contactForm.city && ContainsChars(document.contactForm.city.value)) {
          ErrMsg += "  => City field can only contain letters, blanks, and hypens.\n"; 
          ErrCnt += 1;        
      }
      
      // Validate state
      if (document.contactForm.state && ContainsChars(document.contactForm.state.value)){
            ErrMsg += "  => State can only contain letters.";
            ErrCnt += 1;
      }
      
      // Validate zip
      if (document.contactForm.zip && IsZip(document.contactForm.zip.value)) {
          ErrMsg += "  => Zip code field can only contain numbers, and hypens.\n";  
          ErrCnt += 1;        
      }
      
      // Validate phone
      if (document.contactForm.phone && IsPhone(document.contactForm.phone.value)) {
          ErrMsg += "  => Phone field can only contain numbers, parenthesis,and hypens.\n";
          ErrCnt += 1;        
      } 
      
      // Validate email
      if (document.contactForm.email && IsEmail(document.contactForm.email.value)) {
          ErrMsg += "  => Email field not valid. (ex. name@domain.com).\n";
          ErrCnt += 1;            
      }
      
      // Validate Question/Comment
      if (document.contactForm.questionsComments && IsEmpty(document.contactForm.questionsComments.value)) {
        ErrMsg += "  => Questions & Comments is a required field.\n";
        ErrCnt += 1;      
      } 
      
      // Validate company
      if (document.contactForm.company && ContainsNumAndChars(document.contactForm.company.value)){
            ErrMsg += "  => Company can only contain numbers and letters.";
            ErrCnt += 1;
      }
      
      // Validate position
      if (document.contactForm.position && ContainsNumAndChars(document.contactForm.position.value)){
            ErrMsg += "  => Position can only contain numbers and letters.";
            ErrCnt += 1;
      }
      
    }
    
    // If we had any errors we need to alert the user
    if (ErrCnt != 0)
    {
        ErrMsg = ErrCnt + ((ErrCnt == 1)? " Error has" : " Errors have") + " occurred, please see below for details. \n\n" + ErrMsg;
        alert(ErrMsg);
        return false;    
    }
}