function hiddeBox(winGris, winFenetre) { // effacement du container
   if (winGris) {
      if (winFenetre) {
         winGris.removeChild(winFenetre);
      }
      if(document.getElementById("ajaxwin2").hasChildNodes()) document.getElementById("ajaxwin2").removeChild(winGris);
      if(document.getElementById("ajaxwin").hasChildNodes()) document.getElementById("ajaxwin").removeChild(winGris);
   } 
   else
   {
      if(document.getElementById("ajaxwin2").hasChildNodes()) document.getElementById("ajaxwin2").removeChild(winFenetre);
      if(document.getElementById("ajaxwin").hasChildNodes()) document.getElementById("ajaxwin").removeChild(winFenetre);
   }
}

function hiddeBox2(conti,win) { // effacement du container
   var winFenetre = document.getElementById('gris');
   if(document.getElementById(conti).hasChildNodes()) {
      document.getElementById(conti).removeChild(winFenetre);
   }
}


function hiddeImage(win) { // effacement du container
   a=document.getElementById(win);
   document.getElementById("ajaxwin2").removeChild(a);
}

function modalDelete() {
   document.getElementById("delete").value = "ok";
   var a=document.getElementsByTagName("form").item(0);
   a.submit();
   return true;
}

function modalDeleteGrid() {
   var a=document.getElementsByTagName("form").item(0);
   a.submit();
   return;
}

function modalValidate() {
   document.getElementById("valid").value = "ok";
   document.getElementById("draft").value = "0"; // mode brouillon désactivé
   var a = document.getElementsByTagName("form").item(0);
   a.submit();
   return true;
}

function modalDraft() {
   document.getElementById("valid").value = "ok";
   document.getElementById("draft").value = "1"; // mode brouillon activé
   var a=document.getElementsByTagName("form").item(0);
   a.submit();
   return true;
}

var fencurIE6 = 0;

// Affichage d'une image
function imageDisplay(contid,winid,srcimage,idimage) {
   var ajaxwin = document.getElementById(contid);
   var image = document.getElementById(idimage);
   var fenetre = document.createElement("div");

   // Création de l'image
   var imageNode = document.createElement("img");
   imageNode.src = srcimage;

   // dimensions de l'écran
   var larg_screen = screen.width;
   var haut_screen = screen.height;

   // Position de l'icone
   var posimage = getPosition(image);
   var leftim = posimage.x;
   var topim = posimage.y ;
   //var widthim = imageNode.width;
   //var heightim = imageNode.height;

   var rapport = imageNode.width / imageNode.height;
   if(imageNode.height > 300) {
      imageNode.height = 300;
      imageNode.style.height = "300px";
      imageNode.width = 300 * rapport;
      imageNode.style.width = (300 * rapport)+"px";
   }

   // positionnement de l'image en vertical
   var left = leftim + 30;

   if((left + imageNode.width) > 980) left = leftim - imageNode.width - 180;

   // positionnement de l'image en horizontal
   var gap = haut_screen - imageNode.height;
   var top = 0;
   if(gap >= 0) top = gap / 2;
 
   fenetre.id = winid;
   fenetre.style.position="fixed";
   fenetre.style.left=left+"px";
   fenetre.style.top=top+"px";

   fenetre.appendChild(imageNode)
   ajaxwin.appendChild(fenetre);
}



