function calendar(elm)
{	
	var r,c,a,i;
	var _this=this;
	this.host=elm;
	this.now=new Date();
	this.days=['Lu','Ma','Me','Je','Ve','Sa','Di'];
	this.months=['Janvier','F&eacute;vrier','Mars','Avril','Mai','Juin','Juillet','Ao&ucirc;t','Septembre','Octobre','Novembre','D&eacute;cembre'];
	this.table=document.createElement('table');
	this.table.className='jsCalendar';
	
	this.thead=this.table.createTHead();
	this.tbody=this.table.appendChild(document.createElement('tbody'));
	this.tbody.onclick=function(){_this.toggle()}
	
	r=this.thead.insertRow(-1);
	c=r.insertCell(-1);
	a=c.appendChild(document.createElement('a'));
	a.innerHTML='&lt;&lt;';
	a.onclick=function(){
		_this.now.setDate(0);
		_this.draw();
	}
	a.onselectstart=function(){return false}
	
	this.month=r.insertCell(-1);
	this.month.colSpan=5;
	
	c=r.insertCell(-1);
	a=c.appendChild(document.createElement('a'));
	a.innerHTML='&gt;&gt;';
	a.onclick=function(){
		_this.now.setDate(1);
		_this.now.setMonth(_this.now.getMonth()+1);
		_this.draw();
	}
	a.onselectstart=function(){return false}
	
	r=this.thead.insertRow(-1);
	r.className='days';
	for(i=0;i<this.days.length;i++){
		c=r.insertCell(-1);
		c.innerHTML=this.days[i];
	}
	
	this.draw();
	
	this.host.readOnly=true;
	this.table.style.position='absolute';
	elm.parentNode.style.position='relative';
	elm.parentNode.appendChild(this.table);
	this.visible=true;
}
calendar.prototype.draw=function()
{
	var r,c,d,m,y,i;
	var _this=this;
	dow=function(d){return (d.getDay()+6)%7}
	l0=function(n){return (n<10?'0':'')+n}
	
	m=this.now.getMonth();
	y=this.now.getFullYear();
	d=new Date(y,m,1);
	this.month.innerHTML=this.months[m]+' '+y;
	
	while(this.table.rows.length>2)
		this.table.deleteRow(2);
	
	r=this.tbody.insertRow(-1);
	for(i=0;i<dow(d);i++){
		c=r.insertCell(-1);
		c.innerHTML='-';
	}
	
	do{
		var day=d.getDate();
		c=r.insertCell(-1);
		c.innerHTML=day;
		c.returnValue=l0(day)+'-'+l0(m+1)+'-'+y;
		c.onclick=function(){_this.host.value=this.returnValue}
		if(dow(d)==6)
			r=this.tbody.insertRow(-1);
		d.setDate(day+1);
	}
	while(d.getDate()>1);
	
	if(dow(d)>0){
		for(i=6;i>=dow(d);i--){
			c=r.insertCell(-1);
			c.innerHTML='-';
		}
	}
}
calendar.prototype.toggle=function()
{
	this.visible=!this.visible;
	this.table.style.display=this.visible?'block':'none';
}
