function getHTTPObject() {
	// Native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		try { var connexion = new XMLHttpRequest(); } catch(e) { var connexion = false; }
	// IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		try { var connexion = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch(e) { 
			try { var connexion = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { var connexion = false; }
		}
	}

	if (connexion) {
		connexion.onreadystatechange = function() {
		if ( connexion.readyState == 4 && connexion.status == 200) {
			// récupère les villes renvoyées
			var resultat = connexion.responseText;
			var tabVilles = resultat.split('@@');			
			// objets de la page
			comboListe = document.getElementById("liste");
			champVille = document.getElementById("ville");
			comboListe.options.length = 0;
			// aucune ville : saisie manuelle dans champ suivant
			if ( tabVilles.length == 1 ) {
				champVille.focus();
				comboListe.style.visibility = 'hidden';
				}
			// 1 seule ville : choix automatique
			if ( tabVilles.length == 2 ) {
				champVille.value = unescape(tabVilles[0]);
				comboListe.style.visibility = 'hidden';
				}
			// plusieurs villes : liste de sélection
			if ( tabVilles.length > 2 ) {
				comboListe.style.visibility = 'visible';
				for (var i=0; i < tabVilles.length-1; i++) {
					comboListe.options[comboListe.options.length] = new Option(unescape(tabVilles[i]),unescape(tabVilles[i]));
					}
				};
			}
		}
	}
	return connexion;
};

function verifCP() {
	var comboListe = document.getElementById("liste");
	var champVille = document.getElementById("ville");
	var champCodep = document.getElementById("cp");
	if ( !isNaN(champCodep.value) ) { 		
		champVille.value = "";
		// masque et supprime liste si cp incorrect
		if ( champCodep.value.length < 5 ) {
			comboListe.options.length = 0;
			comboListe.style.visibility = "hidden";
			};
		// envoie des données
		if ( champCodep.value.length == 5 ) {
			window.status = "Recherche de la ville en cours...";
			return !sendData("villes.php","cp=" + champCodep.value);			
			};
		};
	};
 
 function sendData(url, data) {
	var connexion = getHTTPObject();
	if ( !connexion ) {
	        alert("Objet non créé");
	        window.status = "";
	        return false;	        
		}
		else
		{
		connexion.open("POST", url, true); //ouverture asynchrone
		connexion.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		connexion.send(data);
		window.status = "";
		};	
	return true;
	};

