// Gestion dynamique en mode AJAX des communes selon un code postal
// para aleatoire pour forcer l'appel
var cur_appel=0;
function GetRandParam(){
	var id_appel=Math.round(Math.random(0)*2000000)+1;	
	if (cur_appel==id_appel){Math.round(Math.random(0)*2000000)+1;}else{cur_appel=id_appel;}
	return id_appel
}
// affichage infos dynamique
function GetCommunes(CP,V){					
	var pg_dst="../../commun/prg/communes.aspx?cp="+CP+"&v="+V+"&na="+GetRandParam();
	var xhr_object = null; 	
	if(window.XMLHttpRequest) // Firefox 
	   	xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	  	xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur renvoie false pour ouvrir une fenetre	   	
	   	alert("Non supporte"); 
	} 	
 	try	
		{						
			var obj=document.getElementById("BT_COMMUNES");						
			xhr_object.open("GET", pg_dst , true);  
			xhr_object.onreadystatechange = function() { 
		  		if(xhr_object.readyState == 4) {	
		  			if(xhr_object.status==200){
		  				obj.innerHTML=xhr_object.responseText;		  				
		  				obj.style.visibility='visible';
		  				
		  				
		  				// si div google_map existe source page = devis sur espace client
		  				if (document.getElementById("google_map")){
		  					try{GC_GetDSRegion(CP,V);}
		  					catch (e){}
		  				}		  				
		  			}
		  			else{
		  				// affichage si erreur
		  				alert("Service non disponible.");		  				
		  			}		  			 
		  		} 
			} 
			xhr_object.send(null);			
		}
	catch (e){alert("Erreur");}	
}

// défini le block commune lors d'un chragement initial ou d'un rechargement
function SetCommune() 
{	
	var CP=document.forms[0].code_postal.value;
	if (CP!=""){
		try{var V=document.forms[0].commune.value;GetCommunes(CP,V);}
		catch (e){GetCommunes(CP,'');}				
	}
}

// Change code postal
function ChangeCP(CP) 
{
	if (verif_code_postal(CP)){GetCommunes(CCP,'');}
	else {alert("Code postal invalide");document.forms[0].code_postal.focus();}	
}

/* Changement de CP sur on blur si changement OCP valeur origine, CCP valeur en cours*/
function BlurChangeCP(CCP,OCP) 
{	
	if (CCP!=""){GetCommunes(CCP,'');}
}
/* saisie sur code postal */
function valid_code_postal(obj)
{
	if (obj.value.length == 5){document.forms[0].commune.focus();}
}
/* verif code postal */
function verif_code_postal(CP)
{
	try{
		if((CP<1000)||(CP>95000)){return false;}else{return true;}
		}
	catch (e){return false;}	
}

