var tab_enfants = [
	0,0,0,0
];

function cacherRetour(){
	jQuery('#label_retour').css('visibility', 'hidden');
	jQuery('#date_retour').css('visibility', 'hidden');
	jQuery('#img_date_retour').css('visibility', 'hidden');
}

function afficherRetour(){
	jQuery('#label_retour').css('visibility', 'visible');
	jQuery('#date_retour').css('visibility', 'visible');
	jQuery('#img_date_retour').css('visibility', 'visible');
}

// Fonction qui reconstruit le tableau des paxs en fontion du nombre de chambre
function modifier_div_pax(nb_chambres){
	// On met en visible les nb_chambres premières chambres en visible, et en hidden les restantes
	// idem pour le bloc des ages des enfants
	var max_nb_enfants = 0;
	for(i=1;i<=parseInt(nb_chambres);i++){
		jQuery('#chambre_' + i + '_repartition').show();
		// Si on a au moins un enfant dans la chambre alors on affiche les ages des enfants dans cette chambre
		nb_enfants = parseInt(jQuery('#nbChilds_room_' + i).val());
		if(nb_enfants > 0){
			jQuery('#chambre_' + i + '_age_enfants').show();
		}
		if(max_nb_enfants < nb_enfants)
			max_nb_enfants = nb_enfants;
	}
	// Gestion des entetes des enfants
	affiche_entete_enfants(max_nb_enfants);
	for(i=parseInt(nb_chambres)+1;i<=4;i++){
		jQuery('#chambre_' + i + '_repartition').hide();
		jQuery('#chambre_' + i + '_age_enfants').hide();
	}
}

// Fonction qui construit les select pour la répartition des passagers dans les chambres
function construire_select(type, indice_chambre){
	switch(type){
		case 'adulte' :{
			nb_max = 4;
			page = 
				'<select name="nbAdults_room_' + indice_chambre + '">';
		}break;
		case 'enfant' :{
			nb_max = 3;
			page = 
				'<select name="nbChilds_room_' + indice_chambre + '" onchange="ageEnfants(' + indice_chambre + ', this.value);">';
		}break;
	}
	
	for(j=1;j<=nb_max;j++){
		page += '<option value="' + j + '">' + j + '</option>';
	}
	page += '</select>';
	return page;
}

// Fonction qui construit les select pour la défintiion des ages des enfants dans le bloc bloc_enfants
function ageEnfants(indice_chambre, nb_enfants){
	tab_enfants[parseInt(indice_chambre)-1] = nb_enfants;
	// On affiche la chambre indice_chambre
	if(nb_enfants > 0){
		jQuery('#bloc_enfants').show();
		jQuery('#chambre_' + indice_chambre + '_age_enfants').show();
		for(i=1;i<=parseInt(nb_enfants);i++){
			jQuery('#childAge_room_' + indice_chambre + '_' + i).show();
		}

		for(i=parseInt(nb_enfants)+1;i<=3;i++){
			jQuery('#childAge_room_' + indice_chambre + '_' + i).hide();
		}
	}
	else
		jQuery('#chambre_' + indice_chambre + '_age_enfants').hide();
	
	// Gestion des entetes du tableau des ages des enfants
	// On parcourt le tableau tab_enfants, et on prend le max de ce tableau
	max_enfant = tab_enfants[0];
	for(i=1;i<4;i++){
		if(tab_enfants[i] > max_enfant)
			max_enfant = tab_enfants[i];
	}
	affiche_entete_enfants(max_enfant);
}

function affiche_entete_enfants(max_enfants){
	switch(parseInt(max_enfants)){
		case 0 : {// Aucun enfant
			jQuery('#bloc_enfants').hide();
			jQuery('#entete_enfant1').hide();
			jQuery('#entete_enfant2').hide();
			jQuery('#entete_enfant3').hide();
		}break;
		
		case 1 :{// On a un seul enfant max on affiche l'entete 1 et on vire les entete 2 et 3
			jQuery('#entete_enfant1').show();
			jQuery('#entete_enfant2').hide();
			jQuery('#entete_enfant3').hide();
			
		}break;
		
		case 2 :{// On a 2 enfants on affiche les entete 1 et 2 et on vire l'entete 3
			jQuery('#entete_enfant1').show();
			jQuery('#entete_enfant2').show();
			jQuery('#entete_enfant3').hide();
		}break;
		
		case 3 :{// On a 3 enfants on affiche les entete 1,2 et 3
			jQuery('#entete_enfant1').show();
			jQuery('#entete_enfant2').show();
			jQuery('#entete_enfant3').show();
		}break;
	}
	
}

