//-----------------------------------------------------------------------------
// Fonctions de calendriers
//-----------------------------------------------------------------------------
// Pour debug :
var DebugOn=false;
function DisplayDebug(theString) {	
	if ( DebugOn ) alert(theString); }

// Cf expos_edit.asp dans Symop

// Ici on déclare les dates des jours fériés fixes
var ferie=new Array("01/01","01/05","08/05","14/07","15/08","01/11","11/11","25/12");
var mois=new Array("Janvier","F&eacute;vrier","Mars","Avril","Mai","Juin","Juillet","Ao&ucirc;t","Septembre","Octobre","Novembre","D&eacute;cembre");
var moisAbrg=new Array("Jan","F&eacute;v","Mar","Avr","Mai","Jun","Jul","Ao&ucirc;","Sep","Oct","Nov","D&eacute;c");
var moisInitiale=new Array("J","F","M","A","M","J","J","A","S","O","N","D");
var joursEntiers=new Array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
var joursAbrg=new Array("Dim","Lun","Mar","Mer","Jeu","Ven","Sam");
var joursInitiale=new Array("D","L","M","M","J","V","S");
var debutSem=1; //0=Dim, 6=Sam 
var reposSem=new Array(0,6); //0=Dim, 6=Sam 

var dDate = new Date();
var dCurMonth = dDate.getMonth();
var dCurDayOfMonth = dDate.getDate();
var dCurYear = dDate.getFullYear();
var objPrevElement = new Object();

NbJourMois = new Array(12);
NbJourMois[0]=31;
NbJourMois[1]=28;
NbJourMois[2]=31;
NbJourMois[3]=30;
NbJourMois[4]=31;
NbJourMois[5]=30;
NbJourMois[6]=31;
NbJourMois[7]=31;
NbJourMois[8]=30;
NbJourMois[9]=31;
NbJourMois[10]=30;
NbJourMois[11]=31;


//-----------------------------------------------------------------------------
// Pour regler valeurs par default en cas de nouvel enregistrement
//-----------------------------------------------------------------------------
function AdjustDefaultDatas(Field) {	
	// Sous NS, bien initialiser la valeur du champ ˆ "" !
	//DisplayDebug("AdjustDefaultDatas : " + Field + " >" + Field.value + "<");
	if ( Field.value == "") {
		DateJour=new Date();
		Field.value=DateJour.getDate() + "/"  + (DateJour.getMonth()+1) + "/"  + DateJour.getFullYear();
		//DisplayDebug("AdjustDefaultDatas : " + Field.value);
	}
	
}


//-----------------------------------------------------------------------------
// Pour regler les Popup de dates sur la date du jour au chargement de la page
//-----------------------------------------------------------------------------
function AdjustDatePopup(listeJ,listeM,listeA,champLie) {
	// Date du jour
	DateJour=champLie.value.split("\/");
	Moi=DateJour[1];
	Da=DateJour[0];
	an=DateJour[2];
	//DisplayDebug(" AdjustDatePopup : " + Da + " " + Moi + " " + an);
	if ((Da>=0) && (Moi>=0) && (an>=0)) {
	//listeJ.value=Da-1;
	listeJ.selectedIndex=(Da-1);
	listeM.selectedIndex=(Moi-1);
	listeA.selectedIndex=(an-2000);
	}
}


//-----------------------------------------------------------------------------
// Pour regler le nb de jour par mois dans un Popup
//-----------------------------------------------------------------------------
function AjusteNbJourMois (listeJ,listeM,listeA,champLie) {
	Bisextile(listeA.options[listeA.selectedIndex].value);
	listeJ.length=NbJourMois[listeM.options[listeM.selectedIndex].value-1];
	for (var i=28;i<=NbJourMois[listeM.options[listeM.selectedIndex].value-1];i++) { 
		listeJ.options[i-1].value=i;
		listeJ.options[i-1].text=i;
	}
	champLie.value="" + listeJ.options[listeJ.selectedIndex].value + "/"  + listeM.options[listeM.selectedIndex].value + "/"  + listeA.options[listeA.selectedIndex].value;
}


