// This file contains javascript functions used in dp search engines


	// BEGIN DP departure cities management
   function DepartureCity(code, city) {
      this.code = code;
      this.city = city;
   }		

	var dpDepartureCities = new Array;
		var i = 0;
		dpDepartureCities[i++] = new DepartureCity("PAR", "Paris");
		dpDepartureCities[i++] = new DepartureCity("AJA", "Ajaccio");
		dpDepartureCities[i++] = new DepartureCity("MLH", "B&acirc;le-Mulhouse");
		dpDepartureCities[i++] = new DepartureCity("BIA", "Bastia");
		dpDepartureCities[i++] = new DepartureCity("BIQ", "Biarritz");
		dpDepartureCities[i++] = new DepartureCity("BOD", "Bordeaux");
		dpDepartureCities[i++] = new DepartureCity("BES", "Brest");
		dpDepartureCities[i++] = new DepartureCity("CFE", "Clemont-Ferrand");
		dpDepartureCities[i++] = new DepartureCity("FDF", "Fort-de-France");
		dpDepartureCities[i++] = new DepartureCity("LIL", "Lille");
		dpDepartureCities[i++] = new DepartureCity("LYS", "Lyon");
		dpDepartureCities[i++] = new DepartureCity("MRS", "Marseille");
		dpDepartureCities[i++] = new DepartureCity("MPL", "Montpellier");
		dpDepartureCities[i++] = new DepartureCity("NTE", "Nantes");
		dpDepartureCities[i++] = new DepartureCity("NCE", "Nice");
		dpDepartureCities[i++] = new DepartureCity("PTP", "Pointe-&agrave;-Pitre");
		dpDepartureCities[i++] = new DepartureCity("RNS", "Rennes");
		dpDepartureCities[i++] = new DepartureCity("SXB", "Strasbourg");
		dpDepartureCities[i++] = new DepartureCity("TLN", "Toulon");
		dpDepartureCities[i++] = new DepartureCity("TLS", "Toulouse");
		dpDepartureCities[i++] = new DepartureCity("", "-----------------");
		dpDepartureCities[i++] = new DepartureCity("AMS", "Amsterdam");
		dpDepartureCities[i++] = new DepartureCity("BCN", "Barcelone");
		dpDepartureCities[i++] = new DepartureCity("BRU", "Bruxelles");
		dpDepartureCities[i++] = new DepartureCity("CGN", "Cologne");
		dpDepartureCities[i++] = new DepartureCity("FRA", "Francfort");
		dpDepartureCities[i++] = new DepartureCity("GVA", "Gen&egrave;ve");
		dpDepartureCities[i++] = new DepartureCity("LIS", "Lisbonne");
		dpDepartureCities[i++] = new DepartureCity("LON", "Londres");
		dpDepartureCities[i++] = new DepartureCity("LUX", "Luxembourg");
		dpDepartureCities[i++] = new DepartureCity("MAD", "Madrid");
		dpDepartureCities[i++] = new DepartureCity("MIL", "Milan");
		dpDepartureCities[i++] = new DepartureCity("YMQ", "Montr&eacute;al");
		dpDepartureCities[i++] = new DepartureCity("OPO", "Porto");
		dpDepartureCities[i++] = new DepartureCity("ROM", "Rome");
		dpDepartureCities[i++] = new DepartureCity("VIE", "Vienne ");
		dpDepartureCities[i++] = new DepartureCity("ZRH", "Zurich");

	
	function writeDpDepartureOptions() {
		
		for (var i = 0; i < dpDepartureCities.length; i++)
			document.write('<option value="'+dpDepartureCities[i].code+'">'+dpDepartureCities[i].city+'</option>'+'\n');

	}	
	// END DP departure cities management



	// BEGIN FROM search_box.js
	var dpDelay = 3;
	var all_month = new Array("JAN", "FEV", "MAR", "AVR", "MAI", "JUN", "JUL", "AOU", "SEP", "OCT", "NOV", "DEC");
	
function fillDDayDP(document,all_month,dpDelay)
{
	var today = new Date()
	today.setDate(today.getDate() + dpDelay);
	var departureDay = today.getDate();

	//we build the menu
	var dayString, libSelected;

	for (i=1;i<=31;i++)
	{
		if(i<10)
			dayString = '0'+i;
		else
			dayString = i;

		if (i == departureDay)
			libSelected = " selected";
		else
			libSelected = "";
		
		document.write('<option value="'+dayString+'"'+libSelected+'>'+dayString+'</option>');
	}
}

