

(function() {
	var window = this, document = window.document, WRT = 'write';

	window.getStyle = function(style,id) {
		var elem = document.getElementById(id);
		if (elem.currentStyle) {
			return parseInt(elem.currentStyle[style], 10);
		}else if (window.getComputedStyle(elem, '')) {
			var compStyle = window.getComputedStyle(elem, '');
			return parseInt(compStyle.getPropertyValue(style), 10);
		}
		return 0;
	};

	window.quick = function(tab,error,defaultTab) {
		if (!defaultTab) {
			defaultTab = 'vol';
		}
		if (tab === 'null') {
			tab = defaultTab;
		}
		if (!error) {
			switch (tab) {
				case 'sejour': try { window.selectSejour(); }catch (e1) {if (window.console) {window.console.error('window#quick(): ' + e1);}} break;
				case 'circuit': try { window.selectCircuit(); }catch (e2) {if (window.console) {window.console.error('window#quick(): ' + e2);}} break;
				case 'voiture': try { window.selectVoiture(); }catch (e3) {if (window.console) {window.console.error('window#quick(): ' + e3);}} break;
				case 'hotel': try { window.selectHotel(); }catch (e4) {if (window.console) {window.console.error('window#quick(): ' + e4);}} break;
				case 'vol': try { window.selectVol(); }catch (e5) {if (window.console) {window.console.error('window#quick(): ' + e5);}} break;
				default: break;
			}
		}
	};
	// Des surcharges de la précédente
	window.getHeight = function(id) {
		return window.getStyle('height', id);
	};
	window.getTop = function(id) {
		return window.getStyle('top', id);
	};
	window.selectTypeAS = function() {
		document.getElementById('retour').style.visibility = 'hidden'; // cache le bloc retour
		document.getElementById('trajetRetour').style.visibility = 'hidden'; // cache le bloc retour
	};
	window.selectTypeAR = function() {
		document.getElementById('retour').style.visibility = 'visible'; // affiche le bloc retour
		document.getElementById('trajetRetour').style.visibility = 'hidden'; // cache le bloc trajetRetour
		document.getElementById('dateRetour').style.top = (-1 * window.getHeight('trajetRetour')) + 'px'; // remonte le bloc dateRetour
	};
	window.selectTypePC = function() {
		document.getElementById('retour').style.visibility = 'visible'; // affiche le bloc retour
		document.getElementById('trajetRetour').style.visibility = 'visible'; // affiche le bloc trajetRetour
		document.getElementById('dateRetour').style.top = '0px'; // définit une marge nulle pour le bloc dateRetour
	};

	var LUNDI = 1,
			MARDI = 2,
			MERCREDI = 3,
			JEUDI = 4,
			VENDREDI = 5,
			SAMEDI = 6,
			DIMANCHE = 0,
			aujourdhui = new Date(),
			jourDeSemaine = aujourdhui.getDay(),
			interval = 0;

	switch (jourDeSemaine) {
		case LUNDI:
			interval = 4;
			break;
		case MARDI:
			interval = 3;
			break;
		case MERCREDI:
			interval = 9;
			break;
		case JEUDI:
			interval = 8;
			break;
		case VENDREDI:
			interval = 7;
			break;
		case SAMEDI:
			interval = 6;
			break;
		case DIMANCHE:
			interval = 5;
			break;
	}

	var dateAller = new Date(aujourdhui.getTime());
	var dateDeRetour = new Date(aujourdhui.getTime());
	dateAller.setDate(aujourdhui.getDate() + interval);
	dateDeRetour.setDate(aujourdhui.getDate() + interval + 7);

	window.formatNb2Chf = function(nb) {
		return nb < 10 ? '0' + nb : '' + nb;
	};
	var now = new Date(), before = new Date();
	before.setTime(now.getTime() + 7 * 24 * 3600 * 1000);
	var bj = before.getDate();
	if (bj < 10) {
		bj = '0' + bj;
	}
	var bm = before.getMonth() + 1;
	if (bm < 10) {
		bm = '0' + bm;
	}
	var by = before.getFullYear();
	var after = new Date();
	after.setTime(now.getTime() + 14 * 24 * 3600 * 1000);
	var aj = after.getDate();
	if (aj < 10) {
		aj = '0' + aj;
	}
	var am = after.getMonth() + 1;
	if (am < 10) {
		am = '0' + am;
	}
	var ay = after.getFullYear();
	window.corrigerAccents = function(input) {
		var valeur = input.value;
		valeur = valeur.replace(/ä/g, 'a');
		valeur = valeur.replace(/â/g, 'a');
		valeur = valeur.replace(/à/g, 'a');
		valeur = valeur.replace(/é/g, 'e');
		valeur = valeur.replace(/è/g, 'e');
		valeur = valeur.replace(/ê/g, 'e');
		valeur = valeur.replace(/ë/g, 'e');
		valeur = valeur.replace(/ï/g, 'i');
		valeur = valeur.replace(/î/g, 'i');
		valeur = valeur.replace(/ö/g, 'o');
		valeur = valeur.replace(/ô/g, 'o');
		valeur = valeur.replace(/ü/g, 'u');
		valeur = valeur.replace(/û/g, 'u');
		valeur = valeur.replace(/ù/g, 'u');
		valeur = valeur.replace(/ç/g, 'c');
		valeur = valeur.replace(/-/g, ' ');
		valeur = valeur.replace(/_/g, ' ');
		valeur = valeur.replace(/'/g, ' ');
		valeur = valeur.toLowerCase();
		valeur = valeur.replace(/st /g, 'saint ');
		valeur = valeur.replace(/ste /g, 'sainte ');
		input.value = valeur;
	};
	window.optionsMois = function() {
		var now = new Date();
		var nowMonth = now.getMonth();
		var nowYear = now.getFullYear();
		for (var i = nowMonth; i <= nowMonth + 12; i++) {
			if ((i % 12 + 1) < 10) {
				document[WRT]('<option value=\"0' + (i % 12 + 1) + '/' + nowYear + '\">0' + (i % 12 + 1) + '/' + nowYear + '</option>');
			}
			else {
				document[WRT]('<option value=\"' + (i % 12 + 1) + '/' + nowYear + '\">' + (i % 12 + 1) + '/' + nowYear + '</option>');
			}
			if (i == 11) {
				nowYear++;
			}
		}
	};
	window.optionsMois2 = function(selectedMonth,selectedYear) {
		var now = new Date(dateAller.getTime());
		var nowMonth, nowYear;
		for (var i = 0; i < 12; ++i) {
			nowMonth = now.getMonth();
			nowYear = now.getFullYear();
			if (nowMonth < 9) {
				document[WRT]('<option value=\"0' + (nowMonth + 1) + '/' + nowYear + '\"' + ((nowMonth == selectedMonth && nowYear == selectedYear) ? ' selected' : '') + '>0' + (nowMonth + 1) + '/' + nowYear + '</option>');
			}
			else {
				document[WRT]('<option value=\"' + (nowMonth + 1) + '/' + nowYear + '\"' + ((nowMonth == selectedMonth && nowYear == selectedYear) ? ' selected' : '') + '>' + (nowMonth + 1) + '/' + nowYear + '</option>');
			}
			now.setMonth(now.getMonth() + 1);
		}
	};
	window.verify = function(f) {
		f.departAller.value = f.departAller.value.replace(/^\s+/g, '').replace(/\s+$/g, '');
		f.arriveeAller.value = f.arriveeAller.value.replace(/^\s+/g, '').replace(/\s+$/g, '');
		window.corrigerAccents(f.departAller);
		window.corrigerAccents(f.arriveeAller);
	};
	window.verifyMEH = function(f) {
		f.ville.value = f.ville.value.replace(/^\s+/g, '').replace(/\s+$/g, '');
		window.corrigerAccents(f.ville);
		f.md1.value = f.mad1.value.substring(0, 2);
		f.ad1.value = f.mad1.value.substring(3, 7);
		f.md2.value = f.mad2.value.substring(0, 2);
		f.ad2.value = f.mad2.value.substring(3, 7);
	};
	window.verifyAPI = function(f) {
		if (f.formulaName.options[f.formulaName.selectedIndex].value == 'sejour_forme') {
			f.formulaName.options[f.formulaName.selectedIndex].value = '';
			f.themeName.value = 'thalasso';
		}
		f.minPrice.value = f.budget.value.replace(/z.*$/ig, '');
		f.maxPrice.value = f.budget.value.replace(/^.*z/g, '');
	};
	/* FONCTIONS DE GESTION DE L'AFFICHAGE */
	window.setClassName = function(id,nm) { if (document.getElementById(id)) { document.getElementById(id).className = nm; } };
	window.setVisibilityStyle = function(id,sv) {if (document.getElementById(id)) { document.getElementById(id).style.visibility = sv;}};
	window.selectVol = function() {
		window.setClassName('navMEV', 'navMEVon bouton on');
		window.setClassName('navMES', 'navMESoff bouton off');
		window.setClassName('navMEH', 'navMEHoff bouton off');
		window.setClassName('navMEC', 'navMECoff bouton off');
		window.setClassName('navCAR', 'navCARoff bouton off');
		window.setVisibilityStyle('formMEV', 'visible');
		window.setVisibilityStyle('formMES', 'hidden');
		window.setVisibilityStyle('formMEH', 'hidden');
		window.setVisibilityStyle('formMEC', 'hidden');
		window.setVisibilityStyle('formCAR', 'hidden');
		if (document.getElementById('formMEV')) {
			document.getElementById('formMEV').departAller.focus();
		}
	};
	window.selectSejour = function() {
		window.setClassName('navMEV', 'navMEVoff bouton off');
		window.setClassName('navMES', 'navMESon bouton on');
		window.setClassName('navMEH', 'navMEHoff bouton off');
		window.setClassName('navMEC', 'navMECoff bouton off');
		window.setClassName('navCAR', 'navCARoff bouton off');
		window.setVisibilityStyle('formMEV', 'hidden');
		window.setVisibilityStyle('formMES', 'visible');
		window.setVisibilityStyle('formMEH', 'hidden');
		window.setVisibilityStyle('formMEC', 'hidden');
		window.setVisibilityStyle('formCAR', 'hidden');
	};
	window.selectHotel = function() {
		window.setClassName('navMEV', 'navMEVoff bouton off');
		window.setClassName('navMES', 'navMESoff bouton off');
		window.setClassName('navMEH', 'navMEHon bouton on');
		window.setClassName('navMEC', 'navMECoff bouton off');
		window.setClassName('navCAR', 'navCARoff bouton off');
		window.setVisibilityStyle('formMEV', 'hidden');
		window.setVisibilityStyle('formMES', 'hidden');
		window.setVisibilityStyle('formMEH', 'visible');
		window.setVisibilityStyle('formMEC', 'hidden');
		window.setVisibilityStyle('formCAR', 'hidden');
		if (document.getElementById('formMEH')) {
			document.getElementById('formMEH').lieu.focus();
		}
	};
	window.selectCircuit = function() {
		window.setClassName('navMEV', 'navMEVoff bouton off');
		window.setClassName('navMES', 'navMESoff bouton off');
		window.setClassName('navMEH', 'navMEHoff bouton off');
		window.setClassName('navMEC', 'navMECon bouton on');
		window.setClassName('navCAR', 'navCARoff bouton off');
		window.setVisibilityStyle('formMEV', 'hidden');
		window.setVisibilityStyle('formMES', 'hidden');
		window.setVisibilityStyle('formMEH', 'hidden');
		window.setVisibilityStyle('formMEC', 'visible');
		window.setVisibilityStyle('formCAR', 'hidden');
	};
	window.selectVoiture = function() {
		window.setClassName('navMEV', 'navMEVoff bouton off');
		window.setClassName('navMES', 'navMESoff bouton off');
		window.setClassName('navMEH', 'navMEHoff bouton off');
		window.setClassName('navMEC', 'navMECoff bouton off');
		window.setClassName('navCAR', 'navCARon bouton on');
		window.setVisibilityStyle('formMEV', 'hidden');
		window.setVisibilityStyle('formMES', 'hidden');
		window.setVisibilityStyle('formMEH', 'hidden');
		window.setVisibilityStyle('formMEC', 'hidden');
		window.setVisibilityStyle('formCAR', 'visible');
		if (document.getElementById('formCAR')) {
			document.getElementById('formCAR').pickUpLocation.focus();
		}
	};
	window.showMEV = function() { try { window.selectVol(); }catch (e) {if (window.console) {window.console.error('window#showMEV(): ' + e);}} };
	window.showMEH = function() { try { window.selectHotel(); }catch (e) {if (window.console) {window.console.error('window#showMEH(): ' + e);}} };
	window.showMEC = function() { try { window.selectVoiture(); }catch (e) {if (window.console) {window.console.error('window#showMEC(): ' + e);}} };

	var typeVol = 'AR';

	/* Selecteur AR AS PC */
	window.setAS = function(maj) {
		if (maj != 'keep') {
			typeVol = 'AS';
		}
		try {
			window.hide('dateRetourMEV_LBL');window.hide('dateRetourMEV_INPUT');
			window.hide('departRetourMEV_LBL');window.hide('arriveeRetourMEV_LBL');window.hide('departRetourMEV_INPUT');window.hide('arriveeRetourMEV_INPUT');
		}catch (e) {
			if (window.console) {window.console.error('window#setAS(): ' + e);}
		}
	};

	window.setAR = function() {
		typeVol = 'AR';
		try {
			window.show('dateRetourMEV_LBL');window.show('dateRetourMEV_INPUT');
			/*
									document.getElementById('dateRetourMEV_LBL').style.position='absolute';
									document.getElementById('dateRetourMEV_INPUT').style.position='absolute';
									setTop('dateRetourMEV_LBL',87);setTop('dateRetourMEV_INPUT',95);
	 */
			window.hide('departRetourMEV_LBL');window.hide('arriveeRetourMEV_LBL');window.hide('departRetourMEV_INPUT');window.hide('arriveeRetourMEV_INPUT');
		}catch (e) {
			if (window.console) {window.console.error('window#setAR(): ' + e);}
		}
	};

	window.setPC = function() {
		typeVol = 'PC';
		try {
			window.show('dateRetourMEV_LBL');window.show('dateRetourMEV_INPUT');
			/*
									document.getElementById('dateAllerMEV_LBL').style.position='absolute';
									document.getElementById('dateRetourMEV_LBL').style.position='absolute';
									document.getElementById('dateRetourMEV_INPUT').style.position='absolute';
									setTop('dateRetourMEV_LBL',137);setTop('dateRetourMEV_INPUT',145);
									setTop('dateAllerMEV_LBL',137);
									 */
			window.show('departRetourMEV_LBL');window.show('arriveeRetourMEV_LBL');window.show('departRetourMEV_INPUT');window.show('arriveeRetourMEV_INPUT');
		}catch (e) {
			if (window.console) {window.console.error('window#setPC(): ' + e);}
		}
	};

	window.dirou = function() {
		var choix = document.forms.formulaireMEV.elements.type.selectedIndex;
		switch (choix) {
			case 0: window.setAS();break;
			case 1: window.setAR();break;
			case 2: window.setPC();break;
		}
	};

	window.redirigerPC = function() {
		window.open('form.jsp', '_self');
	};

	window.dirou2 = function() {
		var choix = document.forms.formulaireMEV.elements.type.selectedIndex;
		switch (choix) {
			case 0: window.setAS();break;
			case 1: window.setAR();break;
			case 2: window.redirigerPC();break;
		}
	};

	window.setType = function(type) {
		try {
			document.forms.formulaireMEV.elements.type.selectedIndex = type;
			window.dirou();
		}catch (e) {
			if (window.console) {window.console.error('window#setType(): ' + e);}
		}
	};

	window.fermer = function() {
		var tabTmp = document.getElementsByTagName('*'), i;
		for (i = 0; i < tabTmp.length; i++) {
			if (tabTmp[i].className == 'errorContainer') {
				tabTmp[i].style.visibility = 'hidden';
			}
		}
	};

	window.preloadImg = function() {
		var d = document;
		if (d.images) {
			if (!d.p) {
				d.p = [];
			}
			var i, j = d.p.length,
					a = arguments;
			for (i = 0; i < a.length; i++) {
				if (a[i].indexOf('#')) {
					d.p[j] = new window.Image();
					d.p[j++].src = a[i];
				}
			}
		}
	};
}());

