/**
* Fichier: calendar.js
* Auteur: Damien Harper (harperd@keops.net)
* Version: 1.2.0
* Description: Objet permettant de creer un calendrier popup
* Compatibilite: NS 6+, IE 5+
* Utilisation:
**/ 

function Calendar() {
	var cal;						// reference vers le popup du calendrier
	if (arguments.length > 0) { cal = new winPopup(arguments[0]); }
	else { cal = new winPopup(); cal.setSize(175,148); }
	cal.offsetX = 0;			// decalage en X par rapport au lien appelant le calendrier
	cal.offsetY = 17;			// decalage en Y par rapport au lien appelant le calendrier
	cal.setHideMode(true);	// le calendrier se cachera automatiquement si on clique en dehors du popup
	
	cal.monthNames				= new Array('Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre');
	cal.dayHeaders				= new Array('Dim','Lun','Mar','Mer','Jeu','Ven','Sam');
	cal.returnFunction			= "_tmpReturnFunction";
	cal.dateChangeFunction		= "_tmpDateChangeFunction";
	cal.weekStartDay			= 1;
	cal.isYearNavigationON		= false;
	cal.currentDate				= null;
	cal.todayText				= "Aujoud'hui";
	cal.weekText				= "Sem";
	cal.linkedObject			= null;
	cal.linkedObjectFormat		= "dd/mm/yyyy";
	cal.styles					= "";
	
	cal.setReturnFunction		= _setReturnFunction;				// fonction executee qd clic sur un jour
	cal.setDateChangeFunction	= _setDateChangeFunction;
	cal.setMonthNames			= _setMonthNames;						// setter des noms de mois
	cal.setDayHeaders			= _setDayHeaders;						// setter des abbrev. de jour				
	cal.setWeekStartDay			= _setWeekStartDay;					// setter du jour de debut de la semaine
	cal.setTodayText 			= _setTodayText;						// setter du texte du lien "Aujourd'hui"
	cal.setWeekText				= _setWeekText;						// setter de l'abbrev. de semaine
	cal.setYearNavigation 		= _setYearNavigation;				// activateur de navigation sur les annees
	cal.show			 		= _show;									// affiche le calendrier
	cal.hide			 		= _hide;									// cache le calendrier
	cal.getDefaultStyles 		= _getDefaultStyles;					// getter des styles par defaut
	cal.refresh			 		= _refresh;								// rafraichit le calendrier
	cal.getContent 				= _getContent;							// construit le contenu (HTML) du popup
	cal.select 					= _select;								// lie un objet de form au calendrier
	cal.setStyles				= _setStyles;							// setter des styles
	cal.setXYOffset				= _setXYOffset;						// setter des offsets X et Y
	cal.setInitDate				= _setInitDate;						// setter de date initiale
	cal.getCurrentDate			= _getCurrentDate;
	cal.setImages				= _setImages;
	
	return cal;
}

function showMonthSelect(year,idx,flg) {
	var obj = getCalendarObject(idx);
	var content = "";
	var popup = new winPopup("monthSelect"+idx);
	var ow,oh,lw,lh;
	popup.setHideMode(true);
	
	content += '<table border="0" cellspacing="0" cellpadding="0" class="calendarText" width="@width@">\n';
	for (i=0;i<12;i++) {
		content += '<tr onclick="goMonth('+year+','+i+','+idx+');" onmouseover="this.className=\'calendarSelector\'" onmouseout="this.className=\'\'">\n';
		content += '<td align="center" width="100%">'+obj.monthNames[i]+(!flg ? '' : ' '+year)+'</td>\n';
		content += '</tr>\n';
	}
	content += '</table>\n';

	document.getElementById("monthSelect"+idx).innerHTML = content;
	ow = document.getElementById("monthSelect"+idx).offsetWidth;
	oh = document.getElementById("monthSelect"+idx).offsetHeight;
	document.getElementById("monthSelect"+idx).innerHTML = '';
	
	coord = getPagePosition("ms"+idx);
	lw = document.getElementById("ms"+idx).offsetWidth;
	lh = document.getElementById("ms"+idx).offsetHeight;

	popup.offsetX = ((lw - ow) / 2) - obj.x;
	popup.offsetY = 0 - obj.y - 23;
	
	if ((obj.y-23+oh)>(document.body.clientTop+document.body.clientHeight+document.body.style.border)) {
		popup.offsetY -= (obj.y-15+oh)-(document.body.clientTop+document.body.clientHeight+document.body.style.border);
	}
	content = content.split('@width@').join(ow);
	popup.setContent(content);
	popup.showPopup('ms'+idx);
}