// Affichage d'une fenêtre générique couvrant tous les cas d'utilisation
// contid : container supérieur dans lequel se crée la boite
// winid : nom de la fenêtre
// left, top : position à l'écran
// width, height, dimensions extérieures de la boite
// titreboite : titre de la fenêtre
// textequote : texte entant dans le corps de la boite
// bk : si TRUE écran grisé
// modal :
// flag :
// commande:
// action:
function displayBox(contid,winid,left,top,width,height,titreboite,textequote,bk,modal,flag,commande, action) {
   // Test si boîte déjà ouverte
   // Si cette fenêtre existe déjà on en ouvre pas une nouvelle...
   if (document.getElementById("insertion"+winid)) return;

   // Traitement des quotes du texte
   var textea = textequote.replace(/%quote/g,"'");
   var texte = textea.replace(/%guill/g,'"');

   // Passage des hauteurs et largeurs de base
   initDim(height,width);

   // Container supérieur AJAXWIN2 ----------------------------------------------------------------
   var ajaxwin = document.getElementById(contid);

   // Container GRIS ------------------------------------------------------------------------------
   var gris = document.getElementById('gris');
   if (!gris) {
      var gris = document.createElement("div");
      gris.id = "gris";
      gris.className = "greybox";
   }
   // Grisé derrière la boîte et sur toute la surface de l'écran'
   if(bk==1) {
      gris.style.background = "url(fileadmin/templates/img/fond01.png) repeat left top";
      gris.style.width = screen.width + "px";
      gris.style.height = screen.height + "px";
   }


   // Container FENETRE ---------------------------------------------------------------------------
   var fenetre = document.createElement("div");
   fenetre.id = winid;
   fenetre.style.left=left+"px";
   fenetre.style.top=top+"px";
   fenetre.className = "genericbox";

   // ===================== DEBUT
   // MPS le 16/10/09: Le redimensionnment automatique de la fenêtre est 
   // temporairement désactivé, pour permettre d'afficher l'aide avec une largeur fixe.
   // Se pourrait être un paramètre par exemple ...
   if(isIE7 || isIE6) {
      fenetre.style.width=width+"px";
      fenetre.style.height=height+"px";
   }
   else { // pour tous les vrais navigateurs
      //if(fenetre.offsetWidth > width && width !=0) fenetre.style.width=width+"px";
      fenetre.style.width=width+"px";
      if(fenetre.offsetHeight > height && height !=0) fenetre.style.height=height+"px";
   }
   // ====================== FIN

   // Positionnement fenêtre pour IE6, pas de déplacement ni redimensionement
   if(isIE6) {
      ajaxwin.position="absolute";

      if(fencurIE6 == 0) {
         var rt = document.getElementById('ajaxwin2');
         var fencur = getPosition(rt);
         fencurIE6 = fencur.y;
      }
      ajaxwin.style.top=((fencurIE6*-1)+200)+"px";
      var r = (screen.width - width)/2;
      ajaxwin.style.left=r.toString() + "px";
      gris.style.width="1px";
      gris.style.height="1px";
   }


   // Création du tableau titre/image -----------------------------------------
   var boxtitle = document.createElement("table");
   boxtitle.id = "boxtitle";
   boxtitle.style.width = "100%";
   boxtitle.style.height = "20px";
   var tbody = document.createElement("tbody");
   var lignetitre = document.createElement("tr");
   var titre = document.createElement("th");
   titre.className = "genericboxtitle";
   titre.style.width = "95%";
   if(titreboite) titre.id = "titrebox";
   titre.appendChild(document.createTextNode(titreboite));
   lignetitre.appendChild(titre);

   // Image
   if(modal == 'accept') {
      // Le croix de fermeture n'apparait pas en mode 'acceptation'
      modal = "";
      titre.style.width = "100%";
   }
   else {
      var btclose = document.createElement("th");
      btclose.width = "5%";
      btclose.className="boximage";

      var imgNode = document.createElement("img");
      imgNode.src = "fileadmin/templates/img/remove.png";
      imgNode.style.margin = "0 0 0 0";

      // Suppresion de la boite fenetre
      addEvent(imgNode,"click",function (){
         hiddeBox(gris,fenetre)
      });
      btclose.appendChild(imgNode);
      lignetitre.appendChild(btclose);
   }

   // Assemblage table à la boite

   tbody.appendChild(lignetitre);
   boxtitle.appendChild(tbody);
   fenetre.appendChild(boxtitle);


   // Affichage du message ------------------------------------------------------------------------
   var ctext = document.createElement("div");
   ctext.style.padding='15px';
   ctext.style.overflow='auto';
   //ctext.style.width="100%";
   // On ne définit pas la hauteur si modal ou flag pour laisser la place pour le modal et le flag
   if (!modal && !flag) {
      ctext.style.height=(height * 0.9)+"px";
   }

   ctext.id = "insertion"+winid;
   // Assemblage à la boite
   fenetre.appendChild(ctext);


   // Table pour ajout de boutons suivant valeur de 'modal' ---------------------------------------
   // submit form ou cancel (cas des brouillons ou des 'delete')
   // La table 'modal' est toujours affichée même sans les boutons pour le redimensionnement
   var boxmodal = document.createElement("table");
   boxmodal.style.width = "100%";
   var tbodymodal = document.createElement("tbody");
   var lignemodal = document.createElement("tr");
   var yesmodalalign = document.createElement("td");
   yesmodalalign.style.width = "25%";
   lignemodal.appendChild(yesmodalalign);

   // On affiche les boutons validation et cancel uniquement si ils sont demandés ...
   if ((action) && (commande)) {
      if(isIE7 || isIE6) fenetre.style.height=parseInt(width+80)+"px";
      // bouton Validation
      lignemodal.appendChild(buttonValid(action, commande, gris));

      // Bouton Cancel
      lignemodal.appendChild(buttonCancel(gris));
   }

   // Validation
   var yesmodal = document.createElement("td");
   if (modal =='close') {
      yesmodal.style.textAlign = "center";
      yesmodal.style.paddingLeft = "40px";
   }
   else {
      yesmodal.style.width = "33%";
      yesmodal.id = "yesmodal";
   }

   if(modal) {
      if(isIE7 || isIE6) {
         fenetre.style.height = parseInt(width) + 40 + "px";
      }
      var imgNode = document.createElement("img");
      imgNode.src = "fileadmin/templates/img/valid.png";

      if (modal=='valid')
         addEvent(imgNode,"click",function (){
            modalValidate()
         });

      if (modal=='draft')
         addEvent(imgNode,"click",function (){
            modalDraft()
         });

      if (modal=='delete')
         addEvent(imgNode,"click",function (){
            modalDelete()
         });

      if (modal=='deletegrid')
         addEvent(imgNode,"click",function (){
            modalDeleteGrid()
         });
            

      if (modal=='close')
         addEvent(imgNode,"click",function (){
            hiddeBox(gris,fenetre)
         });

      imgNode.className = "cursor";
      yesmodal.appendChild(imgNode);
      lignemodal.appendChild(yesmodal);
   }

   // Drapeau
   if(flag) {
      var imgNode = document.createElement("img");
      imgNode.src = 'fileadmin/templates/img/'+flag;
      yesmodal.appendChild(imgNode);
      lignemodal.appendChild(yesmodal);
      yesmodalalign.style.width = "45%"; // pour centrer l'image
   }


   // Cancel
   var nomodal = document.createElement("td");
   nomodal.style.width = "33%";
   if(modal && modal != 'close') {
      var imgNode = document.createElement("img");
      imgNode.src = "fileadmin/templates/img/cancel.png";
      addEvent(imgNode,"click",function (){
         hiddeBox(gris,fenetre)
      });
      imgNode.className = "cursor";
      nomodal.appendChild(imgNode);
   }
   lignemodal.appendChild(nomodal);
   tbodymodal.appendChild(lignemodal);
   boxmodal.appendChild(tbodymodal);
   fenetre.appendChild(boxmodal);


   // Assemblage au container supérieur -----------------------------------------------------------
   gris.appendChild(fenetre);
   ajaxwin.appendChild(gris);
   premier_plan(fenetre);


   if ((isWebKit && (ua.indexOf('4.0.3',0)) !== -1) || (-1 == ua.indexOf('safari',0))) {
      // Evènements
      if (isIE6 == true) {
      // pas de déplacement
      }
      else {
         addEvent(ajaxwin,"mousemove", function (event){
            deplacer_fenetre(event, fenetre)
         });
         addEvent(titre,"mouseout", function (){
            arreter_deplacement()
         });
         addEvent(titre,"mousedown",function (event){
            commencer_deplacement(event,fenetre)
         });
         addEvent(fenetre,"mouseup", function (){
            arreter_deplacement()
         });
      }
   }

   // Insertion du texte en HTML ou non
   document.getElementById("insertion"+winid).innerHTML=texte;
   return;
}


