/************************************************/
/*                                              */
/*    Fichier javascript comportant les         */
/*        fonctions communes                    */
/*                                              */
/************************************************/
/************************************************/
/*  Fonctionnalités associé au fichier :        */
/************************************************/
/*  Vérification date-mail-numérique            */
/*  Coloriage                                   */
/*  Traitements de chaine                       */
/*  Gestion des DIVs                            */
/*  PopUp Générique                             */
/*  Aide en ligne                               */
/*  Recherche Code Article                      */
/*  Pénalités                                   */
/*  Main Courante                               */
/*  InfoBulle                                   */
/************************************************/
GL_N_x = 0;
GL_N_y = 0;
GL_N_opacite = 0;
GL_N_TempoOpacite = 2;
GL_N_Detail = 0;
var timerOpacite = null;
/* Variable globale pour contrôle des dates */
var GL_A_DATE= '';
var e;

 if(navigator.appName.substring(0,3) == "Net")
 {
	//e=document.captureEvents(Event.MOUSEMOVE);
   document.addEventListener("mousemove",RecupMousePos,false);
   document.addEventListener("mouseover",RecupMousePos,false);
}
document.onmousemove = RecupMousePos;



function RecupMousePos(e)
{
   if (navigator.appName == "Netscape")
   {
      GL_N_x = e.pageX ;
      GL_N_y = e.pageY ;
   }
   else
   {
      GL_N_x = window.event.x + document.documentElement.scrollLeft;
      GL_N_y = window.event.y + document.documentElement.scrollTop;
   }
   
}



/************************************************/
/*     Controles de validité des saisies        */
/************************************************/

/* vérification d'une date */
function isDate(d,StringAllowed)
{
  /* Cette fonction permet de vérifier la validité d'une date au format jj/mm/aa ou jj/mm/aaaa */
  GL_A_DATE ='';
  
  if (d == "") /* si la variable est vide on retourne faux */
    return false;

  e = new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/([0-9]{2}|[0-9]{4})$");

  if (!e.test(d)) /* On teste l'expression régulière pour valider la forme de la date */
  {
     GL_A_DATE = d;
     return StringAllowed; /* Si pas bon et que l'on tolère les chaines, il peut s'agir d'une date texte (fin mai 2005 par ex) */    
  }    
  else
  {
  /* On sépare la date en 3 variables pour vérification, parseInt() converti du texte en entier */
  j = parseInt(d.split("/")[0], 10); // jour
  m = parseInt(d.split("/")[1], 10); // mois
  a = parseInt(d.split("/")[2], 10); // année

  /* Si l'année n'est composée que de 2 chiffres on complète automatiquement */
  if (a < 1000) {
    if (a < 89)  a+=2000; /* Si a < 89 alors on ajoute 2000 sinon on ajoute 1900 */
    else a+=1900;
  }

  /* Définition du dernier jour de février
     Année bissextile si annnée divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400 */
  if (a%4 == 0 && a%100 !=0 || a%400 == 0) fev = 29;
  else fev = 28;

  /*  Nombre de jours pour chaque mois */
  nbJours = new Array(31,fev,31,30,31,30,31,31,30,31,30,31);

  /* Mise à jour de la variable globale date au format JJ/MM/AAAA */
  if(j <= 9)
    j= '0'+j;
  if(m <= 9)
    m = '0'+m;
   
  GL_A_DATE = j+'/'+m+'/'+a;
  /* Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux */
  return ( m >= 1 && m <=12 && j >= 1 && j <= nbJours[m-1] );
  }
}

function CalcAn(dat)
{
   var an = parseFloat(dat.substring(6,10));
   if (an < 70)
      an += 2000;
   else
   if(an < 100)
      an += 1900;
   return an;
}

