/***************************************/
/* Nom du script : calendar_fonc v1.0  */
/* Auteur: Frédéric REMISE  (Derf)     */
/* Date de création: 14/01/2003        */
/* Email : frederic.remise@wanadoo.fr  */
/***************************************/
d = window.document;

aujourdhui = new Date();
aujourdhuiJ = aujourdhui.getDate();		//Jour contractuel
aujourdhuiM = aujourdhui.getMonth();		//Mois contractuel
aujourdhuiA = aujourdhui.getFullYear();		//Année contractuellle

cejour = null;
cemois = null;
cetan = null;

firstUse = false;						//Deja une utilisation avec selection de date (o/n)
nav=false;							//Mode navigation (o/n)
navencours=false;						//Navigation en cours (o/n)
nodeVisible=false;						//Visibilité du node TABLE (0/n)

moisSelect=aujourdhuiM;						//Mois selectionné
anSelect=aujourdhuiA;						//Année selectionnée

function fDrawTable(top_pos,left_pos,chemin)
{
	//window.alert(chemin);
	//Def. styles DIV id=ECRAN 
	with(d.getElementById('ecran').style)
	{
		position = 'absolute';
		width = '127px';
		height = '141px';

	// Initialisation de la position du calendrier dans la page
		left = left_pos+'px';
		top = top_pos+'px';
	}
	
	//Structure HTML du tableau Calendrier
	tbHTML = "<table id='tbCal' border='0' bgcolor='#FFFFFF' CELLPADDING='0' CELLSPACING='0'>"
	
	+ "<tr><td><IMG SRC='"+chemin+"/puce_vert.gif' BORDER='0' WIDTH='1' HEIGHT='1'></td><td colspan='7' background='"+chemin+"/puce_vert.gif'></td><td><IMG SRC='"+chemin+"/puce_vert.gif' BORDER='0' WIDTH='1' HEIGHT='1'></td></tr>"
	
	+ "<tr class='P1B'><td rowspan='11' background='"+chemin+"/puce_vert.gif' width='1'></td><td width='18' height='18' align='center'><a href='#' onClick='fCalendrier(mois-=1,true)' title='Mois précédent'><IMG SRC='"+chemin+"/b_aro_left.gif' BORDER='0' style='filter:alpha(opacity=50)' onmouseover='nereidFade(this,100,30,30)' onmouseout='nereidFade(this,50,20,10)' ALT='Mois précédent'></a></td><td id='lemois' colspan='5' align='center'></td><td width='18' align='center'><a href='#' border='0' onClick='fCalendrier(mois+=1,true)' title='Mois suivant'><IMG SRC='"+chemin+"/b_aro_right.gif' BORDER='0' ALT='Mois suivant' style='filter:alpha(opacity=50)' onmouseover='nereidFade(this,100,30,30)' onmouseout='nereidFade(this,50,20,10)' alt='Mois suivant'></a></td><td rowspan='11' background='"+chemin+"/puce_vert.gif' width='1'></td></tr>"
	
	+ "<tr class='P1B' align='center'><td width='18' height='18'>S</td><td width='18'>D</td><td width='18'>L</td><td width='18'>M</td><td width='18'>M</td><td width='18'>J</td><td width='18'>V</td></tr>" 
	
	+ "<tr><td colspan='7' height='1' background='"+chemin+"/puce_vert.gif'></td></tr>"

	+ "<tr class='P1' align='center'><td id='0' class='BGG' height='16'></td><td id='1' class='BGG'></td><td id='2' class='BG2'></td><td id='3' class='BG2'></td><td id='4' class='BG2'></td><td id='5' class='BG2'></td><td id='6' class='BG2'></td></tr>"
	
	+ "<tr class='P1' align='center'><td id='7' class='BGG' height='16'></td><td id='8' class='BGG'></td><td id='9' class='BG2'></td><td id='10' class='BG2'></td><td id='11' class='BG2'></td><td id='12' class='BG2'></td><td id='13' class='BG2'></td></tr>"
	
	+ "<tr class='P1' align='center'><td id='14' class='BGG' height='16'></td><td id='15' class='BGG'></td><td id='16' class='BG2'></td><td id='17' class='BG2'></td><td id='18' class='BG2'></td><td id='19' class='BG2'></td><td id='20' class='BG2'></td></tr>"
	
	+ "<tr class='P1' align='center'><td id='21' class='BGG' height='16'></td><td id='22' class='BGG'></td><td id='23' class='BG2'></td><td id='24' class='BG2'></td><td id='25' class='BG2'></td><td id='26' class='BG2'></td><td id='27' class='BG2'></td></tr>"
	
	+ "<tr class='P1' align='center'><td id='28' class='BGG' height='16'></td><td id='29' class='BGG'></td><td id='30' class='BG2'></td><td id='31' class='BG2'></td><td id='32' class='BG2'></td><td id='33' class='BG2'></td><td id='34' class='BG2'></td></tr>" 
	
	+ "<tr class='P1' align='center'><td id='35' class='BGG' height='16'></td><td id='36' class='BGG'></td><td id='37' class='BG2'></td><td id='38' class='BG2'></td><td id='39' class='BG2'></td><td id='40' class='BG2'></td><td id='41' class='BG2'></td></tr>" 
	
	+ "<tr><td colspan='7' height='1' background='"+chemin+"/puce_vert.gif'></td></tr>"

	+ "<tr class='P1B'><td></td><td width='18' height='18' align='center'><a href='#' onClick='fCalendrier(mois-=12,true)' title='Année précédente'><IMG SRC='"+chemin+"/b_aro_left2.gif' BORDER='0' style='filter:alpha(opacity=50)' onmouseover='nereidFade(this,100,30,30)' onmouseout='nereidFade(this,50,20,10)' ALT='Année précédente'></a></td><td id='lannee' colspan='3' align='center'></td><td><a href='#' onClick='fCalendrier(mois+=12,true)' title='Année suivante'><IMG SRC='"+chemin+"/b_aro_right2.gif' BORDER='0' style='filter:alpha(opacity=50)' onmouseover='nereidFade(this,100,30,30)' onmouseout='nereidFade(this,50,20,10)' ALT='Année suivante'></a></td><td><a href='#' onClick='fEraseTable(this,true);' title='Fermer'><IMG SRC='"+chemin+"/b_cross.gif' BORDER='0' WIDTH='16' HEIGHT='16' style='filter:alpha(opacity=50)' onmouseover='nereidFade(this,100,30,30)' onmouseout='nereidFade(this,50,20,10)' alt='Fermer le calendrier'></a></td></tr>"
	
	+ "<tr><td><IMG SRC='"+chemin+"/puce_vert.gif' BORDER='0' WIDTH='1' HEIGHT='1'></td><td colspan='7' height='1' background='"+chemin+"/puce_vert.gif'></td><td><IMG SRC='"+chemin+"/puce_vert.gif' BORDER='0' WIDTH='1' HEIGHT='1'></td></tr>"

	+ "</table>";
	
	d.getElementById('ecran').innerHTML	= tbHTML;	// Ecriture du tableau dans le DIV id=ecran
	
	eTBL = d.getElementById('tbCal');			// Element TABLE id=tbCal
	cTR = eTBL.getElementsByTagName('tr');			// Collection des TR de la TABLE
	
	cTD = new Array();					// Collection des TD ligne par ligne
	for (i = 0; i < cTR.length; i++){
		cTD[i] = cTR[i].getElementsByTagName('td');
	}
			
	//Def. style TABLE
	with(eTBL.style) {
//		width = '172px';
//		height = '182px';
//		borderCollapse = 'collapse';
//		fontFamily = 'arial';
//		fontSize = '12px';	
	}
	//Def. style TABLE : TR 1
	with(cTR[0].style) {
//		width = '140px';
//		height = '16px';
//		borderSpacing = '0px';
//		padding = '0px';	
	}
	//Def. style TABLE : TDs : TR_1
	with(cTD[0].item(0).style) {
//		width = '16px';
//		height = '16px';
//		borderSpacing = '0px';
//		padding = '0px';
//		textAlign = 'left';
//		verticalAlign = 'top';			
	}
	with(cTD[0].item(1).style) {
//		width = '140px';
//		height = '1px';
//		borderSpacing = '0px';
//		emptyCells = 'show';
//		borderTopStyle = 'solid';
//		borderTopWidth = '1px'
//		borderColor = '#158D75';
//		padding = '0px';			
	}
	with(cTD[0].item(2).style) {
//		width = '16px';
//		height = '16px';
//		borderSpacing = '0px';
//		padding = '0px';
//		textAlign = 'right';
//		verticalAlign = 'top';			
	}
	
	//Def. style TABLE : TR 2
	with(cTR[1].style) {
//		width = '172px';
//		height = '20px';
//		borderSpacing = '0px';
//		padding = '0px';
//		fontSize = '16px';
//		fontWeight = 'bold'	
	}
	//Def. style TABLE : TDs : TR_2
	with(cTD[1].item(0).style) {
//		width = '16px';
//		height = '20px';
//		borderSpacing = '0px';
//		borderLeftStyle = 'solid';
//		borderLeftWidth = '1px'
//		borderColor = '#8183A2';
//		padding = '0px';
//		textAlign = 'center';
//		verticalAlign = 'top';			
	}
	with(cTD[1].item(1).style) {
//		width = '140px';
//		height = '20px';
//		borderSpacing = '0px';
//		emptyCells = 'show';
//		padding = '0px';
//		textAlign = 'center';
//		verticalAlign = 'middle';			
	}
	with(cTD[1].item(2).style) {
//		width = '16px';
//		height = '20px';
//		borderSpacing = '0px';
//		borderRightStyle = 'solid';
//		borderRightWidth = '1px'
//		borderColor = '#8183A2';
//		padding = '0px';
//		textAlign = 'center';
//		verticalAlign = 'top';			
	}
	
	//Def. style TABLE : TR 3
	with(cTR[2].style) {
//		height = '20px';
//		borderSpacing = '0px';
//		padding = '0px';
//		fontSize = '11px';
//		fontWeight = 'bold'
//		textAlign = 'center';
//		verticalAlign = 'middle';	
	}
	//Def. style TABLE : TDs : TR_3
	for (i=0;i<7;i++) {
		with(cTD[2].item(i).style) {
//			width = '20px';
//			height = '20px';
//			borderSpacing = '0px';
//			padding = '0px';			
		}
	}
	
	//Def. style TABLE : TR 4 --> TR 9
	for (i=4;i<10;i++) {
		with(cTR[i].style) {
//			height = '20px';
//			borderSpacing = '0px';
//			padding = '0px';
//			fontSize = '11px';
//			textAlign = 'center';
//			verticalAlign = 'middle';	
		}
		//Def. style TABLE : TDs : TR_4 --> TR_9
		for (j=0;j<7;j++){
			with(cTD[i].item(j).style) {
//				width = '20px';
//				height = '20px';
//				borderSpacing = '0px';
//				padding = '0px';			
			}
		}
	}
	
	//Def. style TABLE : TR 10
	with(cTR[9].style) {
//		height = '16px';
//		width = '172px';
//		borderSpacing = '0px';
//		padding = '0px';
//		fontSize = '16px';
//		fontWeight = 'bold'	
	}
	//Def. style TABLE : TDs : TR_10
	with(cTD[9].item(0).style) {
//		width = '16px';
//		height = '16px';
//		borderSpacing = '0px';
//		padding = '0px';
//		textAlign = 'left';
//		verticalAlign = 'bottom';			
	}
	with(cTD[9].item(1).style) {
//		width = '140px';
//		height = '16px';
//		borderSpacing = '0px';
//		borderBottomStyle = 'solid';
//		borderBottomWidth = '1px'
//		borderColor = '#8183A2';
//		emptyCells = 'show';
//		padding = '0px';
//		textAlign = 'center';
//		verticalAlign = 'middle';			
	}
	with(cTD[9].item(2).style) {
//		width = '16px';
//		height = '16px';
//		borderSpacing = '0px';
//		padding = '0px';
//		textAlign = 'right';
//		verticalAlign = 'bottom';			
	}
}

