/* Variable applicative indiquant que la classe KaInputField a été chargée */
_KaInputField_Loaded = true;

function KaInputField() {

  // Déclaration des champs
  this.fValue = null;
  this.fValueLength = null;
  this.fMinChars = null;
  this.fMaxChars = null;
  this.fMandatory = null;
  this.fCharsAllowed = null;
  this.fCharsDenied = null;
  this.fRegExpSyntax = null;
  this.fIsBlocking = null;


  /****************************************************************************************************
    this.setMinChars = function setMinChars(pMinChars)
    Méthode d'affectation du champ fMinChars
  */
  this.setMinChars = function setMinChars(/*Number*/pMinChars) {
    this.fMinChars = pMinChars;
  }


  /****************************************************************************************************
    this.setMaxChars = function setMaxChars(pMaxChars)
    Méthode d'affectation du champ fMaxChars
  */
  this.setMaxChars = function setMaxChars(/*Number*/pMaxChars) {
    this.fMaxChars = pMaxChars;
  }


  /****************************************************************************************************
    this.setMandatory = function setMandatory(pMandatory)
    Méthode d'affectation du champ fMandatory
  */
  this.setMandatory = function setMandatory(/*Boolean*/pMandatory) {
    this.fMandatory = pMandatory;
  }


  /****************************************************************************************************
    this.setCharsAllowed = function setCharsAllowed(pCharsAllowed)
    Méthode d'affectation du champ fCharsAllowed
  */
  this.setCharsAllowed = function setCharsAllowed(/*String*/pCharsAllowed) {
    this.fCharsAllowed = pCharsAllowed;
  }


  /****************************************************************************************************
    this.setCharsDenied = function setCharsDenied(pCharsDenied)
    Méthode d'affectation du champ fCharsDenied
  */
  this.setCharsDenied = function setCharsDenied(/*String*/pCharsDenied) {
    this.fCharsDenied = pCharsDenied;
  }


  /****************************************************************************************************
    this.setStringType = function setStringType(pStringType)
    Méthode d'affectation du champ fStringType
  */
  this.setStringType = function setStringType(/*String*/pStringType) {
    this.fMinChars = gStringType[pStringType]['fMinChars'];
    //this.fMaxChars = gStringType[pStringType]['fMaxChars'];
    this.fCharsAllowed = gStringType[pStringType]['fCharsAllowed'];
    this.fRegExpSyntax = gStringType[pStringType]['fStringType'];
  }


  /****************************************************************************************************
    this.setIsBlocking = function setIsBlocking(pIsBlocking)
    Méthode d'affectation du champ fIsBlocking
  */
  this.setIsBlocking = function setIsBlocking(/*Boolean*/pIsBlocking) {
    this.fIsBlocking = pIsBlocking;
  }


  /****************************************************************************************************
    this.getCharsAllowed = function getCharsAllowed()
    Méthode qui retourne les caractères autorisés (cad ceux qui sont autorisés et pas interdits = fCharsAllowed - fCharsDenied)
  */
  this.getCharsAllowed = function getCharsAllowed() {
    var vCharsAllowed = this.fCharsAllowed;
    var vCharsDenied = this.fCharsDenied;

    if (vCharsDenied != null) {
      for (var vId = 0; vId < vCharsDenied.length; vId++)
        vCharsAllowed = vCharsAllowed.replace(vCharsDenied.charAt(vId), '');
    }

    return vCharsAllowed;
  }


  // Initialisation de l'objet KaInputField
  this.init(arguments);
}

if (_KaField_Loaded) {
  /* Héritage de l'objet KaField */
  KaInputField.prototype = new KaField();
  //KaInputField.prototype.constructor = KaInputField;
  KaInputField.superclass = KaField.prototype;
}
else
  alert('** KaInputField : La classe KaField n\'est pas chargée !');


/*
  Constructeur de la classe KaInputField
*/
KaInputField.prototype.init = function(/*Array*/pArguments) {
  // Appel à la méthode de la super classe
  //KaInputField.superclass.init.call(this, pArguments);
  KaInputField_init(this, pArguments);
}


/*
  Méthode de DEBUG
*/
KaInputField.prototype.debug = function() {
  // Appel à la méthode de la super classe
  //KaInputField.superclass.debug.call(this);
  KaInputField_debug(this);
}