function showYearSelect(year,month,idx) {
	var obj = getCalendarObject(idx);
	var content = "";
	var popup = new winPopup("monthSelect"+idx);
	var ow,oh,lw,lh;
	popup.setHideMode(true);
	
	content += '<table border="0" cellspacing="0" cellpadding="0" class="calendarText" width="@width@">\n';
	for (i=(parseInt(year)-5);i<(parseInt(year+5));i++) {
		content += '<tr onclick="goMonth('+i+','+month+','+idx+');" onmouseover="this.className=\'calendarSelector\'" onmouseout="this.className=\'\'">\n';
		content += '<td align="center" width="100%">'+i+'</td>\n';
		content += '</tr>\n';
	}
	content += '</table>\n';

	document.getElementById("monthSelect"+idx).innerHTML = content;
	ow = document.getElementById("monthSelect"+idx).offsetWidth;
	oh = document.getElementById("monthSelect"+idx).offsetHeight;
	document.getElementById("monthSelect"+idx).innerHTML = '';
	
	coord = getPagePosition("ys"+idx);
	lw = document.getElementById("ys"+idx).offsetWidth;
	lh = document.getElementById("ys"+idx).offsetHeight;

	popup.offsetX = ((lw - ow) / 2) - obj.x;
	popup.offsetY = 0 - obj.y - 23;

	if ((obj.y-23+oh)>(document.body.clientTop+document.body.clientHeight+document.body.style.border)) {
		popup.offsetY -= (obj.y-15+oh)-(document.body.clientTop+document.body.clientHeight+document.body.style.border);
	}
	
	content = content.split('@width@').join(ow);
	popup.setContent(content);
	popup.showPopup('ys'+idx);
}

function goMonth(year,month,idx) {
	var obj = getCalendarObject(idx);
	var d = new Date(year,month,obj.currentDate.getDate(),0,0,0);
	obj.refresh(idx,(parseInt(month)+1),year);

	var windowRef = '';
	eval(windowRef+obj.dateChangeFunction+'('+year+','+(month+1)+','+idx+')');
}

function _tmpReturnFunction(y,m,d,idx) { 
	var obj = getCalendarObject(idx);
	if (obj.linkedObject != null) {
		var d = new Date(y,m-1,d,0,0,0);
		obj.linkedObject.value = formatDate(d,obj.linkedObjectFormat,obj.monthNames);
		obj.setInitDate(formatDate(d,obj.linkedObjectFormat,obj.monthNames),obj.linkedObjectFormat);
	} else {
		alert('Utilisez setReturnFunction() pour définir quelle fonction executer lors de la sélection d\'un jour dans le calendrier');
	}
}

function _tmpDateChangeFunction(y,m,idx) { }
function getCalendarObject(idx) { return window.winPopupObjects[idx]; }
function getLinkedObject(idx) { return window.winPopupObjects[idx].linkedObject; }

function _getCurrentDate() {
	if (this.currentDate == null) { return new Date(); } else { return this.currentDate; }
}

function _setXYOffset(x,y) { this.offsetX = x; this.offsetY = y; }
function _setReturnFunction(name) { this.returnFunction = name; }

function _setInitDate(dateString, dateFormat) {
	var time = getDateFromFormat(dateString,dateFormat,this.monthNames);
	if (time == 0) { this.currentDate = null; } else { this.currentDate = new Date(time); }
	_refresh(this.index);
}

function _setDateChangeFunction(name) { this.dateChangeFunction = name; }
function _setMonthNames() { for (var i=0; i<arguments.length; i++) { this.monthNames[i] = arguments[i]; } }
function _setStyles(styles) { this.styles = styles; }
function _setImages(imgP,imgN) { this.imgPrevious = imgP; this.imgNext = imgN; }
function _setDayHeaders() { for (var i=0; i<arguments.length; i++) { this.dayHeaders[i] = arguments[i]; } }
function _setWeekStartDay(day) { this.weekStartDay = day; }
function _setYearNavigation(flg) {
	this.isYearNavigationON = flg;
	if (flg && this.type == "WINDOW") {	this.setSize(210,145); }
}

function _setTodayText(text) { this.todayText = text; }
function _setWeekText(text) { this.weekText = text; }

function _hide() {
	if (arguments.length > 0) { window.winPopupObjects[arguments[0]].hidePopup(); }
	else { this.hidePopup(); }
}

function _refresh(index) {
	var calObj = window.winPopupObjects[index];
	if (arguments.length > 1) { 
		calObj.setContent(calObj.getContent(arguments[1],arguments[2],arguments[3],arguments[4],arguments[5]));
	} else {
		calObj.setContent(calObj.getContent());
	}
	calObj.refreshPopup();
}

function _show(aName) { this.setContent(this.getContent()); this.showPopup(aName); }