function comp_date(date1,date2)
{
   return (
      (CalcAn(date1) - CalcAn(date2))*10000 +
      (parseFloat(date1.substring(3,5)) - parseFloat(date2.substring(3,5)))*100 +
      (parseFloat(date1.substring(0,2)) - parseFloat(date2.substring(0,2)))
   );
}

/* Vérification d'une chaine numérique */
function isNum(obj,masque) {
  var ch = obj.value
  var tmp = ""
  var j = 0
  ch.toString()

  if (window.event.keyCode != 37 && window.event.keyCode != 39 && window.event.type != "keydown" && window.event.keyCode != 8 && window.event.keyCode != 46) {
    if (window.event.type == "keyup") {
      for (i=0; i<ch.length; i++) {
        if (!isNaN(ch.charAt(i)) && ch.charAt(i) != " ") { tmp += ch.charAt(i) }
      }

      ch = ""

      for (i=0; i<masque.length; i++) {
        if (masque.charAt(i)  == "0") {
          if (tmp.charAt(j) != "" ) {
            ch += tmp.charAt(j)
            j++
          }
          else { ch += " " }
        }
        else { ch += masque.charAt(i) }
      }
    }

    obj.value = ch
  }
}

/* Vérification d'un Email */
function isEmail(obj)
{
   var varp = obj.value;
   if (varp.indexOf("@")==-1)
   {
      alert('Une adresse E-mail doit contenir un \'@\'');
      return false;
   }
   if (varp.indexOf(".")==-1)
   {
      alert('Une adresse E-mail doit contenir au moins un \'.\'');
      return false;
   }
   if ((varp.indexOf(" ")!=-1) ||
   (varp.indexOf("&")!=-1)  || (varp.indexOf("é")!=-1)||
   (varp.indexOf("è")!=-1)  ||
   (varp.indexOf("¨")!=-1)  || (varp.indexOf(";")!=-1)||
   (varp.indexOf("ç")!=-1)  ||
   (varp.indexOf("|")!=-1)  || (varp.indexOf("°")!=-1)||
   (varp.indexOf("à")!=-1)  ||
   (varp.indexOf("¤")!=-1)  || (varp.indexOf("ê")!=-1)||
   (varp.indexOf("%")!=-1)  ||
   (varp.indexOf("?")!=-1)  || (varp.indexOf("!")!=-1)||
   (varp.indexOf("§")!=-1)  ||
   (varp.indexOf(":")!=-1)  || (varp.indexOf("/")!=-1)||
   (varp.indexOf("²")!=-1)  ||
   (varp.indexOf("{")!=-1)  || (varp.indexOf("}")!=-1)||
   (varp.indexOf("(")!=-1)  ||
   (varp.indexOf("[")!=-1)  || (varp.indexOf("]")!=-1)||
   (varp.indexOf(")")!=-1)  ||
   (varp.indexOf("`")!=-1)  || (varp.indexOf("=")!=-1)||
   (varp.indexOf("+")!=-1)  ||
   (varp.indexOf("<")!=-1)  || (varp.indexOf(">")!=-1)||
   (varp.indexOf("~")!=-1)  ||
   (varp.indexOf("\\")!=-1) || (varp.indexOf("#")!=-1)||
   (varp.indexOf("'")!=-1)  ||
   (varp.indexOf("\"")!=-1) || (varp.indexOf("*")!=-1)||
   (varp.indexOf("^")!=-1))
   {
      alert('Une adresse E-mail ne doit pas contenir de caractères spéciaux');
      return false;
   }
   return true;
}

/************************************************/
/*             Coloriage                        */
/************************************************/

function ligneParam(NumLigne,CleFichier)
{
   if(top.frames[0].GL_A_LigneChoisie != 0)
      eval('document.getElementById("TR'+ top.frames[0].GL_A_LigneChoisie +'").className = "FOND_Données"');
   eval('document.getElementById("TR'+ NumLigne +'").className = "LigneSelect"');
   top.frames[0].GL_A_LigneChoisie = NumLigne;
   document.getElementById("HI_CleFic").value = CleFichier;
}