function fillDMonthDP(document,all_month,dpDelay)
{
	var now = new Date();
	now.setDate(now.getDate() + dpDelay);
	newDate=new Date(Date.UTC(now.getFullYear(),now.getMonth(),now.getDate()+0*1,0,0,0));

	for (i=0;i<13;i++)
	{

			var option = '<option value="'+all_month[now.getMonth()]+' '+now.getFullYear()+ '"';
							
			if(now.getMonth()==newDate.getMonth() && newDate.getYear()==now.getYear())
				option += ' selected="selected"';
			option += '>'+all_month[now.getMonth()]+' '+now.getFullYear()+'</option>';
			
			document.write(option);

		now.setDate(28);
		now.setMonth(now.getMonth()+1);
	}
}

function fillRDayDP(document,all_month,dpDelay)
{
	fillDDayDP(document,all_month,dpDelay+7)
}

function fillRMonthDP(document,all_month,dpDelay)
{
	var now = new Date();
	now.setDate(now.getDate() + dpDelay);

	newDate=new Date(Date.UTC(now.getFullYear(),now.getMonth(),now.getDate()+7*1,0,0,0));	
	newDate=new Date(now.getTime() + 7*24*60*60*1000);			

	for (i=0;i<19;i++)
	{
		
			var option = '<option value="'+all_month[now.getMonth()]+' '+now.getFullYear()+ '"';
							
			if(now.getMonth()==newDate.getMonth() && newDate.getYear()==now.getYear())
				option += ' selected="selected"';
			option += '>'+all_month[now.getMonth()]+' '+now.getFullYear()+'</option>';
			
			document.write(option);
			
		now.setDate(28);
		now.setMonth(now.getMonth()+1);
	}

}

/*----------------*/
/*-- monthNumber -*/
/*----------------*/
function monthNumber(monthName)
{
	var returnedMonth='';

	switch(monthName)
	{
		case 'JAN':
			returnedMonth='01';
			break;
		case 'FEV':
			returnedMonth='02';
			break;
		case 'MAR':
			returnedMonth='03';
			break;
		case 'AVR':
			returnedMonth='04';
			break;
		case 'MAI':
			returnedMonth='05';
			break;
		case 'JUN':
			returnedMonth='06';
			break;
		case 'JUL':
			returnedMonth='07';
			break;
		case 'AOU':
			returnedMonth='08';
			break;
		case 'SEP':
			returnedMonth='09';
			break;
		case 'OCT':
			returnedMonth='10';
			break;
		case 'NOV':
			returnedMonth='11';
			break;
		case 'DEC':
			returnedMonth='12';
			break;
		default:
			break;
	}
	return returnedMonth;
}

/*------------------*/
/*-- putDateDelay --*/
/*------------------*/

function putDateDelay(delay)
{
	var today = new Date();
	today.setDate(today.getDate()+delay);
	strToday = today.getMonth() + 1;

	if (strToday < 10)
		strToday = '0'+strToday;

	if (today.getDate()<10)
		strToday = strToday+ '/0'+today.getDate();
	else
		strToday = strToday+ '/'+today.getDate();

	strToday = strToday+'/'+today.getFullYear();
	return strToday;
}
	
