

/**
 * Encapsulation de variables locales (privées).
 */
(function () {
	//  "use strict";
	// Raccourci vers window
	var WIN = this,
		// Raccourci vers document
		DOC = WIN.document,
		// Raccourci vers window.ev
		EASY = WIN.ev,
		//  // Raccourci vers window.ev.log
		//  LOG=EASY&&EASY.log,
		// Raccourci vers window.ev.log
		LANG = EASY && EASY.lang,
		// Raccourci vers window.ev.dom
		DOM = EASY && EASY.dom,
		//  create=DOM&&DOM.create,
		create = function (name) {return DOC.createElement(name); },
		createText = DOM.createText,
		addClass = DOM && DOM.addClass,
		removeClass = DOM && DOM.removeClass,
		getJourTexteCourt = WIN.getJourTexteCourt,
		getMoisTexteLong = WIN.getMoisTexteLong,
		genericNavigator = WIN.genericNavigator,
		MSIE = WIN.MSIE,
		calendarPool,
		txtClose,
		// Dimensions du iframe pour les MSIE<=6.9
		MSIE6_WIDTH = 500, 
		MSIE6_HEIGHT = 200;

	if (!EASY || !DOM) { throw 'Needs ev.core and ev.dom modules!'; }

	/**
	 * Initialisation des constantes d'affichage multilingues
	 */
	switch (LANG.current) {
	case 'es_ES':
		txtClose = 'Cerrar';
		break;
	case 'it_IT':
		txtClose = 'Chiudere';
		break;
	case 'en_GB':
		txtClose = 'Close';
		break;
	case 'de_DE':
		txtClose = 'Schlie\u00DFen';
		break;
	//    case 'fr_FR':
	default:
		txtClose = 'Fermer';
		break;
	}
	WIN.TXT_FERMER = txtClose;

	/**
	 * L'objet CalendarPool contient tous les calendriers
	 * créés sur la page. Il est référencé dans la variable
	 * globale calendarPool.
	 *
	 * @constructor
	 */
	function CalendarPool () {
		var thePl = this, array = [];
		thePl.add = function(calendar) {
			array.push(calendar);
			return array.length - 1;
		};
		thePl.get = function (index) {
			return array[index];
		};
		thePl.closeAll = function () {
			var n = array.length;
			while (n) {
				--n;
				array[n].hide('cache TOUT');
			}
		};
	}
	WIN.calendarPool = (calendarPool = new CalendarPool());

	/**
	 * L'objet Calendar lorsqu'il est créé ajoute dans le body la structure HTML
	 * suivante: <div class="calendar"> <!-- Le div et la table suivants se trouvent
	 * en deux exemplaires (2 mois) puisqu'on est en calendrier double --> <div
	 * class="title"><a href="">&lt;&lt;</a> fev. 2007 <a href="">&gt;&gt;</a></div>
	 * <table> <thead>
	 * <tr>
	 * <td>lun.</td>
	 * <td>mar.</td>
	 * <td>mer.</td>
	 * <td>jeu.</td>
	 * <td>ven.</td>
	 * <td>sam.</td>
	 * <td>dim.</td>
	 * </tr>
	 * </thead> <tbody>
	 * <tr>
	 * <td></td>
	 * <td></td>
	 * <td></td>
	 * <td><a href="">1</a></td>
	 * <td><a href="">2</a></td>
	 * <td><a href="">3</a></td>
	 * <td><a href="">4</a></td>
	 * </tr>
	 * <tr>
	 * <td><a href="">5</a></td>
	 * <td><a href="">6</a></td>
	 * <td><a href="">7</a></td>
	 * <td><a href="">8</a></td>
	 * <td><a href="">9</a></td>
	 * <td><a href="">10</a></td>
	 * <td><a href="">11</a></td>
	 * </tr> . . . </tbody> </table> <div class="close"><a
	 * href="javascript:">Fermer</a><div> <div>
	 *
	 * L'apparence de cette structure peut être facilement modifiée à l'aide de
	 * feuilles de style (se référer aussi à la feuille de style par défaut QUI NE
	 * DOIT PAS ETRE MODIFIEE calendar.css, on peut être ammené à surcharger les
	 * styles de cette dernière)
	 *
	 * Pour utiliser ce calendrier, on doit impérativement utiliser des sélecteurs
	 * jour et mois qui contiennent respectivement les valeurs date codée sur deux
	 * digits 00 01 ... 31 et le mois sur deux digits concaténé avec l'année sur 4
	 * digits séparés par "/" 01/2007 par exemple. Ces sélecteurs sont transmis à
	 * l'instance Calendar en paramètre sous forme de nodes elements: new
	 * Calendar(jourElement,moisElement); On doit de plus s'assurer d'inclure dans
	 * la page utilisant calendar.js, la feuille de style calendar.css
	 *
	 * Paramètres du constructeur: - jourElement l'élément selecteur de jour (tel
	 * que décrit au dessus) - moisElement l'élément selecteur de mois (tel que
	 * décrit au dessus) Méthodes utiles: - show() - hide() La fonction de ces
	 * méthodes est évidente, et il n'est d'ailleurs pas nécessaire d'utiliser
	 * hide() qui est déjà wrappé sur les liens du calendrier.
	 *
	 * @constructor
	 */
	WIN.Calendar = function(jourElement, moisElement, div, listener) {
		//LOG.info("NEW CALENDAR "+jourElement+moisElement+div+listener)
		// variables de travail
		var theCal = this, i, thead, tr, jourTexte, td, captionTitle1, captionTitle2, divClose, aClose, spanAClose,
				// Date use
				now, changeRetour, DAY = 24 * 60 * 60 * 1000;

		// ATTENTION: Le sélecteur mois contient à la fois le mois et l'année au format mm/aaaa et souvent on fait
		// l'amalgame entre mois et mois+année, parfois on fait la différence entre mois et année, il faut suivre...
		// On place le nouveau calendrier créé dans le pool
		theCal.poolIndex = calendarPool.add(theCal);
		// On référence les sélecteurs jour et mois qui vont être affectés par ce calendrier
		theCal.jourElement = jourElement;
		theCal.moisElement = moisElement;
		// On définit les variables de date sélectionnée
		theCal.formatedJour = '01';
		theCal.formatedMois = '01';
		theCal.formatedAn = '2000';

		// On définit la date actuellement affichée par le sélecteur dans les propriétés associées
		theCal.selectedJour = theCal.jourElement.value;
		theCal.selectedMois = theCal.moisElement.value.replace(/\/[0-9]{4}$/, '');
		theCal.selectedAn = theCal.moisElement.value.replace(/^[0-9]{2}\//, '');

		// On définit les bornes minimale et maximale des mois et années définies dans le sélecteur mois
		theCal.minSelectableMois = theCal.moisElement.options[0].value.replace(/\/[0-9]{4}$/, '');
		theCal.minSelectableAn = theCal.moisElement.options[0].value.replace(/^[0-9]{2}\//, '');
		theCal.maxSelectableMois = theCal.moisElement.options[theCal.moisElement.options.length - 1].value.replace(/\/[0-9]{4}$/, '');
		theCal.maxSelectableAn = theCal.moisElement.options[theCal.moisElement.options.length - 1].value.replace(/^[0-9]{2}\//, '');

		// Définition de la fonction extérieur appelée lors d'un clic sur le formulaire (OPTIONNEL).
		theCal.calendarListener = listener;

		// On commence maintenant à créer l'arborescence DOM contenant le calendrier
		// D'abord on crée les propriétés qui doivent référencer ces éléments
		// Le div principal (cf. structure décrite plus haut)
		theCal.divCalendar = div;
		theCal.divCalendar = create('DIV');
		theCal.divCalendar.className = 'calendar';
		theCal.divCalendar.style.display = 'none';

		// Les éléments se trouvant dans le div interne qui contient le titre du calendrier 1
		theCal.a1Down = create('A');
		theCal.a1Down.className = 'leftCal';
		theCal.title1 = createText('Title1');
		theCal.a1Up = create('A');
		theCal.a1Up.className = 'rightCal';

		// La table et le tbody du calendrier 1
		// NOTA: IE ne parvient pas à afficher une table DOM sans tbody!
		theCal.table1 = create('TABLE');
		theCal.table1.className = 'mois1';
		theCal.tbody1 = create('TBODY');

		// Les éléments se trouvant dans le div interne qui contient le titre du calendrier 2
		theCal.title2 = createText('Title2');

		// La table et le tbody du calendrier 2
		// NOTA: IE ne parvient pas à afficher une table DOM sans tbody!
		theCal.table2 = create('TABLE');
		theCal.table2.className = 'mois2';
		theCal.tbody2 = create('TBODY');

		// On modifie maintenant les propriétés DOM créées plus haut et on organise le tout hiérarchiquement
		// Dans le div title
		theCal.spanA1Down = create('SPAN');
		theCal.a1Down.appendChild(theCal.spanA1Down);
		theCal.spanA1Down.appendChild(createText('<<'));
		captionTitle1 = create('CAPTION');
		captionTitle1.className = 'title';
		captionTitle1.appendChild(theCal.a1Down);
		captionTitle1.appendChild(theCal.title1);
		//theCal.divCalendar.appendChild(captionTitle1);
		theCal.table1.appendChild(captionTitle1);

		// Dans table on place un thead qui va contenir les noms de jour, puis le tbody vide
		thead = create('THEAD');
		tr = create('TR');
		thead.appendChild(tr);
		for (i = 1; i <= 7; i++) {
			jourTexte = getJourTexteCourt(i % 7);
			td = create('TD');
			td.appendChild(createText(jourTexte));
			tr.appendChild(td);
			//on rajoute une classe au td pour les jours de fin de semaine
			if (i === 6 || i === 7) {
				addClass(td, 'dayWeekEnd');
			}
		}
		theCal.table1.appendChild(thead);
		theCal.table1.appendChild(theCal.tbody1);
		theCal.divCalendar.appendChild(theCal.table1);

		// On recommence pour le calendrier 2
		captionTitle2 = create('CAPTION');
		captionTitle2.className = 'title';
		captionTitle2.appendChild(theCal.title2);
		theCal.spanA1Up = create('SPAN');
		theCal.a1Up.appendChild(theCal.spanA1Up);
		theCal.spanA1Up.appendChild(createText('>>'));
		captionTitle2.appendChild(theCal.a1Up);
		//theCal.divCalendar.appendChild(captionTitle2);
		theCal.table2.appendChild(captionTitle2);

		thead = create('THEAD');
		tr = create('TR');
		thead.appendChild(tr);
		for (i = 1; i <= 7; i++) {
			jourTexte = getJourTexteCourt(i % 7);
			td = create('TD');
			td.appendChild(createText(jourTexte));
			tr.appendChild(td);
			//on rajoute une classe au td pour les jours de fin de semaine
			if (i === 6 || i === 7) {
				addClass(td, 'dayWeekEnd');
			}
		}
		theCal.table2.appendChild(thead);
		theCal.table2.appendChild(theCal.tbody2);
		theCal.divCalendar.appendChild(theCal.table2);

		/**
		 * CLICACTION : Effectue les actions des différents boutons du calendrier.
		 *
		 * @param {number=} cal Indice du calendrier courant dans la pile [optionnel ; défaut le calendrier courant, correspondant au contexte "this"].
		 * @param {boolean=} isCalIcon Calendar Icon possède un comportement particulier : type SHOW/HIDE. [optionnel ; défaut false].
		 * @param {Element=} currentTarget Élément HTML cible du calendrier (date,...) [optionnel ; défaut undefined].
		 */
		theCal.clicAction = function(cal, isCalIcon, currentTarget) {
			//LOG.log("ACTION: cal '"+(typeof cal=='number'? cal: 'this')+"', isCalIcon '"+isCalIcon+"', currentTarget '"+currentTarget.id+"'");
			cal = (typeof cal === 'number' && calendarPool.get(cal)) || theCal;
			if (currentTarget && !isCalIcon) {
				jourElement.value = currentTarget.id.substring(0, 2);
				moisElement.value = currentTarget.id.substring(3, 10);
			}
			if (isCalIcon && theCal.divCalendar.style.display !== 'block') {
				cal.show();
			}
			else {
				cal.hide(true);
			}
		};

		/**
		 * Génère une fonction qui prendra en charge l'événement
		 * 'click' d'un élément du calendrier.
		 *
		 * @param {Element=} elt élément DOM contenant une date à modifier
		 *   (si besoin) lors du click [optionnel ; par défaut aucun].
		 */
		function getOnClickHandler(elt) {
			return function() {
				theCal.clicAction(theCal.poolIndex, false, elt);
			};
		}

		// Div de fermeture
		divClose = create('DIV');
		divClose.className = 'close';
		aClose = create('A');
		spanAClose = create('SPAN');
		aClose.appendChild(spanAClose);
		spanAClose.appendChild(createText(txtClose));
		aClose.onclick = getOnClickHandler();
		divClose.appendChild(aClose);
		//captionTitle1.appendChild(divClose);
		theCal.divCalendar.appendChild(divClose);

		// Finalement on met le calendrier dans le body
		//  var body=DOC.getElementsByTagName("BODY")[0];
		//  body.appendChild(theCal.divCalendar);
		div.appendChild(theCal.divCalendar);

		/**
		 * SHOW : Affiche le calendrier sélectionné.
		 * @param {Object=} cal Calendrier à afficher (optionnel et obsolète ; par défaut le calendrier courant, correspondant au contexte "this").
		 * @param {number=} index indice du calendrier dans le pool sur lequel on pourra cliquer (optionnel ; par défaut aucun).
		 */
		theCal.show = function(cal, index) {
			cal = cal || theCal;
			calendarPool.closeAll();
			cal.selectedJour = cal.jourElement.value;
			cal.selectedMois = cal.moisElement.value.replace(/\/[0-9]{4}$/, '');
			cal.selectedAn = cal.moisElement.value.replace(/^[0-9]{2}\//, '');
			cal.setMonth(cal.selectedMois, cal.selectedAn);
			cal.divCalendar.style.display = 'block';
			//on change l'attribut onclick de l'icone du calendrier afin de pouvoir le fermer en cliquant sur l'icone
			if (typeof index === 'number' && index >= 0) {
				cal.onclick = getOnClickHandler(cal);
			}
			if (genericNavigator.navigator.id === MSIE && genericNavigator.navigator.version <= 6.9) {
				var iframe = create('IFRAME');
				iframe.style.display = 'none';
				iframe.style.display = 'block';
				iframe.style.position = 'absolute';
				iframe.style.top = '0';
				iframe.style.left = '0';
				iframe.style.zIndex = '-1';
				iframe.style.filter = 'mask()';
				iframe.style.width = (MSIE6_WIDTH + 10) + 'px';
				iframe.style.height = (MSIE6_HEIGHT + 10) + 'px';
				iframe.style.background = 'none';
				iframe.frameBorder = 0;
				iframe.scrolling = 'no';
				cal.divCalendar.appendChild(iframe);
			}
		};
		/**
		 * HIDE : Supprime le calendrier courant et test le formulaire.
		 *
		 * @param {Boolean} valuesAreTested : appel la fonction de test du formulaire.
		 *
		 */
		theCal.hide = function(forModify) {
			//console.log(forModify);
			theCal.divCalendar.style.display = 'none';
			if (typeof theCal.calendarListener === 'function' && forModify === true) {
				theCal.calendarListener(this.jourElement.id);
			}
		};

		// Modifie les propriétés du calendrier avec un nouveau mois (et année)
		theCal.setMonth = function(mois,an) {
			an = parseInt(an, 10);
			mois = parseInt(mois, 10);
			now.an = parseInt(now.getFullYear(), 10);
			now.mois = parseInt((now.getMonth() + 1), 10);
			//LOG.info(an+typeof(an)+","+mois+typeof(mois)+","+now.an+typeof(now.an)+","+now.mois+typeof(now.mois));
			/*if(now.an===an && now.mois===mois){LOG.warn("IF ANNEE "+an+" MOIS "+mois);}
			else{LOG.warn("ELSE ANNEE "+now.an+" MOIS "+now.mois);}*/

			theCal.setTitle(mois, an);
			theCal.setTable(mois, an);
			theCal.setUp(mois, an);
			theCal.setDown(mois, an);
		};

		// Modifie les textes dans les div title en fonction du nouveau mois (et année)
		theCal.setTitle = function(mois,an) {
			//LOG.log("setTITLE ANNEE "+an+" MOIS "+mois);
			if (!mois || !an) {
				mois = theCal.selectedMois;
				an = theCal.selectedAn;
			}
			theCal.title1.nodeValue = ' ' + getMoisTexteLong(mois) + ' ' + an + ' ';
			mois++;
			if (mois > 12) {
				mois = 1;
				an++;
			}
			theCal.title2.nodeValue = ' ' + getMoisTexteLong(mois) + ' ' + an + ' ';
		};
		// Modifie les tables (les tbody pour être précis) en analysant le mois (et année du calendrier 1)
		theCal.setTable = function(mois,an) {
			// variables de travail
			var date, nbLineCalMax, nbLineCal;
			if (!mois || !an) {
				mois = theCal.selectedMois;
				an = theCal.selectedAn;
			}

			theCal.table1.removeChild(theCal.tbody1);
			theCal.tbody1 = create('TBODY');
			theCal.table1.appendChild(theCal.tbody1);
			date = new Date();
			date.setDate(1);
			date.setMonth(mois - 1);
			date.setFullYear(an);
			while (date.getDay() !== 1) {// while(date.getDay()!=LUNDI)
				date.setTime(date.getTime() - DAY);
			}
			nbLineCalMax = 6;//nombre de ligne d'un table
			nbLineCal = 1;
			while (nbLineCal <= nbLineCalMax) {theCal.initTd1(date, mois, an);nbLineCal++;}
			mois++;
			if (mois > 12) {mois = 1;an++;}
			if (theCal.isSelectable(mois, an)) {
				theCal.table2.removeChild(theCal.tbody2);
				theCal.tbody2 = create('TBODY');
				theCal.table2.appendChild(theCal.tbody2);
				theCal.table2.style.display = '';
				date = new Date();
				date.setDate(1);
				date.setMonth(mois - 1);
				date.setFullYear(an);
				while (date.getDay() !== 1) {// while(date.getDay()!=LUNDI)
					date.setTime(date.getTime() - DAY);
				}
				nbLineCal = 1;
				while (nbLineCal <= nbLineCalMax) {
					theCal.initTd2(date, mois, an);
					nbLineCal++;
				}
			}
			else {
				theCal.title2.nodeValue = '';
				theCal.table2.style.display = 'none';
			}
		};

		now = new Date();
		changeRetour = true;
		// Crée un td contenant la date passée en paramètre dans le tbody
		theCal.initTd1 = function(date,mois,an) {
			var tr = create('TR'), td,
					// variables de travail
					i, jourElementRetour, moisElementRetour, fullDateStart, fullDateCal, a, formatedJour, formatedMois;
			theCal.tbody1.appendChild(tr);
			for (i = 1; i <= 7; i++) {
				if (theCal.jourElement.id === 'jourMEVAller') {jourElementRetour = 'jourMEVRetour';}
				if (theCal.moisElement.id === 'moisMEVAller') {moisElementRetour = 'moisMEVRetour';}
				if (theCal.jourElement.id === 'jourDebutMEH') {jourElementRetour = 'jourFinMEH';}
				if (theCal.moisElement.id === 'moisDebutMEH') {moisElementRetour = 'moisFinMEH';}
				if (theCal.jourElement.id.indexOf('jourDebutMEH') !== -1) {jourElementRetour = 'jourFinMEH';}
				if (theCal.moisElement.id.indexOf('moisDebutMEH') !== -1) {moisElementRetour = 'moisFinMEH';}
				if (theCal.jourElement.id === 'jourDebutMEC') {jourElementRetour = 'jourFinMEC';}
				if (theCal.moisElement.id === 'moisDebutMEC') {moisElementRetour = 'moisFinMEC';}
				if (theCal.jourElement.id === 'jourMEVRetour') {jourElementRetour = 'jourMEVAller';}
				if (theCal.moisElement.id === 'moisMEVRetour') {moisElementRetour = 'moisMEVAller';}
				if (theCal.jourElement.id === 'jourFinMEH') {jourElementRetour = 'jourDebutMEH';}
				if (theCal.moisElement.id === 'moisFinMEH') {moisElementRetour = 'moisDebutMEH';}
				if (theCal.jourElement.id.indexOf('jourFinMEH') !== -1) {jourElementRetour = 'jourDebutMEH';}
				if (theCal.moisElement.id.indexOf('moisFinMEH') !== -1) {moisElementRetour = 'moisDebutMEH';}
				if (theCal.jourElement.id === 'jourFinMEC') {jourElementRetour = 'jourDebutMEC';}
				if (theCal.moisElement.id === 'moisFinMEC') {moisElementRetour = 'moisDebutMEC';}

				if (now.getMonth() < 10) {fullDateStart = now.getFullYear() + '0' + now.getMonth();}
				else {
					//fullDateStart = now.getFullYear() + '' + now.getMonth();
					fullDateStart = (now.getFullYear().toString() + now.getMonth().toString());
				}

				if (now.getDate() < 10) {fullDateStart = fullDateStart + '0' + now.getDate();}
				else {
					//fullDateStart = fullDateStart + '' + now.getDate();
					fullDateStart =(fullDateStart.toString() + now.getDate().toString());
				}

				if (date.getMonth() < 10) {fullDateCal = date.getFullYear() + '0' + date.getMonth();}
				else {fullDateCal = (date.getFullYear().toString()+ date.getMonth().toString());}

				if (date.getDate() < 10) {fullDateCal = fullDateCal + '0' + date.getDate();}
				else {fullDateCal = (fullDateCal.toString() + date.getDate().toString());}

				td = create('TD');

				if (fullDateCal <= fullDateStart || (date.getFullYear() > now.getFullYear() && date.getMonth() > now.getMonth())) {
					td.className = 'dayOff';
					td.appendChild(createText(date.getDate()));
				}
				else if (date.getMonth() === mois || date.getMonth() === mois - 2 || date.getFullYear() !== an) {
					td.className = 'dayOnOff';
					a = create('A');

					formatedJour = date.getDate();
					if (formatedJour < 10) {formatedJour = '0' + formatedJour;}

					formatedMois = date.getMonth() + 1;
					if (formatedMois < 10) {formatedMois = '0' + formatedMois;}

					if (theCal.jourElement.id === 'jourMEVAller') {jourElementRetour = 'jourMEVRetour';}
					if (theCal.moisElement.id === 'moisMEVAller') {moisElementRetour = 'moisMEVRetour';}
					if (theCal.jourElement.id === 'jourDebutMEH') {jourElementRetour = 'jourFinMEH';}
					if (theCal.moisElement.id === 'moisDebutMEH') {moisElementRetour = 'moisFinMEH';}
					if (theCal.jourElement.id.indexOf('jourDebutMEH') !== -1) {jourElementRetour = 'jourFinMEH';}
					if (theCal.moisElement.id.indexOf('moisDebutMEH') !== -1) {moisElementRetour = 'moisFinMEH';}
					if (theCal.jourElement.id === 'jourDebutMEC') {jourElementRetour = 'jourFinMEC';}
					if (theCal.moisElement.id === 'moisDebutMEC') {moisElementRetour = 'moisFinMEC';}
					if (jourElementRetour && moisElementRetour) {
						a.onclick = getOnClickHandler(a);
						theCal.formatedJour = formatedJour;theCal.formatedMois = formatedMois;theCal.formartedAn = date.getFullYear();
						a.id = theCal.formatedJour + '/' + theCal.formatedMois + '/' + theCal.formartedAn;
					}
					/*
					 * Changer le mode de sélection
					 * i = element('"+theCal.jourElement.id+"').value='"+formatedJour+"';element('"+theCal.moisElement.id+"').value;
					 * DOC.formMEV.moisMEVAller.options[i].selected = true;
					 */
					else {
						a.onclick = getOnClickHandler(a);
						theCal.formatedJour = formatedJour;theCal.formatedMois = formatedMois;theCal.formartedAn = date.getFullYear();
						a.id = theCal.formatedJour + '/' + theCal.formatedMois + '/' + theCal.formartedAn;
					}
					a.appendChild(createText(date.getDate()));
					td.appendChild(a);
				}
				else {
					td.className = 'dayOn';

					if (now.getFullYear() === date.getFullYear() && now.getMonth() === date.getMonth() && (now.getDate() + 1) === date.getDate()) {
						addClass(td, 'dayToday');
					}

					a = create('A');

					formatedJour = date.getDate();
					if (formatedJour < 10) {formatedJour = '0' + formatedJour;}

					formatedMois = date.getMonth() + 1;
					if (formatedMois < 10) {formatedMois = '0' + formatedMois;}
					//sur les Aller on change les Retours la toute première fois
					if (changeRetour && theCal.poolIndex % 2 === 0) {
						a.onclick = getOnClickHandler(a);
						theCal.formatedJour = formatedJour;
						theCal.formatedMois = formatedMois;
						theCal.formartedAn = date.getFullYear();
						a.id = theCal.formatedJour + '/' + theCal.formatedMois + '/' + theCal.formartedAn;
					}
					//sur les Retours on fait rien
					else {
						a.onclick = getOnClickHandler(a);
						theCal.formatedJour = formatedJour;theCal.formatedMois = formatedMois;theCal.formartedAn = date.getFullYear();
						a.id = theCal.formatedJour + '/' + theCal.formatedMois + '/' + theCal.formartedAn;
					}
					a.appendChild(createText(date.getDate()));
					td.appendChild(a);
				}
				if (date.getFullYear() === theCal.selectedAn && date.getMonth() === (theCal.selectedMois - 1) && date.getDate() === theCal.selectedJour) {
					addClass(td, 'selected');
				}
				else {
					removeClass(td, 'selected');
				}
				//on rajoute une classe au td pour les jours de fin de semaine
				if (i === 6 || i === 7) {
					addClass(td, 'weekEnd');
				}
				tr.appendChild(td);
				date.setTime(date.getTime() + DAY);
			}
		};
		// Crée un td contenant la date passée en paramètre dans le tbody 2
		theCal.initTd2 = function(date, mois, an) {
			var tr = create('TR'), td,
					// variables de travail
					i, jourElementRetour, moisElementRetour, a, formatedJour, formatedMois;
			theCal.tbody2.appendChild(tr);
			for (i = 1; i <= 7; i++) {
				if (theCal.jourElement.id === 'jourMEVAller') {jourElementRetour = 'jourMEVRetour';}
				if (theCal.moisElement.id === 'moisMEVAller') {moisElementRetour = 'moisMEVRetour';}
				if (theCal.jourElement.id === 'jourDebutMEH') {jourElementRetour = 'jourFinMEH';}
				if (theCal.moisElement.id === 'moisDebutMEH') {moisElementRetour = 'moisFinMEH';}
				if (theCal.jourElement.id.indexOf('jourDebutMEH') !== -1) {jourElementRetour = 'jourFinMEH';}
				if (theCal.moisElement.id.indexOf('moisDebutMEH') !== -1) {moisElementRetour = 'moisFinMEH';}
				if (theCal.jourElement.id === 'jourDebutMEC') {jourElementRetour = 'jourFinMEC';}
				if (theCal.moisElement.id === 'moisDebutMEC') {moisElementRetour = 'moisFinMEC';}
				if (theCal.jourElement.id === 'jourMEVRetour') {jourElementRetour = 'jourMEVAller';}
				if (theCal.moisElement.id === 'moisMEVRetour') {moisElementRetour = 'moisMEVAller';}
				if (theCal.jourElement.id === 'jourFinMEH') {jourElementRetour = 'jourDebutMEH';}
				if (theCal.moisElement.id === 'moisFinMEH') {moisElementRetour = 'moisDebutMEH';}
				if (theCal.jourElement.id.indexOf('jourFinMEH') !== -1) {jourElementRetour = 'jourDebutMEH';}
				if (theCal.moisElement.id.indexOf('moisFinMEH') !== -1) {moisElementRetour = 'moisDebutMEH';}
				if (theCal.jourElement.id === 'jourFinMEC') {jourElementRetour = 'jourDebutMEC';}
				if (theCal.moisElement.id === 'moisFinMEC') {moisElementRetour = 'moisDebutMEC';}
				td = create('TD');
				if (date.getMonth() === (mois - 1)) {td.className = 'dayOn';a = create('A');
					formatedJour = date.getDate();
					if (formatedJour < 10) {formatedJour = '0' + formatedJour;}
					formatedMois = date.getMonth() + 1;
					if (formatedMois < 10) {formatedMois = '0' + formatedMois;}
					//sur les Aller on change les Retours la toute première fois
					if (changeRetour && theCal.poolIndex % 2 === 0) {
						a.onclick = getOnClickHandler(a);
						theCal.formatedJour = formatedJour;theCal.formatedMois = formatedMois;theCal.formartedAn = date.getFullYear();
						a.id = theCal.formatedJour + '/' + theCal.formatedMois + '/' + theCal.formartedAn;
					}
					//sur les Retours on fait rien
					else {
						a.onclick = getOnClickHandler(a);
						theCal.formatedJour = formatedJour;theCal.formatedMois = formatedMois;theCal.formartedAn = date.getFullYear();
						a.id = theCal.formatedJour + '/' + theCal.formatedMois + '/' + theCal.formartedAn;
					}
					a.appendChild(createText(date.getDate()));
					td.appendChild(a);
				}
				else {
					td.className = 'dayOnOff';
					a = create('A');
					formatedJour = date.getDate();
					if (formatedJour < 10) {formatedJour = '0' + formatedJour;}
					formatedMois = date.getMonth() + 1;
					if (formatedMois < 10) {formatedMois = '0' + formatedMois;}
					a.onclick = getOnClickHandler(a);
					theCal.formatedJour = formatedJour;theCal.formatedMois = formatedMois;theCal.formartedAn = date.getFullYear();
					a.id = theCal.formatedJour + '/' + theCal.formatedMois + '/' + theCal.formartedAn;
					a.appendChild(createText(date.getDate()));
					td.appendChild(a);
				}
				if (date.getFullYear() === theCal.selectedAn && date.getMonth() === (theCal.selectedMois - 1) && date.getDate() === theCal.selectedJour) {
					addClass(td, 'selected');
				}
				else {
					removeClass(td, 'selected');
				}
				//on rajoute une classe au td pour les jours de fin de semaine
				if (i === 6 || i === 7) {addClass(td, 'weekEnd');}
				tr.appendChild(td);
				date.setTime(date.getTime() + DAY);
			}
		};
		// Modifie le lien up dans le div title en fonction du nouveau mois (et année)
		theCal.setUp = function(mois,an) {
			if (!mois || !an) {
				mois = theCal.selectedMois;
				an = theCal.selectedAn;
			}
			mois = Number(mois) + 1;
			if (mois > 12) {an++;mois = 1;}
			if (theCal.isSelectable(mois, an)) {
				theCal.a1Up.style.display = 'inline';
				theCal.a1Up.href = '#';
				theCal.a1Up.onclick = function() {
					calendarPool.get(theCal.poolIndex).setMonth(mois, an);
					WIN.cancelIEEvent();
					return false;
				};
			}
			else {
				theCal.a1Up.style.display = 'none';
			}
		};
		// Modifie le lien down dans le div title en fonction du nouveau mois (et année)
		theCal.setDown = function(mois,an) {
			if (!mois || !an) {mois = theCal.selectedMois;an = theCal.selectedAn;}
			mois = Number(mois) - 1;
			if (mois < 1) {an--;mois = 12;}
			if (theCal.isSelectable(mois, an)) {
				theCal.a1Down.style.display = 'inline';
				theCal.a1Down.href = '#';
				theCal.a1Down.onclick = function() {
					calendarPool.get(theCal.poolIndex).setMonth(mois, an);
					WIN.cancelIEEvent();
					return false;
				};
			}
			else {
				theCal.a1Down.style.display = 'none';
			}
		};
		// Indique si le mois (et année) existe dans le sélecteur mois affecté au calendrier
		theCal.isSelectable = function(mois, an) {
			var aM,am,mm,mM;
			// Conversion an Max. en entier
			aM=parseInt(theCal.maxSelectableAn,10);
			// Conversion an Min. en entier
			am=parseInt(theCal.minSelectableAn,10);
			// Conversion mois Max. en entier
			mM=parseInt(theCal.maxSelectableMois,10);
			// Conversion mois Min. entier
			mm=parseInt(theCal.minSelectableMois,10);
			return ((an > am || (an === am && mois >= mm)) && (an < aM || (an === aM && mois <= mM)));
		};
	};
}()); // Exécution ici