/************************************************/
/*            Gestion des DIVs                  */
/************************************************/

function ToggleBlock(blk1,blk2)
{
   if(document.getElementById(blk1).style.display == '')
       document.getElementById(blk1).style.display = 'none';
   else
       document.getElementById(blk1).style.display = '';
       
   if(document.getElementById(blk2).style.display == '')
       document.getElementById(blk2).style.display = 'none';
   else
       document.getElementById(blk2).style.display = '';
}

function Toggle_div(div)
{
   if (document.getElementById(div).style.display == '')
      document.getElementById(div).style.display = 'none';
   else
      document.getElementById(div).style.display = '';
}

/************************************************/
/*              PopUp Générique                 */
/************************************************/

function FermeturePopUp()
{
   parent.parent.opener.location.reload();
   parent.parent.window.close();
}

function Hide_PopUp_Onglet()
{
   top.frames[0].document.getElementById("DV_Detail").style.display = 'none';
   top.frames[0].document.getElementById("DV_Correspondance").style.display ='none';
   top.frames[0].document.getElementById("DV_PJ").style.display ='none';
}

/************************************************/
/*          Aide en ligne                       */
/************************************************/

function op() { //fonction utilisée pour les dossiers qui n'ont pas de liens
}

function HELP_open(Frameset)
{
   window.open('/qualite/'+ Frameset,'HELP','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=50,top=20,width=820,height=600');
}

function Support()
{
   alert("En cas de difficulté d'utilisation de cette application, merci de contacter le support client\n\n"+
         "- au  08 00 04 20 84  du Lundi au Vendredi de 7h à 19h et le Samedi de 8h à 13h,\n\n"+
         "- au  03 20 47 09 90  (numéro d'urgence) tous les jours de 7h à 22h.\n");
}

/************************************************/
/*          Recherche Code Ingrédient           */
/************************************************/

