// <!--
// création le 13/11/2001
// derniere mise a jour le 05/11/2004, 14h30
// 05/11/2004 : CtrlDateFr(obj) avec trois para.
// 04/03/2004 : CtrlDateFr(obj)
// 20/02/2004 : TraitUpperNoAccent.
// 04/06/2003 : maj expression reg pour IE6
// 15/12/2001 : gestion popup pour aide à la saisie

// function ouvrewindow(url)
// function InArray(aTab, val)
// -->

function ouvrewindow(url)
{
   if (document.forms[0].name == "form_gest")
   { url = url + "&id_tour=" + document.forms[0].id_tour.value; }
   var pop = window.open(url,"Informations","location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width=450,height=100");
   if (navigator.appVersion.substring(1,0)>="3")
   {
      if (pop.focus != null)
      { pop.focus(); }
   }
   return (pop);
}

function ouvrewindowmenu(url)
{
   var pop = window.open(url,"Informations","location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,copyhistory=0,width=500,height=500");
   if (navigator.appVersion.substring(1,0)>="3")
   {
      if (pop.focus != null)
      { pop.focus(); }
   }
   return (pop);
}

function MsgTest()
{
   alert("Message de test pour form_lib.js !");
}

function InArray(aTab, val)
{
   posi = -1;
   for (n = 0; n < aTab.length; n ++)
   {
      if (val == aTab[n])
      { posi = n;}
   }
   return posi;
}

function CtrlNonVide(frm,nom_var,libel)
{
   var texte = eval('frm.'+nom_var+'.value');
   if (texte.length == 0)
   {
      cTemp = nom_var.substr(2);
      if (cTemp.indexOf("__") >= 0)
      { cTemp = cTemp.substr(0,cTemp.lastIndexOf("__")); }
      if (arguments.length >= 3)
      { cTemp = libel; }
      //var obj = eval('frm.'+nom_var);
      //obj.focus();
      alert("Attention : La variable '"+cTemp+"' doit être non vide !!");
   }
   return false;
}

function CtrlFormat(frm,nom_var,typ_format)
{
   var texte = eval('frm.'+nom_var+'.value');
   var motif = "", msg = "";

   cTemp = nom_var.substr(2);
   if (cTemp.indexOf("__") >= 0)
   { cTemp = cTemp.substr(0,cTemp.lastIndexOf("__")); }

   if (texte.length > 0)
   {
      if (typ_format == 1)
      {
         // La zone doit etre numerique
         motif = "(^[0-9]*$|^[0-9]+\.?[0-9]*$)";
         msg = "Attention : La variable '"+cTemp+"' doit être numérique !!";
      }
      if (typ_format == 2)
      {
         // La zone doit etre alphabetique
         motif = "^[a-zA-Zéèàçù\'\" ]*$";
         msg = "Attention : La variable '"+cTemp+"' doit être alphabétique !!";
      }
      if (typ_format == 3)
      {
         // La zone doit etre alphanumerique
         motif = "^[0-9a-zA-Zéèàçù\'\" ]*$";
         msg = "Attention : La variable '"+cTemp+"' doit être alphanumérique !!";
      }
      if (motif != "")
      {
         regx = new RegExp(motif,"g");
         regx.compile(motif,"g");
         if ( !regx.test(texte) )
         {
            alert(msg);
            //var obj = eval('frm.'+nom_var);
            //obj.focus();
         }
      }
   }
   return false;
}

function CtrlFormatSpecial(frm,nom_var,format,txt_format)
{
   var texte = eval('frm.'+nom_var+'.value');

   cTemp = nom_var.substr(2);
   if (cTemp.indexOf("__") >= 0)
   { cTemp = cTemp.substr(0,cTemp.lastIndexOf("__")); }

   msg_format = format;
   if (txt_format.length > 0)
   { msg_format = txt_format; }
   if (texte.length > 0)
   {
      regx = new RegExp(format,"g");
      regx.compile(format,"g");
      if ( !regx.test(texte) )
      { alert("Attention : La variable '"+cTemp+"' ne correspond pas\n au format "+msg_format+" !!"); }
   }
   return false;
}

function CtrlSepa(frm,nom_var)
{
   var texte = eval('frm.'+nom_var+'.value');

   cTemp = nom_var.substr(2);
   if (cTemp.indexOf("__") >= 0)
   { cTemp = cTemp.substr(0,cTemp.lastIndexOf("__")); }

   if (texte.length > 0)
   {
      if (texte.indexOf(sepa_val) >= 0)
      {
         alert("Attention : La variable '"+cTemp+"' (="+texte+")\ncontient "+sepa_val+" !!");
         oObj = eval('frm.'+nom_var);
         oObj.value = oObj.defaultValue;
      }
   }
   return false;
}

