
//Popup de calendrier
	var	fixedX = -1; // x position (-1 if to appear below control)
	var	fixedY = -1; // y position (-1 if to appear below control)
	var startAt = 1; // 0 - sunday ; 1 - monday	
	var imgDir = "img/popup/";	// directory for images ... e.g. var imgDir="./img/"
	
	var dayName = new Array("Lun","Mar","Mer","Jeu","Ven","Sam","Dim");	
	var	monthName =	new Array("Janvier","Fevrier","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Decembre");
	
	var	crossobj, crossMonthObj, crossYearObj, monthSelected, yearSelected, dateSelected, omonthSelected, oyearSelected, odateSelected, monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlNow, dateFormat, nStartingYear;

	var ctlToPlaceValueJour, ctlToPlaceValueMois, ctlToPlaceValueAnnee;

	var	bPageLoaded=false;
	var	ie=document.all;
	var	dom=document.getElementById;

	var	ns4=document.layers;
	var	today =	new	Date();
	var	dateNow	 = today.getDate();
	var	monthNow = today.getMonth();
	var	yearNow	 = today.getYear();
	var	imgsrc = new Array("drop1.gif","drop2.gif","left1.gif","left2.gif","right1.gif","right2.gif");
	var	img	= new Array();

	var bShow = false;
	
	var HolidaysCounter = 0;
	var Holidays = new Array();
	
	if (dom)
	{
		for	(i=0;i<imgsrc.length;i++)
		{
			img[i] = new Image;
			img[i].src= imgDir + imgsrc[i];
		}
		document.write ("<div onclick='bShow=true' id='calendar' class='div-style' ><table width='220' class='table-style'><tr class='title-background-style' ><td><table width='218' style='border: 0px;'><tr><td class='title-style'><B><span id='caption'></span></B></td><td align=right><a href='javascript:hideCalendar()'><IMG SRC='"+imgDir+"close.gif' WIDTH='15' HEIGHT='13' BORDER='0' ALT='Close the Calendar'></a></td></tr></table></td></tr><tr><td class='body-style'><span id='content'></span></td></tr>");
			
		document.write ("</table></div><div id='selectMonth' class='div-style'></div><div id='selectYear' class='div-style'></div>");
	}
	

	function swapImage(srcImg, destImg){
		if (ie)	{ document.getElementById(srcImg).setAttribute("src", imgDir + destImg) }
	}

	function init()	{
		if (!ns4)
		{
			if (!ie) { yearNow += 1900	}

			crossobj=(dom)?document.getElementById("calendar").style : ie? document.all.calendar : document.calendar;
			hideCalendar();

			crossMonthObj=(dom)?document.getElementById("selectMonth").style : ie? document.all.selectMonth	: document.selectMonth;

			crossYearObj=(dom)?document.getElementById("selectYear").style : ie? document.all.selectYear : document.selectYear;

			monthConstructed=false;
			yearConstructed=false;
			
			sHTML1= "<span id='spanLeft' class='title-control-normal-style'";
			sHTML1+=" onmouseover='swapImage(\"changeLeft\",\"left2.gif\");this.className=\"title-control-select-style\";'";
			sHTML1+=" onclick='javascript:decMonth()' onmouseout='clearInterval(intervalID1);swapImage(\"changeLeft\",\"left1.gif\");this.className=\"title-control-normal-style\";'";
			sHTML1+=" onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartDecMonth()\",500)'";
			sHTML1+=" onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp<IMG id='changeLeft' SRC='"+imgDir+"left1.gif' width=10 height=11 border=0>&nbsp</span>&nbsp;";
			
			sHTML1+="<span id='spanRight' class='title-control-normal-style'";
			sHTML1+=" onmouseover='swapImage(\"changeRight\",\"right2.gif\");this.className=\"title-control-select-style\";'";
			sHTML1+=" onmouseout='clearInterval(intervalID1);swapImage(\"changeRight\",\"right1.gif\");this.className=\"title-control-normal-style\";'";
			sHTML1+=" onclick='incMonth()' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartIncMonth()\",500)'";
			sHTML1+=" onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp<IMG id='changeRight' SRC='"+imgDir+"right1.gif'	width=10 height=11 border=0>&nbsp</span>&nbsp";
			
			sHTML1+="<span id='spanMonth' class='title-control-normal-style'";
			sHTML1+=" onmouseover='swapImage(\"changeMonth\",\"drop2.gif\");this.className=\"title-control-select-style\";'";
			sHTML1+=" onmouseout='swapImage(\"changeMonth\",\"drop1.gif\");this.className=\"title-control-normal-style\";'";
			sHTML1+=" onclick='popUpMonth()'></span>&nbsp;";
			
			sHTML1+="<span id='spanYear'  class='title-control-normal-style'";
			sHTML1+=" onmouseover='swapImage(\"changeYear\",\"drop2.gif\");this.className=\"title-control-select-style\";'";
			sHTML1+=" onmouseout='swapImage(\"changeYear\",\"drop1.gif\");this.className=\"title-control-normal-style\";'";
			sHTML1+=" onclick='popUpYear()'></span>&nbsp;";
			
			document.getElementById("caption").innerHTML  =	sHTML1;

			bPageLoaded=true;
		}
	}

	function hideCalendar()	{
		crossobj.visibility="hidden";
		
		if (crossMonthObj != null)
			crossMonthObj.visibility="hidden";
			
		if (crossYearObj !=	null)
			crossYearObj.visibility="hidden";
	}

	function padZero(num) {
		return (num	< 10)? '0' + num : num ;
	}

	function closeCalendar() {			
		hideCalendar();		
		
		ctlToPlaceValueJour.value=padZero(dateSelected);
		ctlToPlaceValueMois.value=padZero(Number(monthSelected)+1);
		ctlToPlaceValueAnnee.value=yearSelected;
	}

	/*** Month Pulldown	***/
	function StartDecMonth()
	{
		intervalID1=setInterval("decMonth()",80);
	}

	function StartIncMonth()
	{
		intervalID1=setInterval("incMonth()",80);
	}

	function incMonth () {
		monthSelected++;
		if (monthSelected>11) {
			monthSelected=0;
			yearSelected++;
		}
		constructCalendar();
	}

	function decMonth () {
		monthSelected--;
		if (monthSelected<0) {
			monthSelected=11;
			yearSelected--;
		}
		constructCalendar();
	}

	function constructMonth() {
		popDownYear();
		if (!monthConstructed) {
			sHTML =	"";
			for	(i=0; i<12;	i++) {
				sName =	monthName[i];
				if (i==monthSelected){
					sName =	"<strong><font color=\"#FF0000\">" +	sName +	"</font></strong>";
				}
				sHTML += "<tr><td id='m" + i + "' onmouseover='this.className=\"dropdown-select-style\"' onmouseout='this.className=\"dropdown-normal-style\"' onclick='monthConstructed=false;monthSelected=" + i + ";constructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>";
			}

			document.getElementById("selectMonth").innerHTML = "<table style='border: 0px;' width=70	class='dropdown-style' cellspacing=0 onmouseover='clearTimeout(timeoutID1)'	onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" +	sHTML +	"</table>";

			monthConstructed=true;
		}
	}

	function popUpMonth() {
		constructMonth();
		crossMonthObj.visibility = (dom||ie)? "visible"	: "show";
		crossMonthObj.left = parseInt(crossobj.left) + 50 + "px";
		crossMonthObj.top =	parseInt(crossobj.top) + 26 + "px";		
	}

	function popDownMonth()	{
		crossMonthObj.visibility= "hidden";
	}

	/*** Year Pulldown ***/
	function incYear() {
		for	(i=0; i<7; i++){
			newYear	= (i+nStartingYear)+1;
			
			if (newYear==yearSelected)
				txtYear =	"&nbsp;<strong><font color=\"#FF0000\">"	+ newYear +	"</font></strong>&nbsp;";
			else
				txtYear =	"&nbsp;" + newYear + "&nbsp;";
				
			document.getElementById("y"+i).innerHTML = txtYear;
		}
		nStartingYear ++;
		bShow=true;
	}

	function decYear() {
		for	(i=0; i<7; i++){
			newYear	= (i+nStartingYear)-1;
			
			if (newYear==yearSelected)
				txtYear =	"&nbsp;<strong><font color=\"#FF0000\">"	+ newYear +	"</font></strong>&nbsp;";
			else
				txtYear =	"&nbsp;" + newYear + "&nbsp;";
				
			document.getElementById("y"+i).innerHTML = txtYear;
		}
		nStartingYear --;
		bShow=true;
	}

	function selectYear(nYear) {
		yearSelected=parseInt(nYear+nStartingYear);
		yearConstructed=false;
		constructCalendar();
		popDownYear();
	}

	function constructYear() {
		popDownMonth();
		sHTML =	"";
		if (!yearConstructed) {

			sHTML =	"<tr><td align='center'	onmouseover='this.className=\"dropdown-select-style\"' onmouseout='clearInterval(intervalID1);this.className=\"dropdown-normal-style\"' onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>";
			j =	0;
			nStartingYear =	yearSelected-3;
			for	(i=(yearSelected-3); i<=(yearSelected+3); i++) {
				sName =	i;
				if (i==yearSelected){
					sName =	"<strong><font color=\"#FF0000\">" +	sName +	"</font></strong>";
				}

				sHTML += "<tr><td id='y" + j + "' onmouseover='this.className=\"dropdown-select-style\"' onmouseout='this.className=\"dropdown-normal-style\"' onclick='selectYear("+j+");event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>";
				j ++;
			}

			sHTML += "<tr><td align='center' onmouseover='this.className=\"dropdown-select-style\"' onmouseout='clearInterval(intervalID2);this.className=\"dropdown-normal-style\"' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)'	onmouseup='clearInterval(intervalID2)'>+</td></tr>";

			document.getElementById("selectYear").innerHTML	= "<table width=44 class='dropdown-style' onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing=0>"	+ sHTML	+ "</table>";

			yearConstructed	= true;
		}
	}

	function popDownYear() {		
		clearInterval(intervalID1);
		clearTimeout(timeoutID1);
		clearInterval(intervalID2);
		clearTimeout(timeoutID2);
		crossYearObj.visibility= "hidden";
	}

	function popUpYear() {
		var	leftOffset;

		constructYear();
		crossYearObj.visibility	= (dom||ie)? "visible" : "show";
		leftOffset = parseInt(crossobj.left) + document.getElementById("spanYear").offsetLeft;
		if (ie)
		{
			leftOffset += 6;
		}
		crossYearObj.left =	leftOffset + "px";
		crossYearObj.top = parseInt(crossobj.top) +	26 + "px";
	}

	

	function takeYear(theDate)
	{
		x = theDate.getYear();
		var y = x % 100;
		y += (y < 38) ? 2000 : 1900;
		return y;
	}

	function constructCalendar () {
		var	startDate =	new	Date (yearSelected,monthSelected,1)
		var	endDate	= new Date (yearSelected,monthSelected+1,1);
		endDate	= new Date (endDate	- (24*60*60*1000));
		numDaysInMonth = endDate.getDate();

		datePointer	= 0;
		dayPointer = startDate.getDay() - startAt;
		
		if (dayPointer<0)
		{
			dayPointer = 6;
		}

		sHTML =	"<table	border=0 class='body-style'><tr>";		

		for	(i=0; i<7; i++)	{
			sHTML += "<td width='27' align='right'><B>"+ dayName[i]+"</B></td>";
		}
		sHTML +="</tr><tr>";
		

		for	( var i=1; i<=dayPointer;i++ )
		{
			sHTML += "<td>&nbsp;</td>";
		}
	
		for	( datePointer=1; datePointer<=numDaysInMonth; datePointer++ )
		{
			dayPointer++;
			sHTML += "<td align=right>";

			var sStyle="normal-day-style"; //regular day

			if ((datePointer==dateNow)&&(monthSelected==monthNow)&&(yearSelected==yearNow)) //today
			{ 
				sStyle = "current-day-style"; 
			} 
			else if	(dayPointer % 7 == (startAt * -1) +1) //end-of-the-week day
			{ 
				sStyle = "end-of-weekday-style"; 
			}

			//selected day
			if ((datePointer==odateSelected) &&	(monthSelected==omonthSelected)	&& (yearSelected==oyearSelected))
			{ 
				sStyle += " selected-day-style"; 
			}

			sHint = "";
			for (k=0;k<HolidaysCounter;k++)
			{
				if ((parseInt(Holidays[k].d)==datePointer)&&(parseInt(Holidays[k].m)==(monthSelected+1)))
				{
					if ((parseInt(Holidays[k].y)==0)||((parseInt(Holidays[k].y)==yearSelected)&&(parseInt(Holidays[k].y)!=0)))
					{
						sStyle += " holiday-style";
						sHint+=sHint==""?Holidays[k].desc:"\n"+Holidays[k].desc;
					}
				}
			}

			var regexp= /\"/g;
			sHint=sHint.replace(regexp,"&quot;");

			sHTML += "<a class='"+sStyle+"' title=\"" + sHint + "\" href='javascript:dateSelected="+datePointer+";closeCalendar();'>&nbsp;" + datePointer + "&nbsp;</a>";

			sHTML += "";
			if ((dayPointer+startAt) % 7 == startAt) { 
				sHTML += "</tr><tr>" ;				
			}
		}

		document.getElementById("content").innerHTML   = sHTML;
		document.getElementById("spanMonth").innerHTML = "&nbsp;" +	monthName[monthSelected] + "&nbsp;<IMG id='changeMonth' SRC='"+imgDir+"drop1.gif' WIDTH='12' HEIGHT='10' BORDER=0>";
		document.getElementById("spanYear").innerHTML =	"&nbsp;" + yearSelected	+ "&nbsp;<IMG id='changeYear' SRC='"+imgDir+"drop1.gif' WIDTH='12' HEIGHT='10' BORDER=0>";
	}

	function popUpCalendar(ctlJour,ctlMois,ctlAnnee, positionX, positionY) {
	
		var	leftpos = 0;
		var	toppos = 0;
		if (ie) {
			leftpos = positionX;
			toppos = positionY;
		}
		else {
			leftpos = 0;
			toppos = 0;
		}
	
		if (bPageLoaded)
		{
			if ( crossobj.visibility ==	"hidden" ) {
				ctlToPlaceValueJour	= ctlJour;
				ctlToPlaceValueMois	= ctlMois;
				ctlToPlaceValueAnnee = ctlAnnee;
								
				// use user's date
				if(ctlToPlaceValueJour.value==""||isNaN(ctlToPlaceValueJour.value)) {
					dateSelected = dateNow;
				} else {
					dateSelected = Number(ctlToPlaceValueJour.value);
				} 
				
				if(ctlToPlaceValueMois.value==""||isNaN(ctlToPlaceValueMois.value)) {
					monthSelected =	monthNow;
				} else {
					monthSelected = Number(ctlToPlaceValueMois.value)-1;
				}
				
				if(ctlToPlaceValueAnnee.value==""||isNaN(ctlToPlaceValueAnnee.value)) {
					yearSelected = yearNow;
				} else {
					yearSelected = Number(ctlToPlaceValueAnnee.value);
				}
				
				odateSelected=dateSelected;
				omonthSelected=monthSelected;
				oyearSelected=yearSelected;

				aTag = ctlJour;
				
				if (ie) {
					do {
						aTag = aTag.offsetParent;
						leftpos	+= aTag.offsetLeft;
						toppos += aTag.offsetTop;
					} while(aTag.tagName!="BODY");
					toppos += 40;
				}
				else {
					leftpos	+= aTag.offsetLeft;
					toppos += aTag.offsetTop;
				}

				if (fixedX == -1) {
					crossobj.left = leftpos + "px";
				}
				else {
					crossobj.left = fixedX + "px";
				}
				if (fixedY == -1) {
					crossobj.top = toppos + "px";
				}
				else {
					crossobj.top = fixedY + "px";
				}
				constructCalendar (1, monthSelected, yearSelected);
				crossobj.visibility=(dom||ie)? "visible" : "show";
				
				bShow = true;
			}
		}
		else
		{
			init();
			popUpCalendar(ctlJour,ctlMois,ctlAnnee, positionX, positionY);
		}
	}

	if(ie)
	{
		init();
	}
	else
	{
		window.onload=init;
	}