function Code_Ingred(CodeCaract,VarCodeIng,VarLibIng,BtnEffacer,Refresh,User)
{
  window.open('/Magic94Scripts/mgrqispi94.dll?appname=qualiteWeb&prgname=PAR_appel_find_ingred&Arguments=-N'+CodeCaract+',-A'+VarCodeIng+',-A'+VarLibIng+',-A'+BtnEffacer+',-A'+Refresh+',-A'+User,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=50,top=20,width=520,height=400');
}

/************************************************/
/*          Recherche Code Article              */
/************************************************/

function Code_Art(PgmToCall,VarCode,VarLib,IgnorerArticlesLocaux)
{
   window.open('/Magic94Scripts/mgrqispi94.dll?appname=qualiteWeb&prgname=PAR_appel_cde_art&Arguments=-A'+PgmToCall+',-A'+VarCode+',-A'+VarLib+',-L'+IgnorerArticlesLocaux,'PopUp_article','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=50,top=20,width=600,height=400');
}

function Choose_Art(Code,VarCode,Lib,VarLib)
{
 
   /* mise à jour du code et libellé article dans la feuille appelante */
   opener.document.getElementById(VarCode).value = Code;
   if (VarLib != '')
      opener.document.getElementById(VarLib).value = Lib;

   /* si besoin, appel d'un programme magic pour la mise à jour globale de la feuille */
   if(document.getElementById('PgmToCall').value != '')
   {
      opener.document.forms[0].prgname.value = document.getElementById('PgmToCall').value;
      opener.document.forms[0].submit();
   }

   window.close();
}




/************************************************/
/*          Recherche Code Article RQA          */
/************************************************/

function RQA_Code_Art(PgmToCall,VarCode,VarLib,IgnorerArticlesLocaux,user)
{
   window.open('/Magic94Scripts/mgrqispi94.dll?appname=qualiteWeb&prgname=PAR_RQA_appel_cde_art&Arguments=-A'+PgmToCall+',-A'+VarCode+',-A'+VarLib+',-L'+IgnorerArticlesLocaux+',-A'+user,'PopUp_article','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=50,top=20,width=600,height=400');
}

function RQA_Choose_Art(Code,VarCode,Lib,VarLib)
{
 
   /* mise à jour du code et libellé article dans la feuille appelante */
   opener.document.getElementById(VarCode).value = Code;
   if (VarLib != '')
      opener.document.getElementById(VarLib).value = Lib;

   /* si besoin, appel d'un programme magic pour la mise à jour globale de la feuille */
   if(document.getElementById('PgmToCall').value != '')
   {
      opener.document.forms[0].prgname.value = document.getElementById('PgmToCall').value;
      opener.document.forms[0].submit();
   }

   window.close();
}















/************************************************/
/*            Mise à jour d'une page            */
/************************************************/

function Find(PgmToCall)
{
   /* on pointe sur le pgm magic pour la mise à jour de la feuille */
   document.forms[0].prgname.value = PgmToCall;

   /* appel du programme */
   document.forms[0].submit();
}

/************************************************/
/*              InfoBulle                       */
/************************************************/

function InfoBulle(Div)
{
	
	if(document.getElementById('SL_Copie')!=null)
   {
		var taille=150;
		document.getElementById('SL_Copie').style.display ='none';
		document.getElementById('DV_SELCOPIE').style.height=taille;
   }


   GL_N_x = '200px';
   eval('document.getElementById("'+Div+'").style.top  = GL_N_y');
   eval('document.getElementById("'+Div+'").style.left = GL_N_x');
   document.getElementById(Div).style.display ='';
}

function Hide_InfoBulle(Div)
{
   
   if(document.getElementById('SL_Copie')!=null)
   {
		document.getElementById('SL_Copie').style.display ='';
   }
   
   document.getElementById(Div).style.display ='none';
   
}

/************************************************/
/*           Appel du calendrier                */
/************************************************/

function AfficheCalendrier(Date,Top,Left)
{
   document.getElementById("calend").style.top = Top+'px';
   document.getElementById("calend").style.left = Left+'px';
   eval("CL.init('calend',document.getElementById('"+Date+"'));");
   CL.isDragable(false);
   CL.show();
}

/************************************************/
/*                 Filtres                      */
/************************************************/

function CritereFiltre(Critere,Univers)
{
   window.open('/Magic94Scripts/mgrqispi94.dll?appname=qualiteWeb&prgname=PAR_CritereFiltre&Arguments=-A'+Critere+',-N'+Univers,'FEN_Critere','channelmode=no,toolbar=no,location=no,menubar=no,Left=350,top=150,width=300,height=350');
}

function SupFiltre(Critere)
{
   eval("document.getElementById('HI_"+Critere+"')").value = '';
   eval("document.getElementById('TA_"+Critere+"')").value = '';
}

/************************************************/
/*        Correspondance - PJ                   */
/************************************************/

function Correspondance()
{
   document.images["IM_1"].src='/qualite/image/onglet/onglet_inactif_fiche.gif';
   document.images["IM_2"].src='/qualite/image/onglet/onglet_actif_messages.gif';
   document.images["IM_3"].src='/qualite/image/onglet/onglet_inactif_pj.gif';
   window.open(parent.opener.top.frames[0].GL_A_URL+'PAR_correspondance&Arguments=-N'+parent.opener.top.frames[0].GL_N_Dossier+',-A'+parent.opener.top.frames[0].GL_A_User+',-N'+parent.opener.top.frames[0].GL_N_Session,'FR_PopUpPrincipal','');
}

function Open_PJ(Num)
{
   window.open('/Magic94Scripts/mgrqispi94.dll?appname=qualiteWeb&prgname=PAR_OuvrirPJ&Arguments=-N'+Num,'','toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,left=150,top=10,width=648,height=891');
}

function Piece_Jointe()
{
   document.images["IM_1"].src='/qualite/image/onglet/onglet_inactif_fiche.gif';
   document.images["IM_2"].src='/qualite/image/onglet/onglet_inactif_messages.gif';
   document.images["IM_3"].src='/qualite/image/onglet/onglet_actif_pj.gif';
   window.open(parent.opener.top.frames[0].GL_A_URL+'PAR_piece_jointe&Arguments=-N'+parent.opener.top.frames[0].GL_N_Dossier+',-A'+parent.opener.top.frames[0].GL_A_User,'FR_PopUpPrincipal','');
}

function PieceJointe()
{
   /* Masque le lien */
   document.getElementById("DV_LienPJ").style.display = 'none';
   /* Ouvre le frame utilisé pour l'ajout de pièces jointes*/
   document.getElementById("DV_PJ").style.display = '';
   window.open(parent.opener.top.frames[0].GL_A_URL+'PAR_liste_PJ&Arguments=-A'+document.forms[0].HI_ListePieceJointe.value+',-Lfalse,-N'+document.forms[0].HI_Session.value,'FR_PJ','');
}

function NomPJ()
{
   var Chaine = document.forms[0].FL_fic.value;
   var DernierePosition = Chaine.lastIndexOf('\\');
   document.forms[0].HI_NomPJ.value = Chaine.substr(DernierePosition+1,Chaine.length-1);
}


function NomPJ_par(Chaine)
{
   var DernierePosition = Chaine.lastIndexOf('\\');
   return(Chaine.substr(DernierePosition+1,Chaine.length-1));
}


function SUPPRIMERNomPJ2(Chaine)
{
   var DernierePosition = Chaine.lastIndexOf('\\');
   return(Chaine.substr(DernierePosition+1,Chaine.length-1));	
}


function AppelDocument(RefDocument,Instance,Modifiable)
{
   window.open('/Magic94Scripts/mgrqispi94.dll?appname=qualiteWeb&prgname=PAR_AffDocument&Arguments=-A'+RefDocument+',-N'+Instance+',-N1,-N'+Modifiable,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,left=150,top=10,width=630,height=891');
}

function DisplayProgressif(DVToDisp)
{
   if(document.all)
   {
       // Augmente l'opacité du MenuAction
       GL_N_opacite += 5;
       document.getElementById(DVToDisp).style.filter = "alpha(opacity = " + GL_N_opacite + ")";

       // Le MenuAction n'est pas entièrement opaque
       if(GL_N_opacite < 100 )
          timerOpacite = setTimeout("DisplayProgressif('"+DVToDisp+"')", GL_N_TempoOpacite);
   }
   // Le navigateur est Mozilla
   else
   {
       // Augmente l'opacité du MenuAction
       GL_N_opacite += 0.05;
       document.getElementById(DVToDisp).style.opacity = GL_N_opacite;

       // Le MenuAction n'est pas entièrement opaque
       if(GL_N_opacite < 1 )
          timerOpacite = setTimeout("DisplayProgressif('"+DVToDisp+"')", GL_N_TempoOpacite);
   }
}

function BuildListe(Source,Destination)
{
   for(i=0;i<=document.getElementById(Source).length-1;i++)
     if(document.getElementById(Source).options[i].selected)
        document.getElementById(Destination).value += document.getElementById(Source).options[i].value + '|';
}

// Sélection d'une liste de restaurants
function SelectRestos(VarCible,ListeCourante)
{
   window.open('/Magic94Scripts/mgrqispi94.dll?appname=qualiteWeb&prgname=PAR_ListeRestau&Arguments=-A'+VarCible+',-A'+ListeCourante,'','toolbar=no,location=no,menubar=no,Left=280,top=70,width=400,height=550');
}



function trim (myString) 
{ 
return myString.replace(/^\s+/g,'').replace(/\s+$/g,'') 
}