function _select(inputObj, format) {
	if (inputObj.type != "text" && inputObj.type != "hidden" && inputObj.type != "textarea") { 
		alert("Le calendrier n'est pas lie à un objet 'text', 'hidden' ou 'textarea'"); 
		this.linkedObject=null;
		return;
	}
	this.linkedObject = inputObj;
	if (inputObj.value != "") {
		var time = getDateFromFormat(inputObj.value,format,this.monthNames);
		if (time == 0) { this.currentDate = null; }
		else { this.currentDate = new Date(time); }
	} else { this.currentDate = null; }
	this.linkedObjectFormat = format;
}
	
function _getDefaultStyles() {
	var result = "";
	result += "<STYLE>\n";
	result += ".calendar {  background-color: #ffffff; font-weight: bold;	font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: #003366; text-decoration: none; border-color: #003366 black; border-style: solid; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px }\n";
	result += ".calendarBody { font-weight: bold;	font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: #003366; text-decoration: none; border-color: #003366 black; border-style: solid; border-top-width: 1px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px }\n";
	result += ".calendarHeaders {  background-color: #CCCCCC; font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366; text-decoration: none ;}\n";
	result += ".calendarWeek {  background-color: #CCCCCC; font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366; text-decoration: none ;}\n";
	result += ".calendarMonth {	background-color: #AADDC6; font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366; text-decoration: none ;}\n";
	result += ".calendarMonthOther {	background-color: #e1f3eb; font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366; text-decoration: none ;}\n";
	result += ".calendarThisMonth {	font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366; text-decoration: none ;}\n";
	result += ".calendarOtherMonth {  font-weight: bold; color: 808080; font-family: Arial, Helvetica, sans-serif; font-size: 10px; text-decoration: none ;}\n";
	result += ".calendarSelected {  background-color: #50b68b; font-weight: bold; color: 003366; font-family: Arial, Helvetica, sans-serif; font-size: 10px; text-decoration: none ;}\n";
	result += "a.calendarSelected:link {  background-color: #50b68b; font-weight: bold; color: 003366; font-family: Arial, Helvetica, sans-serif; font-size: 10px; text-decoration: none ;}\n";
	result += "a.calendarSelected:active {  background-color: #50b68b; font-weight: bold; color: 003366; font-family: Arial, Helvetica, sans-serif; font-size: 10px; text-decoration: none ;}\n";
	result += "a.calendarSelected:visited {  background-color: #50b68b; font-weight: bold; color: 003366; font-family: Arial, Helvetica, sans-serif; font-size: 10px; text-decoration: none ;}\n";
	result += "a.calendarSelected:hover {  background-color: #e1f3eb; font-weight: bold; color: 003366; font-family: Arial, Helvetica, sans-serif; font-size: 10px; text-decoration: none ;}\n";
	result += "a.calendarThisMonth:link {  font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366}\n";
	result += "a.calendarThisMonth:active {  font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366}\n";
	result += "a.calendarThisMonth:visited {  font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366}\n";
	result += "a.calendarThisMonth:hover {	font-weight: normal; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: 003366}\n";
	result += ".calendarText {	font-family: Arial, Helvetica, sans-serif; font-weight: bold;	font-size: 11px;	color: #003366;	padding-top: 2px;	padding-right: 2px;	padding-bottom: 2px;	padding-left: 2px }\n";
	result += ".calendarControl {	background-color: #ffffff;font-family: Arial, Helvetica, sans-serif; font-weight: bold;	font-size: 11px;	color: #003366;	padding-top: 2px;	padding-right: 2px;	padding-bottom: 2px;	padding-left: 2px }\n";
	result += "a.calendarControl:hover {	background-color: #ffffff;font-family: Arial, Helvetica, sans-serif; font-weight: bold;	font-size: 11px;	color: #003366;	padding-top: 2px;	padding-right: 2px;	padding-bottom: 2px;	padding-left: 2px }\n";
	result += "a.calendarControl:link {	background-color: #ffffff;font-family: Arial, Helvetica, sans-serif; font-weight: bold;	font-size: 11px;	color: #003366;	padding-top: 2px;	padding-right: 2px;	padding-bottom: 2px;	padding-left: 2px }\n";
	result += "a.calendarControl:active {	background-color: #ffffff;font-family: Arial, Helvetica, sans-serif; font-weight: bold;	font-size: 11px;	color: #003366;	padding-top: 2px;	padding-right: 2px;	padding-bottom: 2px;	padding-left: 2px }n";
	result += "a.calendarControl:visited {	background-color: #ffffff;font-family: Arial, Helvetica, sans-serif; font-weight: bold;	font-size: 11px;	color: #003366;	padding-top: 2px;	padding-right: 2px;	padding-bottom: 2px;	padding-left: 2px }\n";
	result += ".calendarCombo {	font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 10px; color: #003366; text-decoration: none; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px }\n";
	result += ".calendarSelector {	background-color: #e1f3eb;	font-family: Arial, Helvetica, sans-serif; font-weight: bold;	font-size: 11px;	color: #003366;	text-decoration: none; }\n";
	result += ".boutons { font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: ffffff; font-weight: bold; background-color: #006699}\n";
	result += "</STYLE>\n";
	return result;
}