/*
  Méthode de vérification du champ
*/
KaInputField.prototype.check = function() {
  var vCheckOK = KaInputField_check(this);

  return vCheckOK;
}





/****************************************************************************************************
  function checkMinChars(pMinChars, pValueLength)
  Fonction de vérification du nombre minimum de caractères
  -> utilisée par la méthode check()
  En entrée :
    pMinChars = le nombre minimum de caractères prévus
    pValueLength = Le nom de caractères saisis
  En sortie :
    true si le nombre de caractères saisis est supérieur ou égal au nombre minimum de caractères prévus et false sinon
*/
function checkMinChars(/*Number*/pMinChars, /*Number*/pValueLength) {
  if ((pMinChars != null) && (pValueLength != null) && (pValueLength > 0)) {
    if ((pMinChars > 0) && (pValueLength >= pMinChars))
      return true;
    else
      return false;
  }
  return true;
}


/****************************************************************************************************
  function checkMaxChars(pMaxChars, pValueLength)
  Fonction de vérification du nombre maximum de caractères
  -> utilisée par la méthode check()
  En entrée :
    pMaxChars = le nombre maximum de caractères prévus
    pValueLength = Le nom de caractères saisis
  En sortie :
    true si le nombre de caractères saisis est supérieur ou égal au nombre maximum de caractères prévus et false sinon
*/
function checkMaxChars(/*Number*/pMaxChars, /*Number*/pValueLength) {
  if ((pMaxChars != null) && (pValueLength != null) && (pValueLength > 0)) {
    if ((pMaxChars > 0) && (pValueLength <= pMaxChars))
      return true;
    else
      return false;
  }
  return true;
}


/****************************************************************************************************
  function checkMandatory(pValueLength)
  Fonction de vérification du caractère obligatoire de la saisie
  -> utilisée par la méthode check()
  En entrée :
    pValueLength = Le nom de caractères saisis
  En sortie :
    true si une valeur a été saisie pour ce champ et false sinon
*/
function checkMandatory(/*Boolean*/pValueLength) {
  if ((pValueLength != null) && (pValueLength > 0))
    return true;
  else
    return false;
}


/****************************************************************************************************
  function checkCharsAllowed(pValue, pCharsAllowed)
  Cette fonction permet de tester que pValue ne contient bien que des caractères définis dans pCharsAllowed.
  En entrée : pValue = La String à tester
                   pCharsAllowed = une chaine de caractères qui définit les caractères autorisés pour la valeur du champ
                   (ex: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$&#@')
  En sortie : "true" si le champ ne contient que des caractères définis dans pCharsAllowed et "false" sinon

  Version				Date								Auteur					Navigateurs										Description des modifications
  ---------		 ----------------		-----------			--------------------------		--------------------------------------------------
    1.0 					19/09/2003 			OLD							IE5+ et Netscape6+				Code original
*/
function checkCharsAllowed(/*String*/pValue, /*String*/pCharsAllowed) {
  var vCharForbidden = null;

  if ((pValue != null) && (pCharsAllowed != null) && (typeof(pCharsAllowed) == 'string')) {
    var vCpt = 0;
    while ((vCharForbidden == null) && (vCpt < pValue.length)) {
      if (pCharsAllowed.indexOf(pValue.charAt(vCpt)) == -1)
        vCharForbidden = pValue.charAt(vCpt);
      vCpt++;
    }
  }

  return vCharForbidden;
}


/****************************************************************************************************
  function checkCharsDenied(pValue, pCharsDenied) {
  Cette fonction permet de tester que pValue ne contient pas de caractères définis dans pCharsDenied.
  En entrée : pValue = La String à tester
                   pCharsDenied = une chaine de caractères qui définit les caractères interdits pour la valeur du champ
                   (ex: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$&#@')
  En sortie : "true" si le champ ne contient pas de caractères définis dans pCharsDenied et "false" sinon

  Version				Date								Auteur					Navigateurs										Description des modifications
  ---------		 ----------------		-----------			--------------------------		--------------------------------------------------
    1.0 					19/09/2003 			OLD							IE5+ et Netscape6+				Code original
*/
function checkCharsDenied(/*String*/pValue, /*String*/pCharsDenied) {
  var vCharForbidden = null;

  if ((pValue != null) && (pCharsDenied != null) && (typeof(pCharsDenied) == 'string')) {
    var vCpt = 0;
    while ((vCharForbidden == null) && (vCpt < pValue.length)) {
      if (pCharsDenied.indexOf(pValue.charAt(vCpt)) != -1)
        vCharForbidden = pValue.charAt(vCpt);
      vCpt++;
    }
  }

  return vCharForbidden;
}



