daysArray = new Array("L","M","M","J","V","S","D");
monthArray = new Array("Jan","Fev","Mar","Avr","Mai","Juin","Juil","Aout","Sept","Oct","Nov","Dec");

VMdateNow = new Date();
VMMonth = VMdateNow.getMonth();
VMYear = VMdateNow.getFullYear();
VMDate = VMdateNow.getDate();
nbFev = (VMYear%4 == 0) ? 29 : 28;
daysInMonths = new Array(31,nbFev,31,30,31,30,31,31,30,31,30,31);
function calendar(imgPrev,imgNext,imgPrevYear,imgNextYear,width,bgColor,borderColor,oDate,handler)
{
	this.imgPrev      = imgPrev;
	this.imgNext      = imgNext;
	this.imgPrevYear  = imgPrevYear;
	this.imgNextYear  = imgNextYear;
	this.width        = width;
	this.bgColor      = bgColor;
	this.borderColor  = borderColor;
	this.init         = initCalendar;
	this.onselectday  = handler;
	this.dateInit     = oDate;
	this.container    = "";
	this.selectedDate = this.dateInit;
}

function initCalendar(item)
{
	document.getElementById(item).innerHTML = "";
	
	this.container = item;
	calendarSubWindow="\"/system/object-catalog/editor/viewers/sub-calendar.xml?month=" + this.dateInit.getMonth() + "&year=" + this.dateInit.getFullYear() + "\"";
	oTab = document.createElement("TABLE");
	oTab.cellPadding = 0;
	oTab.cellSpacing = 0;
	oTab.width = this.width;
	oTab.style.marginBottom = "5px";
	oRow = oTab.insertRow(0);
	obj = this;
	oCell = oRow.insertCell(0);
	oCell.align = "left";
	oCell.innerHTML = "<A href='javascript:prevYear(obj)'><IMG border='0' id=calendarPrevY src='" + this.imgPrevYear + "'></a>";
	oCell = oRow.insertCell(1);
	oCell.align = "left";
	oCell.id = "calendarMonth";
	oCell.innerHTML = "<A href='javascript:prevMonth(obj)'><IMG border='0' id=calendarPrev src='" + this.imgPrev + "'></a>";
	oCell = oRow.insertCell(2);
	oCell.align = "center";
	oCell.id = "calendarMonth";
	oCell.innerHTML = "<A href='javascript:goToday(obj)'><img src='/graphics/clock.gif' border='0'></A>";
	oCell = oRow.insertCell(3);
	oCell.align = "center";
	oCell.id = "calendarMonth";
	oCell.innerHTML = monthArray[this.dateInit.getMonth()] + " " + this.dateInit.getFullYear();
	oCell = oRow.insertCell(4);
	oCell.align = "center";
	oCell.id = "calendarMonth";
	oCell.innerHTML = "<img src='/graphics/cancel.gif' border='0'>";
	oCell = oRow.insertCell(5);
	oCell.align="right";
	oCell.innerHTML = "<A href='javascript:nextMonth(obj)'><IMG border='0' id=calendarNext src='" + this.imgNext + "'></a>";
	oCell = oRow.insertCell(6);
	oCell.align="right";
	oCell.innerHTML = "<A href='javascript:nextYear(obj)'><IMG border='0' id=calendarNextY src='" + this.imgNextYear + "'></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.width = this.width/7;
		oCell.align = "center";
		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)
		{
			oCell = oRow.insertCell(j);
			oCell.height = 20;
			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 ((isOk)&&(this.dateInit.getMonth()==this.selectedDate.getMonth())&&(this.dateInit.getFullYear()==this.selectedDate.getFullYear())&&(cpt==this.selectedDate.getDate())) oCell.bgColor = this.bgColor;
			if (isOk) 
			{
				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.style.color = 'black';
				//oAnchor.onclick = clickOnDate;
				oAnchor.innerHTML = cpt;
				oAnchor.style.textDecoration = "none";
				oCell.appendChild(oAnchor);
				if (isToday(new Date(obj.dateInit.getFullYear(),obj.dateInit.getMonth(),cpt)))
				{
					oCell.style.background = "silver";
				}
				cpt++;
			}
			else oCell.innerHTML = "&nbsp;";
			j++;
		}
		i++;
	}
	document.getElementById(item).appendChild(oTab);
}
function isToday(obj)
{
	today = new Date();
	if (obj.getFullYear() == today.getFullYear() &&
	    obj.getMonth()    == today.getMonth()    &&
	    obj.getDate()     == today.getDate())
	{
		return true;
	}
	else
	{
		return false;
	}
}
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 nextYear(obj)
{
	oDate = new Date(obj.dateInit.getFullYear()+1,obj.dateInit.getMonth(),15);
	obj.dateInit = oDate;
	obj.init(obj.container);
}
function prevYear(obj)
{
	oDate = new Date(obj.dateInit.getFullYear()-1,obj.dateInit.getMonth(),15);
	obj.dateInit = oDate;
	obj.init(obj.container);
}
function goToday(obj)
{
	oDate = new Date();
	obj.dateInit = oDate;
	obj.init(obj.container);
}
function clickOnDate()
{
	obj.init(obj.container);
}