//... Valide si un champs est vide et si oui, ajoute un message dans la pile des messages.
function IsEmpty(objField, strLabel)
{
  if(Trim(objField.value) == '' && !objField.options) {
    if(strLabel != '') {
      AddMsg(strLabel, 'EMPTY');
    }
    return true;
  }
  else {
    if(objField.options) {
      if (objField.options[objField.selectedIndex].value == '')
      {
        if(strLabel != '') {
          AddMsg(strLabel, 'EMPTY');
        }
        return true;
      }
    }
  }
  return false;
}

//... Valide si un champs est vide et si oui, ajoute un message dans la pile des messages et permet 
//... d'afficher un message d'erreur sur mesure et non bâti automatiquement..
function IsEmptyCustom(objField, strLabel)
{
  if(Trim(objField.value) == '' && !objField.options) {
    if(strLabel != '') {
      AddMsg(strLabel, 'CUSTOM_ERR_MSG');
    }
    return true;
  }
  else {
    if(objField.options) {
      if (objField.options[objField.selectedIndex].value == '')
      {
        if(strLabel != '') {
          AddMsg(strLabel, 'CUSTOM_ERR_MSG');
        }
        return true;
      }
    }
  }
  return false;
}

//... Valide si un champs de type "radio" a été coché.
function IsChecked(objField, strLabel)
{
  var i;
  for (i = 0; i < objField.length; i++) {
    if (objField[i].checked == true) {
      return true;
    }
  }

  //... Aucune des options est cochée.
  if(strLabel != '') {
    AddMsg(strLabel, 'CHECKED');
  }
  return false;
}

//... Valide si un champs de type "radio" a été coché.
function IsCheckedMandatory(objField, strLabel)
{
  var i;
  for (i = 0; i < objField.length; i++) {
    if (objField[i].checked == true) {
      return true;
    }
  }

  //... Aucune des options est cochée.
  if(strLabel != '') {
    AddMsg(strLabel, 'EMPTY');
  }
  return false;
}
//... Valide si un champs de type "radio" a été coché et permet d'afficher un message d'erreur
//... sur mesure et non bâti automatiquement.
function IsCheckedCustom(objField, strLabel)
{
  var i;
  for (i = 0; i < objField.length; i++) {
    if (objField[i].checked == true) {
      return true;
    }
  }

  //... Aucune des options est cochée.
  if(strLabel != '') {
    AddMsg(strLabel, 'CUSTOM_ERR_MSG');
  }
  return false;
}

//... Valide si un champs de type "checkbox" a été coché.
function IsCheckedBox(objField, strLabel)
{
  if (objField.checked == true) {
    return true;
  }

  //... La case n'est pas cochée.
  if(strLabel != '') {
    AddMsg(strLabel, 'CHECKBOX');
  }
  return false;
}

