/********** Au chargement de la page **********/
daysArray		= new Array("D","L","M","M","J","V","S");
monthArray		= new Array("Jan","Fév","Mar","Avr","Mai","Juin","Juil","Aou","Sep","Oct","Nov","Déc");
VMdateNow		= new Date();
VMMonth			= VMdateNow.getMonth();
VMYear			= VMdateNow.getFullYear();
VMDate			= VMdateNow.getDate();
maxDate			= VMdateNow;
maxDate.setMonth(maxDate.getMonth()+17);
nbFev			= (VMYear%4 == 0) ? 29 : 28;
daysInMonths	= new Array(31,nbFev,31,30,31,30,31,31,30,31,30,31);

/********** Au chargement de la page **********/

function calendar(imgPrev,imgNext,width,bgColor,borderColor,oDate,handler)
{
	this.imgPrev = imgPrev;
	this.imgNext = imgNext;
	this.width = width;
	this.bgColor = bgColor;
	this.borderColor = borderColor;
	this.init = initCalendar;
	this.onselectday = handler;
	this.dateInit = oDate;
	this.container = "";
	this.selectedDate = this.dateInit;
}

/********** Au survol de la souris (où item est l'élément cible passé en paramètre) **********/

function initCalendar(item)
{
	document.getElementById(item).innerHTML = "";

	this.container = item;
	oTab = document.createElement("TABLE");
	oTab.cellPadding = 0;
	oTab.cellSpacing = 0;
	oTab.width = this.width;
	oTab.style.marginBottom = "5px";
	oRow = oTab.insertRow(0);
	oCell = oRow.insertCell(0);
	obj = this;
	if ((this.dateInit.getMonth() != VMMonth) || (this.dateInit.getFullYear() != VMYear)) oCell.innerHTML = "<A href='javascript:prevMonth(obj)'><IMG id=calendarPrev src='" + this.imgPrev + "' border=0></A>";
	oCell = oRow.insertCell(1);
	oCell.align = "center";
	oCell.id = "calendarMonth";
	oCell.style.color = "#000";
	oCell.innerHTML = monthArray[this.dateInit.getMonth()] + " " + this.dateInit.getFullYear();
	oCell = oRow.insertCell(2);
	oCell.align="right";
	if (!((this.dateInit.getMonth()==maxDate.getMonth())&&(this.dateInit.getFullYear()==maxDate.getFullYear()))) oCell.innerHTML = "<A href='javascript:nextMonth(obj)'><IMG id=calendarNext src='" + this.imgNext + "' border=0></A>";
	document.getElementById(item).appendChild(oTab);
	
	oTab = document.createElement("TABLE");
	oTab.cellPadding = 0;
	oTab.cellSpacing = 0;
	oTab.width = this.width;
	oTab.style.border = "1px solid " + this.borderColor;
	oRow = oTab.insertRow(0);
	oRow.bgColor = this.bgColor;
	i = 0;
	while (i < 7) 
	{
		oCell = oRow.insertCell(i);
		oCell.style.border = "1px solid " + this.borderColor;
		oCell.width = this.width/7;
		oCell.align = "center";
		oCell.style.color = "#000";
		oCell.innerHTML = daysArray[i];
		i++;
	}
	oRow = oTab.insertRow(1);
	oRow.bgColor = this.borderColor;
	oCell = oRow.insertCell(0);
	oCell.colSpan = 7;
	oCell.height = 1;
	i = 0;
	dayNow = this.dateInit.getDay();
	dateNow = this.dateInit.getDate();
	dayFirst = dayNow;
	dateFirst = dateNow;
	while (dateFirst > 1)
	{
		dayFirst --;
		if (dayFirst < 0) dayFirst = 6;
		dateFirst--;
	}
	dayFirst--;
	if (dayFirst < 0) dayFirst = 6;
	cpt = 1;
	isOk = false;
	while (i < 6)
	{
		oRow = oTab.insertRow(2+i);
		j = 0;
		while (j < 7)
		{
			isActive = true;
			oCell = oRow.insertCell(j);
			oCell.height = 15;
			oCell.align = "center";
			oCell.id = "calendarR" + i + "C" + j;
			if ((i == 0)&&(!isOk)) isOk = (j == dayFirst);
			daysInMonths[1] = (this.dateInit.getFullYear()%4 == 0) ? 29 : 28;
			if (cpt > daysInMonths[this.dateInit.getMonth()]) isOk = false;
			if ((this.dateInit.getMonth()==VMMonth)&&(this.dateInit.getFullYear()==VMYear)&&(cpt<VMDate)) isActive = false;
			if ((isOk)&&(this.dateInit.getMonth()==this.selectedDate.getMonth())&&(this.dateInit.getFullYear()==this.selectedDate.getFullYear())&&(cpt==this.selectedDate.getDate())) oCell.bgColor = this.bgColor;
			if (isOk)
			{
				if (isActive) {
				oAnchor = document.createElement("A");
				obj = this;
				oAnchor.href = "javascript:obj.selectedDate=new Date("+obj.dateInit.getFullYear()+","+obj.dateInit.getMonth()+","+cpt+");this.bgColor='"+this.bgColor+"';obj.onselectday("+cpt+","+(obj.dateInit.getMonth()+1)+","+obj.dateInit.getFullYear()+");clickOnDate();";
				oAnchor.innerHTML = cpt;
				oAnchor.style.textDecoration = "none";
				oAnchor.style.color = "#000";
				oCell.appendChild(oAnchor);}
				else oCell.innerHTML = "<span style='color:#AAA'>"+cpt+"</span>";
				cpt++;
			}
			else oCell.innerHTML = "&nbsp;";
			j++;
		}
		i++;
	}
	document.getElementById(item).appendChild(oTab);
}

function nextMonth(obj)
{
	oDate = new Date(obj.dateInit.getFullYear(),obj.dateInit.getMonth()+1,15);
	obj.dateInit = oDate;
	obj.init(obj.container);
}

function prevMonth(obj)
{
	oDate = new Date(obj.dateInit.getFullYear(),obj.dateInit.getMonth()-1,15);
	obj.dateInit = oDate;
	obj.init(obj.container);
}

function clickOnDate()
{
	obj.init(obj.container);
}