function changerListeVilleOption(code){
	// top desti => parcours des pays
	if(code==""){
		for(i in listeDestinationLocation){
			if(typeof listeDestinationLocation[i] == 'object'){
				retour += changerListeVilleOption(i);
			}
		}	
		return retour;		
	}
	
	code = code.split('#');
	if(code.length == 2)
		code = code[1];
	else
		code = code[0];
	
	var codes;
	codes = code.split('.');
	var retour = "";
		
	// pays => parcours des regions
	if(codes.length == 1){
		for(i in listeDestinationLocation[codes[0]]){
			if(typeof listeDestinationLocation[codes[0]][i] == 'object'){
				retour += changerListeVilleOption(code+'.'+i);
			}
		}	
		return retour;			
	}
	// region => parcours des villes
	else{
		for(i in listeDestinationLocation[codes[0]][codes[1]]){
			retour += '<option value="'+listeDestinationLocation[codes[0]][codes[1]][i]+"#"+code+'.'+i+'" >'+listeDestinationLocation[codes[0]][codes[1]][i]+'</option>';
		}				
		return retour;
	}
}

function changerListeVille(code){
	
	var retour;
	retour = '<select class="select_scl" name="ville">';
	retour += '<option value="">Indifférent</option>';
	retour += changerListeVilleOption(code);
	retour += '</select>';
	
	// Modification de la liste
	jQuery('#listeVilles').html(retour);
}

// Fonction qui vérifie pour le formulaire des vols
// Si weekend : date aller + date retour + origine + destination saisie
// Si vol A/R : date aller + date retour + origine + destination saisie
// Si vol Aller simple : date aller + origine + destination saisie
function verifier_vol_week(type_recherche, min_jour_resa){
//ajouter date retour >= date depart pour vol
//ajouter date retour > date depart pour week-end
	var origine = jQuery('#depart').val();
	var destination = jQuery('#destination').val();
	var date_depart = jQuery('#date_depart').val();
	var date_retour = jQuery('#date_retour').val();
	if(origine.length <= 0){
		Sexy.alert('Veuillez saisir une ville de départ');
		return false;
	}
	if(destination.length <= 0){
		Sexy.alert('Veuillez saisir une destination');
		return false;
	}
	if(date_depart.length <= 0){
		Sexy.alert('Veuillez saisir une date de départ');
		return false;
	}
	
	
	switch(type_recherche){
		case 'vol' :{
			if(parseInt(jQuery('input[type=radio][name=oneway]:checked').attr('value')) <= 0){
				// A/R on vérifie que la date de retour a été saisie
				if(date_retour.length <= 0){
					Sexy.alert('Veuillez saisir une date de retour');
					return false;
				}

				if(date_retour.length > 0){
					dateD = date_depart.split('/');
					dateD = new Date(dateD[2], dateD[1]-1, dateD[0], 0, 0, 0);
					dateR = date_retour.split('/');
					dateR = new Date(dateR[2], dateR[1]-1, dateR[0], 0, 0, 0);

					if(dateR < dateD){
						Sexy.alert('Votre date de retour est antérieure à la date d’aller. Vous devez modifier votre recherche.');
						return false;
					}
				}
			}
		}break;
		
		case 'weekend' :{
			if(date_retour.length <= 0){
				Sexy.alert('Veuillez saisir une date de retour');
				return false;
			}

			if(date_retour.length > 0){
				dateD = date_depart.split('/');
				dateD = new Date(dateD[2], dateD[1]-1, dateD[0], 0, 0, 0);
				dateR = date_retour.split('/');
				dateR = new Date(dateR[2], dateR[1]-1, dateR[0], 0, 0, 0);

				if(dateR <= dateD){
					Sexy.alert('Votre date de retour est antérieure ou égale à la date d’aller. Vous devez modifier votre recherche.');
					return false;
				}
			}
		}
	}
	return verifier_min_jour_resa(date_depart, min_jour_resa);
}

function verifier_hotel(type_recherche, min_jour_resa){
//controle date retour > date depart
	switch(type_recherche){
		case 'hotel_point' :{
			var destination = jQuery('#destination').val();
			if(destination.length <= 0){
				Sexy.alert('Veuillez saisir une destination');
				return false;
			}
		}break;
		
		case 'hotel_adresse' :{
			var adresse = jQuery('#adresse').val();
			var cp = jQuery('#cp').val();
			var ville = jQuery('#ville').val();
			var pays = jQuery('#pays').val();
			if(adresse.length <= 0){
				Sexy.alert('Veuillez saisir une adresse');
				return false;
			}
			if(cp.length <= 0){
				Sexy.alert('Veuillez saisir un code postal');
				return false;
			}
			if(ville.length <= 0){
				Sexy.alert('Veuillez saisir une ville');
				return false;
			}
			if(pays.length <= 0){
				Sexy.alert('Veuillez sélectionner un pays');
				return false;
			}
		}
	}
	var date_depart = jQuery('#date_depart').val();
	var date_retour = jQuery('#date_retour').val();
	if(date_depart.length <= 0){
		Sexy.alert('Veuillez saisir une date de départ');
		return false;
	}
	if(date_retour.length <= 0){
		Sexy.alert('Veuillez saisir une date de retour');
		return false;
	}
	
	dateD = date_depart.split('/');
	dateD = new Date(dateD[2], dateD[1]-1, dateD[0], 0, 0, 0);
	dateR = date_retour.split('/');
	dateR = new Date(dateR[2], dateR[1]-1, dateR[0], 0, 0, 0);
	
	if(dateR <= dateD){
		Sexy.alert('Votre date de retour est antérieure ou égale à la date d’aller. Vous devez modifier votre recherche.');
		return false;
	}
	return verifier_min_jour_resa(date_depart, min_jour_resa);
}