/****************************************************************************************************
 * Types de données gérés par l'objet KaInputField
 */
gStringType = new Array();

// Champ E-mail
gStringType['email'] = new Array();
gStringType['email']['fMinChars'] = 5;
gStringType['email']['fCharsAllowed'] = ALPHA_LOWER + DIGIT + MINUS_SIGN + UNDERSCORE + PERIOD + AT;
gStringType['email']['fStringType'] = '^[a-z0-9_\-]+([\.][a-z0-9_\-]+)*@[a-z0-9_\-]+([\.][a-z0-9_\-]+)*[\.][a-z]{2,6}$';
//gStringType['email']['fStringType'] = '/^[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]+(\.[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){2,63}$/i';
//gStringType['email']['fStringType'] = '^([a-z0-9_\-]|[A-Z0-9_\-])+([\.]([a-z0-9_\-]|[A-Z0-9_\-])+)*@([a-z0-9_\-]|[A-Z0-9_\-])+([\.]([a-z0-9_\-]|[A-Z0-9_\-])+)*[\.]([a-z]|[A-Z]){2,6}$';

// Champ Réel pour SOR (montant)
gStringType['realSOR'] = new Array();
gStringType['realSOR']['fMinChars'] = 0;
gStringType['realSOR']['fCharsAllowed'] = DIGIT + PLUS_SIGN + COMMA + SPACE;
gStringType['realSOR']['fStringType'] = '^[0-9]{0,3}( [0-9]{3})*(,[0-9]{0,2})?$';

// Champ Number pour SOR (kms)
gStringType['kms'] = new Array();
gStringType['kms']['fMinChars'] = 0;
gStringType['kms']['fMaxChars'] = 7;
gStringType['kms']['fCharsAllowed'] = DIGIT + SPACE;
gStringType['kms']['fStringType'] = '^[0-9]{0,3}([ ]?[0-9]{3})?$';

// Champ Immat pour SOR (Immatriculation)
gStringType['immat'] = new Array();
gStringType['immat']['fMinChars'] = 0;
gStringType['immat']['fMaxChars'] = 9;
gStringType['immat']['fCharsAllowed'] = DIGIT + ALPHA_UPPER;
gStringType['immat']['fStringType'] = '^[0-9]{2,4}[A-Z]{2,3}([013456789][0-9]|2[1-9]|2A|2B)$';

// Champ Numérique
gStringType['numeric'] = new Array();
gStringType['numeric']['fMinChars'] = 0;
//gStringType['numeric']['fMaxChars'] = 10;
gStringType['numeric']['fCharsAllowed'] = DIGIT ;



/****************************************************************************************************
  function KaInputField_init(pKaInputField, pArguments)
  Fonction d'inititialisation de l'object pKaInputField avec les paramètres pArguments
  -> utilisée par la méthode KaInputField.prototype.init()
  En entrée :
    pKaInputField = L'objet de type KaInputField à initialiser
    pArguments = Les paramètres pour initialisation
*/
function KaInputField_init(/*KaInputField*/pKaInputField, /*Array*/pArguments) {
  // Appel à la fonction de la super classe
  KaField_init(pKaInputField, pArguments);
}


/****************************************************************************************************
  function KaInputField_debug(pKaInputField)
  Fonction de debug qui affiche les champs et leurs valeurs de l'object pKaInputField
  -> utilisée par la méthode KaInputField.prototype.debug()
  En entrée :
    pKaInputField = L'objet de type KaInputField à initialiser
*/
function KaInputField_debug(/*KaInputField*/pKaInputField) {
  // Appel à la fonction de la super classe
  KaField_debug(pKaInputField);

  var vMessage = '';
  vMessage += 'Value=' + pKaInputField.fValue + LF;
  vMessage += 'ValueLength=' + pKaInputField.fValueLength + LF;
  vMessage += 'MinChars=' + pKaInputField.fMinChars + LF;
  vMessage += 'MaxChars=' + pKaInputField.fMaxChars + LF;
  vMessage += 'Mandatory=' + pKaInputField.fMandatory + LF;
  vMessage += 'CharsAllowed=' + pKaInputField.fCharsAllowed + LF;
  vMessage += 'CharsDenied=' + pKaInputField.fCharsDenied + LF;
  vMessage += 'StringType=' + pKaInputField.fRegExpSyntax + LF;
  vMessage += 'IsBlocking=' + pKaInputField.fIsBlocking + LF;
  alert(vMessage);
}


