//Dernière modification le 29/10/2004.

/* Fonctions présentes dans ce fichier:
   f_autoTab(aField,aMaxlength, aEvent, aNext)
*/


// ---------------------------------------
// f_autoTab(aField,aMaxlength, aEvent, aNext)
// ---------------------------------------
// PURPOSE : Switch from a field to another when the maxlength is reached
// IN : object field - aField : field containing the value to test for the length
//      number - aMaxlength : length max of the value in the field
//      event - aEvent : last event that occurs in the field
//      object field - aNext : field to switch to when the maxlength is reached in aField

//      Keycodes used in tab allowedKeyCodes
//         0  : null
//         8  : backspace
//         9  : tab
//         16 : shift tab
//         17 : ctrl
//         18 : alt
//         35 : end
//         36 : home
//         37 : left arrow
//         38 : up arrow
//         39 : right arrow
//         40 : down arrow
//         45 : insert key
//         46 : delete key

function f_autoTab(aField,aMaxlength, aEvent, aNext) {
  var isNetscape = (navigator.appName.indexOf("Netscape")!=-1);

  var keyCode = (isNetscape) ? aEvent.which : aEvent.keyCode;
  var allowedKeyCodes = [0,8,9,16,17,18,35,36,37,38,39,40,45,46];

  if(aField.value.length >= aMaxlength && !f_isAllowedKeyCode(allowedKeyCodes,keyCode)) {
    aNext.focus();
    aField.value = aField.value.substr(0, aMaxlength);
  }

  function f_isAllowedKeyCode(aTabAllowed, aKey) {
    var found = false;
    var cpt = 0;

    while(!found && cpt < aTabAllowed.length){
      if(aTabAllowed[cpt] == aKey){
        found = true;
      } else {
        cpt++;
      }
    }
    return found;
  }

}