function _getContent() {
	var now			= new Date();		// date du jour
	var result		= "";					// chaine contenant le code HTML du calendrier
	var windowRef	= "";					// prefixe javascript a utiliser par la suite si popup est une fenetre
	if (this.type == "WINDOW") { windowRef = "window.opener."; }
	if (this.type == "WINDOW") {
		result += '<HTML><HEAD><TITLE>Calendrier</TITLE>'+(this.styles == "" ? this.getDefaultStyles() : this.styles)+'</HEAD><BODY MARGINWIDTH="1" MARGINHEIGHT="1" TOPMARGIN="1" RIGHTMARGIN="1" LEFTMARGIN="1">\n';
		result += '<TABLE CLASS="calendar" WIDTH="160" BORDER="0" BORDERWIDTH="0" CELLSPACING="0" CELLPADDING="1">\n';
		result += '<TR><TD ALIGN="CENTER">\n';
		result += '<CENTER>\n';
	} else {
		result += '<div id="monthSelect'+this.index+'" name="monthSelect'+this.index+'" style="visibility: hidden; position:absolute;z-index:100;cursor: hand;Top:0px;Left:0px" class="calendar"></div>\n'
		result += '<TABLE CLASS="calendar" WIDTH="160" BORDER="0" BORDERWIDTH="0" CELLSPACING="0" CELLPADDING="1">\n';
		result += '<TR><TD ALIGN="CENTER">\n';
		result += '<CENTER>\n';
	}
	if (this.currentDate == null) { this.currentDate = now; }
	if (arguments.length > 0) { var month = arguments[0]; } else { var month = this.currentDate.getMonth()+1; }
	if (arguments.length > 1) { var year = arguments[1]; } else { var year = this.currentDate.getFullYear(); }
	var daysInMonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	if (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0)) { daysInMonth[2] = 29; }
	var currentMonth	= new Date(year,month-1,1);
	var displayYear	= year;
	var displayMonth	= month;
	var displayDate	= 1;
	var weekDay			= currentMonth.getDay();
	var offset 			= 0;
	var weekNum			= 0;
	if (weekDay >= this.weekStartDay) { offset = weekDay - this.weekStartDay; }
	else { offset = 7-this.weekStartDay+weekDay; }
	if (offset > 0) {
		displayMonth--;
		if (displayMonth < 1) { displayMonth = 12; displayYear--; }
		displayDate = daysInMonth[displayMonth]-offset+1;
	}
	var nextMonth = month + 1;											// mois suivant
	var nextMonthYear = year;											// 
	if (nextMonth > 12) { nextMonth=1; nextMonthYear++; }		// verif changt annee qd mois++
	var lastMonth = month - 1;											// mois precedant
	var lastMonthYear = year;											//
	if (lastMonth < 1) { lastMonth=12; lastMonthYear--; }		// verif changt anne qd mois--
	var dateClass;
	result += '<TABLE WIDTH="160" BORDER="0" BORDERWIDTH="0" CELLSPACING="0" CELLPADDING="0">\n';
	result += '<TR class="calendarControl">\n';
	var refresh = windowRef+'_refresh';
	if (this.isYearNavigationON) {
		var td = '<TD ALIGN="CENTER" VALIGN="MIDDLE" ';
		result += td + ' WIDTH="14"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.dateChangeFunction+'('+lastMonthYear+','+lastMonth+','+this.index+');'+refresh+'('+this.index+','+lastMonth+','+lastMonthYear+');"><img src="'+this.imgPrevious+'" border="0"></A></TD>\n';
		if (this.type != "WINDOW") {
			result += td + ' WIDTH="70" CLASS="calendarControl"><a onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+'showMonthSelect('+year+','+this.index+',false);" id="ms'+this.index+'" name="ms'+this.index+'" class="calendarControl">'+this.monthNames[month-1]+'</A></TD>\n';
		} else {
			result += td + ' WIDTH="70" CLASS="calendarCombo">\n<select class="calendarCombo" onchange="window.opener.goMonth('+year+',this.value,'+this.index+');">\n';
			for (i=0;i<12;i++) {
				result += '<option value="'+i+'"'+(i != (month-1) ? '' : ' selected')+'>'+this.monthNames[i]+'</option>\n';
			}
			result += '</select>\n</TD>\n';
		}
		result += td + ' WIDTH="14"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.dateChangeFunction+'('+nextMonthYear+','+nextMonth+','+this.index+');'+refresh+'('+this.index+','+nextMonth+','+nextMonthYear+');"><img src="'+this.imgNext+'" border="0"></A></TD>\n';
//		result += td + ' WIDTH="14"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.dateChangeFunction+'('+lastMonthYear+','+lastMonth+','+this.index+');'+refresh+'('+this.index+','+month+','+(year-1)+');"><img src="'+this.imgPrevious+'" border="0"></A></TD>\n';
		result += td + ' WIDTH="14"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.dateChangeFunction+'('+(parseInt(year)-1)+','+month+','+this.index+');'+refresh+'('+this.index+','+month+','+(year-1)+');"><img src="'+this.imgPrevious+'" border="0"></A></TD>\n';
		if (this.type != "WINDOW") {
			result += td + ' WIDTH="30" CLASS="calendarControl"><a onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+'showYearSelect('+year+','+(month-1)+','+this.index+');" id="ys'+this.index+'" name="ys'+this.index+'" class="calendarControl">'+year+'</A></TD>';
		} else {
			result += td + ' WIDTH="30" CLASS="calendarCombo">\n<select class="calendarCombo" onchange="window.opener.goMonth(this.value,'+month+','+this.index+');">\n';
			for (i=(parseInt(year)-5);i<(parseInt(year)+5);i++) {
				result += '<option value="'+i+'"'+(i != year ? '' : ' selected')+'>'+i+'</option>\n';
			}
			result += '</select>\n</TD>\n';
		}
//		result += td + ' WIDTH="14"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.dateChangeFunction+'('+nextMonthYear+','+nextMonth+','+this.index+');'+refresh+'('+this.index+','+month+','+(parseInt(year)+1)+');"><img src="'+this.imgNext+'" border="0"></A></TD>';
		result += td + ' WIDTH="14"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.dateChangeFunction+'('+(parseInt(year)+1)+','+month+','+this.index+');'+refresh+'('+this.index+','+month+','+(parseInt(year)+1)+');"><img src="'+this.imgNext+'" border="0"></A></TD>';
	} else {
		result += '	<TD WIDTH="14" ALIGN="CENTER" VALIGN="MIDDLE"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.dateChangeFunction+'('+lastMonthYear+','+lastMonth+','+this.index+');'+refresh+'('+this.index+','+lastMonth+','+lastMonthYear+');"><img src="'+this.imgPrevious+'" border="0"></A></TD>\n';
		result += '	<TD CLASS="calendarControl" WIDTH="132" ALIGN="CENTER" NOWRAP>\n';
		if (this.type != "WINDOW") {
			result += '<a onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+'showMonthSelect('+year+','+this.index+',true);" id="ms'+this.index+'" name="ms'+this.index+'" class="calendarControl">'+this.monthNames[month-1]+' '+year+'</A>\n';
		} else {
			result += '<select id="ms" class="calendarCombo" onchange="window.opener.goMonth('+year+',this.value,'+this.index+');">\n';
			for (i=0;i<12;i++) {
				result += '<option value="'+i+'"'+(i != (month-1) ? '' : ' selected')+'>'+this.monthNames[i]+' '+year+'</option>\n';
			}
			result += '</select>\n';
		}
		result += '</TD>\n';
		result += '	<TD WIDTH="14" ALIGN="CENTER" VALIGN="MIDDLE"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.dateChangeFunction+'('+nextMonthYear+','+nextMonth+','+this.index+');'+refresh+'('+this.index+','+nextMonth+','+nextMonthYear+');"><img src="'+this.imgNext+'" border="0"></A></TD>\n';
	}
	result += '</TR></TABLE>\n';
	result += '<TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="0" ALIGN="CENTER" class="calendarBody">\n';
	result += '<TR>\n';
	var td = '	<TD CLASS="calendarHeaders" ALIGN="RIGHT" WIDTH="14%">';
	for (var j=0; j<7; j++) { result += td + this.dayHeaders[(this.weekStartDay + j)%7] + '</TD>\n'; }
	result += '	<TD CLASS="calendarWeek" ALIGN="RIGHT" WIDTH="14%">' + this.weekText + '</TD>\n';
	result += '</TR>\n';

	for (var row=1; row<=6; row++) {
		result += '<TR>\n';
		for (var col=1; col<=7; col++) {
			if (displayMonth == month) {
				dateClass = "calendarThisMonth";
			} else {
				dateClass = "calendarOtherMonth";
			}
			
			if ((displayMonth == this.currentDate.getMonth()+1) && (displayDate==this.currentDate.getDate()) && (displayYear==this.currentDate.getFullYear())) {
				dateClass = "calendarSelected";
				tdClass="calendarSelected";
			} else {
				if (displayMonth != month)	{ tdClass="calendarMonthOther"; } else { tdClass="calendarMonth"; }
			}
			var selected_date = displayDate;
			var selected_month = displayMonth;
			var selected_year = displayYear;
			result += '	<TD ALIGN="CENTER" CLASS="'+tdClass+'"><A onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.returnFunction+'('+selected_year+','+selected_month+','+selected_date+','+this.index+');'+windowRef+'_hide(\''+this.index+'\');" CLASS="'+dateClass+'">'+displayDate+'</A></TD>\n';
			if (weekNum == 0) { weekNum = getWeek(displayYear,(displayMonth-1),displayDate); }
			displayDate++;
			if (displayDate > daysInMonth[displayMonth]) { displayDate=1; displayMonth++; }
			if (displayMonth > 12) { displayMonth=1; displayYear++; }
		}
		result += '	<TD ALIGN="CENTER" CLASS="calendarWeek">'+weekNum+'</TD>\n';
		weekNum = 0;
		result += '</TR>';
	}
	result += '</TABLE>\n';
	result += '<TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="0" ALIGN="CENTER">\n';
	var current_weekDay = now.getDay();
	if (this.todayText != "") {
		result += '<TR>\n';
		result += '	<TD COLSPAN="8" ALIGN="CENTER">\n';
		result += '<A CLASS="calendarText" onmouseover="window.status=\'\'; return true;" href="javascript:'+windowRef+this.returnFunction+'(\''+now.getFullYear()+'\',\''+(now.getMonth()+1)+'\',\''+now.getDate()+'\',\''+this.index+'\');'+windowRef+'_hide(\''+this.index+'\');">'+this.todayText+'</A>\n';
		result += '	</TD></TR>';
	}
	result += '</TABLE></CENTER></TD></TR></TABLE></TD></TR></TABLE>\n';
	if (this.type == "WINDOW") { result += "</BODY></HTML>\n"; }
	return result;
}