//-----------------------------------------------------------------------------
// Test annee bisextile pour nb jours Fevrier
//-----------------------------------------------------------------------------
function Bisextile(Annee) {
	Annee = Annee % 100;
	Annee = ((Annee < 50) ? (2000 + Annee) : (1900 + Annee));
	if (((Annee % 4 == 0)  && !(Annee % 100 == 0)) ||(Annee % 400 == 0)) {
		//NbJourMois[1]= (iYear % 4 == 0) ? 29 : 28; = moins prŽcis
		NbJourMois[1]=29;
		return true;
	} else {
		NbJourMois[1]=28;
		return false;
	}
}


//-----------------------------------------------------------------------------
// Retourne le nro de la semaine
//-----------------------------------------------------------------------------
function getWorkWeek(iYear,iMonth,iDay) {
// Renvoie le nro de semaine [HJ 05/06/06]
	if (iYear<1) {
		var now = new Date();
		iYear = now.getFullYear();
		iMonth = now.getMonth();
		iDay = now.getDate();
		now = null; // release memory
	}
	var theDate = new Date(iYear,iMonth,iDay);
	var year = theDate.getFullYear();
	var month = theDate.getMonth();
	var date = theDate.getDate();
	theDate = null; // release memory
	
	Bisextile(year);
	
	var newYearDayInstance = new Date(year, 0, 1); //instance of the date for January 1
	var newYearDayOfWeek = newYearDayInstance.getDay(); //day of the week for January 1. 0 is Sunday
	var daysUntilToday = newYearDayOfWeek; //days before January 1 in ww01
	
	for (var j = 0; j <= month - 1; j++) { //count days in all previous months
		daysUntilToday += NbJourMois[j];
	}
	daysUntilToday += date; //add days in current month
	/* DisplayDebug("getWorkWeek : >" +  Math.ceil(daysUntilToday/7)); */
	return Math.ceil(daysUntilToday/7);
}


//-----------------------------------------------------------------------------
// Affiche un calendrier
//-----------------------------------------------------------------------------

/* Utilise les styles suivants (a copier dans la page) :
<STYLE type="text/css"><!--
	.calentete { color: #eff88d; font-weight: bold; font-size: 11px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular; background-color: #07b; text-align: center }
	.calferie { color: red; font-weight: bold; font-size: 10px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular; background-color: #cfc; text-align: center }
	.calstd { color: #0000af; font-size: 9px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular; background-color: #ffc; text-align: center }
	.caltoday { color: #f00; font-weight: bold; font-size: 10px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular; text-align: center }
	.calvide { background-color: #c0c0c0 }
	.calweekend { color: #8b4513; font-size: 10px; font-family: Arial, Helvetica, Geneva, Swiss, SunSans-Regular; background-color: #e2e673; text-align: center }
	a { text-decoration: none }-->
</STYLE>*/


function estFerie(j,m) {
	var nb=ferie.length;
	var test=false;
	for(var i=0;i<nb;i++) {
		if ((ferie[i].substring(0,2)==j)&&(ferie[i].substring(3,5)==m)) return true;
	}
	return false;
}

function fToggleColor(myElement) {
	var toggleColor = "#ff0000";
	if (myElement.id == "calDateText") {
		if (myElement.color == toggleColor) {
			myElement.color = "";
		} else {
			myElement.color = toggleColor;
		}
	} else if (myElement.id == "calCell") {
		theElt=myElement.children;
		for (var i in theElt) {
			if (theElt[i].id == "calDateText") {
				if (theElt[i].color == toggleColor) {
					theElt[i].color = "";
				} else {
					theElt[i].color = toggleColor;
	            }
			}
		}
	}
}