//... Valide si une chaîne ne contient que des chiffres ou le caractère séparateur des décimales.
//... Si le format est incorrect, ajoute le bon message dans la pile des messages.
function IsNumeric(objField, strLabel, strDecimalChar, intNbDecimalsAccept)
{
  var intPosPeriod = 0;
  var intNbDecimals = 0;
  var strCurChar = '';
  var strValOK = '';

  if (intNbDecimalsAccept == 0)
     var strFilter = '0123456789-';
  else
     var strFilter = '0123456789,.-';

  var strDecimalCharSearch = '';
  var strDecimalCharReplace = '';
           
  //... Selon que c'est un entier ou un décimal, vérifier si il y a un caractère interdit dans la chaîne.
  if (intNbDecimalsAccept == 0) {
    //... Entier : vérifier si il y a un caractère décimal.
    for (var i = 0; i < objField.value.length; i++) {
      strCurChar = objField.value.substring(i, i + 1);
      if (strCurChar == '.' || strCurChar == ',') {
        if(strLabel != '') {
          AddMsg(strLabel, 'NUM_INT');
        }
        return false;
      }
    }

    //... Continuer la vérification avec la liste des caractères interdits.
    for (var i = 0; i < objField.value.length; i++) {
      if (strFilter.indexOf(objField.value.substring(i, i + 1)) == -1) {
        if(strLabel != '') {
          AddMsg(strLabel, 'NUM_CHAR_DENIED');
        }
        return false;
      }
    }
  }
  else {
    //... Décimal : vérifier si il y a un caractère interdit dans la chaîne.
    for (var i = 0; i < objField.value.length; i++) {
      if (strFilter.indexOf(objField.value.substring(i, i + 1)) == -1) {
        if(strLabel != '') {
          AddMsg(strLabel, 'NUM_CHAR_DENIED');
        }
        return false;
      }
    }
  }

  //... Remplacer le point par une virgule ou la virgule par un point, selon la langue.
  if (strDecimalChar == '.') {
    strDecimalCharSearch = ',';
    strDecimalCharReplace = '.';
  }
  else {
    strDecimalCharSearch = '.';
    strDecimalCharReplace = ',';
  }
  for (var i = 0; i < objField.value.length; i++) {
    strCurChar = objField.value.substring(i, i + 1);
    if (strCurChar == strDecimalCharSearch)
      strValOK = strValOK + strDecimalCharReplace;
    else
      strValOK = strValOK + strCurChar;
  }

  //... Vérifier si il y a plus que un caractère décimal.
  for (var i = 0; i < strValOK.length; i++) {
    if (strValOK.substring(i, i + 1) == strDecimalChar) {
      intNbDecimals = intNbDecimals + 1;
    }
  }

  if (intNbDecimals > 1) {
    if(strLabel != '') {
      AddMsg(strLabel, 'NUM_DECIMAL_CHAR_NB');
    }
    return false;
  }

  //... Vérifier la position du caractère décimal.
  intPosPeriod = strValOK.indexOf(strDecimalChar);
  if (intPosPeriod != -1) {
    intNbDecimals = ((strValOK.length - intPosPeriod) - 1);

    //... Vérifier si il n'y a pas trop de chiffres après le caractère décimal.
    if (intNbDecimals > intNbDecimalsAccept) {
      if(strLabel != '') {
        AddMsg(strLabel, 'NUM_DECIMAL_NB_AFTER');
      }
      return false;
    }
  }

  //... Ajouter un zéro au début si la valeur est entre 0 et 1 et que le zéro n'est pas déjà présent.
  if(CNum(objField.value) > 0 && CNum(objField.value) < 1 && (objField.value.substring(0, 1) == '.' || objField.value.substring(0, 1) == ',')) {
    objField.value = '0' + objField.value;
  }

  return true;
}

//... Valide si un nombre est plus grand que zéro. *** Le nombre doit avoir été validé au préalable. 
//... Si le nombre n'est pas plus grand que zéro, ajoute un message dans la pile des messages.
function IsPositive(objField, strLabel)
{

  if(CNum(objField.value) <= 0) {
    if(strLabel != '') {
      AddMsg(strLabel, 'ZERO');
    }
    return false;	
  }

  return true;
}

//... Valide si un nombre est plus petit qu'une valeur. *** Le nombre doit avoir été validé au préalable. 
//... Si le nombre est pas plus grand que la valeur maximale, ajoute un message dans la pile des messages.
function IsLessThan(objField, strLabel, strMaxValue)
{

  if(CNum(objField.value) >= CNum(strMaxValue)) {
    if(strLabel != '') {
      AddMsg(strLabel, 'LESS_THAN');
    }
    return false;	
  }

  return true;
}

//... Valide si un champ est d'une longueur suffisante.
function IsLongEnough(objField, strLabel, intLength)
{
  if (objField.value.length < intLength) {
    if(strLabel != '') {
      AddMsg(strLabel, 'LONG_ENOUGH', intLength);
    }
    return false;
  }

  return true;
}

