/********************************************************/
function CLCalendar(voCtrl,voIFrame,bNoStart)
{
	this.oCtrl=voCtrl;
	this.oIFrame=voIFrame;
	this.iYear=0;
	this.iMonth=0;
	this.iDay=0;
	this.bValide=false;
	this.tMonthName = Array("","janvier","f&eacute;vrier","mars","avril","mai","juin","juillet","ao\xfbt","septembre","octobre","novembre","d\xe9cembre");
	this.oldpos=0;
	this.savClassName="";
	this.oWin=null;
	this.oldDate=new Date();
	this.curItemSel=-1;
	this.bkgColor="#FFFFFF";
	this.szFlecheDroitePath="/images/flechedroite.gif";
	this.szFlecheGauchePath="/images/flechegauche.gif";
	this.headerStyle="text-align:right;font-family:MS sans serif;font-size:8pt";
	this.monthStyle="font-family:arial;font-size:8pt;text-align=center;background-color:#C2BFA5";
	this.dateStyle="cursor:hand;font-family: Verdana;font-size:8pt;color:#000000;text-decoration: none";
	this.badDateStyle="cursor:hand;font-family: Verdana;font-size:8pt;color:#666666;text-decoration: none";
	this.todayStyle="cursor:hand;font-family: Verdana;font-size:8pt;color:#000000;text-decoration: underline";
	this.hiliteDateStyle="cursor:hand;font-family: Verdana;font-size:8pt;color:#990000;text-decoration: none";
	this.selDateStyle="cursor:hand;font-family: Verdana;font-size:8pt;color:#000000;font-weight:bold;text-decoration: none";
	this.eventDateHasChanged=null;
	this.tHiliteDate=null;

	/***************************/
	this.showCalendar=function()
	{
		var szOldDate =  this.oCtrl.value;
		oRegEx=new RegExp("^([0123]\\d)\\/([01]\\d)\\/(\\d{4})$");
		tResult=oRegEx.exec(szOldDate);
		this.oldDate.setHours(0,0,0,0);
		if (oRegEx.lastIndex!=0) this.oldDate=new Date(tResult[3],tResult[2]-1,tResult[1]);
		this.iDay = this.oldDate.getDate();
		this.iMonth = this.oldDate.getMonth();
		this.iYear = this.oldDate.getFullYear();
		this.createCalendar(this.iYear,this.iMonth);
	}
	/***************************/
	this.createCalendar=function(vYear,vMonth)
	{
		this.iYear=vYear;
		this.iMonth=vMonth;
		this.curItemSel=-1;
		this.oldpos=0;
		var html = "<title>Calendrier</title><body bgcolor='"+this.bkgColor+"'>";
		html+="<STYLE>";
		html+=".headerStyle{"+this.headerStyle+"}";
		html+=".monthStyle{"+this.monthStyle+"}";
		html+=".dateStyle{"+this.dateStyle+"}";
		html+=".badDateStyle{"+this.badDateStyle+"}";
		html+=".todayStyle{"+this.todayStyle+"}";
		html+=".hiliteDateStyle{"+this.hiliteDateStyle+"}";
		html+=".selDateStyle{"+this.selDateStyle+"}";
		html+="</STYLE>";
		html+=this.createMonthBar();
		html+=this.createDaysTable();
	  
		if (this.oIFrame!="")
		{
			this.oIFrame.document.body.style.backgroundColor=this.bkgColor;			
			this.oIFrame.document.body.innerHTML=html;
			this.oWin=this.oIFrame;
			this.oWin.oCalendar=this;
			if (this.curItemSel>0) this.selectedDay(this.curItemSel);
		}
		else
		{
			this.oWin = window.open("","Calendrier","width=230,height=150,titlebar=no");
			this.oWin.document.open();
			this.oWin.document.writeln(html);
			this.oWin.document.close();
			this.oWin.oCalendar=this;
			if (this.curItemSel>0) this.selectedDay(this.curItemSel);
			this.oWin.focus();
		}
	}
	/***************************/
	this.createMonthBar=function()
	{
		var newMonthDec=this.iMonth-1;
		var newYearDec=this.iYear;
		var newMonthInc=this.iMonth+1;
		var newYearInc=this.iYear;
	
		if (newMonthDec<0)
		{
			newMonthDec=11;
			newYearDec=this.iYear-1;
		}
		if (newMonthInc==12)
		{
			newMonthInc=0;
			newYearInc=this.iYear+1;
		}
		
		var szRetour="<table width='100%'  bgcolor='"+this.bkgColor+"' cellpadding='0' cellspacing='0'>";
		szRetour+="<tr>";
		szRetour+="<td class='monthStyle' align='left' width='15%'><a href='javascript:oCalendar.createCalendar("+newYearDec+","+newMonthDec+");'><img alt='mois pr\xe9c\xe9dent' border='0' src='"+this.szFlecheGauchePath+"'></a></td>";
		szRetour+="<td class='monthStyle' width='70%' colspan='7'>"+this.tMonthName[this.iMonth+1]+" "+this.iYear+"</td>";
		szRetour+="<td class='monthStyle' align='right' width='15%'><a href='javascript:oCalendar.createCalendar("+newYearInc+","+newMonthInc+");'><img alt='mois suivant' border='0' src='"+this.szFlecheDroitePath+"'></a></td>";
		szRetour+="</tr>";
		return szRetour;
	}
	/***************************/
	this.createDaysTable=function()
	{
		var szRetour = "";
		var CurDate = new Date();
		CurDate.setHours(0,0,0,0);
		
		var theDate = new Date();
		theDate.setDate(1);
		theDate.setYear(this.iYear);
		theDate.setMonth(this.iMonth);
		theDate.setHours(0,0,0,0);

		var LastDate = new Date(theDate.getFullYear(),theDate.getMonth()+1,theDate.getDate()-1);
		LastDate=this.addDays(LastDate,6-this.DayFromMonday(LastDate));
		theDate=this.addDays(theDate,-this.DayFromMonday(theDate));

		if (this.tHiliteDate!=null)
		{
			for (var j=0;j<this.tHiliteDate.length;j++)
			{
				if (this.tHiliteDate[j]!=null && this.tHiliteDate[j].constructor()=='')
				{
					this.tHiliteDate[j]=this.convertToDate(this.tHiliteDate[j]);
				}
			}
		}
		
		szRetour+="<tr>";
		szRetour+="<td width='15%'>&nbsp;</td>";
		szRetour+="<td width='10%' class='headerStyle'>l</td>";
		szRetour+="<td width='10%' class='headerStyle'>m</td>";
		szRetour+="<td width='10%' class='headerStyle'>m</td>";
		szRetour+="<td width='10%' class='headerStyle'>j</td>";
		szRetour+="<td width='10%' class='headerStyle'>v</td>";
		szRetour+="<td width='10%' class='headerStyle'>s</td>";
		szRetour+="<td width='10%' class='headerStyle'>d</td>";
		szRetour+="<td width='15%'>&nbsp;</td>";
		szRetour+="</tr>";
		szRetour+="<tr>";
		szRetour+="<td width='15%'><img height='1' src='/images/vide.gif'></td>";
		szRetour+="<td width='70%' colspan='7' class='monthStyle'><img height='1' src='/images/vide.gif'></td>";
		szRetour+="<td width='15%'><img height='1' src='/images/vide.gif'></td>";
		szRetour+="</tr>";
		szRetour+="<tr>";
	
		var i=0;
		while((i<2 && theDate.getDay()>0) || (Date.parse(theDate)<=Date.parse(LastDate)))
		{
			var bToday=Date.parse(theDate)==Date.parse(CurDate);
			var bHilite=false;
			
			if (this.tHiliteDate!=null)
			{
				for (var j=0;j<this.tHiliteDate.length;j++)
				{
					if (this.tHiliteDate[j]!=null && Date.parse(this.tHiliteDate[j])==Date.parse(theDate))
					{
						bHilite=true;
						break;
					}
				}
			}
			
			if (Date.parse(theDate)==Date.parse(this.oldDate)) this.curItemSel=i;
			if (theDate.getDay()==1) szRetour+="<tr><td width='15%'><img height='1' src='/images/vide.gif'></td>";
			szRetour+="<td align='right'><div id='dv"+i+"' class='dateStyle'><a id='ln"+i+"' class='";
			szRetour+= (bToday) ? "todayStyle":(bHilite ? "hiliteDateStyle" :(theDate.getMonth()!=this.iMonth ? "badDateStyle" : "dateStyle"));
			if (theDate.getMonth()!=this.iMonth)
				szRetour+="' href='javascript:void(0);' border='0'>";
			else
				szRetour+="' href='javascript:oCalendar.selectedDay("+i+");oCalendar.bValide=true;oCalendar.majCtrl(\""+theDate+"\");' border='0'>";
			szRetour+=theDate.getDate()+"</a></div>";
			szRetour+="</td>";
			if (theDate.getDay()==0) szRetour+="<td width='15%'><img height='1' src='/images/vide.gif'></td></tr>";
			theDate=this.addDays(theDate,1);
			i++;
		}
		szRetour+="</table>";
		return szRetour;
	}
	/***************************/
	this.selectedDay=function(iDiv)
	{
		if (this.oldpos>0)
		{
			this.oWin.document.getElementById("dv"+this.oldpos).className=this.savClassName;
			this.oWin.document.getElementById("ln"+this.oldpos).className=this.savClassName;
		}
		this.oldpos=iDiv;
		this.savClassName=this.oWin.document.getElementById("dv"+this.oldpos).className;
		this.oWin.document.getElementById("ln"+this.oldpos).className="selDateStyle";
		this.oWin.document.getElementById("dv"+this.oldpos).className="selDateStyle";
	}
	/***************************/
	this.addDays=function(vDate,nbDays)
	{
		var iDay=vDate.getDate()+nbDays;
		var iMonth=vDate.getMonth();
		var iYear=vDate.getFullYear();
		var oDate=new Date(iYear,iMonth,iDay);
		return oDate;
	}
	/***************************/
	this.DayFromMonday=function(dtDate)
	{
		var iDay=dtDate.getDay();
		if (iDay==0) iDay=7;
		iDay--;
		return iDay;
	}
	/***************************/
	this.convertToDate=function(szDate)
	{
		var oDate=null;
		var oRegEx=new RegExp("^([0123]\\d)\\/([01]\\d)\\/(\\d{4})$");
		var tResult=oRegEx.exec(szDate);
		if (oRegEx.lastIndex!=0)
		{
			oDate=new Date(tResult[3],tResult[2]-1,tResult[1]);
			oDate.setHours(0,0,0,0);
		}
		return oDate;
	}
	/***************************/
	this.convertToString=function(oDate)
	{
		var vDay=oDate.getDate();
		var vMonth=oDate.getMonth()+1;
		var vYear=oDate.getFullYear();
		
		var szDate=(vDay<10 ? '0'+vDay:vDay)+'/';
		szDate+=(vMonth<10 ? '0'+vMonth:vMonth)+'/';
		szDate+=vYear;
		return szDate;
	}
	
	/***************************/
	this.majCtrl=function(szDate)
	{
		if (this.bValide)
		{
			var oDate=new Date(szDate);
			this.iDay=oDate.getDate();
			this.iMonth=oDate.getMonth()+1;
			this.iYear=oDate.getFullYear();
			this.oCtrl.value=this.convertToString(oDate);
			if (null!=this.eventDateHasChanged) this.eventDateHasChanged(this.oCtrl);
		}
		this.oWin.close();
	}
	if (bNoStart==undefined || bNoStart==false) this.showCalendar();
}

