/*
@filename: promotions_info_ie.js
@description: contains functions to validate the promotions entry forms.
*/

var errorText = "";
var validationErrors = "";

function groomForm(form) {

   // set cleanForm to true
   var cleanForm = true;
   // clean out any existing errors
   clearErrorStyles('span'); 
   errorText = error_start_text; // from formcheck.js
   requiredErrors = "";
   validationErrors = "";
   
   /*phone is required so concatenate the values and populate phone field with full phone number*/
   form.phone.value = form.areacodephone.value + form.prefixphone.value + form.suffixphone.value;
   
   /*fill in the referrer from javascript so we get the page and not the servlet*/
   
   form.referringpage.value = document.referrer;
   
   // check the required fields
	if(!requiredOk(form.required.value, form)){
      errorText += requiredErrors; 
      cleanForm = false;    
   }
   if(!csContactOk(form)){
      if(foundRequiredErrors.length > 0) {
         for(var reqErr = 0; reqErr < foundRequiredErrors.length; reqErr++) {
            for(var valErr = 0; valErr < foundValidationErrors.length; valErr++) {
               if(foundRequiredErrors[reqErr] == foundValidationErrors[valErr]) {        
                  foundValidationErrorsText[foundValidationErrors[valErr]] = "";
               }
            }
         }
      }
      for(var i in foundValidationErrorsText) {
         validationErrors += foundValidationErrorsText[i];
      }
      if(validationErrors != ""){
         errorText += "<p><b>" + validation_error_header + "</b></p>" + validationErrors;
         cleanForm = false;
      }
   }
   if (cleanForm != true) {
      errorText += error_end_text;      
      document.getElementById("errorText").innerHTML = errorText;
   }
   else {
      form.submit();
   }
}

/* function csContactOk(form)
 * this function checks the contact customer service form for validation errors
 * @param form - the name of the form to check
 * also requires:
 * foundValidationErrors array to store the field names where the error
 *                       was found
 * validationErrorCount  a counter to count the errors
 * foundValidationErrorsText array to store the text of the error messages
 */
var foundValidationErrors = new Array(); 
var validationErrorCount = 0; 
var foundValidationErrorsText = new Array(); 

function csContactOk(form){ 
   //clean out any old errors in the found errors array
   foundValidationErrors.length = 0;
   //ensure the error count is 0 to start
   validationErrorCount = 0;
   //ensure the validaton errors array is clear   
   foundValidationErrorsText = new Array(); 
   
   if(!isValidEmail(form.uemail.value)){
      foundValidationErrorsText["uemail"] = "<li>" + error["uemail,label"]+ ": " + error["uemail," + emailErrorCode] + "</li>";
      foundValidationErrors[validationErrorCount] = "uemail";
      validationErrorCount +=1;
      document.getElementById(form.name + "uemailContainer").className="error";	
   }
      
   if(!isUSPhoneNumber(form.phone.value)) {
      foundValidationErrorsText["phone"] = "<li>" + error["phone,label"]+ ": " + error["phone,invalid_phone_number"] + "</li>";
      foundValidationErrors[validationErrorCount] = "phone";
      validationErrorCount +=1;
      document.getElementById(form.name + "phoneContainer").className="error";
   }

   if(foundValidationErrors.length > 0) {
      return false;
   }
   return true;
}


