
// -------------------------------------------------------------------
// FORM FIELD ERRORS, DEFINITIONS & RESET
// -------------------------------------------------------------------

// The default border color and bg color are initialized at the first resetFormErrors
// call. We assume all fields use the same color.
var defaultBorderColor ="#";
var defaultBackgroundColor = "#";

// This is the style colors we use to mark fields as in Error
var errorBorderColor = "#FF4444"
var errorBackgroundColor = "#FFCCCC"

// any error found in the currenlty processed form ?
var flagFormHasErrors;
var flagMissingMandatoryFields;
var flagTextareaFieldsTooLong;
var flagBadNumericFields;
var flagPasswordFieldsDontMatch;
var flagBadLoginField;
var flagBadEmailField;

// Resets the fields marked in error. Does not change the values, only changes the display
// back to normal.
// IMPORTANT : we only check text, multiple select, password & textarea fields.
//             We assume single select, radio & checkboxes always have a default value selected.
// @param srcForm form to reset
function resetFormErrors(srcForm){

  // we reset the error form flags
  flagFormHasErrors = false;
  flagMissingMandatoryFields = false;
  flagTextareaFieldsTooLong = false;
  flagBadNumericFields = false;
  flagPasswordFieldsDontMatch = false;
  flagBadLoginField = false;
  flagBadEmailField = false;

  // Loop on form fields
  for (i = 0; i < srcForm.elements.length; i++) {

      var formElement = srcForm.elements[i];

      switch (formElement.type) {
          case 'text':
          case 'textarea':
          case 'password':
          case 'select-one':

              // we initialize our colors if they were not initialized
              if(defaultBorderColor=="#" || defaultBackgroundColor=="#") {
                 defaultBorderColor = formElement.style.borderColor;
                 defaultBackgroundColor = formElement.style.backgroundColor;
              }
              else {
                // be sure the colors have been reseted
                formElement.style.borderColor = defaultBorderColor;
                formElement.style.backgroundColor = defaultBackgroundColor;
              }

              break;
      }
  }
}

// Checks if this mandatory text field has content
// @param formField field to test
function checkMandatoryTextField(formField) {

   if(!formField.value || trim(formField.value)=="") {
      // No text ! error found
      flagFormHasErrors = true;
      flagMissingMandatoryFields = true;

      // we set the colors to the error ones
      formField.style.borderColor = errorBorderColor;
      formField.style.backgroundColor = errorBackgroundColor;
   }
}

// Checks if this textarea field has no more chars than specified
// @param formField field to test
// @param maxLength max chars
function checkTextareaField(formField,maxLength) {

   if(formField.value && formField.value.length>maxLength) {
      // text too long ! error found
      flagFormHasErrors = true;
      flagTextareaFieldsTooLong = true;

      // we set the colors to the error ones
      formField.style.borderColor = errorBorderColor;
      formField.style.backgroundColor = errorBackgroundColor;
   }
}

// Checks if this text field is numeric
// @param formField field to test
function checkYearField(formField) {

   var pattern = /^[0-9]+$/;

   if(!formField) {
      return;
   }

   if(formField && formField.value
       && (isNaN(parseInt(formField.value,10)) || formField.value.indexOf('.')>0) || !pattern.test(formField.value)
       || formField.value<=1930) {
      // bad int ! error found
      flagFormHasErrors = true;
      flagBadNumericFields = true;

      // we set the colors to the error ones
      formField.style.borderColor = errorBorderColor;
      formField.style.backgroundColor = errorBackgroundColor;
   }
}

// Checks if the two password fields are equal. Does not test if fields are present.
// @param passwordField1 password field 1 to test
// @param passwordField2 password field 2 to test
function checkPasswordFields(passwordField1,passwordField2) {

   if(passwordField1.value && passwordField2.value && passwordField1.value!=passwordField2.value) {
      // not equal ! error found
      flagFormHasErrors = true;
      flagPasswordFieldsDontMatch = true;

      // we set the colors to the error ones
      passwordField1.style.borderColor = errorBorderColor;
      passwordField1.style.backgroundColor = errorBackgroundColor;

      passwordField2.style.borderColor = errorBorderColor;
      passwordField2.style.backgroundColor = errorBackgroundColor;
   }
}

// Checks if this login field is alphanumeric only
// @param loginField field to test
function checkLoginField(loginField) {

   var pattern = /^[a-zA-Z0-9]+$/;

   if(loginField.value
       && !pattern.test(loginField.value)) {
      // bad chars in login ! error found
      flagFormHasErrors = true;
      flagBadLoginField = true;

      // we set the colors to the error ones
      loginField.style.borderColor = errorBorderColor;
      loginField.style.backgroundColor = errorBackgroundColor;
   }
}

// Checks if this email field has a correct format
// @param emailField field to test
function checkEmailField(emailField) {

   var pattern = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;;

   if(emailField.value
       && !pattern.test(emailField.value)) {
      // bad format ! error found
      flagFormHasErrors = true;
      flagBadEmailField = true;

      // we set the colors to the error ones
      emailField.style.borderColor = errorBorderColor;
      emailField.style.backgroundColor = errorBackgroundColor;
   }
}

// Simple trim function
// @param str String to trim
function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

// -------------------------------------------------------------------
// FORM FIELD ERROR DISPLAY
// -------------------------------------------------------------------

// Returns true if errors have been spotted
function hasFormErrors() {
    return flagFormHasErrors;
}

// Displays an alert popup with error messages
function displayFormErrors() {

    if(flagFormHasErrors==false) {
       return; // protection
    }

    // Display in the right language
    var txt = "";

    if(lang=='FR') {
       if(flagMissingMandatoryFields==true) {
         txt += "Des champs obligatoires n'ont pas &eacute;t&eacute; saisis.<br/>";
       }

       if(flagTextareaFieldsTooLong==true) {
         txt += "Votre saisie 'probl&egrave;mes de sant&eacute;/mobilit&eacute;' est trop longue (200 caract&egrave;res max).<br/>";
       }

       if(flagBadNumericFields==true) {
         txt += "Votre ann&eacute;e de naissance a un mauvais format. Le format doit &ecirc;tre aaaa comme 1975.<br/>";
       }

       if(flagPasswordFieldsDontMatch==true) {
         txt += "- les deux mots de passe entr&eacute;s ne correspondent pas.<br/>";
       }

       if(flagBadLoginField==true) {
         txt += "- le login ne doit contenir que des lettres et des chiffres.<br/>";
       }

       if(flagBadEmailField==true) {
         txt += "- l'adresse courriel a un mauvais format.<br/>";
       }
    }
    else {
       if(flagMissingMandatoryFields==true) {
         txt += "Mandatory fields have not been entered.<br/>";
       }

       if(flagTextareaFieldsTooLong==true) {
         txt += "Your medical/mobility notes are too long (200 characters max).<br/>";
       }

       if(flagBadNumericFields==true) {
         txt += "Your birth year has an invalid format. The format should be yyyy like 1975.<br/>";
       }

       if(flagPasswordFieldsDontMatch==true) {
         txt += "- the two passwords entered don't match.<br/>";
       }

       if(flagBadLoginField==true) {
         txt += "- the login must only contain letters and numbers.<br/>";
       }

       if(flagBadEmailField==true) {
         txt += "- the email address has a bad format.<br/>";
       }
    }

    dialog_alert(txt, null,440, 120);
}

