<!--
// Transfert d'informations entre le gestionnaire de contenu et un champs hidden :
// Ŕ faire pour chaque itération de l'application
// document.f.<NomduChamps>.value = document.<NomduChamps>_Applet.getHTMLData("http://")
//
// Validation des champs :
// validateCodePostale : fieldName, fieldDescription, errorLabel
// validateSIN : fieldName, fieldDescription, errorLabel
// isRequired:      field_name, field_label, error_label
// checkValidChars:   field_name, field_label, error_label
// validateMinMax:    field_name, field_label, min_lenght, max_lenght
// checkInValidChars:   field_name, field_label, error_label
// checkIdentical:    field_name, field_label, error_label
// checkSequential:   field_name, field_label, error_label
// validateNumBox:    field_name, field_label, error_label
// validateEmailBox:  field_name, field_label, error_label (if any, it becomes a
//                      general label, if not - a separate label is built for each error)
// validateSelectBox: field_name, field_label, error_label, non_valid_options_value
// validateBtnsBox:   field_name, field_label, error_label
// AJAXLookup fieldName, fieldDescription, AJAXFile


// Format the error message.
var errorLine1    = "Please note that you must respond to all the questions follow by * in order to have your record considered valid   \n\nBefore you continue, please fix the fields listed below.";
var errorLine2    = "_________________________________________________       ";
var errorLine3    = "Form validation errors : ( All fields follow by * must be fill )\n";
var errorBullet   = " ·  ";

// Define the valid characters. Used by checkValidChars().
var validChars    = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var invalidChars    = "{}[]<>()#%&'`/\:;=?|$*!^";

// Define the non-sequential order. Used by checkIdentical().
var numSeqStr     = 6;
var strSeq      = new Array();
strSeq[1]     = "abcdefghijklmnopqrstuvwxyz";
strSeq[2]     = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
strSeq[3]     = "zyxwvutsrqponmlkjihgfedcba";
strSeq[4]     = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
strSeq[5]     = "0123456789";
strSeq[6]     = "9876543210";


// ********** BEGIN VALIDATION FUNCTIONS ******************************************************************************
function runValidation() {

  // Add the error messages, if any
  var obj_Error = new ErrorMsg();
  obj_Error.AddCategory();
  for ( var i = 1; i < ( numChecks + 1 ); i++ )  {
    if ( checkFields[i] != "" ) obj_Error.AddMessage( checkFields[i] );
  }

  // Display errors
  if( obj_Error.errors == false ) {
    document.forms[0].submit();
    return true;
    }
  else {
    obj_Error.DisplayMessage();
    return false;
  }
}

// ===== Check if a postal code is valid =====
function validateCodePostale(fieldName, fieldDescription, errorLabel) {
  errorMsg = "";
  if (document.forms[0][fieldName].value != "" ) {
  txt = document.forms[0][fieldName].value;
  if(((txt.charAt(0) <"A" || txt.charAt(0)>"Z") && (txt.charAt(0) <"a" || txt.charAt(0)>"z"))||
        txt.charAt(1) <"0" || txt.charAt(1)>"9" ||
     ((txt.charAt(2) <"A" || txt.charAt(2)>"Z") && (txt.charAt(2) <"a" || txt.charAt(2)>"z")) ||
        txt.charAt(3) <"0" || txt.charAt(3)>"9" ||
     ((txt.charAt(4) <"A" || txt.charAt(4)>"Z") && (txt.charAt(4) <"a" || txt.charAt(4)>"z")) ||
        txt.charAt(5) <"0" || txt.charAt(5)>"9" )
           {
      errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel + "";
      document.forms[0][fieldName].focus();
      }
  }
  return errorMsg;
}

// ===== Check if field is required =====
function isRequired ( fieldName, fieldDescription, errorLabel ) {
  errorMsg  = "";
  fieldValue  = document.forms[0][fieldName].value;
  if(( fieldValue == "" ) || ( fieldValue == " " )) {
    errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel + "";
    document.forms[0][fieldName].focus();
  }
  return errorMsg ;
}


