function trim (myString)
{
return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
} 

/*function ChargementStation(){
	$("#choix_lieu").hide();
	$("#select_lieu").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
	$("#select_lieu").show();
	$.ajax({
		url: 'traitementStation.php', 
		type:'POST', 
		data: "action=liste_station",
		cache:false, 
		success:function(reponse){
			$("#select_lieu").empty();
			$("#select_lieu").append(reponse);
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}

function ChargementLieu(step){
	
	
	if(step == "choix_type_lieu"){
		var url = "action=liste_type";	
	}
	
	if(step == "choix_lieu"){
		var type_lieu_id = $("#select_type_lieu").attr('value');
		var url = "action=liste_lieu&type_lieu_id="+type_lieu_id;
		
	}
	
	$("#choix_lieu").hide();
	$("#select_lieu").empty();
	$("#select_lieu").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
	$("#select_lieu").show();
	
	$.ajax({
		url: 'traitementLieux.php', 
		type:'POST', 
		data: url,
		cache:false, 
		success:function(reponse){
			$("#select_lieu").empty();
			$("#select_lieu").append(reponse);
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}


function CalculItineraire(step){
	
	var titre = $("#titre").attr('value');
	if(step == 'station'){
		var chaine = $("#select_station_arrivee").attr('value');
		chaine = chaine.split(":");
		var station_id = chaine[0];
		var station = chaine[1];
		var station_dep = $("#station_depart").attr('value');
		var lang = $("#select_lang").attr('value');
	}
	
	if(step == 'lieu'){
		var chaine = $("#select_lieu_arrivee").attr('value');
		
		chaine = chaine.split(":");
		var station_id = chaine[0];
		var station = chaine[1];
		var station_dep_fr = chaine[2];
		var station_dep_gb = chaine[3];
		var station_dep = $("#station_depart").attr('value');
		var lang = $("#select_lang").attr('value');
	}
	
	$("#choix_lieu").hide();
	$("#select_lieu").empty();
	$("#select_lieu").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
	$("#select_lieu").show();
	
	
	
	$("#votre_destination").empty();
	$("#votre_destination").append("Votre trajet :");
	

	$.ajax({
		url: 'traitementItineraire.php', 
		type:'POST', 
		data: "action=calcul_itineraire&station_depart="+station_dep+"&station_arrivee="+station+"&station_id="+station_id+"&lang="+lang,
		cache:false, 
		success:function(reponse){
			$("#select_lieu").hide();
			$("#trajet").empty();
			$("#trajet").append(reponse);
			$("#contenu_trajet").show();
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}

function Retour(){
	$("#votre_destination").empty();
	$("#votre_destination").append("Choisissez votre destination :");
	$("#select_lieu").empty();
	$("#select_lieu").hide();
	$("#trajet").empty();
	$("#contenu_trajet").hide();
	$("#choix_lieu").show();
	
}

function Imprimer(){
	
	$("#trajet").jqprint();
	
}



function TraitementLieu(param, id){

	if(param == "appel_ajout"){
		$("#liste_lieux").hide();
		$("#liste_types").hide();
		$("#liste_partenaires").hide();
		$("#traitement").empty();
		$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
		$("#traitement").show();
		var url_post = "action="+param;			
	}
	
	if(param == "appel_modif" ||param == "suppr"){
		$("#liste_lieux").hide();
		$("#liste_types").hide();
		$("#liste_partenaires").hide();
		$("#traitement").empty();
		$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
		$("#traitement").show();
		var url_post = "action="+param+"&lieu_id="+id;			
	}
	
	if(param == "ajout" || param == "modif"){
		var nom_fr = $("#nom_fr").attr('value');
		var nom_en = $("#nom_en").attr('value');
		var type = $("#select_type").attr('value');
		var station = $("#select_station").attr('value');
		var message_error = "";
		$("#error").hide();
		
		if(trim(nom_fr) == ""){
			message_error += "<li>Le nom fran&ccedil;ais n'a pa &eacute;t&eacute; saisi</li>";
		}
		
		if(trim(nom_en) == ""){
			message_error += "<li>Le nom anglais n'a pa &eacute;t&eacute; saisi</li>";
		}
		
		if(message_error != ""){
			$("#error").empty();
			$("#error").show();
			$("#error").append("<strong>Erreur !</strong><ul>"+message_error+"</ul>");
			return false;
		}
		else{
			$("#traitement").empty();
			$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
			$("#traitement").show();
			var url_post = "action="+param+"&nom_fr="+nom_fr+"&nom_en="+nom_en+"&type_id="+type+"&station_id="+station+"";	
			
			if(param == "modif"){ url_post = url_post + "&lieu_id="+id; }
		}
	}
	

	$.ajax({
		url: 'traitementLieux.php', 
		type:'POST', 
		data: url_post,
		cache:false, 
		success:function(reponse){
			
			if(param == 'appel_ajout' || param == 'appel_modif'){
				$("#traitement").empty();
				$("#traitement").append(reponse);
			}
			else{
				RefreshListeLieux();
			}
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}


function TraitementPartenaire(param, id){

	if(param == "appel_ajout"){
		$("#liste_lieux").hide();
		$("#liste_types").hide();
		$("#liste_partenaires").hide();
		$("#traitement").empty();
		$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
		$("#traitement").show();
		var url_post = "action="+param;			
	}
	
	if(param == "appel_modif" || param == "suppr" || param == "suppr_station"|| param == "liste_stations" || param == "appel_ajout_station"){
		$("#liste_lieux").hide();
		$("#liste_types").hide();
		$("#liste_partenaires").hide();
		$("#traitement").empty();
		$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
		$("#traitement").show();
		
		if(param == "suppr_station"){
			var chaine = id.split(":");
			
			partner_station_id = chaine[0];
			id = chaine[1];
			
			var url_post = "action="+param+"&partner_station_id="+partner_station_id;		
		}
		else{
			var url_post = "action="+param+"&partner_id="+id;		
		}
			
	}
	
	if(param == "ajout" || param == "modif"){
		var nom = $("#nom").attr('value');
		var adresse = $("#adresse").attr('value');
		var cp = $("#cp").attr('value');
		var ville = $("#ville").attr('value');
		var tel = $("#tel").attr('value');
		var message_error = "";
		$("#error").hide();
		
		if(trim(nom) == ""){
			message_error += "<br/>Le nom n'a pa &eacute;t&eacute; saisi";
		}
		
		if(trim(adresse) == ""){
			message_error += "<br/>L'adresse n'a pa &eacute;t&eacute; saisi";
		}
		
		if(trim(cp) == ""){
			message_error += "<br/>Le code postal n'a pa &eacute;t&eacute; saisi";
		}
		
		if(trim(ville) == ""){
			message_error += "<br/>La ville n'a pa &eacute;t&eacute; saisie";
		}
		
		if(trim(tel) == ""){
			message_error += "<br/>Le t&eacute;l&eacute;phone n'a pa &eacute;t&eacute; saisi";
		}

		
		if(message_error != ""){
			$("#error").empty();
			$("#error").show();
			$("#error").append("<strong>Erreur !</strong><ul>"+message_error+"</ul>");
			return false;
		}
		else{
			$("#traitement").empty();
			$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
			$("#traitement").show();
			var url_post = "action="+param+"&nom="+nom+"&adresse="+adresse+"&cp="+cp+"&ville="+ville+"&tel="+tel;	
			
			if(param == "modif"){ url_post = url_post + "&partner_id="+id; }
		}
	}
	
	
	if(param == "ajout_station"){
		var station_id = $("#select_station_arrivee").attr('value');

		$("#traitement").empty();
		$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
		$("#traitement").show();
		var url_post = "action="+param+"&station_id="+station_id+"&partner_id="+id;	
		
	}
	

	$.ajax({
		url: 'traitementPartenaires.php', 
		type:'POST', 
		data: url_post,
		cache:false, 
		success:function(reponse){
			
			if(param == 'appel_ajout' || param == 'appel_modif' || param == 'liste_stations' || param == 'appel_ajout_station'){
				$("#traitement").empty();
				$("#traitement").append(reponse);
			}
			else{
				
				if(param == 'ajout_station' || param == 'suppr_station'){
					RefreshListeStationsPartenaires(id);
				}
				else{
					alert(reponse);
					RefreshListePartenaires();
				}
			}
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}


function RefreshListeLieux(){
	
	$("#traitement").hide();
	$("#traitement").empty();
	$("#liste_lieux").empty();
	$("#liste_lieux").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
	$("#liste_lieux").show();
	
	

	$.ajax({
		url: 'traitementLieux.php', 
		type:'POST', 
		data: "action=liste",
		cache:false, 
		success:function(reponse){
			$("#liste_lieux").empty();
			$("#liste_lieux").append(reponse);
			$("#liste_types").show();
			$("#liste_partenaires").show();
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}


function RefreshListeTypes(){
	
	$("#traitement").hide();
	$("#traitement").empty();
	$("#liste_types").empty();
	$("#liste_types").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
	$("#liste_types").show();
	
	

	$.ajax({
		url: 'traitementTypes.php', 
		type:'POST', 
		data: "action=liste",
		cache:false, 
		success:function(reponse){
			$("#liste_types").empty();
			$("#liste_types").append(reponse);
			$("#liste_lieux").show();
			$("#liste_partenaires").show();
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}

function RefreshListePartenaires(){
	
	$("#traitement").hide();
	$("#traitement").empty();
	$("#liste_partenaires").empty();
	$("#liste_partenaires").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
	$("#liste_partenaires").show();
	
	

	$.ajax({
		url: 'traitementPartenaires.php', 
		type:'POST', 
		data: "action=liste",
		cache:false, 
		success:function(reponse){
			$("#liste_partenaires").empty();
			$("#liste_partenaires").append(reponse);
			$("#liste_lieux").show();
			$("#liste_types").show();
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}

function RefreshListeStationsPartenaires(id){
	
		$.ajax({
		url: 'traitementPartenaires.php', 
		type:'POST', 
		data: "action=liste_station&partner_id="+id,
		cache:false, 
		success:function(reponse){
			$("#traitement").empty();
			$("#traitement").append(reponse);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}

function AnnulerAdmin(){
	$("#traitement").hide();
	$("#liste_lieux").show();
	$("#liste_types").show();
	$("#liste_partenaires").show();
}


function ConfirmSuppr(param, id){
	if(param == "lieu"){
		if(confirm('Confirmez-vous la suppression de ce lieu ?')){ 
			TraitementLieu('suppr', id);
		}	
	}
	
	if(param == "type"){
		if(confirm('Confirmez-vous la suppression de ce type de lieu ?')){ 
			TraitementType('suppr', id);
		}	
	}
	
	if(param == "partenaire"){
		if(confirm('Confirmez-vous la suppression de ce partenaire ?')){ 
			TraitementPartenaire('suppr', id);
		}	
	}
	
	if(param == "station_partenaire"){
		if(confirm('Confirmez-vous la suppression de cette station pour ce partenaire ?')){ 
			TraitementPartenaire('suppr_station', id);
		}	
	}
}


function TraitementType(param, id){

	if(param == "appel_ajout"){
		$("#liste_lieux").hide();
		$("#liste_types").hide();
		$("#liste_partenaires").hide();
		$("#traitement").empty();
		$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
		$("#traitement").show();
		var url_post = "action="+param;			
	}
	
	if(param == "appel_modif" || param == "suppr"){
		$("#liste_lieux").hide();
		$("#liste_types").hide();
		$("#liste_partenaires").hide();
		$("#traitement").empty();
		$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
		$("#traitement").show();
		var url_post = "action="+param+"&type_id="+id;			
	}
	
	if(param == "ajout" || param == "modif"){
		var nom_type = $("#nom_type").attr('value');
		var message_error = "";
		$("#error").hide();
		
		if(trim(nom_type) == ""){
			message_error += "<li>Le nom du type n'a pa &eacute;t&eacute; saisi</li>";
		}

		if(message_error != ""){
			$("#error").empty();
			$("#error").show();
			$("#error").append("<strong>Erreur !</strong><ul>"+message_error+"</ul>");
			return false;
		}
		else{
			$("#traitement").empty();
			$("#traitement").append("<p align='center'>Chargement en cours...<br /><img src='style/images/loading.gif' /></p>");
			$("#traitement").show();
			var url_post = "action="+param+"&nom_type="+nom_type;	
			
			if(param == "modif"){ url_post = url_post + "&type_id="+id; }
		}
	}
	

	$.ajax({
		url: 'traitementTypes.php', 
		type:'POST', 
		data: url_post,
		cache:false, 
		success:function(reponse){
			
			if(param == 'appel_ajout' || param == 'appel_modif'){
				$("#traitement").empty();
				$("#traitement").append(reponse);
			}
			else{
				RefreshListeTypes();
			}
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			alert(textStatus);
		}
	})
}


function ChangeTypeCalcul(param){
	if(param == "lieu"){
		$("#bt_calcul").empty();
		$("#bt_calcul").append("<input onmouseout=\"this.className='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'\" onmouseover=\"this.className='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover'\" class=\"ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only\" onClick=\"CalculItineraire('lieu');\" name=\"\" type=\"button\" value=\"Calculer Itin&eacute;raire\">");
		
	}
	else{
		$("#bt_calcul").empty();
		$("#bt_calcul").append("<input onmouseout=\"this.className='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'\" onmouseover=\"this.className='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover'\" class=\"ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only\" onClick=\"CalculItineraire('station');\" name=\"\" type=\"button\" value=\"Calculer Itin&eacute;raire\">");
	
	}
}*/