function isIE5() { return (document.getElementById && !!document.all); }
function isNS6() { return (document.getElementById && !document.all); }

function winPopup() {
	if (!window.winPopupIndex)		window.winPopupIndex = 0;
	if (!window.winPopupObjects)	window.winPopupObjects = new Array();
	if (!window.listenerAttached)	{ window.listenerAttached = true; _attachListener(); }
	this.index		= winPopupIndex++;
	winPopupObjects[this.index] = this;
	this.divName	= null;
	this.winPopup	= null;
	this.width		= 0;
	this.height		= 0;
	this.populated	= false;
	this.visible	= false;
	this.hideMode	= true;					// auto hide by default
	this.contents = "";
	if (arguments.length > 0) {
		this.type="DIV";
		this.divName = arguments[0];
	} else {
		this.type="WINDOW";
	}
	if (!isIE5() && !isNS6()) { this.type = "WINDOW"; }
	this.offsetX = 0;
	this.offsetY = 0;

	this.setCoord			= _setCoord;				// fixe les coord du popup
	this.setContent		= _setContent;				// fixe le contenu du popup
	this.refreshPopup		= _refreshPopup;			// rafraichit
	this.showPopup			= _showPopup;				// affiche le popup
	this.setHideMode		= _setHideMode;			// fixe le mode de fermeture
	this.hidePopup			= _hidePopup;				// cache le popup
	this.setSize			= _setSize;					// definit la taille du popup
	this.isClicked			= _isClicked;				// popup clicke ?
	this.hideIfNotClicked = _hideIfNotClicked;
}

