// JavaScript Document

function SearchCodePostalByPays(actionOnBlur) {

	var action = 'SearchCodePostalByPays';
	var idPays = $F('pays');
	var code_postal = $F('code_postal');

	if (code_postal != '') {
		var url = '/ajax/geonames.php';
		var parametres = 'action=' + action + '&idPays=' + idPays + '&code_postal=' + code_postal + '&onBlur=' + actionOnBlur;

		var myAjax = new Ajax.Updater(
				'ResultSearchCodePostalByPays',
				url,
				{
				method: 'get',
				parameters: parametres,
				onFailure: rapporteErreur
				//onComplete: afficheDivListeCode
				}
			);
		$('ResultSearchCodePostalByPays').show();
		$('LabelSearchCodePostalByPays').show();
	}
}

//Dans l'inscription d'un membre, recherche la ville en fonction du code postal saisi
function SearchVilleByPostalCode(champPays, champCodePostal, champResult, champResultLabel, varOnBlur) {
	var action = 'SearchCodePostalByPays';
	var idPays = $F(champPays);
	var code_postal = $F(champCodePostal);

	if (code_postal != '') {
		var url = '/ajax/geonames.php';
		var parametres = 'action=' + action + '&idPays=' + idPays + '&code_postal=' + code_postal;

		var myAjax = new Ajax.Updater(
				champResult,
				url,
				{
				method: 'get',
				parameters: parametres,
				onFailure: rapporteErreur
				//onComplete: afficheDivListeCode
				}
			);
		$(champResult).show();
		$(champResultLabel).show();
	}
}

//Recherche les lieux à proximité d'une ville
function majListeLieuProximite(champGeonameid, nbLieu, km, champResult, idLieuDefault) {
	action = 'GetLieuPublicProximite';
	geonameid = $F(champGeonameid);
	
	parametres = 'action=' + action + '&geonameid=' + geonameid + '&nbLieu=' + nbLieu + '&km=' + km + '&idLieuDefault=' + idLieuDefault;

	jjj.ajax({
				type: 'GET',
				url: '/ajax/membres.php',
				data: parametres,
				error: rapporteErreur,
				success: function(data) {
				jjj(champResult).html(data);
			}
	});
	
	//jjj('#ResultVerifCodeEntreprise').show();
	//jjj('#LabelVerifCodeEntreprise').show(); 
}

function SearchTelByPays() {

	var idPays = $F('pays');
	var url = '/ajax/geonames.php';
	var parametres = 'action=SearchTelByPays' + '&idPays=' + idPays;

	var myAjax = new Ajax.Request(
			url,
			{
			method: 'get',
			parameters: parametres,
			onComplete: afficheReponse
			}
		);
}

function afficheReponse(requete) {
		//affiche le XML dans le textarea
		$('tel_1_code').innerHTML = requete.responseText;
	}


function SearchVille(nomChamp, champResultats, champRes, champSearch) {
	var idPays = $F('paysDepart');
	var search = $F(nomChamp);

	var url = '/ajax/geonames.php';
	var parametres = 'action=SearchVille' + '&idPays=' + idPays + '&search=' + search + '&champRes=' + champRes + '&champSearch=' + champSearch + '&champResultats=' + champResultats;

	$(champResultats).show();

	var myAjax = new Ajax.Updater(
			champResultats,
			url,
			{
			method: 'get',
			parameters: parametres,
			onFailure: rapporteErreur
			}
		);
}

function SearchVilleEtape(nomChamp, champResultats, champRes, champSearch) {
	var idPays = $F('paysDepart');
	var search = $F(nomChamp);

	var url = '/ajax/geonames.php';
	var parametres = 'action=SearchVilleEtape' + '&idPays=' + idPays + '&search=' + search + '&champRes=' + champRes + '&champSearch=' + champSearch + '&champResultats=' + champResultats;

	$(champResultats).show();

	var myAjax = new Ajax.Updater(
			champResultats,
			url,
			{
			method: 'get',
			parameters: parametres,
			onFailure: rapporteErreur
			}
		);
}

function UpdateInput(id, div, div2) {
	$(div).value = id;

	if(div2.length > 3) {
		$(div2).hide();
	}
}

function UpdateVillesEtapes(idVille, idTrajet) {
	var url = '/ajax/geonames.php';
	var parametres = 'action=AddVilleEtape' + '&idVille=' + idVille + '&idTrajet=' + idTrajet;

	var myAjax = new Ajax.Request(
			url,
			{
			method: 'get',
			parameters: parametres,
			evalScripts: true,
			onFailure: rapporteErreur,
			onComplete: majVillesEtapes
			}
		);
}

function deleteVilleEtape(idVille) {
	var url = '/ajax/geonames.php';
	var parametres = 'action=DeleteVilleEtape' + '&idVille=' + idVille;

	var myAjax = new Ajax.Request(
			url,
			{
			method: 'get',
			parameters: parametres,
			evalScripts: true,
			onFailure: rapporteErreur,
			onComplete: majVillesEtapes
			}
		);
}

function majVillesEtapes(idTrajet) {
	idTrajet = parseInt(idTrajet);

	if (isNaN(idTrajet) == false) {
		//idTrajet = null;
	}

	//if (isNaN(idTrajet) == false) {
		var champResultats = 'listeVillesEtapes';
		$('ResultSearchVilleEtape').hide();

		var url = '/ajax/geonames.php';
		var parametres = 'action=DisplayVilleEtape' + '&idTrajet=' + idTrajet;

		var myAjax = new Ajax.Updater(
				champResultats,
				url,
				{
				method: 'get',
				parameters: parametres,
				evalScripts: true,
				onFailure: rapporteErreur
				}
			);
	//}
}

function rapporteErreur(requete) {
	//alert("Désolé, une erreur s'est produite.");
}