//Fonction de vérification des dates
function ordredate(jdeb, mdeb, adeb, jfin, mfin, afin)
{
	if(adeb>afin){
		return false;
	}
	else
	{
		if(mdeb>mfin && adeb==afin){
			return false;
		}
		else
		{
			if(jdeb>jfin && adeb==afin && mdeb==mfin){
				return false;
			}
			else{
				return true;
			}
		}
	}	
} 


function ValideRecherche(){
	/*var date = $("#datepicker").attr('value');
	var erreur_date = $("#erreur_date_vide").attr('value');
	if(trim(date) == ""){
		alert(erreur_date);
		return false;
	}
	else{
		$("#FormRecherche").submit();
	}*/
	var dat = new Date();
	var jour_actuel = dat.getDate();
	var mois_actuel = (dat.getMonth()+1);
	var annee_actuel = (dat.getFullYear());
	
	if(ordredate(jour_actuel, mois_actuel, annee_actuel, 14, 02, 2011) == false){ alert("date fausse"); }
	
}


function UpdateTotal(){
	
	var total_general = 0;
	var prix_total_chambre = 0;
	var nb_ligne = $("#hidden_nb_ligne").attr('value');
		
	for(i=0;i<nb_ligne;i++){
			ch_obj_chambre = "#select_nb_chambre_"+i;
			nb_chambre = $(ch_obj_chambre).attr('value');
			if(nb_chambre != 0){			
				ch_obj_total = "#total_nb_chambre_"+i;
				prix_total_chambre = $(ch_obj_total).attr('value');
				total_general = parseFloat(total_general)+parseFloat(prix_total_chambre*nb_chambre);
			}
	}
	
	
	$("#result").empty();
	$("#result").append(total_general.toFixed(2));
	$("#result").append(" &euro;");
	
}


function SubmitForm(){
	var dat = new Date();
	var jour_actuel = dat.getDate();
	var mois_actuel = (dat.getMonth()+1);
	var annee_actuel = (dat.getFullYear());
	
	var date = $("#datepicker").attr('value');
	
	var chaine = date.split("/");
	
	var jour_select = chaine[0];
	var mois_select = chaine[1];
	var annee_select = chaine[2];
	
	
	var erreur_date_vide = $("#dico_date_vide").attr('value');
	var erreur_date_anterieure = $("#dico_invalid_date").attr('value');
	
	if(trim(date) == ""){
		alert(erreur_date_vide);
		return false;
	}
	else{
		if(ordredate(jour_actuel, mois_actuel, annee_actuel, jour_select, mois_select, annee_select) == false){ 
			alert(erreur_date_anterieure); 
			return false;
		}
		else{	
			//$("#yann_load").append("&nbsp;<img src=\"http://salonbourget.generale-hotellerie.com/images/loader.gif\" />");
			//$("#yann_load").show();
			document.getElementById('FormRecherche').submit();
		}
	}
}