function _setCoord(aName) {
	if (this.type == "WINDOW") { coord = getScreenPosition(aName); }
	else { coord = getPagePosition(aName); }
	this.x = coord.x;
	this.y = coord.y;
}

function _setSize(width,height) { this.width = width; this.height = height; }

function _setContent(contents) {
	if (arguments.length > 1) { this.URLcontent = arguments[1]; } else { this.URLcontent = false; }
	this.contents = contents;
	this.populated = false;
}

function _refreshPopup() {
	if (this.divName != null && this.divName != '') {
		if (isIE5() || isNS6()) { document.getElementById(this.divName).innerHTML = this.contents; }
	} else {
		if (this.winPopup != null && !this.winPopup.closed) {
			this.winPopup.document.open();
			if (!this.URLcontent) {
				this.winPopup.document.writeln(this.contents);
			} else {
				this.winPopup.document.location.href = this.contents;
			}
			this.winPopup.document.close();
			this.winPopup.focus();
		}
	}
}

function _showPopup(aName) {
	this.setCoord(aName);
	this.x += this.offsetX;
	this.y += this.offsetY;
	
	if (!this.populated && (this.contents != "")) {
		this.populated = true;
		this.refreshPopup();
	}
	
	if (this.divName != null && this.divName != '') {
		if (isIE5() || isNS6()) {
			document.getElementById(this.divName).style.left = this.x;
			document.getElementById(this.divName).style.top = this.y;
			document.getElementById(this.divName).style.visibility = "visible";
		}
	} else {
		if (this.winPopup == null || this.winPopup.closed) {
			if (screen && screen.availHeight) {
				if ((this.y + this.height) > screen.availHeight) {
					this.y = screen.availHeight - this.height;
				}
			}
			if (screen && screen.availWidth) {
				if ((this.x + this.width) > screen.availWidth) {
					this.x = screen.availWidth - this.width;
				}
			}
			this.winPopup = window.open("about:blank","window_"+aName,"toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable,alwaysRaised,dependent,titlebar=no,width="+this.width+",height="+this.height+",screenX="+this.x+",left="+this.x+",screenY="+this.y+",top="+this.y+"");
		}
		this.refreshPopup();
	}
}