//... Valide si un champ code postal est valide.
function IsValidPostalCode(objField1, objField2, strLabel)
{
  if (IsEmpty(objField1, strLabel))
    return false;	
  else
    if (IsEmpty(objField2, strLabel))
      return false;	

  regExp = /^([a-z]{1}\d[a-z]{1})( |-)?(\d[a-z]{1}\d)$/i

  var blnValidCode = regExp.exec(objField1.value+objField2.value)

  if (!blnValidCode) {
    if(strLabel != '') {
      AddMsg(strLabel, 'POSTAL_CODE');
    }
    return false;	
  }

  return true;
}

//... Valide si un champ courriel est valide.
function IsValidEMail(objField, strLabel)
{
  if (IsEmpty(objField, strLabel))
    return false;	

  var regExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ 
   
  var blnValidEmail = regExp.exec(objField.value)

  if (!blnValidEmail) {
    if(strLabel != '') {
      AddMsg(strLabel, 'E_MAIL');
    }
    return false;	
  }

  return true;
}

//... Valide si un champ téléphone est valide.
function IsValidTelephone(objField1, objField2, objField3, strLabel, strLabelReg)
{
  if (IsEmpty(objField1, '') && IsEmpty(objField2, '') && IsEmpty(objField3, '')) {
    IsEmpty(objField1, strLabel);
    return false;
  }
  else {
    if (IsEmpty(objField1, '') && !IsEmpty(objField2, '') && !IsEmpty(objField3, '')) {
      IsEmpty(objField1, strLabelReg);
      return false;
    }
  }

  var regExp = /^\(?(\d{3})\)?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/ 
   
  var blnValidTelephone = regExp.exec(objField1.value+objField2.value+objField3.value)

  if (!blnValidTelephone) {
    if(strLabel != '') {
      AddMsg(strLabel, 'TELEPHONE');
    }
    return false;	
  }

  return true;
}

//... Valide si un champ date est valide.
function IsValidDate(objField1, objField2, objField3, strLabelDay, strLabelMonth, strLabelYear, strLabelDate)
{
  var blnValidDate = true;
  var intMaxDay = 0;
  var intDay = objField1.value;
  var intMonth = objField2.value;
  var intYear = objField3.value;

  //... Vérifier si toutes les cases ont été remplies 
  if (IsEmpty(objField1, strLabelDay))
    return false;	
  else
    if (IsEmpty(objField2, strLabelMonth))
      return false;	
    else
      if (IsEmpty(objField3, strLabelYear))
        return false;	

  //... Vérifier la longueur de ce qui a été entrée dans le jour et ajouter un zéro devant le chiffre si 
  //... un seul chiffre a été entré.
  if (!IsLongEnough(objField1, '', 2))
    if (IsNumeric(objField1, '', '<%= strLblDecimalSeparator %>', 0))
      if (IsPositive(objField1, ''))
        objField1.value = '0' + objField1.value;    

  //... Vérifier la longueur de ce qui a été entré dans l'année 
  if (!IsLongEnough(objField3, strLabelYear, 4))
    return false;	

  //... Vérifier si ce qui a été entré dans le jour est numérique et positif 
  if (!IsNumeric(objField1, '', '<%= strLblDecimalSeparator %>', 0))
    blnValidDate = false;
  if (blnValidDate)
    if (!IsPositive(objField1, ''))
      blnValidDate = false;

  //... Vérifier si ce qui a été entré dans le mois est numérique et positif 
  if (!IsNumeric(objField2, '', '<%= strLblDecimalSeparator %>', 0))
    blnValidDate = false;
  if (blnValidDate)
    if (!IsPositive(objField2, ''))
      blnValidDate = false;

  //... Vérifier si ce qui a été entré dans l'année est numérique et positif 
  if (!IsNumeric(objField3, '', '<%= strLblDecimalSeparator %>', 0))
    blnValidDate = false;
  if (blnValidDate)
    if (!IsPositive(objField3, ''))
      blnValidDate = false;

  if (!blnValidDate) {
    if(strLabelDate != '') {
      AddMsg(strLabelDate, 'DATE');
    }
    return false;	
  }

  //... Formattage OK, vérifier si la date est vraiment une date valide
  intMaxDay = GetMaxDayOfMonth(intMonth, intYear);  

  if((intDay <= 0) || (intDay > intMaxDay)){ 
    blnValidDate = false;
  }
  else if((intMonth <= 0) || (intMonth > 12)) { 
    blnValidDate = false;
  }
  else if((intYear <= 0)) {
    blnValidDate = false;
  } 

  if (!blnValidDate) {
    if(strLabelDate != '') {
      AddMsg(strLabelDate, 'DATE');
    }
    return false;	
  }

  return true;
}