// Passer le champ cible en parametre (document.frmCalendarSample.theSelDate)
function fSetSelectedDay(myElement){
	// Renvoie la date
	//document.frmCalendarSample.theSelDate.value=document.all.calSelectedDate.value+"/"+document.frmCalendarSample.tbSelMonth.options[document.frmCalendarSample.tbSelMonth.selectedIndex].value+"/"+document.frmCalendarSample.tbSelYear.options[document.frmCalendarSample.tbSelYear.selectedIndex].value;
	if (myElement.id == "calCell") {
		// ATTENTION : innerText ne marche que sur IE !!!
		if (!isNaN(parseInt(myElement.children["calDateText"].innerText))) {
			myElement.bgColor = "#c0c0c0";
			objPrevElement.bgColor = "";
			document.all.calSelectedDate.value = parseInt(myElement.children["calDateText"].innerText);
			objPrevElement = myElement;
			// Renvoie la date
			document.frmCalendarSample.theSelDate.value=document.all.calSelectedDate.value+"/"+document.frmCalendarSample.tbSelMonth.options[document.frmCalendarSample.tbSelMonth.selectedIndex].value+"/"+document.frmCalendarSample.tbSelYear.options[document.frmCalendarSample.tbSelYear.selectedIndex].value;
      }
   }
}


// Gestion des interactions
//-----------------------------------------------------------------------------

function fSetSelectedDate(theDay,theForm){
	// Renvoie la date dans le champ "theSelDate" du formualire "theForm"
	// Passer le form cible en parametre
	// Si cases actives :
	//		- Popup mois : nom = tbSelMonth (facultatif)
	//		- Popup an   : nom = tbSelYear (facultatif)
	//		- Champ date : nom = theSelDate
	var isMonth = false;
	var isYear = false;
	var now = new Date();
	
	// Test existence des popups "tbSelMonth" et "tbSelYear"
	for (var i=0; i< theForm.elements.length; i++) {
		if (theForm.elements[i].name.indexOf('tbSelMonth',0)!= -1) {
			isMonth = true;
		} else {
			if (theForm.elements[i].name.indexOf('tbSelYear',0)!= -1) {
				isYear = true;
			} 
		}
	}

	// Recuperation des valeurs
	if (isMonth) {
		theMonth = theForm.tbSelMonth.options[theForm.tbSelMonth.selectedIndex].value;
	} else {
		theMonth = now.getMonth()+1;
	}
	
	if (isMonth) {
		theYear = theForm.tbSelYear.options[theForm.tbSelYear.selectedIndex].value;
	} else {
		theYear = now.getFullYear();
	}
	
	// Ecriture dans le champ cible
	theForm.theSelDate.value=theDay+"/"+theMonth+"/"+theYear;
	return false;
}


function fGetDaysInMonth(iMonth,iYear) {
	Bisextile(iYear);
	//DisplayDebug(" fGetDaysInMonth : " + iMonth + " >" + NbJourMois[iMonth-1]);
	return NbJourMois[iMonth-1];
}

// iDayStyle : 0=tres court ,1=abrege ,2=entier
function fBuildCal(iYear, iMonth, iDayStyle) {
var aMonth = new Array();
aMonth[0] = new Array(7);
aMonth[1] = new Array(7);
aMonth[2] = new Array(7);
aMonth[3] = new Array(7);
aMonth[4] = new Array(7);
aMonth[5] = new Array(7);
aMonth[6] = new Array(7);
var dCalDate = new Date(iYear, iMonth-1, 1);
var iDayOfFirst = dCalDate.getDay(); // 0=DIM, 6=SAM
var iDaysInMonth = fGetDaysInMonth(iMonth,iYear);
var iVarDate = 1;
var i, d, w;

// switch = pas avec NS !
/* switch (iDayStyle) { */
/* 	case 2 : */
/* 		NomsJours=joursEntiers; */
/* 		break; */
/* 	default : */
/* 		NomsJours=joursInitiale; */
/* 		// Autres cas */
/* 	} */
if (iDayStyle==2) {
	NomsJours=joursEntiers;
} else {
	if (iDayStyle==1) {
		NomsJours=joursAbrg;
	} else {
		NomsJours=joursInitiale;
	}
}

for (i = 0; i < 7; i++) {
	if ((i+debutSem) < 7) {
		aMonth[0][i] = NomsJours[i+debutSem];
	} else {
		aMonth[0][i] = NomsJours[i+debutSem-7];
	}
}
start=iDayOfFirst-debutSem;
if (start <0) {start=start+7;}
for (d = start; d < 7; d++) {
	aMonth[1][d] = iVarDate;
	iVarDate++;
}
for (w = 2; w < 7; w++) {
	for (d = 0; d < 7; d++) {
		if (iVarDate <= iDaysInMonth) {
			aMonth[w][d] = iVarDate;
			iVarDate++;
      }
   }
}
return aMonth;
}