/****************************************************************************************************
  function KaInputField_check(pKaInputField)
  Fonction de vérification de la valeur fValue de l'object pKaInputField
  -> utilisée par la méthode KaInputField.prototype.check()
  En entrée :
    pKaInputField = L'objet de type KaInputField à vérifier
*/
function KaInputField_check(/*KaInputField*/pKaInputField) {
  var vCheckOK = true;

  // Affectation de la valeur
  pKaInputField.fValue = pKaInputField.fComponent.value;
  pKaInputField.fValueLength = pKaInputField.fValue.length;

  if ((vCheckOK) && (pKaInputField.fMandatory != null) && (pKaInputField.fMandatory)) {
    // Valeur à saisir obligatoirement
    if (!checkMandatory(pKaInputField.fValueLength)) {
      vCheckOK = false;
      pKaInputField.alertMessage('checkMandatory_0', LOCALE);
    }
  }

  if ((vCheckOK) && (pKaInputField.fMinChars != null) && (pKaInputField.fMinChars > 0)) {
    // Le nombre minimum de caractères à saisir est défini
    if (!checkMinChars(pKaInputField.fMinChars, pKaInputField.fValueLength)) {
      vCheckOK = false;
      pKaInputField.alertMessage('checkMinChars_0', LOCALE);
    }
  }

  if ((vCheckOK) && (pKaInputField.fMaxChars != null) && (pKaInputField.fMaxChars > 0)) {
    // Le nombre maximum de caractères à saisir est défini
    if (!checkMaxChars(pKaInputField.fMaxChars, pKaInputField.fValueLength)) {
      vCheckOK = false;
      pKaInputField.alertMessage('checkMaxChars_0', LOCALE);
    }
  }

  if ((vCheckOK) && (pKaInputField.fCharsAllowed != null) && (pKaInputField.fCharsAllowed.length > 0)) {
    // Les caractères autorisés sont définis
    var vCharForbidden = checkCharsAllowed(pKaInputField.fValue, pKaInputField.fCharsAllowed);
    if (vCharForbidden != null) {
      vCheckOK = false;
      if ((vCharForbidden == '\n') || (vCharForbidden == '\r'))
        pKaInputField.alertMessage('checkCharsAllowed_1', LOCALE);
      else
        pKaInputField.alertMessage('checkCharsAllowed_0', LOCALE, [vCharForbidden]);
    }
  }

  if ((vCheckOK) && (pKaInputField.fCharsDenied != null) && (pKaInputField.fCharsDenied.length > 0)) {
    // Les caractères interdits sont définis
    var vCharForbidden = checkCharsDenied(pKaInputField.fValue, pKaInputField.fCharsDenied);
    if (vCharForbidden != null) {
      vCheckOK = false;
      //pKaInputField.alertMessage('checkCharsDenied_0', LOCALE, [vCharForbidden]);
      pKaInputField.alertMessage('checkCharsDenied_0', LOCALE, [pKaInputField.getCharsAllowed()]);
    }
  }

  if ((vCheckOK) && (pKaInputField.fRegExpSyntax != null) && (pKaInputField.fValue.length > 0)) {
    // Expression régulière
    if (!new RegExp(pKaInputField.fRegExpSyntax).test(pKaInputField.fValue)) {
      vCheckOK = false;
      if ((pKaInputField.fIsBlocking != null) && (pKaInputField.fIsBlocking))
        vCheckOK = pKaInputField.warningMessage('checkStringType_0', LOCALE);
      else
        pKaInputField.alertMessage('checkStringType_0', LOCALE);
    }
  }

  if (!vCheckOK)
    pKaInputField.fComponent.focus();

  return vCheckOK;
}