function fShowTable(top_pos,left_pos,chemin) {
// les variables top_pos et left_pos permettent de positionner le calendrier dans la page
	d.getElementById('ecran').style.width  = '127px';
	d.getElementById('ecran').style.height = '154px';

	//Si le calendrier n'est pas affiché
	if (!nodeVisible) {
		// Si il y a eu précédement une selection de date
		//on insère le clone de tabNode.
		if (firstUse){			
			d.getElementById('ecran').appendChild(tabNodeCopy);
			nodeVisible = true;		//Le node est affiché donc visible
		
		// Sinon, on dessine la table et on passe au remplissage du calendrier
		} else {
			fDrawTable(top_pos,left_pos,chemin);		//Affichage du tableau
			// avec les variables positions top et left
			fCalendrier();		//Remplissage
		}
	}
}

function fCalendrier(pmois,nav) {
	tMois = new Array('Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Decembre');
	dNow = new Date();			//Récupération de la date courante

	if (nav && !navencours){								//Si mode navigation enclenché et qu'il n'y à pas de navigation en cours
		tabNode = d.getElementById('ecran').firstChild;		//Préserver le node avec le valeurs précédentes	
		tabNodeCopy = tabNode.cloneNode(true);				//Puis le cloner
		navencours=true;									//La navigation est en cours
	}
	
	ijour = 1;					//Initialisation de l'index du premiser jour du mois
	dNow.setDate(ijour);		//Initialisation du jour du mois à l'index
	
	//Navigation du calendrier
	if (pmois == null) {
		mois = dNow.getMonth();		//Mois courant
		an = dNow.getFullYear();	//Année courante
	} else {
		if (pmois<0){
			an = dNow.getFullYear(dNow.setFullYear(an-1));
			mois = dNow.getMonth(dNow.setMonth(11));		
		} else if (pmois>11){
			an = dNow.getFullYear(dNow.setFullYear(an+1));		
			mois = dNow.getMonth(dNow.setMonth(0));
		} else {
			an = dNow.getFullYear(dNow.setFullYear(an));
			mois = dNow.getMonth(dNow.setMonth(pmois));
		}
	}	

	//Affichage des informations Mois et Année
	d.getElementById('lemois').innerHTML = tMois[mois];		//Affichage du mois
	d.getElementById('lannee').innerHTML = an;				//Affichage de l'année
	
	//Effacer le contenu de toutes les cellule du calendrier (de id=0 à id=41)
	for(i=0;i<42;i++) {
		d.getElementById(i).innerHTML = "";
	}
	
	//Correlation entre le 1er jour du mois, le jour de la semaine correspondant 
	//et la cellule du tableau correspondante
	switch (dNow.getDay()) {
		case 6 : id = 0; break; // S
		case 0 : id = 1; break; // D
		case 1 : id = 2; break; // L
		case 2 : id = 3; break; // M
		case 3 : id = 4; break; // M
		case 4 : id = 5; break; // J
		case 5 : id = 6; break; // V		
	}

	//Ecriture des dates pour le mois (remplissage du tableau avec les date)
	while(mois == dNow.getMonth()) {
		d.getElementById(id).innerHTML = "<a href='#' onClick='fEraseTable(this,false);'>" + dNow.getDate() + "</a>";
	
		if (ijour == aujourdhuiJ && mois == aujourdhuiM && an == aujourdhuiA) {
			//Si le date est la date contractuelle
			with(d.getElementById(id).firstChild.style){
				width = "18px";
				height = "16px";
				backgroundColor = "#FDE8D8";
				color = "#457171";
				borderStyle = "solid";
				borderColor = "#457171";
				borderWidth = "1pt";
				textDecoration = "none";
			}
		} else if (ijour == Number(cejour) && mois == moisSelect && an == anSelect) {
			//Si le date est celle du jour dernièrement selectionné
			with(d.getElementById(id).firstChild.style){
				color = "#457171";
				fontWeight = 'bold';
				textDecoration = "underline";
			}
		} else {
			//Pour toutes les autres dates
			with(d.getElementById(id).firstChild.style){
				color = "#457171";
				textDecoration = "none";
			}	
		}

		ijour++;
		id++;
		dNow.setDate(ijour);		//Jour suivant
	} 
}