function fDrawCal(iYear, iMonth, iCellWidth, iCellHeight, iDayStyle,iFormName) {
	var myMonth;
	var d_jour=new Date();
	var firstDay=new Date(iYear,iMonth-1,1);
	var j1=firstDay.getDay(); if (j1==0) j1=7;
	var jour=0;

	myMonth = fBuildCal(iYear, iMonth, iDayStyle);
	document.write("<table border='0' cellpadding='0' cellspacing='1'>");
	// Ecriture entete avec nom des jours
	document.write("<tr>");
	for (i = 0; i < 7; i++) {
		document.write("<td class='calentete'>" + myMonth[0][i] + "</td>");
	}
	document.write("</tr>");
	// Fin entete
	// Tableau de 6 lignes
	DisplayDebug("fDrawCal : >" + iFormName + "< / >" + (iFormName != "" && iFormName != "undefined") );
	for (w = 1; w < 7; w++) {
		document.write("<tr>");
		// Tableau de 7 colonnes
		for (d = 0; d < 7; d++) {
			theStyle="calstd";
			theLinkS="";
			theLinkE="";
			aff_j="&nbsp;";
			// Test si la case est non vide (=myMonth[w][d] est un nombre)
			if (!isNaN(myMonth[w][d])) {
				// Non vide
				aff_j=myMonth[w][d];
				
				// HJ 11/07/01 : Ajouter une condition pour actif ou non :
				if (iFormName != "") {
					theLinkS="<A HREF='#' onclick='return fSetSelectedDate("+myMonth[w][d]+","+iFormName+");'>";
					theLinkE="</A>";
				}
				
				// Test si jour courant :
				if ((myMonth[w][d]==d_jour.getDate())&&(iMonth==d_jour.getMonth()+1)) {aff_j="<SPAN class='caltoday'>"+myMonth[w][d]+"</SPAN>";}
					// Test si jour ferie hebdo :
					for (fh=0;fh<reposSem.length;fh++) {
						if ((reposSem[fh]-debutSem) < 0) {
							pos = reposSem[fh]-debutSem+7;
						} else {
							pos = reposSem[fh]-debutSem;
						}	
						if (pos==d) {
							theStyle="calweekend";
						}
					}
					// Test si jour ferie annuel :
					if ((estFerie(myMonth[w][d],iMonth))) {theStyle="calferie";}

			} else {
				// Vide
				theStyle="calvide";
			}
			if (iCellWidth>0) {
				theW="width='" + iCellWidth + "' ";
			} else {
				theW="";
			}
			if (iCellHeight>0) {
				theH="height='" + iCellHeight + "' ";
			} else {
				theH="";
			}
			//document.write("<td valign='top' " + theW + theH + " id=calCell style='CURSOR:Hand' class='" + theStyle + "'>");
			document.write("<td valign='top' " + theW + theH + " class='" + theStyle + "'>");
			//document.write("<font id=calDateText>" + theLinkS + aff_j + theLinkE + "</font>");
			document.write( theLinkS + aff_j + theLinkE);
			document.write("</td>");
		}
		document.write("</tr>");
	}
	document.write("</table>");
}