// ===== AJAX Lookup =====
function AJAXLookup ( fieldName, fieldDescription, AJAXFile ) {
  errorMsg  = "";
  var response ; 
  fieldValue  = document.forms[0][fieldName].value;
  if ( fieldValue != "" ) {
    response = loadTextFromURL(AJAXFile + fieldValue);	
        if(response == "1")
        {
          errorMsg = "\"" + fieldDescription +  "\"  - '" + fieldValue + "' is already taken. Try an other one " ;
          document.forms[0][fieldName].focus();
        }
   }
   return errorMsg ;
}


// ===== Validate field minimum and maximum length =====
function validateMinMax ( fieldName, fieldDescription, minLength, maxLength ) {
  errorMsg  = "";
  fieldValue  = document.forms[0][fieldName].value;
  fieldLength = document.forms[0][fieldName].value.length;
  if ( fieldLength < minLength ) {
    errorMsg = "\"" + fieldDescription +  "\"  - Enter more than " + minLength + " characters.";
    document.forms[0][fieldName].focus();
  } else if (( fieldLength > maxLength ) && ( maxLength > 0 )) {
    errorMsg = "\"" + fieldDescription +  "\"  - Enter less than " + maxLength + " characters.";
    document.forms[0][fieldName].focus();
  }
  return errorMsg ;
}

// ===== Check if entry contains only valid characters =====
function checkValidChars( fieldName, fieldDescription, errorLabel ) {
  errorMsg  = "";
  fieldValue  = document.forms[0][fieldName].value;
  fieldLength = document.forms[0][fieldName].value.length;
  for( var i=0; i<fieldLength; i++ ) {
    if ( validChars.indexOf( fieldValue.charAt( i )) == -1 ) {
      errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
      document.forms[0][fieldName].focus();
    }
  }
  return errorMsg ;
}



// ===== Check if entry not contains invalid characters =====
function checkInValidChars( fieldName, fieldDescription, errorLabel ) {
  errorMsg  = "";
  fieldValue  = document.forms[0][fieldName].value;
  fieldLength = document.forms[0][fieldName].value.length;
  for( var i=0; i<fieldLength; i++ ) {
    if ( invalidChars.indexOf( fieldValue.charAt( i )) != -1 ) {
      errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
      document.forms[0][fieldName].focus();
    }
  }
  return errorMsg ;
}

// ===== Check if entry contains entirely identical characters =====
function checkIdentical( fieldName, fieldDescription, errorLabel ) {
  errorMsg  = "";
  fieldValue  = document.forms[0][fieldName].value;
  fieldLength = document.forms[0][fieldName].value.length;

  if (( fieldLength > 1 ) && ( isComposedOfChars( fieldValue.charAt( 0 ), fieldValue ))) {
    errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
    document.forms[0][fieldName].focus();
  }
  return errorMsg ;
}

function isComposedOfChars( curChar, inString ) {
  return ( indexOfFirstNotIn( curChar, inString ) == -1 );
}

function indexOfFirstNotIn( okayChars, inString ) {
  for ( var i=0; i<inString.length; i++ ) {
    if ( okayChars.indexOf( inString.charAt( i )) == -1 ) {
      return i;
    }
  }
  return -1;
}

// ===== Check if entry contains entirely sequential characters =====
function checkSequential( fieldName, fieldDescription, errorLabel ) {
  errorMsg  = "";
  fieldValue  = document.forms[0][fieldName].value;
  fieldLength = document.forms[0][fieldName].value.length;

  if ( fieldLength > 1 ) {
    for ( var i = 1; i < ( numSeqStr + 1 ); i++ )  {
      if ( strSeq[i].indexOf( fieldValue ) != -1 ) {
        errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
        document.forms[0][fieldName].focus();
      }
    }
  }
  return errorMsg ;
}