function fEraseTable(obj,btnfermer){
	if(btnfermer) {
		//Gestion du node Table id=tbCal si fermeture avec bouton Fermer
		//	
		//Efface le node Table														
		d.getElementById('ecran').removeChild(d.getElementById('ecran').firstChild);
		
		//Si une date a été selectionnée antérieurement 


		

		if(firstUse){	
			an = dNow.getFullYear(dNow.setFullYear(anSelect));	//Initialisation de l'année à l'année selectionnée pour la prochaine navigation
			mois = dNow.getMonth(dNow.setMonth(moisSelect));	//Initialisation du compteur de mois au mois selectionné pour la prochaine navigation
			navencours=false;				//Plus de navigation en cours
			nav=false;						//Desactivation du mode navigation
			nodeVisible = false;			//Le node est supprimé donc invisible
		}
	} else {
	//Gestion du node Table id=tbCal si fermeture avec click sur une date
	//
	//Met en rouge la date choisie
	obj.style.color = '#457171';
	obj.style.fontWeight = 'bold';
	obj.style.textDecoration = "underline";
	
	//A partir de maintenant on considère qu'une première utilisation 
	//du calendrier avec sélection de date a été faite.
	firstUse=true;
	
	//Mémorise le mois et l'année selectionnés
	moisSelect = mois;
	anSelect = an;

	
	//Affichage de la date choisie dans le formulaire
	cejour = obj.innerHTML;
	cejour = (cejour<10)? "0"+cejour : cejour;
	cemois = ((mois+1)<10)? "0"+(mois+1) : mois+1;
	cetan = an;
	
	//Récupération du nom du champ à mettre à jour
	var nom_du_champ = eval('d.'+d.lien.le_champ.value);
	//window.alert(nom_du_champ.name+"="+nom_du_champ.value);
	nom_du_champ.value = cejour + "/" + cemois + "/" + an;
	maj_chp_date();
	//////////////////////////////////////////////////////

	//Gestion du node Table id=tbCal
	tabNode = d.getElementById('ecran').firstChild;									//Preserve le node Table
	tabNodeCopy = tabNode.cloneNode(true);											//Clone le node Table
	d.getElementById('ecran').removeChild(d.getElementById('ecran').firstChild);	//Efface le node Table
	nav=false;						//On est plus en mode Navigation.
	navencours=false;				//Il n'y a plus de navigation en cours.
	nodeVisible = false;			//Le node est supprimé donc invisible
	an = dNow.getFullYear(dNow.setFullYear(anSelect));	//Initialisation de l'année à l'année selectionnée pour la prochaine navigation
	mois = dNow.getMonth(dNow.setMonth(moisSelect));	//Initialisation du compteur de mois au mois selectionné pour la prochaine navigation

	
	d.getElementById('ecran').style.width  = '0px';
	d.getElementById('ecran').style.height = '0px';

	}
}