//... Retourne le plus grand jour d'un mois.
function GetMaxDayOfMonth(intMonth, intYear)
{
  var intMaxDay;

  if((intMonth == 4) || (intMonth == 6) || (intMonth == 9) || (intMonth == 11)) { 
    intMaxDay = 30;
  }
  else if(intMonth == 2) {
    intMaxDay = IsLeapYear(intYear) ? 29 : 28;    
  }
  else {
    intMaxDay = 31;
  }
  
  return intMaxDay; 
}

//... Détermine si une année est une année bissextile.
function IsLeapYear(intYear)
{
  if (intYear % 2 == 0) 
    return true;
  
  return false;
}

//... Valide si un champ date est inférieur à la date du jour.
function IsDateBefore(objField1, objField2, objField3, strLabelDate)
{
  var blnValidDate = true;
  var intDay = objField1.value;
  var intMonth = objField2.value;
  var intYear = objField3.value;
  var d = new Date();
  var curDate = new Date(d.getFullYear(), d.getMonth(), d.getDate());
  var inDate = new Date(intYear-0, intMonth-1, intDay-0);

  if (inDate >= curDate)
    blnValidDate = false;

  if (!blnValidDate) {
    if(strLabelDate != '') {
      AddMsg(strLabelDate, 'DATE_BEFORE');
    }
    return false;	
  }

  return true;
}

//... Valide si un champ date est supérieur à la date du jour.
function IsDateAfter(objField1, objField2, objField3, strLabelDate)
{
  var blnValidDate = true;
  var intDay = objField1.value;
  var intMonth = objField2.value;
  var intYear = objField3.value;
  var d = new Date();
  var curDate = new Date(d.getFullYear(), d.getMonth(), d.getDate());
  var inDate = new Date(intYear-0, intMonth-1, intDay-0);

  if (inDate <= curDate)
    blnValidDate = false;

  if (!blnValidDate) {
    if(strLabelDate != '') {
      AddMsg(strLabelDate, 'DATE_AFTER');
    }
    return false;	
  }

  return true;
}

//... Converti une chaîne numérique et la formatte avec un point comme séparateur des décimales. 
function CNum(strNumber)
{
  var strCurChar = '';
  var strNumberOut = '';
           
  //... Remplacer la virgule par un point.
  for (var i = 0; i < strNumber.length; i++) {
    strCurChar = strNumber.substring(i, i + 1);
    if (strCurChar == ',')
      strNumberOut = strNumberOut + '.';
    else
      strNumberOut = strNumberOut + strCurChar;
  }

  return parseFloat(strNumberOut);
}

//... Élimine les espaces superflus de gauche et de droite.
function Trim(strString)
{			
  return TrimLeft(TrimRight(strString));		
}

//... Élimine les espaces superflus de droite.
function TrimRight(strString)
{
  var strSpace = new String(" \t\n\r");
  var strFormat = new String(strString);

  //... On vérifie s'il y a des espaces à la fin
  if (strSpace.indexOf(strFormat.charAt(strFormat.length-1)) != -1) {
    //... On trouve la fin de la chaîne
    var intPos = strFormat.length - 1;

    //... On retrouve la position de début des espaces de fin de chaîne
    while (intPos >= 0 && strSpace.indexOf(strFormat.charAt(intPos)) != -1) {
      intPos--;
    }

    //... On conserve la chaîne sans les espaces de fin
    strFormat = strFormat.substring(0, intPos+1);
  }

  return strFormat;
}

