
// ----------------------------------------------------------------------------
//   __   _  __    __  _____    _____   _____   _____   _     _ 
//  |  \ | | \ \  / / |  _  \  /  _  \ |  _  \ | ____| | |   / /
//  |   \| |  \ \/ /  | |_| |  | | | | | | | | | |__   | |  / / 
//  | |\   |   \  /   |  _  /  | | | | | | | | |  __|  | | / /  
//  | | \  |   / /    | | \ \  | |_| | | |_| | | |___  | |/ /   
//  |_|  \_|  /_/     |_|  \_\ \_____/ |_____/ |_____| |___/    
//
// ----------------------------------------------------------------------------
//
// Script écrit par Cédric Nirousset (cedric@nyrodev.com)
//		Entreprise NyroDev : http://www.nyrodev.com
//
// Ce code est strictement réservé à son utilisation.
// Toute modifcation ou réutilisation est interdite par son auteur.
//
// Pour toutes questions, merci de me contacter.
//
// ----------------------------------------------------------------------------
function s2charDate(str) {
 str = escape(str);
 if (str.length == 1) {
  str = "0"+str;
 }
 return str;
}
function calend(name,divT,form,champ1,champ2,heure) {
 var name,chp1, chp2, heure, div;
 var aJour = new Array("Lu", "Ma", "Me", "Je", "Ve", "Sa", "Di");
 var aMois = new Array("Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre");
 var aTravail = new Array(1,1,1,1,1,0,0);
 this.name = name;
 this.div = divT;
 this.chp1 = document.forms[form].elements[champ1];
 this.chp2 = document.forms[form].elements[champ2];
 this.heure = heure;
 this.changeMois = function(mois) {
  var a = document.getElementById("anneeCalend"+this.name);
  var aDeb = a.options[0].value;
  var aFin = a.options[a.options.length-1].value;
  mois++;
  var date = a.options[a.selectedIndex].value+"-"+mois+"-0";
  this.faireCalend(aDeb,aFin,date,1);
 };
 this.changeAnnee = function(annee) {
  var a = document.getElementById("anneeCalend"+this.name);
  var aDeb = a.options[0].value;
  var aFin = a.options[a.options.length-1].value;
  var m = document.getElementById("moisCalend"+this.name);
  var mois = parseInt(m.options[m.selectedIndex].value)+1;
  var date = annee+"-"+mois+"-0";
  this.faireCalend(aDeb,aFin,date,1);
 };
 this.faireCalend = function(aDeb,aFin,date,dDef) {
  // On détermine la date à afficher
  var aDate = new Array(3);
  var aHeure = new Array(2);
  if (date == "0000-00-00") dDef = 0;
  if (dDef == 1) {
   var reg1 = new RegExp("[ ]+", "g");
   aTemp = date.split(reg1);
   var reg2 = new RegExp("[-]+", "g");
   aDate = aTemp[0].split(reg2);
   if (aTemp[1]) {
    var reg3 = new RegExp("[:]+", "g");
    aHeure = aTemp[1].split(reg3);
   } else if (this.heure && document.getElementById("heureChoixCalend"+this.name)) {
    var hh = document.getElementById("heureChoixCalend"+this.name);
    aHeure[0] = hh.options[hh.selectedIndex].value;
    var mm = document.getElementById("minChoixCalend"+this.name);
    aHeure[1] = mm.options[mm.selectedIndex].value;
   }
   aDate[1]--;
  } else {
   var maDate = new Date();
   // Si rien n'est envoyé, on paramètre à la date du jour
   aDate[0] = maDate.getFullYear();
   aDate[1] = maDate.getMonth();
   aDate[2] = maDate.getDate();
   if (document.getElementById("heureChoixCalend"+this.name)) {
    var hh = document.getElementById("heureChoixCalend"+this.name);
    aHeure[0] = hh.options[hh.selectedIndex].value;
    var mm = document.getElementById("minChoixCalend"+this.name);
    aHeure[1] = mm.options[mm.selectedIndex].value;
   } else {
    aHeure[0] = s2charDate(maDate.getHours());
    aHeure[1] = s2charDate(maDate.getMinutes());
   }
  }
  // On prépare ce que l'on va écrire
  var html = "";
  html+= '<table class="calend">';
  html+= '<tr><td colspan="7" align="center">';
  html+= '<select id="moisCalend'+this.name+'" onchange="javascript:window.'+this.name+'.changeMois(this.value);">';
  for(i=0;i<aMois.length;i++) {
   html+= '<option value="'+i+'">'+aMois[i]+'</option>';
  }
  html+= '</select>';
  html+= '&nbsp;&nbsp;';
  html+= '<select id="anneeCalend'+this.name+'" onchange="javascript:window.'+this.name+'.changeAnnee(this.value);">';
  for (i=aDeb;i<=aFin;i++) {
   html+= '<option value="'+i+'">'+i+'</option>';
  }
  html+= '</select>';
  html+= '&nbsp;&nbsp;';
  html+= '<a href="javascript:window.'+this.name+'.ferme();" class="fermeCalend">X</a>';
  html+= '</td></tr>';
  html+= '<tr>';
  
  if (this.heure == 1) {
   // On affiche le choix pour les heures
   html+= '<tr><td colspan="7" align="left">Heure : ';
   html+= '<select id="heureChoixCalend'+this.name+'">';
   for (i=0;i<24;i++) {
    var st = s2charDate(i);
    if (aHeure[0] == st) {
     html+= '<option value="'+st+'" selected>'+st+'</option>';
    } else {
     html+= '<option value="'+st+'">'+st+'</option>';
    }
   }
   html+= '</select> : ';
   html+= '<select id="minChoixCalend'+this.name+'">';
   for (i=0;i<60;i++) {
    var st = s2charDate(i);
    if (aHeure[1] == st) {
     html+= '<option value="'+st+'" selected>'+st+'</option>';
    } else {
     html+= '<option value="'+st+'">'+st+'</option>';
    }
   }
   html+= '</select> ';
   html+= '</td></tr>';
  }
  
  // On affiche le nom des jours
  for(i=0;i<aJour.length;i++) {
   html+= '<td class="jour">'+aJour[i]+'</td>';
  }
  html+= '</tr>';
  // On détermine le nombre de jour du mois
  var joursMois = 1
  for (j=27;j<33;j++){
   var cDate = new Date(aDate[0],aDate[1],j);
   if (cDate.getDate() == 1) {
    joursMois = j-1;
   }
  }
  // On détermine le 1er jour
  var cDate = new Date(aDate[0],aDate[1],1);
  var jourPre = cDate.getDay();
 
  if (jourPre == 0) {
   jourPre = 7;
  }
  html+= '<tr>';
  // On écrit les premiers jours vides
  for (i=1;i<jourPre;i++) {
   html+= '<td></td>';
  }
  var cont = true;
  var nb = 1;
  var onClick = "";
  while (cont) {
   on = 'onclick="javascript:window.'+this.name+'.choixDate('+nb+');" onMouseOver="javascript:this.className=\'jourC jourHov\';"';
   if (nb == aDate[2]) {
    html+= '<td class="jourC jourSel" '+on+' onMouseOut="javascript:this.className=\'jourC jourSel\';">'+nb+'</td>';
   } else if ((jourPre+nb)%7 == 1 || (jourPre+nb)%7 == 0) {
    html+= '<td class="jourC jourTra" '+on+' onMouseOut="javascript:this.className=\'jourC jourTra\';">'+nb+'</td>';
   } else {
    html+= '<td class="jourC jourNor" '+on+' onMouseOut="javascript:this.className=\'jourC jourNor\';">'+nb+'</td>';
   }
   if ((jourPre+nb)%7 == 1) {
    // Nouvelle semaine
    html+= '</tr><tr>';
   }
   nb++;
   if (nb > joursMois) {
    html+= '</tr>';
    cont = false;
   }
  }
 
  html+= '</table>';
  document.getElementById(this.div).innerHTML = html;
  document.getElementById("moisCalend"+this.name).selectedIndex = aDate[1];
  if (document.getElementById("anneeCalend"+this.name)) {
   if (document.getElementById("anneeCalend"+this.name).options.length >= aDate[0]-aDeb) {
    document.getElementById("anneeCalend"+this.name).selectedIndex = aDate[0]-aDeb;
   } else {
    document.getElementById("anneeCalend"+this.name).selectedIndex = document.getElementById("anneeCalend"+this.name).options.length-1;
   }
  }
 };
 this.choixDate = function(j) {
  // Lorsqu'on clique sur une date
  j = s2charDate(j);

  var a = document.getElementById("anneeCalend"+this.name);
  var m = document.getElementById("moisCalend"+this.name);

  var mois = escape(parseInt(m.options[m.selectedIndex].value)+1);
  mois = s2charDate(mois);
 
  var choix = a.options[a.selectedIndex].value+"-"+mois+"-"+j;
  var choixAff = j+"/"+mois+"/"+a.options[a.selectedIndex].value; 

  if (this.heure == 1) {
   var hh = document.getElementById("heureChoixCalend"+this.name);
   var mm = document.getElementById("minChoixCalend"+this.name);
   choix+= " "+hh.options[hh.selectedIndex].value+":"+mm.options[mm.selectedIndex].value;
   choixAff+= " à "+hh.options[hh.selectedIndex].value+":"+mm.options[mm.selectedIndex].value;
  }
  
  this.chp1.value = choix;
  this.chp2.value = choixAff;
  document.getElementById(this.div).style.display = "none";
  if (this.callback) {
   eval(this.callback);
  }
 };
 
  this.choixDateMGE = function(callb) {
  // Lorsqu'on clique sur une date
  
  var j = this.chp2.value.substring(0,2);
  var a = this.chp2.value.substring(6,10);
  var m = this.chp2.value.substring(3,5);

  this.chp1.value = a+'-'+m+'-'+j;
  
  if (callb) {
   eval(callb);
  }
 };
 
 
 
 this.ferme = function() {
  document.getElementById(this.div).style.display = "none";
 };
 this.affCalend = function(aDeb,aFin,evt,decY,decX,callb) {
  this.callback = callb;
  var def = 0;
  if (this.chp1.value) {
   def = 1;
  }
  this.faireCalend(aDeb,aFin,this.chp1.value,def);
  var x,y;
  if (evt.x) {
   x = evt.x + document.body.scrollLeft;
  } else {
   x = evt.pageX;
  }
  if (evt.y) {
   y = event.y + document.body.scrollTop;
  } else {
   y = evt.pageY;
  }
  if (decY != undefined) {
   y += decY;
  }
  if (decX != undefined) {
   x += decX;
  }
  document.getElementById(this.div).style.display = "block";
  document.getElementById(this.div).style.left = parseInt(x+20)+"px";
  document.getElementById(this.div).style.top = y+"px";
 };
 this.efface = function() {
  this.chp1.value = "";
  this.chp2.value = "";
 }
}
function rien() {}

