function showCalendar(y,m,d) {	
			
		 if (document.getElementById('divCalendar').style.visibility=='visible') {
		 	document.getElementById('divCalendar').style.visibility='hidden';
		 } else {
		   document.getElementById('divCalendar').style.visibility='visible';
		 }
		 y=y*1;
		 m=m*1;
		 d=d*1;
		 auj=new Date(y,m-1,d);
		 CheckData(auj,'0');
}

function remplit(y,m,d){  
  d=d<10?"0"+d:d;
  m=m<10?"0"+m:m;	
  y=y.toString(); 
  document.forms[0].textDate.value=d;
  document.forms[0].textDate.value=document.forms[0].textDate.value+m;
  document.forms[0].textDate.value=document.forms[0].textDate.value+y.substr(0,4);	
  showCalendar("","","");
}

var HTMLCode = "";
var DaysList = new Array("Jour_Vide", "L", "M", "M", "J", "V", "S", "D");
var MonthsList = new Array("Mois_Vide", "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre");
var MonthLength = new Array("Mois_longueur_vide",31,29,31,30,31,30,31,31,30,31,30,31);

var QueryDate = 0; /* Jour demande (date)*/
var QueryMonth = 0; /* Mois demande*/
var QueryYear = 0; /* Annee demandee*/
var QueryDay = 0; /* Jour de la semaine du jour demande, inconnu*/
var FirstDay = 0; /* Jour de la semaine du 1er jour du mois*/
var WeekRef = 0; /* Numerotation des semaines*/
var WeekOne = 0; /* Numerotation des semaines*/

var Today = new Date();
var TodaysYear = Today.getYear();
var TodaysMonth = Today.getMonth() + 1;
var TodaysDate = Today.getDate();
var TodaysDay = Today.getDay() + 1;
if (TodaysYear < 2000) { TodaysYear += 1900; }

/* On commence par verifier les donnees fournies par l'utilisateur*/
function CheckData(ladate,ind)
{	  


if (ind!=0) {	 
   
   if (ind=="1") {	  
   	  x=((ladate.getMonth()+2)==13)?1:ladate.getMonth()+2;
	  //alert(x+"/"+MonthLength[x]+"/"+ladate.getDate()) 
   	  if (MonthLength[x] < ladate.getDate()) /* on verifie si la date est coherente*/
   	  {		
		 ladate.setDate(MonthLength[x]);
   	  } 
	  mo=ladate.getMonth()+1;
   } else {
   	  x=((ladate.getMonth())<0)?11:ladate.getMonth();
	  //alert(x+"/"+MonthLength[x]+"/"+ladate.getDate()) 
   	  if (MonthLength[x] < ladate.getDate()) /* on verifie si la date est coherente*/
   	  {		
		 ladate.setDate(MonthLength[x]);
   	  } 
	  mo=ladate.getMonth()-1;
   }
   //alert(ladate.getMonth()+"/"+mo);
   ladate.setMonth(mo);   
} 

QueryDate=ladate.getDate();	 
QueryMonth=ladate.getMonth()+1;	
QueryYear=ladate.getFullYear();	
laDate=new Date(QueryYear,QueryMonth-1,QueryDate);
MonthLength[2] = CheckLeap(QueryYear);
//alert ((QueryYear * 10000 + QueryMonth * 100 + QueryDate))
//alert(MonthLength[QueryMonth]+"/"+ QueryDate)
/* on teste si la date choisie est anterieure au lundi 20 decembre 1582*/
if ((QueryYear * 10000 + QueryMonth * 100 + QueryDate) < 15821220)
{
alert("Vous avez choisi une date antérieure au 20 décembre 1582, hors du calendrier Grégorien. \nVeuillez sélectionner une date plus récente.");
document.Cal.reset();
CheckData();
}
else if (MonthLength[QueryMonth] < QueryDate) /* on verifie si la date est coherente*/
{		
//alert("Il n'y a pas " + QueryDate + " jours en " + MonthsList[ladate.getMonth()] + " " + QueryYear + " mais " + MonthLength[ladate.getMonth()] + ". \nVeuillez choisir une autre date.");
//document.Cal.reset();
//CheckData();
}
else { DisplaySchedule(); }

}
/* Teste une annee pour determiner si elle est bissextile ou pas*/
function CheckLeap(yy)
{
if ((yy % 100 != 0 && yy % 4 == 0) || (yy % 400 == 0)) { return 29; }
else { return 28; }
}

/* Renvoie le numero de la semaine correspondant a la date requise*/
function DefWeekNum(dd)
{
numd = 0;
numw = 0;
for (n=1; n<QueryMonth; n++)
{
numd += MonthLength[n];
}
numd = numd + dd - (9 - DefDateDay(QueryYear,1,1));
numw = Math.floor(numd / 7) + 1;

if (DefDateDay(QueryYear,1,1) == 1) { numw++; }
return numw;
}