function chercher(url, fenetre){
   alert('Appel de la fonction chercher() : affichée depuis oa_genericbox.js');
}

function filtrer(url, fenetre){
   alert('Appel de la fonction filtrer() : affichée depuis oa_genericbox.js');
}

//ancienne fenetre
function configurer(url, fenetre){
   var a = document.getElementById("coladd").value;
   var b = document.getElementById("coldel").value;
   var c = document.getElementById("nbrecpage").value;
   try
   {
      var d = document.getElementById("etat").value;
   }
   catch(e) {};

   var url2 = '';

   if(a != 0) url2 += "&addcol="+a;
   if(b != 0) url2 += "&delcol="+b;
   if(c != 0) url2 += "&rowperpage="+c;
   if(d != 0) url2 += "&archive="+d;


   if(url2 != '') redirext(url+url2);
   else hiddeBox('',fenetre);

   return true;
}


//Bouton valid
function buttonValid(action, commande, fenetre){
   var yesmodal = document.createElement("td");
   yesmodal.style.width = "33%";

   var imgNode = document.createElement("img");
   imgNode.src = "fileadmin/templates/img/valid.png";

   /* addEvent(imgNode, "click", function (){
               modalSubmit(commande, fenetre);
            });*/


   switch(commande){
      case 'chercher':
         addEvent(imgNode, "click", function (){
            chercher(action, fenetre);
         });
         break;

      case 'filtrer':
         addEvent(imgNode, "click", function (){
            filtrer(action, fenetre);
         });
         break;

      case 'configurer':
         addEvent(imgNode, "click", function (){
            configurer(action, fenetre);
         });
         break;

      default :
         addEvent(imgNode, "click", function (){
            alert('Commande ' + commande + ' inconnue, appelée depuis la méthode displayToolBox() de oa_genericbox.js')
         });
   }

   imgNode.className = "cursor";
   yesmodal.appendChild(imgNode);

   return yesmodal;
}

// Bouton Cancel : bouton qui ferme la fenetre passée en parametre
function buttonCancel(fenetre){
   var nomodal = document.createElement("td");
   nomodal.style.width = "33%";

   var imgNode = document.createElement("img");
   imgNode.src = "fileadmin/templates/img/cancel.png";
   addEvent(imgNode,"click",function (){
      hiddeBox('',fenetre)
   });
   imgNode.className = "cursor";
   nomodal.appendChild(imgNode);

   return nomodal;
}
