var CL = new GnooCalendar("CL", 10, 20 );
/******************************
* GnooCalendar 1.4
* calendrier dynamique en javascript
* v1 	27/04/2004
* v1.1	19/09/2004 + drag&drop
* v1.2	03/10/2004 + bouton de fermeture 
                   + passage de l'ann?en param?e
                   + passage du mois en param?e
* v1.3	25/11/2004 + fermeture automatique ?a s?ction d'une date
				   + correction bug li?u positionnement lors du masquage
* v1.4	09/01/2005 + correction bug scrollbar des listes avec Mozilla
* Auteur : zegnoo
* Contact : gleroy@zegnoo.net
******************************/
function GnooCalendar(n, min, max)
{
	this.target= null;
	this.dateStr=null;
	this.name =  new String(n);
	this.tag =  new String();
	this.title =  new String("GnooCalendar");
	this.date=new Date();
	this.moving = new Boolean(false);
	this.vis = new Boolean(false);
	this.free = new Boolean(true);
	this.curYear = new Number(this.date.getFullYear());
	this.maxYear = new Number(this.curYear+max);
	this.minYear = new Number(this.curYear-min);
	this.curMonth= new Number(this.date.getMonth());
	this.curDay = new Number(this.date.getDay());
/*
* GnooCalendar.setFree(Boolean vis)
* affiche ou non des jours feries
*/
	this.setFree=function(vis)
	{
		this.free=vis;
		if(this.vis==true)
			this.show();
		return;
	}
/*
* GnooCalendar.IsDragable(Boolean drag)
* active/desactive l'option drag&drop 
*/
	this.isDragable=function(drag)
	{
		if(drag==true && !document.layers)
		{
			document.onmousemove=this.getMouse;
			document.getElementById(this.div).root=this;
			document.getElementById(this.div).onmousedown=this.startMove;
			document.getElementById(this.div).onmouseup=this.endMove;
		}
		return;
	}
/*
* GnooCalendar.startMove()
* initialise le drag & drop
*/
	this.startMove=function(evt)
	{
		var event;
		var tmp;
		if(this.root.vis!=true)
			return;
		if(document.all)
		{
			event = window.event;
			tmp = event.srcElement;
			if(!tmp.form)
			{
				this.root.fixX = tmp.offsetLeft+event.offsetX;
				this.root.fixY = event.offsetY;
				while(tmp.parentElement!=document.getElementById(this.root.div) && tmp.parentElement!=null )
				{
					this.root.fixX += tmp.parentElement.offsetLeft;
					this.root.fixY += tmp.parentElement.offsetTop;
					tmp = tmp.parentElement;
				}
				this.root.moving=true;
				self.movingCalendar = this.root;
			}
		}
		else
		{
			event = evt;
			tmp = event.target;
			if( !tmp.form && tmp.tagName!="SELECT" && tmp.tagName!="OPTION" && tmp.tagName!="scrollbar" )
			{			
				this.root.fixX = event.layerX;
				this.root.fixY = event.layerY;
				this.root.moving=true;
				self.movingCalendar = this.root;
			}
		}
		return evt;
	}
/*
* GnooCalendar.endMove()
* termine le drag & drop
*/
	this.endMove=function()
	{
		if(self.movingCalendar!=null)
		{
			self.movingCalendar.moving=false;
		}
		return false;
	}
/*
* ExpertLine.getNextDay(Date date, addDay)
* retourne la date  date + addDays jours 
*/
	this.getNextDay= function addDays(date,addDays){
		var jour=date.substr(0, 2);
		var mois=date.substr(3, 2);
		var annee=date.substr(6, 4);
		date = new Date(parseInt(annee,10), parseInt(mois,10)-1, parseInt(jour,10)+addDays);

		var moisN = date.getMonth()+1;
		if(moisN<10){
			moisN = '0' +  moisN;
		}
		var jourN = date.getDate();
		if(jourN<10){
			jourN = '0' +  jourN;
		}
		dateS = jourN + "/" + moisN + "/" + date.getFullYear();
		return dateS;
	}
/*
* GnooCalendar.getMouse(Event evt)
* change la liste des jours feri?jours feries
*/
	this.getMouse=function(evt)
	{
		
		var event;
		if(document.all)
		{
			event = window.event;
			var scrollObj = (document.documentElement) ? document.documentElement : document.body;
			this.posX = self.event.clientX+scrollObj.scrollLeft;
			this.posY = self.event.clientY+scrollObj.scrollTop;
		}
		else
		{
			event = evt;
			this.posX = evt.pageX ;
			this.posY = evt.pageY ;
		}
		if(self.movingCalendar!=null)
		{
			if(self.movingCalendar.moving==true && self.movingCalendar.vis==true )
				self.movingCalendar.move(this.posY-self.movingCalendar.fixY, this.posX-self.movingCalendar.fixX);
		}
		return false;
	}
/*
* GnooCalendar.move(Number x, Number y)
* d?ace le calendrier
*/
	this.move=function(x, y)
	{
		if(!document.layers)
		{
			document.getElementById(this.div).style.top = x+"px";
			document.getElementById(this.div).style.left = y+"px";
		}
		return false;
	}

/*
* GnooCalendar.setFreeDay(Array fd)
* change la liste des jours feri?jours feries
*/
	this.setFreeDay=function(fd)
	{
		this.freeday.length=0;
		for(var i=0; i<fd.length; i++)
			this.freeday[i] = new String(fd[i]);
		if(this.vis==true)
			this.show();
		return;
	}
/*
* GnooCalendar.isFreeDay(d,m)
* true si jour feri?*/
	this.isFreeDay=function(d,m)
	{
		var tmp = this.checkDate(d)+"/"+this.checkDate(parseInt(m)+1);
		if(this.free==false)
			return false;
		for(var i=0; i<this.freeday.length; i++)
			if( tmp == this.freeday[i])
				return true;
		return false;
	}
	
/*
* GnooCalendar.isCurrentDay(d,m)
* true si jour courant*/
	this.isCurrentDay=function(d,m,y)
	{
		var today = new Date();
		var annee = today.getFullYear();

		var tmp = this.checkDate(d)+"/"+this.checkDate(parseInt(m)+1)+"/"+this.checkDate(parseInt(y));
		var date_select = this.dateStr;
		if(tmp == date_select)
			return true;
	}

	
	
/*
* GnooCalendar.setTitle(t)
* change le titre du calendrier
*/
	this.setTitle=function(t)
	{
		this.title = t;
		if(this.vis==true)
			this.show();
		return;
	}
/*
* GnooCalendar.mList()
* retourne la liste des mois 
*/
	this.mList=function()
	{
		var tmp = "<table border='0' cellpadding='0' cellspacing='0' class='Gtab'>";
	
		tmp += "<tr><td align='center' colspan='2' class='Gname'>";
/*		if(document.layers)
		{
				tmp += "<table width='100%'>";
				tmp += "<tr>";
				tmp += "<td class='Gname' align='center'>"+this.title+"</td>";
				tmp += "<td><a href='javascript://' onclick='"+this.name+".hide();' class='Gname'>X</a></td>";
				tmp += "</tr>";
				tmp += "</table>";
		}
		else
		{
			tmp += "<span style='float:left; text-indent:20px;'>"+this.title+"</span>";
			tmp += "<span style='float:right;'>";
			tmp += "<a href='javascript://' onclick='"+this.name+".hide();' class='Gname'>X</a>";
			tmp += "</span>";
		} */
		tmp += "</td></tr>";
		tmp += "<tr>";
		tmp += "<td class='Gtxt'><select name='"+this.name+"month' class='Gtxt' ";
		tmp += "onchange='"+this.name+".setMonth(this[this.selectedIndex].value)' >\n";
		for(var i=0; i<this.month.length; i++)
		{			
			tmp += "<option value='"+i+"'";
			if(this.curMonth==i)
				tmp += " selected";
			tmp += ">"+ this.month[i] +"</option>\n";
		}
		tmp += "</select></td><td class='Gtxt'>\n";
		tmp += this.yList();
		tmp += "</td></tr></table>";
		tmp += this.dList();
		return tmp;
	}
/*
* GnooCalendar.yList()
* retourne la liste des ann? 
*/
	this.yList=function()
	{
		var tmp = "<select name='"+this.name+"year' class='Gtxt' ";
		tmp += "onchange='"+this.name+".setYear(this[this.selectedIndex].value);' >\n";
		for(var i=this.minYear; i<=this.maxYear; i+=1)
		{
			tmp += "<option value='"+i+"'";
			if(this.curYear==i)
				tmp += " selected";
			tmp += ">"+ i +"</option>\n";
		}
		tmp += "</select>\n";
		return tmp;
	}

	this.dayCell=function(d,n)
	{
		var tmp = new String("");
		if(this.isCurrentDay(d,this.curMonth,this.curYear)){
			tmp += "<td class='GCD'";
		}else{
			if(this.isFreeDay(d,this.curMonth))
				tmp += "<td class='Gfree'";
			else
				tmp += "<td class='Gc"+n+"'";
		}
		if(!document.layers)
		{
			tmp += "title='"+this.checkDate(d)+" "+this.month[this.curMonth]+" "+this.curYear+"'";
			tmp += " onmousedown='"+this.name+".getDate(\"";
			tmp += this.checkDate(d)+"/";
			tmp += this.checkDate(1+parseInt(this.curMonth))+"/";
			tmp += this.curYear+"\"";
			tmp += ");' ";
			tmp += " onmouseover='this.className=this.className+\"on\";' ";
			tmp += " onmouseout='this.className= this.className.substring(0,this.className.indexOf(\"on\"));'";
		}
		else
			tmp += " width='22' height='16' ";
		tmp += ">";
		return tmp;
	}
/*
* GnooCalendar.dList()
* retourne le tableau des jours
*/
	this.dList=function()
	{
		var cur=new Number(1);
		var tmpDate = new Date();
		var tmp = new String("<table border='0' cellpadding='0' cellspacing='0' class='Gtab'>");
		tmpDate.setDate(cur);
		tmpDate.setMonth(this.curMonth);
		tmpDate.setFullYear(this.curYear);
		tmp += "<tr>\n";
		for(var i=1; i<this.day.length; i++)
			tmp += "<td class='Gh"+i+"'>"+ this.day[i] +"</td>\n";
		tmp += "<td class='Gh0'>"+ this.day[0] +"</td>\n";
		
		var myDate = new Date ();
 		var myMonth = myDate.setMonth (tmpDate.getMonth() + 1);
 		var theDay = myDate.setDate (0);
 		var lastDay = myDate.getDate ();
		
		tmp += "<td></td>\n";
		
		tmp += "</tr>\n";
		
		for(var j=0; j<6; j++)
		{
			tmp += "<tr>\n";
			var atLeastOneDay=false;
			var thereIsAMonday=false;
			for(var i=1; i<this.day.length; i++)
			{
				
				tmpDate.setDate(cur);			
				if( cur<=31 && i==tmpDate.getDay() && this.curMonth==tmpDate.getMonth())
				{
					if(i==1) {
						//There will be an export button at the end of this colomn
						thereIsAMonday=true;
						//Prepare dates for word export
						dateD= this.getUrl(cur);
						dateF= this.getNextDay(dateD,6);
						
					}
					tmp += this.dayCell(cur,i);
					tmp += this.getLink(cur);
					cur+=1;
					atLeastOneDay=true;	
				}
				else
				{
					tmp += "<td class='Gc"+i+"'";
					tmp += ">&nbsp;";
					
				}
				tmp += "</td>\n";
				tmpDate.setDate(cur);
			}
			if( cur==tmpDate.getDate() && this.curMonth==tmpDate.getMonth())
			{
				tmp += this.dayCell(cur,0);
				tmp += this.getLink(cur);
				cur+=1;
			}
			else
			{
				tmp += "<td class='Gc0'";
				tmp += ">&nbsp;";
			}
			if(thereIsAMonday){
				tmp += "</td>\n<td></td></tr>\n";
			}else{
				tmp += "</td>\n<td></td></tr>\n";
			}
		}
		tmp += "</table>\n";
		return tmp;
	}
/*
* GnooCalendar.getLink(c)
* retourne la balise d'un lien
*/
	this.getLink = function(c)
	{
		var tmp = new String("");
		if(document.layers)
		{
			tmp = "<a href='javascript://' ";
			tmp += "onclick='"+this.name+".getDate(\"";
			tmp += this.getUrl(c)+"\"";
			tmp += ");' class='NSday'>"+(c)+"</a>";
		}
		else
		{
			tmp = (c);
		}
		return tmp;
	}
/*
* GnooCalendar.getLink(c)
* retourne la balise d'un lien
*/
this.getUrl = function(c)
{
	var tmp = new String("");
	tmp = this.checkDate(c)+"/";
	tmp += this.checkDate(1+parseInt(this.curMonth))+"/";
	tmp += this.curYear;
	return tmp;
}
/*
* GnooCalendar.setMonth( Integer m )
* modifie le mois ?fficher
*/
	this.setMonth = function(m)
	{
		
		this.curMonth = m;
		this.show();
		return;
	}
/*
* GnooCalendar.getYear( Integer y )
* modifie l'ann??fficher
*/
	this.getYear = function (y)
	{
		if(document.layers)
		{
			for(var i=0; i<document.layers[this.div].document.forms[this.name+"_form"][this.name+"year"].length; i++)
			{
				if(document.layers[this.div].document.forms[this.name+"_form"][this.name+"year"][i].value==y)
				{
					document.layers[this.div].document.forms[this.name+"_form"][this.name+"year"].selectedIndex=i;
					this.setYear(y);
					return;
				}
			}
		}
		else
		{
			for(var i=0; i<document.forms[this.name+"_form"].elements[this.name+"year"].length; i++)
			{
				if(document.forms[this.name+"_form"].elements[this.name+"year"][i].value==y)
				{
					document.forms[this.name+"_form"].elements[this.name+"year"].selectedIndex=i;
					this.setYear(y);
					return;
				}
			}
		}
		return;
	}
/*
* GnooCalendar.getMonth( Integer m )
* modifie le mois ?fficher
*/
	this.getMonth = function (d)
	{
		if(document.layers)
		{
			for(var i=0; i<document.layers[this.div].document.forms[this.name+"_form"][this.name+"month"].length; i++)
			{
				if(document.layers[this.div].document.forms[this.name+"_form"][this.name+"month"][i].value==d)
				{
					document.layers[this.div].document.forms[this.name+"_form"][this.name+"month"].selectedIndex=i;
					this.setMonth(d);
					return;
				}
			}
		}
		else
		{
			for(var i=0; i<document.forms[this.name+"_form"].elements[this.name+"month"].length; i++)
			{
				if(document.forms[this.name+"_form"].elements[this.name+"month"][i].value==d)
				{
					document.forms[this.name+"_form"].elements[this.name+"month"].selectedIndex=i;
					this.setMonth(d);
					return;
				}
			}
		}
		return;
	}
/*
* GnooCalendar.setYear( Integer y )
* modifie l'ann??fficher
*/
	this.setYear = function (y)
	{
		this.curYear = y;
		this.show();
		return;
	}
/*
* GnooCalendar.setTarget( Object obj )
* change le champs cible d'affichage de la date
* BUG OPERA!
*/
	this.setTarget = function (obj)
	{
		this.target = obj;
		return;
	}
/*
* GnooCalendar.hide()
* masque le calendrier
*/
	this.hide = function()
	{
		if(document.layers)
			document.layers[this.div].visibility='hide';
		else
		{
			document.getElementById(this.div).style.visibility = 'hidden';
			document.getElementById(this.div).style.display = 'none';
		}
		this.vis = false;
		this.endMove();
		return;
	}

/*
* GnooCalendar.getDate()
* retourne la date selectionn?dans le champs cible
*/
	this.getDate = function(d)
	{
		if(this.target!=null)
		{
			this.target.value=d;
			var reload="false";
			var dateLinkT=d;
			var jour=dateLinkT.substr(0, 2);
			var mois=dateLinkT.substr(3, 2);
			var annee=dateLinkT.substr(6, 4);
			dateLinkd = new Date(parseInt(annee,10), parseInt(mois,10)-1, parseInt(jour,10));
			window.location.replace('/screens/agenda/sw_show_agenda.php?dateSelectionnee='+d+'&dateAgenda='+dateLinkd.getTime()/1000+'#'+d);
			//this.hide();
		}
		return;
	}

/*
* GnooCalendar.show()
* affiche le calendrier
*/
	this.show = function()
	{
		this.vis = true;
	//	this.tag = "<form name='"+this.name+"_form' method='post'>\n";
	//	this.tag += this.mList();
	//	this.tag += "</form>\n";
		this.tag = this.mList();
		if(document.layers)
		{
			with(document.layers[this.div])
			{				
				document.open("text/html");
				document.write(this.tag);
				document.close();
				visibility='show';
			}
		}
		else
		{
			document.getElementById(this.div).innerHTML = ""+this.tag;
			document.getElementById(this.div).style.visibility = 'visible';
			document.getElementById(this.div).style.display = 'block';
		}
		return;
	}
/*
* GnooCalendar.init( String d )
* initialise le Calendrier ?a date actuelle
* param?es : 
* d : nom du calque d'affichage
* obj : objet dont la propri? value va recevoir le String de la date
*/
	this.init = function(d, obj)
	{
		var dateOld=obj;
		var jour=dateOld.substr(0, 2);
		var mois=dateOld.substr(3, 2);
		var annee=dateOld.substr(6, 4);
		//alert(jour+"-"+mois+"-"+annee);
		this.div=d;
		this.target = obj;
		this.date = new Date(parseInt(annee,10), parseInt(mois,10)-1, parseInt(jour,10));
		this.dateStr = obj;
		this.curMonth = this.date.getMonth();
		this.curYear = this.date.getFullYear();
		if(!self.movingCalendar)
			self.movingCalendar=null;
		return;
	}
/*
* GnooCalendar.checkDate( Integer d )
* param?e : le jour d'une date
*/
	this.checkDate = function(d)
	{
		if(parseInt(d)<=9)
			return "0"+parseInt(d);
		return parseInt(d);
	}
	return this;
}
function loadCalendar(dateInit){
	if(dateInit==null){
		var dateJour = new Date();
		var jour = dateJour.getDate();
		if(jour<10) jour = "0"+jour;
		var mois = dateJour.getMonth()+1;
		if(mois<10) mois = "0"+mois;
		dateInit= jour+"/"+mois+"/"+dateJour.getFullYear();	
	}
	CL.init("calend", dateInit);
	CL.isDragable(false);
	CL.show();
	//alert(document.getElementById('calend').innerHTML);
}
/******************************/
GnooCalendar.prototype.day = ["S", "M", "T", "W", "T", "F", "S" ];
GnooCalendar.prototype.month = ["January", 
				"February", 
				"March", 
				"April", 
				"May", 
				"June", 
				"July", 
				"August", 
				"September", 
				"October", 
				"November", 
				"December"];
/* jours feri?fran?s */
GnooCalendar.prototype.freeday= [];
/******************************/