// ===== Check if entry contains only numbers and , .  =====
function validateNumBox( fieldName, fieldDescription, errorLabel ) {
  errorMsg  = "";
  fieldValue  = document.forms[0][fieldName].value;
  fieldLength = document.forms[0][fieldName].value.length;

  for( var i = 0; i < fieldLength; i++ ) {
    var ch = fieldValue.substring( i, i + 1 );
    if ((( ch < "0" ) || ( ch > "9" )) && (( ch != ",") && ( ch != ".")))  {
      errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
      document.forms[0][fieldName].focus();
    }
  }
  return errorMsg ;
}

// ===== Validate an email address =====
function validateEmailBox ( fieldName, fieldDescription, errorLabel ) {
  errorMsg  = "";
  fieldValue  = document.forms[0][fieldName].value;
  fieldLength = document.forms[0][fieldName].value.length;

  if ( fieldLength > 0 ) {

    if (( errorLabel == "" ) || ( errorLabel == null )) {

      errorLabel1 = "Email address is invalid.";   // Not valid
      errorLabel2 = errorLabel1;    // Missing "@"
      errorLabel3 = errorLabel1;   // Missing "."
      errorLabel4 = errorLabel1;    // Starts with " "
      errorLabel5 = errorLabel1;    // Starts with "@"
      errorLabel6 = errorLabel1;    // Starts with "."

      if ( fieldValue.indexOf( "@" ) == -1 ) {
        errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel2 + "";
        document.forms[0][fieldName].focus();
      } else if ( fieldValue.indexOf( "." ) == -1 ) {
        errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel3 + "";
        document.forms[0][fieldName].focus();
      } else if ( fieldValue.charAt( 0 ) == " " ) {
        errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel4 + "";
        document.forms[0][fieldName].focus();
      } else if ( fieldValue.charAt( 0 ) == "@" ) {
        errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel5 + "";
        document.forms[0][fieldName].focus();
      } else if ( fieldValue.charAt( 0 ) == "." ) {
        errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel6 + "";
        document.forms[0][fieldName].focus();
      }

    } else {

      if (( fieldValue.indexOf( "@" ) == -1 ) ||   // Missing "@"
        ( fieldValue.indexOf( "." ) == -1 ) ||   // Missing "."
        ( fieldValue.charAt( 0 ) == "@" ) ||   // Starts with "@"
        ( fieldValue.charAt( 0 ) == "." )    // Starts with "."
        ) {
        errorMsg = "\"" + fieldDescription +  "\"  - " + errorLabel1 + "";
        document.forms[0][fieldName].focus();
      }
    }
  }
  return errorMsg ;
}

// ===== Check if an option has been selected =====
function validateSelectBox( fieldName, fieldDescription, errorLabel, checkWhat ) {
  errorMsg  = "";
  optSelected = document.forms[0][fieldName].selectedIndex;
  fieldValue  = document.forms[0][fieldName].options[optSelected].value;
  if( fieldValue == checkWhat ) {
    errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
    document.forms[0][fieldName].focus();
  }
  return errorMsg ;
}

// ===== Check if a checkbox or radio button has been selected =====
function validateBtnsBox( fieldName, fieldDescription, errorLabel ) {
  errorMsg  = "";
  selection = null;
  thisButton = document.forms[0][fieldName];
  for( var i=0; i<thisButton.length; i++ ) {
    if( thisButton[i].checked ) {
       selection = thisButton[i].value;
    }
  }
  if( selection == null ) {
    errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
    //document.forms[0][fieldName].focus();
  }
  return errorMsg;
}