function _hidePopup() {
	if (this.divName != null && this.divName != '') {
		if (isIE5() || isNS6()) {
			if (this.hideMode) { document.getElementById(this.divName).style.visibility = "hidden"; }
		}
	} else {
		if (this.winPopup && !this.winPopup.closed) { this.winPopup.close(); this.winPopup = null; }
	}
}

function _isClicked(e) {
	if (this.divName != null && this.divName != '') {
		if (isIE5()) {
			var t = window.event.srcElement;
			while (t.parentElement != null) {
				if (t.id == this.divName) { return true; }
				t = t.parentElement;
			}
		} else {
			var t = e.originalTarget;
			while (t.parentNode != null) {
				if (t.id == this.divName) { return true; }
				t = t.parentNode;
			}
		}
	}
	return false;
}

function _hideIfNotClicked(e) { 
	if (this.hideMode && !this.isClicked(e)) { 
		this.hidePopup();
	}
}

function _setHideMode(mode) {
	this.hideMode = mode;
}

function _hideWinPopups(e) {
	for (var i=0; i<winPopupObjects.length; i++) {
		if (winPopupObjects[i] != null) { winPopupObjects[i].hideIfNotClicked(e); }
	}
}

function _attachListener() {
	if (window.winPopupOldEventListener != null) {
		addEvent(document, "mouseup", oldPopupListener, true);
	} else {
		addEvent(document, "mouseup", _hideWinPopups, true);
	}
}

function oldPopupListener() {
	window.winPopupOldEventListener();
	_hideWinPopups();
}

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
}

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}


/********************************************************/
function getWeek(year,month,day) {
    var when = new Date(year,month,day);
    var newYear = new Date(year,0,1);
    var offset = 7 + 1 - newYear.getDay();
    if (offset == 8) offset = 1;
    var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) - Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
    var weeknum = Math.floor((daynum-offset+7)/7);
    if (weeknum == 0) {
        year--;
        var prevNewYear = new Date(year,0,1);
        var prevOffset = 7 + 1 - prevNewYear.getDay();
        if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
    }
    return weeknum;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function zeroLeftPadding(x) { return ((x < 0 || x > 9) ? "" : "0") + x }

function formatDate(date) {
	if (typeof(date) == 'string') {
		if (arguments.length < 3) {
			alert('Valid arguments are: dateString,oldFormat,newFormat');
			return "";
		} else {
			var time = getDateFromFormat(arguments[0],arguments[1]);
			if (time == 0) { date = ""; } else { date = formatDate(new Date(time),arguments[2]); }
			return date;
		}
	} else {
		formatString = arguments[1];
		if (arguments.length < 3) {
			monthNames	= new Array('Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre');
		} else {
			monthNames	= arguments[2];
		}
		if (arguments.length < 4) {
			dayNames	= new Array('Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi');
		} else {
			dayNames	= arguments[3];
		}
	}
	formatString = formatString + "";
	var resultString = "";
	var formatIdx = 0;
	var c = "";
	var token = "";
	var y = date.getYear() + "";
	var M = date.getMonth() + 1;
	var d = date.getDate();
	var wd = date.getDay();
	var yyyy,yy,MMM,MM,dd,D;

	var fmtValues = new Object();
	if (y.length < 4) { y = "" + (y - 0 + 1900); }
	fmtValues["y"]		= "" + y;
	fmtValues["yyyy"]	= y;
	fmtValues["yy"]	= y.substring(2,4);
	fmtValues["M"]		= M;
	fmtValues["MM"]	= zeroLeftPadding(M);
	fmtValues["MMM"]	= monthNames[M-1];
	fmtValues["d"]		= d;
	fmtValues["dd"]	= zeroLeftPadding(d);
	fmtValues["DD"]	= dayNames[wd];
	fmtValues["D"]		= dayNames[wd].substring(0,3);
	while (formatIdx < formatString.length) {
		c = formatString.charAt(formatIdx);
		token = "";
		while ((formatString.charAt(formatIdx) == c) && (formatIdx < formatString.length)) { token += formatString.charAt(formatIdx++); }
		if (fmtValues[token] != null) { resultString = resultString + fmtValues[token]; } else { resultString = resultString + token; }
	}
	return resultString;
}
	