function verifier_min_jour_resa(date_depart, min_jour_resa){
	date_courant = new Date();
	heures_courant = date_courant.getHours();
	minutes_courant = date_courant.getMinutes();
	secondes_courant = date_courant.getSeconds();
	date_courant = date_courant.getTime();
	ex = date_depart.split('/');
	date_depart = new Date(ex[2], ex[1]-1, ex[0], heures_courant, minutes_courant+1, secondes_courant);
	date_depart = date_depart.getTime();
	//alert((date_depart - date_courant)/1000 - (min_jour_resa * 24 * 60 * 60));
	if(((date_depart - date_courant)/1000) < (min_jour_resa * 24 * 60 * 60)){       
		Sexy.alert('Départ à moins de '+min_jour_resa+' jours impossible');
		return false;
	}
	return true;
}

function verifier_hotel_vert(type_recherche, min_jour_resa){
	switch(type_recherche){
		case 'hotel_point' :{
			var destination = jQuery('#destination').val();
			if(destination.length <= 0){
				Sexy.alert('Veuillez saisir une destination');
				return false;
			}
		}break;
		
		case 'hotel_adresse' :{
			var adresse = jQuery('#adresse').val();
			if(adresse.length <= 0){
				Sexy.alert('Veuillez saisir une adresse');
				return false;
			}
		}
	}
	var date_depart = jQuery('#date_depart').val();
	var date_retour = jQuery('#date_retour').val();
	if(date_depart.length <= 0){
		Sexy.alert('Veuillez saisir une date de départ');
		return false;
	}
	if(date_retour.length <= 0){
		Sexy.alert('Veuillez saisir une date de retour');
		return false;
	}
	
	return verifier_min_jour_resa(date_depart, min_jour_resa);
}

function verifier_scl(min_jour_resa){
	dd = jQuery('#dd').val();
	dmy = jQuery('#dmy').val();
	
	// destination location obligatoire
	type = jQuery('#c_TYP').val();
	if(type == 'loc'){
		pays = jQuery('#pays').val();
		ville = jQuery('#ville').val();

		if(!ville && !pays){
			Sexy.alert('Veuillez sélectionner une destination');
			return false;
		}
	}
	
	// on autorise date indifférente et mois sans jour
	if(dd.length<=0){
		return true;
	}
	
	if(dmy.length<=0){
		Sexy.alert('Veuillez sélectionner une date de départ valide');
		return false;
	}
	date_depart = dd + '/' + dmy;
	//alert(date_depart);
	
	return verifier_min_jour_resa(date_depart, min_jour_resa);
}

function MAJ_date_retour(type_recherche){
	// Récupération de la date
	var date_depart;
	var nb_jours;
	var timestamp;
	var ex;
	date_depart = jQuery('#date_depart').val();
	if(date_depart != ''){
		ex = date_depart.split('/');
		date_depart = new Date(ex[2],parseInt(ex[1]-1),ex[0]);
		//timestamp = date_depart.getTime();
		switch(type_recherche){
			case 'vol':{
				nb_jours = 7;
			}break;
			
			case 'hotel_point':
			case 'hotel_adresse':{
				nb_jours = 1;
			}break;
			
			case 'weekend':{
				nb_jours = 2;
			}break;
		}
		
		// Calcul de la date retour
		//alert(nb_jours * 24 * 60 * 60);
		date_retour = new Date(date_depart.getTime() + (1000 * 60 * 60 * 24 * nb_jours + 3600000));// Ajoute nb_jours à la date de départ, on ajoute 3600000 à cause de l'heure d'été (+1heure)
		jour2 = date_retour.getDate();
		if(jour2<10)
			jour2 = '0'+jour2;

		mois2 = date_retour.getMonth()+1;
		if(mois2<10)
			mois2 = '0'+mois2;
		// return true;
		annee2 = date_retour.getFullYear();
		jQuery('#date_retour').val(jour2+'/'+mois2+'/'+annee2);
	}
}