function fGetDateAgenda(currentDay,currentMonth,updateDate)
{
	cptMonth = -1;
	cday = currentDay.value;
	i=0;
	while (i<all_month.length && cptMonth == -1)
	{
		if (currentMonth.value.substring(0,3) == all_month[i])
			cptMonth = i+1;
		i++;
	}
	
	if (cptMonth < 10)
		cptMonth = '0' + cptMonth;
	if (cday < 10)
		cday = '0' + cday;

	cdate = cptMonth+'/'+cday+'/'+currentMonth.value.substring(4,8);
	updateDate.value = cdate;
}

	function selectPax(ObjMaster,ObjCustom)
	{
		var minCustom = parseInt(ObjCustom.options[0].value);
		var ObjCustomMaxLength = 9 - parseInt(ObjMaster.value);
		var ObjCustomLength = ObjCustom.options.length;
		var ObjCustomSelect = ObjCustom.value;
		for (j = ObjCustomLength-1; j >0; j--)
			ObjCustom.options[j] = null;
		for (j=0;j<=ObjCustomMaxLength-minCustom;j++)
			ObjCustom.options[j] = new Option(j+minCustom,j+minCustom);
		if (parseInt(ObjCustomSelect) <= ObjCustomMaxLength)
			ObjCustom.value = ObjCustomSelect;
	}
	
	function updateBebe(ObjMaster,ObjCustom)
	{
		var ObjCustomSelect = ObjCustom.value;
		var ObjCustomLength = ObjCustom.options.length;
		var ObjCustomMaxLength = parseInt(ObjMaster.value);
		for (j = ObjCustomLength-1; j >0; j--)
			ObjCustom.options[j] = null;
		for (j=0;j<=ObjCustomMaxLength;j++)
			ObjCustom.options[j] = new Option(j,j);
		if (parseInt(ObjCustomSelect) <= ObjCustomMaxLength)
			ObjCustom.value = ObjCustomSelect;
		else
			ObjCustom.value = ObjCustomMaxLength;
	}
	
	// END FROM search_box.js
		
	function writeHoraire()
	{	
		document.write('<option value="">Sans Préférence</option>');
		document.write('<option value="MORNING"/>Matin (9:00-12:00)');
		document.write('<option value="AFTERNOON"/>Après-Midi (12:00-18:00)');
		document.write('<option value="EVENING"/>Soirée (18:00-00:00)');
		for(var i=1;i<10;i++)
			document.write('<option value="0'+i+'00"/>0'+i+':00');

		for(var i=10;i<=23;i++)
			document.write('<option value="'+i+'00"/>'+i+':00');
	}	
	
	function getNumericOptions(beginIndex, endIndex) {
		var numericOptions = '';

		for(var i=beginIndex; i <= endIndex; i++)
			numericOptions += '<option value="'+i+'">'+i+'</option>\n';

		return numericOptions;
	}

	function writeNumericOptions(beginIndex, endIndex) {
		document.write(getNumericOptions(beginIndex, endIndex));
	}


	function updateWeekDay(objDay, objMonth, objWeekDay)
	{
		var year = objMonth.value.substr(4,4) * 1;
		var monthName = objMonth.value.substr(0,3);
		var month = monthNumber(monthName) * 1 - 1;
		var day = objDay.value * 1;
	
		var d = new Date(Date.UTC(year,month,day,0,0,0));
	
		var tab_jour = new Array("Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");

		objWeekDay.value = tab_jour[d.getDay()];
	}


	// Fonction de soumission d'un formulaire dp hors recherche avancée avec popup d'age des enfants si nbChildren > 0
	function submitDpSearchOrPopup(form) {
		
		var nbChildren = form.NB_CHILDREN.value; 

		// S'il y a des enfants, on affiche la popup permettent de saisir leur age
		if (nbChildren > 0) 
		{
			// Creation des champs des ages
			var popupChildrenAgeFields = '';
			for (var i=1; i <= nbChildren; i++) {

				popupChildrenAgeFields += '\nAge de l&#146;enfant '+i+' : <select name="CHILDREN_AGES" class="selectDate">'+getNumericOptions(2,16)+'</select><br />';
			}
			document.getElementById('popupChildrenAgeFields').innerHTML = popupChildrenAgeFields;

			// Affichage de la popup
			ifrlayer.make(document.getElementById('popupChildren')); 
		}
		// Sinon on soumet le formulaire
		else 
		{
			doSubmitDpSearch(form);
		}
	}

	// Fonction de soumission effective du formulaire dp -- les champs CHILDREN_AGES doivent déjà être gérés
	function doSubmitDpSearch(form) {
			with (form) {
				
				hiddeError("divMessageError");
				
				var treatedD_MONTH = D_MONTH.value;
				var treatedR_MONTH = R_MONTH.value;
				var monthD_MONTH = treatedD_MONTH.substring(0,3);
				var monthR_MONTH = treatedR_MONTH.substring(0,3);
				var yearD_MONTH = treatedD_MONTH.substring(4,9);
				var yearR_MONTH = treatedR_MONTH.substring(4,9);
				monthD_MONTH = monthNumber(monthD_MONTH);
				monthR_MONTH = monthNumber(monthR_MONTH);
				treatedD_MONTH = yearD_MONTH+''+monthD_MONTH+'';
				treatedR_MONTH = yearR_MONTH+''+monthR_MONTH+'';
				
				D_DATE.value =  treatedD_MONTH+''+D_DAY.value;
				R_DATE.value =  treatedR_MONTH+''+R_DAY.value;
				
				if (!checkForm(form)) return;
				
				submit();
			}
	}
	
	// Vérification des champs du formulaire dp
	function checkForm (form) {
		with (form) {
			var dDate = new Date(D_DATE.value.substring(0,4),D_DATE.value.substring(4,6)-1,D_DATE.value.substring(6,8))
			var rDate = new Date(R_DATE.value.substring(0,4),R_DATE.value.substring(4,6)-1,R_DATE.value.substring(6,8))
			var maxReturndate = new Date(dDate);
			maxReturndate.setDate(dDate.getDate()+30);
			
			var inFourDays = new Date();
			inFourDays.setDate(inFourDays.getDate() + dpDelay);
			inFourDays.setHours(0);
			inFourDays.setMinutes(0);
			inFourDays.setSeconds(0);
			inFourDays.setMilliseconds(0);
			
			if (dDate>=rDate) {
				showError("La date de départ doit être antérieure à la date de retour.","divMessageError");
				return false;
			}
			if (rDate>=maxReturndate) {
				showError("Vous ne pouvez pas effectuer de recherche pour un séjour sur place de plus de 30 jours.","divMessageError");
				return false;
			}
			if (dDate < inFourDays) {
				showError("Votre date de départ est trop proche.\nLes réservations de Vol + Hôtel sont possibles au minimum "+dpDelay+" jours avant le départ.","divMessageError");
				return false;
			}
		}
		return true;
	}

	// Pour aller d'un formulaire dp au formulaire de recherche avancée dp sans perdre les valeurs des champs
	function submitToDpAdvSearch(form, appName) {
		with(form) {

			if (appName == undefined)
				appName = 'public-fr';
			
			action = '/'+appName+'/jsp/dphotel/DPAdvancedSearchServlet';
			submit();
		}
	}


	// Objet ifrlayer, permettant dafficher le faux popup de l'age des enfants en insérant dessous 
	// une iframe pour résoudre un probleme de superpositions des elements select propre a IE
	ifrlayer = {

		make:function(obj) {
			obj.style.display = 'block';

			if ((document.getElementsByTagName("select").length>0 || document.getElementsByTagName("embed").length>0 || document.getElementsByTagName("object").length>0) && document.all && document.getElementById && !window.opera) {
				if (obj.parentNode && !obj.iframelayer) var ifr = obj.parentNode.insertBefore(document.createElement("iframe"), obj);
				else return;
				if (obj.currentStyle.zIndex != "" && parseInt(obj.currentStyle.zIndex)>1 ) {
					ifr.style.zIndex = parseInt(obj.currentStyle.zIndex)-1;
				}
				ifr.src = "about:blank";
				with (ifr.style) { 
					filter = "alpha(opacity=0)";
					position = "absolute";
					width =  obj.offsetWidth+"px";
					height =  obj.offsetHeight+"px";
				}
				obj.iframelayer = ifr;
				ifrlayer.move(obj);
			}
		},

		kill:function(obj) {
			obj.style.display = 'none';

			var ifr = obj.iframelayer;
			if (ifr && ifr.parentNode) {
				ifr.parentNode.removeChild(ifr);
				obj.iframelayer = null;
			}
		},

		move:function(obj) {
			if (!obj || !obj.iframelayer) return;
			with (obj.iframelayer.style) {
				top = obj.offsetTop+"px";
				left =  obj.offsetLeft+"px";
			}
		}
	}
	// Fin objet ifrlayer

	// Returns the url of the map page
	function getMapUrl(name, x, y) {
		var url = 'http://www.viamichelin.com/b2b2c/fra/dyn/controller/DynamicPois?productId=79986&XMLRequest=';
		url += '<PoiRequest productID="79986"><InternalPoiList><XYPoi name="'+name+'" x="'+x+'" y="'+y+'"></XYPoi></InternalPoiList></PoiRequest>';
		return url;
	}
	
	function showError(error,divId){
		if(document.getElementById(divId) != null)
			document.getElementById(divId).innerHTML = "<p>"+error+"</p>";
		else
			alert(error);
	}
	
	function hiddeError(divId){
		if(document.getElementById(divId) != null)
			document.getElementById(divId).innerHTML = "";
	}