
function choix_ville(resultats,sPerso) {
	var tabVilles = resultats.split("@@");
	// objets de la page
	comboListe = document.getElementById("liste"+sPerso);
	champVille = document.getElementById("ville"+sPerso);	
	// aucune ville : saisie manuelle dans champ suivant
	if ( tabVilles.length == 1 ) {
		champVille.focus();
		comboListe.style.display = "none";
		}
	// 1 seule ville : choix automatique
	if ( tabVilles.length == 2 ) {
		champVille.value = unescape(tabVilles[0]);
		comboListe.style.display = "none";
		champVille.focus();
		}
	// plusieurs villes : liste de sélection
	if ( tabVilles.length > 2 ) {
		comboListe.options.length = 0;
		comboListe.style.display = "inline";
		for (var i=0; i < tabVilles.length-1; i++) {
			comboListe.options[comboListe.options.length] = new Option(unescape(tabVilles[i]),unescape(tabVilles[i]));
			}
		};
	};

function verifCP(sPerso) {
	var comboListe = document.getElementById("liste"+sPerso);
	var champVille = document.getElementById("ville"+sPerso);
	var champCodep = document.getElementById("cp"+sPerso);
	var champPays  = document.getElementById("pays"+sPerso);
	if ( !isNaN(champCodep.value) ) {
		champVille.value = "";
		// masque et supprime liste si cp incorrect
		if ( champCodep.value.length < 5 || champPays.value != 72 ) {
			comboListe.options.length = 0;
			comboListe.style.display= "none";
			};
		// envoie des données
		if ( champCodep.value.length == 5 && champPays.value == 72 ) {
			window.status = "Recherche de la commune en cours...";
			resultats = query("villes.php","cp=" + champCodep.value );
			choix_ville(resultats,sPerso)
			window.status = "";
			};
		};
	};

// recopie une valeur et va sur le champ si focus=1
function recopie(source,cible,is_focus) {
	var champ_source = document.getElementById(source);
	var champ_cible = document.getElementById(cible);
	if ( !champ_source ||!champ_cible )
		alert("Champs non trouvés");
		else
		{
		champ_cible.value = champ_source.value;
		if ( is_focus == 1 )
			champ_cible.focus();
		};
	};

function compare_email() {
	if ( document.getElementById("adressemail").value != document.getElementById("adressemailconfirm").value || !testAdresse(document.getElementById("adressemail").value) )
		document.getElementById("verifemail").src = "img/icon-notok.png";
		else
		document.getElementById("verifemail").src = "img/icon-ok.png";
	};

function confirmation(lg) {
	if ( lg == "fr" )
		var texte = "Cliquez sur OK pour envoyer votre inscription ou sur Annuler pour corriger.";
	if ( lg == "gb" )
		var texte = "Click OK to confirm your registration or Cancel to go back.";

	if ( confirm(texte) )
		return true;
		else
		return false;
	};

// ******************************************
// interroge un script et retourne la réponse
// ******************************************
function query(fichier,parametres) {
	xmlhttp = getXMLHTTP();
	if ( !xmlhttp )
		return false;
	xmlhttp.open("GET", fichier+'?'+parametres, false);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-scriptencoded");
	xmlhttp.send(null);
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
		return xmlhttp.responseText;
		else
		return "Erreur AJAX query : " + xmlhttp.status;
     };

// ****************************************************************
// retourne un objet xmlHttpRequest.
// méthode compatible entre tous les navigateurs (IE/Firefox/Opera)
// ****************************************************************
function getXMLHTTP() {
    var xmlhttp = null;
    if (window.XMLHttpRequest)
    { // Firefox et autres
        xmlhttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    { // Internet Explorer
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e1)
            {
                xmlhttp = null;
            }
        }
    }
    else
    { // non supporté par le navigateur
        alert("Votre navigateur ne supporte pas Javascript...");
    }
    return xmlhttp;
};
