// JavaScript Document


/*
	###################################################################################

	Affiche la grande photo de l'album Apercu
	
	###################################################################################
*/
function booking_apercu_photo(id, photo)
{
	try
	{
		$('booking_grande_photo').src=photo;
		$('booking_infos').update('<span class="booking_titre_photo">'+$('titre_'+id).value+'</span><br />'+$('description_'+id).value);
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_apercu_photo\' dans booking.js :\n'+e.message);
	}
}

/*
	###################################################################################

	Renvoie le cal des dispo annuelle d'un heberg ou tout
	
	###################################################################################
*/
function booking_get_cal_annuel(id_hebergement)
{
	try
	{
		// On test si on est sur webagoo ou sur le domaine pour l'appel ajax
		var action=testdom('booking', 'ajax_get_cal_annuel.php', '/appel_ajax.php');
		var myAjax = new Ajax.Request(
		action,
		{
			method: 'post',
			asynchronous: false,
			parameters: {
				id_site: ID_SITE,
				id_hebergement: id_hebergement,
				sid: SESSION_ID,
				file: 'ajax_get_cal_annuel.php',
				lng: LNG,
				module: 'booking' },
			onCreate: function(retour) {
				$('booking_icone_loading_disponibilites').setStyle({visibility:'visible'});
			},
			onComplete: function(retour){
				if(retour.status==200)
				{
					$('booking_calendrier_container').update(retour.responseText);
					$('booking_icone_loading_disponibilites').setStyle({visibility:'hidden'});
				}
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_get_cal_annuel\' dans booking.js :\n'+e.message);
	}
}



/*
	###################################################################################

	Renvoie le bloc cal des dispo
	
	###################################################################################
*/
function booking_get_bloc_cal(val, id_container, id_select, id_loader)
{
	try
	{
		switch(val)
		{
			case '+'	:	var tmp=$(id_select).value.split('-'); var mois=new Number(tmp[0])+1; var annee=tmp[1]; break;
			case '-'	:	var tmp=$(id_select).value.split('-'); var mois=new Number(tmp[0])-1; var annee=tmp[1]; break;
			default		: var tmp=val.split('-'); var mois=tmp[0]; var annee=tmp[1]; break;
		}

		// On test si on est sur webagoo ou sur le domaine pour l'appel ajax
		var action=testdom('booking', 'ajax_get_bloc_cal.php', '/appel_ajax.php');
		var myAjax = new Ajax.Request(
		action,
		{
			method: 'post',
			asynchronous: false,
			parameters: {
				id_site: ID_SITE,
				mois: mois,
				annee: annee,
				sid: SESSION_ID,
				file: 'ajax_get_bloc_cal.php',
				lng: LNG,
				module: 'booking' },
			onCreate: function(retour) {
				$(id_loader).setStyle({visibility:'visible'});
			},
			onComplete: function(retour){
				if(retour.status==200)
				{
					var retour=retour.responseJSON;
					$(id_select).value=retour[0];
					$$('select#'+id_select+' option').each(function(o){ 
						if(o.value==retour[0])
						{
							o.selected=true;
							$(id_container).update(retour[1]);
							throw $break;
						}
					});				
					$(id_loader).setStyle({visibility:'hidden'});
				}
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_get_bloc_cal\' dans booking.js :\n'+e.message);
	}
}




/*
	###################################################################################

	Renvoie la page des dispos à partir du bloc résa
	
	###################################################################################
*/
function booking_bloc_resa(date_arrivee, nb_nuit, id_hebergement)
{
	try
	{
		if(date_arrivee!='')
		{
			// On test si on est sur webagoo ou sur le domaine pour l'appel ajax
			var action=testdom('booking', 'ajax_bloc_resa.php', '/appel_ajax.php');
			var myAjax = new Ajax.Request(
			action,
			{
				method: 'post',
				asynchronous: false,
				parameters: {
					id_site: ID_SITE,
					date_arrivee: date_arrivee,
					nb_nuit: nb_nuit,
					id_hebergement: id_hebergement,
					sid: SESSION_ID,
					file: 'ajax_bloc_resa.php',
					lng: LNG,
					module: 'booking' },
				onComplete: function(retour){
					if(retour.status==200)
					{
						document.location=retour.responseText;
					}
				}
			});
		}
		else
		{
			alert('Veuillez choisir une date d\'arriv\351e.');
		}
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_bloc_resa\' dans booking.js :\n'+e.message);
	}
}



/*
	###################################################################################

	Check ou pas les CGL et affiche le reste
	
	###################################################################################
*/
function booking_chk_cgl(mode)
{
	try
	{
		if(mode==true)
		{
			//Effect.BlindDown('shop_paiements_container', { duration: 0.5 });
			//Effect.ScrollTo('shop_ancre_paiements');
		}
		else
		{
			$('booking_container_paiements').update();
		}
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_chk_cgl\' dans shop.js :\n'+e.message);
	}
}





/*
	###################################################################################

	Enregistre ou supprime une option pour la résa
	
	###################################################################################
*/
function booking_save_opt_resa(id_periode, id_hebergement, ind, id_option, mode, type, nb_pers_heberg)
{
	try
	{
		// On test si on est sur webagoo ou sur le domaine pour l'appel ajax
		var action=testdom('booking', 'ajax_save_opt_resa.php', '/appel_ajax.php');
		var myAjax = new Ajax.Request(
		action,
		{
			method: 'post',
			asynchronous: false,
			parameters: {
				id_site: ID_SITE,
				id_periode: id_periode,
				id_hebergement: id_hebergement,
				id_option: id_option,
				mode: mode,
				ind: ind,
				type: type,
				nb_pers_heberg: nb_pers_heberg,
				sid: SESSION_ID,
				file: 'ajax_save_opt_resa.php',
				lng: LNG,
				module: 'booking' },
			onComplete: function(retour){
				try
				{
					//alert(retour.responseText);
					if(retour.status==200)
					{
						var tot=retour.responseJSON;
						if(id_option)
						{
							$('booking_div_opt_heberg_'+id_periode+'_'+id_hebergement+'_'+ind+'_'+id_option).update(tot['hebergements'][id_hebergement][ind]['options'][id_option]['montant_option_afficher']);
						}
						$('booking_div_total').update(tot['montant_total_afficher']);
						$('booking_div_tx_arrhes').update(tot['tx_arrhes']);
						$('booking_div_arrhes').update(tot['arrhes_afficher']);
						$('booking_montant_heberg_'+id_periode+'_'+id_hebergement+'_'+ind).update(tot['prix'][id_hebergement][ind]['montant_afficher']);
						$('booking_prix_heberg_'+id_periode+'_'+id_hebergement+'_'+ind).update(tot['prix'][id_hebergement][ind]['prix_afficher']);
						
						// OPTIONS
						for (var i in tot['hebergements'][id_hebergement][ind]['options'])
						{
							$('booking_div_opt_heberg_'+id_periode+'_'+id_hebergement+'_'+ind+'_'+i).update(tot['hebergements'][id_hebergement][ind]['options'][i]['montant_option_afficher']);
						}
					}
				}
				catch(e)
				{
					alert('Erreur dans la fonction booking_save_opt_resa : '+e.message);
				}
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_save_opt_resa\' dans booking.js :\n'+e.message);
	}
}



/*
	###################################################################################

	Fonction qui met à jour le status d'une résa
	
	###################################################################################
*/
function set_status_reservation(id_site, id_resa, id_paiement, paiement_force, status_force, redirect)
{
	try
	{
		// On test si on est sur webagoo ou sur le domaine pour l'appel ajax
		var action=testdom('booking', 'ajax_action_paiement.php', '/appel_ajax.php');
		var myAjax = new Ajax.Request(
		action,
		{
			method: 'post',
			asynchronous: true,
			parameters: {
				id_site: ID_SITE,
				id_resa: id_resa,
				id_paiement: id_paiement,
				paiement_force: paiement_force,
				status_force: status_force,
				sid: SESSION_ID,
				file: 'ajax_action_paiement.php',
				lng: LNG,
				module: 'booking' },
			onComplete: function(retour){
				if(retour.responseText!="" && redirect)
				{
					document.location=retour.responseText;
				}
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'set_status_resa\' dans booking.js :\n'+e.message);
	}
}




/*
	###################################################################################

	Renvoie les différents modes de paiements
	
	###################################################################################
*/
function booking_get_paiements()
{
	try
	{
		var action=testdom('booking', 'ajax_get_paiements.php', '/appel_ajax.php');
		var myAjax = new Ajax.Request(
		action,
		{
			method: 'post',
			parameters: {
				id_site: ID_SITE,
				file: 'ajax_get_paiements.php',
				sid: SESSION_ID,
				lng: LNG,
				module:'booking'
			},
			onCreate: function() {
				//$('booking_icone_loading').src=THEME_PROGRESS_MINI;
			},
			onComplete: function(retour) {
				$('booking_container_paiements').update(retour.responseText);
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_get_paiements\' dans booking.js :\n'+e.message);
	}
}



/*
	###################################################################################

	Enregistre en session les infos de la résa et passe à l'étape 2
	
	###################################################################################
*/
function booking_save_infos_resa()
{
	try
	{
		var heberg=Array();
		var arrivee=$('booking_date_arrivee').value;
		var depart=$('booking_date_depart').value;
		var nb_nuit=$('booking_nb_nuit').value;
		var nb_personne=$('booking_nb_personne').value;
		var tab_slt_qte=$$('select.booking_slt_qte_heberg');
		for(var i=0; i<tab_slt_qte.length; i++)
		{
			var tmpheb=tab_slt_qte[i].id.split('_');
			var lng=Array();
			lng[0]=tmpheb[tmpheb.length-1];
			lng[1]=tab_slt_qte[i].value;
			heberg[i]=lng;
		}
		var action=testdom('booking', 'ajax_save_heberg.php', '/appel_ajax.php');
		var myAjax = new Ajax.Request(
		action,
		{
			method: 'post',
			parameters: {
				arrivee: arrivee,
				depart: depart,
				nb_nuit: nb_nuit,
				nb_personne: nb_personne,
				heberg: heberg.toJSON(),
				id_site: ID_SITE,
				file: 'ajax_save_heberg.php',
				sid: SESSION_ID,
				lng: LNG,
				module:'booking'
			},
			onCreate: function() {
				//$('booking_icone_loading').src=THEME_PROGRESS_MINI;
			},
			onComplete: function(retour) {
				document.location=retour.responseText;
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_save_infos_resa\' dans booking.js :\n'+e.message);
	}
}




/*
	###################################################################################

	Renvoie les dispo d'une résa côté FRONT
	
	###################################################################################
*/
function booking_check_dispo()
{
	try
	{
		var arrivee=$('booking_date_arrivee').value;
		var depart=$('booking_date_depart').value;
		var nb_nuit=$('booking_nb_nuit').value;
		var nb_personne=$('booking_nb_personne').value;
		var id_hebergement=$('booking_id_hebergement').value;
		if(arrivee=='')
		{
			alert('Veuillez saisir une date d\'arriv\351e valide.');
			return false;
		}
		if(depart=='')
		{
			alert('Veuillez saisir une date de d\351part valide.');
			return false;
		}
		
		var action=testdom('booking', 'ajax_check_dispo.php', '/appel_ajax.php');
		var myAjax = new Ajax.Request(
		action,
		{
			method: 'post',
			parameters: {
				arrivee: arrivee,
				depart: depart,
				nb_nuit: nb_nuit,
				nb_personne: nb_personne,
				id_hebergement: id_hebergement,
				id_site: ID_SITE,
				file: 'ajax_check_dispo.php',
				sid: SESSION_ID,
				lng: LNG,
				module:'booking'
			},
			onCreate: function() {
				//$('booking_icone_loading').src=THEME_PROGRESS_MINI;
			},
			onComplete: function(retour) {
				if(retour.status==200)
				{
					$('booking_container_dispo').update(retour.responseText);
				}
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_check_dispo\' dans booking.js :\n'+e.message);
	}
}


/*
	###################################################################################

	Paramètre de la page côté FRONT
	
	###################################################################################
*/
function affect_option(idpage, id_site)
{
	try
	{
		var myAjax = new Ajax.Request(
		'../../booking/admin/ajax_choix_param_page.php',
		{
			method: 'post',
			parameters: 'id_page='+idpage+'&id_site='+id_site,
			onComplete: function(retour){
				//$('fin_module').innerHTML=retour.responseText;
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'affect_option\' dans booking.js :\n'+e.message);
	}
}



/*
	###################################################################################

	Calcul automatiquement le nombre de nuit à partir de 2 dates
	
	###################################################################################
*/
function booking_auto_nuit(encours)
{
	try
	{
		var arrivee=$('booking_date_arrivee').value;
		var depart=$('booking_date_depart').value;
		var nb_nuit=Math.abs($('booking_nb_nuit').value);
		// Pour les malin
		$('booking_nb_nuit').value=nb_nuit;
		var action=testdom('booking', 'ajax_check_date.php', '/appel_ajax.php');
		var myAjax = new Ajax.Request(
		action,
		{
			method: 'post',
			parameters: {
				arrivee: arrivee,
				depart: depart,
				nb_nuit: nb_nuit,
				mode: encours,
				id_site: ID_SITE,
				file: 'ajax_check_date.php',
				sid: SESSION_ID,
				lng: LNG,
				module:'booking'
			},
			onCreate: function() {
				//$('booking_icone_loading').src=THEME_PROGRESS_MINI;
			},
			onComplete: function(retour) {
				// ON EFFACE LES DISPOS
				$('booking_container_dispo').update();
				var rep=retour.responseJSON;
				if(rep['reponse']=='ok')
				{
					$('booking_date_arrivee').value=rep['arrivee'];
					$('booking_date_depart').value=rep['depart'];
					$('booking_nb_nuit').value=rep['nuits'];
				}
				else
				{
					alert(html_entity_decode(rep['alert'], 'ENT_QUOTES'));
				}
			}
		});
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_auto_nuit\' dans booking.js :\n'+e.message);
	}
}




/*
	###################################################################################

	Enregistre la résa
	
	###################################################################################
*/
function booking_save_resa(id_reservation)
{
	try
	{
		if($('booking_chk_cgl').checked==true)
		{
			var action=testdom('booking', 'ajax_save_reservation.php', '/appel_ajax.php');
			var myAjax = new Ajax.Request(
			action,
			{
				method: 'post',
				parameters: {
					id_reservation: id_reservation,
					id_site: ID_SITE,
					file: 'ajax_save_reservation.php',
					sid: SESSION_ID,
					lng: LNG,
					module:'booking'
				},
				onCreate: function() {
					//$('booking_icone_loading').src=THEME_PROGRESS_MINI;
				},
				onComplete: function(retour) {
					if(retour.status==200)
					{
						booking_get_paiements();
					}
				}
			});
		}
		else
		{
			alert('Vous devez accepter les conditions g\351n\351rales de location en cochant la case ci-dessus.');
		}
	}
	catch(e)
	{
		alert('Erreur dans la fonction \'booking_save_resa\' dans booking.js :\n'+e.message);
	}
}