/* Renvoie le numero du jour de la semaine correspondant a la date requise */
function DefDateDay(yy,mm,dd)
{
return Math.floor((Date2Days(yy,mm,dd)-2) % 7) + 1;
}

/* Transforme la date en nb de jours theoriques */
function Date2Days(yy,mm,dd)
{
if (mm > 2)
{
var bis = Math.floor(yy/4) - Math.floor(yy/100) + Math.floor(yy/400);
var zy = Math.floor(yy * 365 + bis);
var zm = (mm-1) * 31 - Math.floor(mm * 0.4 + 2.3);
return (zy + zm + dd);
}
else
{
var bis = Math.floor((yy-1)/4) - Math.floor((yy-1)/100) + Math.floor((yy-1)/400);
var zy = Math.floor(yy * 365 + bis);
return (zy + (mm-1) * 31 + dd);
}
}

/* Produit le code HTML qui formera le calendrier */
function DisplaySchedule()
{
HTMLCode = "<table cellspacing=0 cellpadding=0 border=0>";
QueryDay = DefDateDay(QueryYear,QueryMonth,QueryDate);
WeekRef = DefWeekNum(QueryDate);
WeekOne = DefWeekNum(1); 
HTMLCode += "<tr height=21 align=center><td class=tr11c width=28><a href='javascript:CheckData(laDate,\"-1\")' class=tr11c><<</a></td><td colspan=5 class=tr11c><b>" + MonthsList[QueryMonth] + " " + QueryYear + "</b></td><td class=tr11c><a href='javascript:CheckData(laDate,\"1\")' class=tr11c>>></a></td ><td class=tr11c><img src='../../rvd/birthday-alert/images/fermer.gif' onclick='showCalendar(\"\",\"\",\"\")'></td></tr><tr  height=21  align=center>";
HTMLCode += "<td class=tr11b width=28><b>S.</b></td>";

for (s=1; s<8; s++)
{ 
if (QueryDay == s) { HTMLCode += "<td class=tr11b><b>" + DaysList[s] + "</b></td>"; }
else { HTMLCode += "<td class=tr11b><b>" + DaysList[s] + "</b></td>"; }
}
HTMLCode += "</tr>";
a = 0;

for (i=(1-DefDateDay(QueryYear,QueryMonth,1)); i<MonthLength[QueryMonth]; i++)
{
HTMLCode += "<tr  height=21 align=center>";	 
if (WeekRef==0) {Week_Ref=52;}	 else { Week_Ref=WeekRef;}
if ((WeekOne+a)==0) {Week_One=52;}	 else { Week_One=(WeekOne+a);}
if ((WeekOne+a) == WeekRef) { HTMLCode += "<td class=tr11b width=28>" + Week_Ref + "</td>"; }
else { HTMLCode += "<td class=tr11b width=28>" + Week_One + "</td>"; }

a++;
for (j=1; j<8; j++)
{
jr=eval(j+i); 

if (page=="evt") {
	if ((i+j) <= 0) { HTMLCode += "<td class=tr11 width=28>&nbsp;</td>"; }
	else if ((i+j) == QueryDate) { HTMLCode += "<td class=tr11a><a href='javascript:remplit("+QueryYear+","+QueryMonth+","+jr+")' class=gf12>" + (i+j) + "</a></td>"; }
	else if ((i+j) > MonthLength[QueryMonth]) { HTMLCode += "<td class=tr11 width=28>&nbsp;</td>"; }
	else { HTMLCode += "<td class=tr11 width=28><a href='javascript:remplit("+QueryYear+","+QueryMonth+","+jr+")' class=gf12>" + (i+j) + "</a></td>"; }
} else if (page=="agenda") {
    if ((i+j) <= 0) { HTMLCode += "<td class=tr11 width=28>&nbsp;</td>"; }
	else if ((i+j) == QueryDate) { HTMLCode += "<td class=tr11a><a href='javascript:agenda("+QueryYear+","+QueryMonth+","+jr+")' class=gf12>" + (i+j) + "</a></td>"; }
	else if ((i+j) > MonthLength[QueryMonth]) { HTMLCode += "<td class=tr11 width=28>&nbsp;</td>"; }
	else { HTMLCode += "<td class=tr11 width=28><a href='javascript:agenda("+QueryYear+","+QueryMonth+","+jr+")' class=gf12>" + (i+j) + "</a></td>"; }
}
}

HTMLCode += "</tr>";
i = i + 6;
}
 //alert(HTMLCode)
document.getElementById("divCalendar").innerHTML = HTMLCode + "</table>";
}

function agenda(y,m,d) {
 y=y.toString();
 m=m.toString();
 d=d.toString();  
 d=d<10?"0"+d:d;
  m=m<10?"0"+m:m;	
 jour=d+m+y;
 chem='index.php?pg=agenda&id='+id+'&cnx='+cnx+'&jour='+jour;
 window.location=chem;

}