//... Élimine les espaces superflus de gauche.
function TrimLeft(strString)
{
  var strSpace = new String(" \t\n\r");
  var strFormat = new String(strString);

  //... On vérifie s'il y a des espaces au début
  if (strSpace.indexOf(strFormat.charAt(0)) != -1) {
    // On définit le départ et la fin
    var intPos = 0, intFin = strFormat.length;

    //... On retrouve la position de fin des espaces de début de chaîne
    while (intPos < intFin && strSpace.indexOf(strFormat.charAt(intPos)) != -1) {
      intPos++;
    }

    //... On conserve la chaîne sans les espaces de début
    strFormat = strFormat.substring(intPos, intFin);
  }
  	
  return strFormat;
}

//... Remplace la première lettre d'une chaîne par une majuscule.
function Capitalize(strString)
{
  return strString.substring(0,1).toUpperCase() + strString.substring(1);;
}

//... Vérifie si un caractère est numérique.
function IsNum(strChar)
{
  if (strChar >= '0' && strChar <= '9')
    return true;
  else
    return false;
}

//... Ajoute un message dans la pile des messages.  La pile des messages est une variable globale.
function AddMsg(strLabel, strMsgType, intNum)
{
  if (strMsgType != 'CUSTOM_ERR_MSG')  
    g_strErrMsg = g_strErrMsg + g_strMsgField + strLabel + ReturnErrMsg(strMsgType, intNum) + '\n';
  else
    g_strErrMsg = g_strErrMsg + strLabel + '\n';
}

//... Affiche la pile des messages et vide la pile.
function Errors()
{
  if (g_strErrMsg != '') {
    alert(unescape(g_strErrMsg));
    g_strErrMsg = '';

    return true;
  }
  	
  return false;
}

//... Première partie d'un message d'erreur (exemple en français = "Le champs ").
var g_strMsgField = g_strMsgStart;

//... Pile des messages.
var g_strErrMsg = '';

//... Retourne un message d'erreur selon un code passé en paramètre.  Les variables contenant
//... les messages ont été initialisées via le fichier des messages selon la bonne langue 
//... (messages-fr.js ou message-en.js) 
function ReturnErrMsg(strMsgType, intNum)
{
  var strErrMsg;
  	
  switch (strMsgType)
  {
    case 'EMPTY':
      strErrMsg = g_strMsgEmpty;
      break;
    case 'CHECKED':
      strErrMsg = g_strMsgChecked;
      break;
    case 'CHECKBOX':
      strErrMsg = g_strMsgCheckBox;
      break;
    case 'NUM_INT':
      strErrMsg = g_strMsgNumInt;
      break;
    case 'NUM_CHAR_DENIED':
      strErrMsg = g_strMsgNumCharDenied;
      break;
    case 'NUM_DECIMAL_CHAR_NB':
      strErrMsg = g_strMsgNumDecimalCharNb;
      break;
    case 'NUM_DECIMAL_NB_AFTER':
      strErrMsg = g_strMsgNumDecimalNbAfter;
      break;
    case 'ZERO':
      strErrMsg = g_strMsgZero;
      break;
    case 'LESS_THAN':
      strErrMsg = g_strMsgLessThan;
      break;
    case 'POSTAL_CODE':
      strErrMsg = g_strMsgPostalCode;
      break;
    case 'E_MAIL':
      strErrMsg = g_strMsgEMail;
      break;
    case 'TELEPHONE':
      strErrMsg = g_strMsgTelephone;
      break;
    case 'DATE':
      strErrMsg = g_strMsgDate;
      break;
    case 'DATE_BEFORE':
      strErrMsg = g_strMsgDateBefore;
      break;
    case 'DATE_AFTER':
      strErrMsg = g_strMsgDateAfter;
      break;
    case 'LONG_ENOUGH':
      strErrMsg = g_strMsgLongEnough1 + intNum + g_strMsgLongEnough2;
      break;
  }
  	
  return(strErrMsg)
}

