/*
@filename: registration_info_ie.js
@description: contains functions to validate registration and login forms.
              on the user registraton and login 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 = "";
// check the required fields
   
//trim whitespace from passwords
if(form.name == "loginForm" || form.name == "passwordForm" || form.name == "registerForm"){
document[form.name].upassword.value = trimLeadTrail(document[form.name].upassword.value);
if(form.name != "loginForm"){
document[form.name].upasswordVal.value = trimLeadTrail(document[form.name].upasswordVal.value);
}
}
	var requiredFields = form.required.value;

	if(form.name == "registerForm" && document.registerForm.urole.value != null && document.registerForm.urole.value.length > 0 && document.registerForm.urole.value.indexOf('Affiliate') > -1 && document.registerForm.orgSiteId != null && document.registerForm.orgSiteId.value.length != 0){
		requiredFields += ",orgSiteId";
	}

	if(!requiredOk(requiredFields, form)){
      errorText += requiredErrors; 
      cleanForm = false;    
   }
/*	
	if(form.name == "registerForm" && document.registerForm.urole.value.length > 0 && document.registerForm.urole.value.indexOf('Affiliate') > -1 && document.registerForm.orgSiteId.value.length == 0){
			alert("Here22!");
         foundValidationErrorsText["orgSiteId"] = "<li>Please select Affiliate field is required to create affiliate Customer Service User.</li>";
         foundValidationErrors[validationErrorCount] = "orgSiteId";
         validationErrorCount +=1;
         document.getElementById("registerFormorgSiteIdContainer").className="error";
	}
*/	
   // do not validate the loginForm -- since old user names do not have to be email format
   if(form.name != "loginForm"){
      if(!registrationOk(form)){
         if(foundRequiredErrors.length > 0) {
            for(var reqErr = 0; reqErr < foundRequiredErrors.length; reqErr++) {
               //dont highlight on the login form
               for(var valErr = 0; valErr < foundValidationErrors.length; valErr++) {
                  if(foundRequiredErrors[reqErr] == foundValidationErrors[valErr]) {        
                     foundValidationErrorsText[foundValidationErrors[valErr]] = "";
                  }
               }
            }
         }
         for(var i in foundValidationErrorsText) {
				//alert(foundValidationErrorsText[i]);
            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;
      window.scrollTo(0,0);
   }
   else {
      form.submit();
   }
}

/* function registrationOk(form)
 * this function checks the registration 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 registrationOk(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(); 
   
   //zipcode
   if(form.uzipcode) {
   //(!isZIPCode(form.uzipcode.value) && !isCS)
      if(!isZIPCode(form.uzipcode.value)) {
         foundValidationErrorsText["uzipcode"] = "<li>" + error["uzipcode,label"]+ ": " + error["uzipcode,invalid_zip_format"] + "</li>";
         foundValidationErrors[validationErrorCount] = "uzipcode";
         validationErrorCount +=1;
         document.getElementById(form.name + "uzipcodeContainer").className="error";	
      }
   }
   
   //email
   if(form.uemail) {
      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";	
      }
   }
   
   //password
   if(form.upassword) {
      if(!isPasswordOk(form.upassword.value)) { 
         foundValidationErrorsText["upassword"] = "<li>" + error["upassword,label"]+ ": " + error["upassword,invalid_password"] + "</li>";
         foundValidationErrors[validationErrorCount] = "upassword";
         validationErrorCount +=1;
         document.getElementById(form.name +"upasswordContainer").className="error";	
      }
   }
   
   // password validation
   if(form.upasswordVal) {
      if(form.upassword.value != form.upasswordVal.value) {
         foundValidationErrorsText["upasswordVal"] = "<li>" + error["upasswordVal,label"]+ ": " + error["upassword,passwords_dont_match"] + "</li>";
         foundValidationErrors[validationErrorCount] = "upasswordVal";
         validationErrorCount +=1;
         document.getElementById(form.name + "upasswordContainer").className="error";
         document.getElementById(form.name + "upasswordValContainer").className="error";
      }
   }
////Test For Generic Password  
if(!testGenericPassword(form.upassword.value)){
         foundValidationErrorsText["upassword"] = "<li>" + error["upassword,label"]+ ": " + error["upassword,generic_password"] + "</li>";
         foundValidationErrors[validationErrorCount] = "upassword";
         validationErrorCount +=1;
         document.getElementById(form.name +"upasswordContainer").className="error";	
}
/////End Test

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


function testGenericPassword(tst){
	var isOk = true;
	var tstString = "";
	var ints = Array(1,2,3,4,5,6,7,8,9);
	
	if(tst.length < 10){
	for(i=0;i<tst.length;i++){
	tstString = tstString + ints[i];
	}
	if(tst == tstString){isOk = false;}
	}
	return isOk;
	}