function getDateFromFormat(val,formatString,monthNames,dayNames) {
	if (arguments.length < 3) {
		monthNames	= new Array('Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre');
	}
	if (arguments.length < 4) {
		dayNames	= new Array('Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi');
	}
	val=val+"";
	formatString=formatString+"";
	var valIdx=0;
	var formatIdx=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=now.getDate();
	var day = now.getDay();
	
	while (formatIdx < formatString.length) {
		c=formatString.charAt(formatIdx);
		token="";
		while ((formatString.charAt(formatIdx)==c) && (formatIdx < formatString.length)) {
			token += formatString.charAt(formatIdx++);
		}
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			year=_getInt(val,valIdx,x,y);
			if (year==null) { return 0; }
			valIdx += year.length;
			if (year.length==2) { if (year > 70) { year=1900+(year-0); } else { year=2000+(year-0); } }
		} else if (token=="MMM"){
			month=0;
			for (var i=0; i<monthNames.length; i++) {
				var mName=monthNames[i];
				if (val.substring(valIdx,valIdx+mName.length).toLowerCase()==mName.toLowerCase()) {
					month=i+1;
					if (month>12) { month -= 12; }
					valIdx += mName.length;
					break;
				}
			}
			if ((month < 1)||(month>12)){return 0;}
		} else if (token=="DD"||token=="D"){
			day=0;
			for (var i=0; i<dayNames.length; i++) {
				var dName=dayNames[i];
				var length=0;
				length = (token=="DD" ? dName.length : 3);
				if (val.substring(valIdx,valIdx+length).toLowerCase()==dName.toLowerCase().substring(0,length)) {
					day=i+1;
					if (day>7) { day -= 7; }
					valIdx += dName.length;
					break;
				}
			}
			if ((day < 1)||(day>7)){return 0;}
		} else if (token=="MM"||token=="M") {
			month=_getInt(val,valIdx,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			valIdx+=month.length;
		} else if (token=="dd"||token=="d") {
			date=_getInt(val,valIdx,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			valIdx+=date.length;
		} else {
			if (val.substring(valIdx,valIdx+token.length)!=token) {return 0;} else {valIdx+=token.length;}
		}
	}
	if (valIdx != val.length) { return 0; }
	if (month==2) {
		if (((year%4==0)&&(year%100 != 0)) || (year%400==0)) {
			if (date > 29) { return false; }
		} else {
			if (date > 28) { return false; }
		}
	}
	if ((month==4)||(month==6)||(month==9)||(month==11)) { if (date > 30) { return false; } }
	var newDate = new Date(year,month-1,date,0,0,0)
	return newDate.getTime();
}

function _isInteger(val) {
	var digits = "1234567890";
	for (var i=0; i < val.length; i++) { if (digits.indexOf(val.charAt(i)) == -1) { return false; } }
	return true;
}

function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength)	return null;
		if (_isInteger(token))	return token;
	}
	return null;
}
	
function getPagePosition(aName) {
	var coord = new Object();
	var x = 0, y = 0;
	if (isIE5() || isNS6()) {
		x = _getPageOffsetLeft(document.getElementById(aName));
		y = _getPageOffsetTop(document.getElementById(aName));
	}
	coord.x = x;
	coord.y = y;
	return coord;
}

function getScreenPosition(aName) {
	var coord = getPagePosition(aName);
	var x = 0, y = 0;
	if (isIE5() || isNS6()) {
		if (isIE5()) {
			x = coord.x - document.body.scrollLeft + window.screenLeft;
			y = coord.y - document.body.scrollTop + window.screenTop;
		} else {
			x = coord.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
			y = coord.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
		}
	}
	coord.x = x;
	coord.y = y;
	return coord;
}

function _getPageOffsetLeft(elt) {
	var ol = elt.offsetLeft;
	while ((elt = elt.offsetParent) != null) { ol += elt.offsetLeft; }
	return ol;
}

function _getPageOffsetTop(elt) {
	var ot = elt.offsetTop;
	while ((elt = elt.offsetParent) != null) { ot += elt.offsetTop; }
	return ot;
}

function _getWindowOffsetLeft(elt) { return _getPageOffsetLeft(elt) - document.body.scrollLeft; }	
function _getWindowOffsetTop(elt) { return _getPageOffsetTop(elt) - document.body.scrollTop; }