function fUpdateCal(iYear, iMonth) {
	myMonth = fBuildCal(iYear, iMonth);
	objPrevElement.bgColor = "";
	document.all.calSelectedDate.value = "";
	for (w = 1; w < 7; w++) {
		for (d = 0; d < 7; d++) {
			if (!isNaN(myMonth[w][d])) {
				calDateText[((7*w)+d)-7].innerText = myMonth[w][d];
			} else {
				calDateText[((7*w)+d)-7].innerText = " ";
			}
		}
	}
}



//-----------------------------------------------------------------------------
// Affiche un calendrier (ancien)
//-----------------------------------------------------------------------------
Moo = new Array( "Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre"); var calcul=0;

function ajj(month, day, href) 
{
	var baz = new Array(3);
	baz[0] = month;
	baz[1] = day;
	baz[2] = href;
	this[calcul++] = baz;
}

function DrawCal(theMonth,theYear) {
	Array.prototype.ajj = ajj;
	Bizz = new Array();
	Bozz = new Array(12);
	Bozz[0]=31;
	Bozz[1]=28;
	Bozz[2]=31;
	Bozz[3]=30;
	Bozz[4]=31;
	Bozz[5]=30;
	Bozz[6]=31;
	Bozz[7]=31;
	Bozz[8]=30;
	Bozz[9]=31;
	Bozz[10]=30;
	Bozz[11]=31;
	
	todayDate=new Date();
	jo=todayDate.getDay();
	Moi=todayDate.getMonth();
	Da=todayDate.getDate();
	an=todayDate.getYear();
	
	an = an % 100;
	an = ((an < 50) ? (2000 + an) : (1900 + an));
	
	// Test anne bisextile pour nb jours Fvrier
	if (((an % 4 == 0)  && !(an % 100 == 0)) ||(an % 400 == 0)) Bozz[1]++;
	
	Dep=Da;
	while (Dep > 7) Dep-=7;
	Dep = jo - Dep + 1;
	if (Dep < 0) Dep+=7;
	
	// Ecriture entete tableau
	document.write("<table border=1 bgcolor=white ");
	document.write("bordercolor=black><font size='2' color=black>");
	document.write("<tr><td colspan=7><center><strong>" + Moo[Moi] + "&nbsp;" + an + "</strong></center></font></td></tr>");
	CoF = "<td bgcolor='CCCCFF'><align=center><font size='1' color='000099'>";
	document.write("<tr>");
	document.write(CoF+"Di</td>");
	document.write(CoF+"Lu</td>");
	document.write(CoF+"Ma</td>");
	document.write(CoF+"Me</td>");
	document.write(CoF+"Je</td>");
	document.write(CoF+"Ve</td>");
	document.write(CoF+"Sa</td>"); 
	document.write("</tr><tr>");
	
	for (s=0;s<Dep;s++) { document.write("<td>&nbsp;</td>"); }
	
	count=1;
	while (count <= Bozz[Moi]) {
		for (b = Dep;b<7;b++) {
			linktrue=false;
			if (count!=Da) { document.write("<td><font size='1'>"); }
	
			for (c=0;c<Bizz.length;c++) {
				if (Bizz[c] != null) {
					if ((Bizz[c][0]==Moi + 1) && (Bizz[c][1]==count)) {
						document.write("<a href=\"" + Bizz[c][2] + "\">");
						linktrue=true;
					}
				}
			}
			
			if (count==Da) { document.write("<td bgcolor='FF0000'><font size='1' color='FFFFF'><strong>"); }
			if (count <= Bozz[Moi]) { document.write(count); }
			else { document.write("&nbsp;"); }
			if (count==Da) { document.write("</strong></font>"); }
			if (linktrue) document.write("</a></font>");
			document.write("</td>");
			count++;
		}
		document.write("</tr><tr>"); Dep=0;
	}
	document.write("</table>");
}