function TraitUpperNoAccent(frm,nom_var)
{
   var texte = eval('frm.'+nom_var+'.value');
   if (texte.length > 0)
   {
      oObj = eval('frm.'+nom_var);
      // Passe en majuscule (moins de cas au remplacement)
      texte = texte.toUpperCase();

      regx = new RegExp("(À|Á|Â|Ã|Ä|Å)","g");
      // Voyelles :
      regx.compile("(À|Á|Â|Ã|Ä|Å)","g");
      texte = texte.replace(regx,"A");
      regx.compile("(È|É|Ê|Ë)","g");
      texte = texte.replace(regx,"E");
      regx.compile("(Ò|Ó|Ô|Õ|Ö|Ø)","g");
      texte = texte.replace(regx,"O");
      regx.compile("(Ì|Í|Î|Ï)","g");
      texte = texte.replace(regx,"I");
      regx.compile("(Ù|Ú|Û|Ü)","g");
      texte = texte.replace(regx,"U");
      // Consonnes :
      regx.compile("Ç","g");
      texte = texte.replace(regx,"C");
      regx.compile("Ñ","g");
      texte = texte.replace(regx,"N");

      oObj.value = texte;
   }
   return false;
}

function TraitUpper(frm,nom_var)
{
   var texte = eval('frm.'+nom_var+'.value');
   if (texte.length > 0)
   {
      oObj = eval('frm.'+nom_var);
      oObj.value = texte.toUpperCase();
   }
   return false;
}

function TraitLower(frm,nom_var)
{
   var texte = eval('frm.'+nom_var+'.value');
   if (texte.length > 0)
   {
      oObj = eval('frm.'+nom_var);
      oObj.value = texte.toLowerCase();
   }
   return false;
}

function StrZero(frm,nom_var,nlong)
{
   var oZone = eval('frm.'+nom_var);
   var texte = oZone.value;
   var complement = '';
   if (nlong > 0)
   {
      for(i = 0; i < nlong; i ++)
      { complement += '0'; }
      oZone.value = complement.substr(0, nlong - texte.length) + texte;
   }
   return false;
}

function Longueur(frm,nom_var,nlong)
{
   var oZone = eval('frm.'+nom_var);
   var texte = oZone.value;
   var complement = '';
   if (nlong > 0)
   {
      for (i = 0; i < nlong; i ++)
      { complement += ' '; }
      oZone.value = texte + complement.substr(0, nlong - texte.length);
   }
   return false;
}

function CtrlDateFr(frm,nom_var,aff)
{
var val = "", regx, obj_date;

   result = false;

   if (arguments.length <= 2)
   { aff = 1; }

   cTemp = nom_var.substr(2);
   if (cTemp.indexOf("__") >= 0)
   { cTemp = cTemp.substr(0,cTemp.lastIndexOf("__")); }

   var obj = eval('frm.'+nom_var);
   val = obj.value;
   if (val.length > 0)
   {
      if (val.length < 8)
      {
         if (aff == 1)
         { alert("Attention : Ne correspond pas au format !"); }
      }
      else
      {
         regx = new RegExp("^[0-3]?[0-9]{1}/[0-1]?[0-9]{1}/[0-9]{4}","gi");
         //regx.exec(val);
         regx.compile("^[0-3]?[0-9]{1}/[0-1]?[0-9]{1}/[0-9]{4}","gi");
         if (! regx.test(val) )
         {
            if (aff == 1)
            { alert("Attention : Date incorrecte ou format non respecté !"); }
         }
         else
         {
            result = true;
            if (arguments.length >= 2)
            {
               // Formatage de la date
               aTemp = val.split("/",3);
               obj_date = new Date();
               obj_date.setFullYear((1*aTemp[2]),(aTemp[1]-1),(1*aTemp[0]));
               if (obj_date.getDate() > 9)
               { val = obj_date.getDate(); }
               else
               { val = "0" + obj_date.getDate(); }
               if (obj_date.getMonth() >= 9)
               { val += "/" + (obj_date.getMonth()+1); }
               else
               { val += "/0" + (obj_date.getMonth()+1); }
               obj.value = val + "/" + obj_date.getFullYear();
            }
         }
      }
   }
   else
   { result = true; }
   return (result);
}