// ===== Check if SIN (Social Insurance Number) is valid =====
function validateSIN( fieldName, fieldDescription, errorLabel ) {

  errorMsg  = "";
  sin_number = document.forms[0][fieldName].value ;

  if(sin_number != "")
  {

    //*** Initialize the variable
     var sin_add = 0;
     var sin_add_int = parseInt(sin_add);

     var sin_div = 0;
     var sin_div_int = parseInt(sin_div);

    //*** Creating loop to go through the sin number  ***
    for(var i=0;i < sin_number.length;i++)
    {
      //*** Assigning field to var ***
      thestr = sin_number;

      //*** Retrieve the value from each position ***
      var pos_value = thestr.substring(i,i+1);

      pos_value_int = parseInt(pos_value);


      //*** If the position is the 2nd 4th 6th or 8th - multiply by two and add the digits seperately ***
      //*** Example: 8 * 2 = 16 -> 1 + 6 = 7  (8 being a character is one of the formentioned positions)***
      if(trim(i) == 1 || trim(i) == 3 || trim(i) == 5 || trim(i) == 7)
      {
        //*** multiply by two ***
        pos_value_int = pos_value_int * 2;

        var pos_value_str = pos_value_int;
        pos_value_str = pos_value_str + "";

        //*** checking to see if the number is double digits ***
        if(pos_value_str.length == "2")
        {
          //*** Adding the digits seperately ***
          var pos1 = pos_value_str.substring(0,1);
          var pos2 = pos_value_str.substring(1,2);

          var pos1_int = parseInt(pos1);
          var pos2_int = parseInt(pos2);

          var doubledigit_add = pos1_int + pos2_int;

          var doubledigit_add_int = parseInt(doubledigit_add);

          //*** Assigning a value to pos_value ***
          pos_value_int = doubledigit_add_int;

        }

      }


      //*** Adding the string as we go through it ***
      sin_add_int = sin_add_int + pos_value_int;

    }

    //*** Dividing the total number by 10 to checks it's validity***
    sin_div_int = sin_add_int / 10;

    //*** Convert to string ***
    var sin_div_str = sin_div_int + "";

    //*** if the number has a decimal, the total was not divisible by 10 and therefor is not valid ***
    for(var x = 0;x < sin_div_str.length; x++)
    {

      if(sin_div_str.substring(x,x+1) == ".")
      {
        errorMsg = "\"" + fieldDescription +  "\" - " + errorLabel + "";
        document.forms[0][fieldName].focus();
      }
    }
  }
  return errorMsg;
   }

// *********** END VALIDATION FUNCTIONS *******************************************************************************


// ************ BEGIN OBJECT FUNCTIONS ********************************************************************************
function AddCategory( str_Name ) {
    // Creates a new category object at the next space in the array
  this.obj_Errors[this.obj_Errors.length] = new NewCategory( str_Name );
  this.NewCategory = NewCategory;
}

function NewCategory( str_Name) {
    //  Initializes the category values
  this.str_Name   = str_Name;
  this.str_Error    = "";
}

function AddMessage( str_Msg ) {
  for ( var i = 0; i < this.obj_Errors.length; i++ )  {
    this.obj_Errors[i].str_Error += errorBullet;
    this.obj_Errors[i].str_Error += str_Msg + "\n";
    this.errors = true;
    return true;
  }
}

//  This is the object you create to keep track of errors in the document
function ErrorMsg() {
  this.obj_Errors   = new Array();
  this.errors   = false;
  this.AddCategory  = AddCategory;
  this.AddMessage   = AddMessage;
  this.DisplayMessage = DisplayMessage;
}

// ************* END OBJECT FUNCTIONS *******************************************************************************

// Displays the error messages.
// If none, false is returned and no message is displayed.
function DisplayMessage() {

  if ( this.errors == false ) {
    return false;
  } else {
    var str_msg = "";
    str_msg += errorLine1 + "\n";
    str_msg += errorLine2 + "\n";
    for ( var i = 0; i < this.obj_Errors.length; i++ ) {  // Go through all of the objects
      if ( this.obj_Errors[i].str_Error != '' ) { // If errors, write the errors
        str_msg += "\n" + errorLine3 + "\n";
        str_msg += this.obj_Errors[i].str_Error + "\n";
      }
    }
    alert( str_msg );                       // Display the error message
    return true;

  }
}
//--!>
