$(document).ready(function(){
	$("#tel").change(function(){
		var tel = ($("#tel").val());
		toreplace=" ";
		replaceby="";
		while(tel.indexOf(toreplace)>-1){
			tel=tel.replace(toreplace,replaceby);
		}
		toreplace=".";
		replaceby="";
		while(tel.indexOf(toreplace)>-1){
			tel=tel.replace(toreplace,replaceby);
		}
		toreplace="/";
		replaceby="";
		while(tel.indexOf(toreplace)>-1){
			tel=tel.replace(toreplace,replaceby);
		}
		$("#tel").val(tel);
	});
        $("#tel2").change(function(){
                var tel = ($("#tel2").val());
                toreplace=" ";
                replaceby="";
                while(tel.indexOf(toreplace)>-1){
                        tel=tel.replace(toreplace,replaceby);
                }
                toreplace=".";
                replaceby="";
                while(tel.indexOf(toreplace)>-1){
                        tel=tel.replace(toreplace,replaceby);
                }
                toreplace="/";
                replaceby="";
                while(tel.indexOf(toreplace)>-1){
                        tel=tel.replace(toreplace,replaceby);
                }
                $("#tel2").val(tel);
        });
});

//Cree un objet xhr pour l'ajax
function getXhr(){
	if(window.XMLHttpRequest){ //Firefox et autres
		xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){ //Internet Explorer
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else{ // XMLHttpRequest non supporte par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		xhr = false; 
	} 	
}

function initialize(){
	var select_ville = document.getElementById("ville");
	longueur = select_ville.length;
	for(j=0;j<=longueur;j++){
		select_ville.remove(0);
	}
}

function recup_insee(){
	$("#envoi_addr").attr("disabled","true");
	ville = $("#ville").val();
	cp = $("#cp").val();
	//ville = document.getElementById("ville").value;
	//cp = document.getElementById("cp").value;
	getXhr();
	if((xhr != null) && (xhr != false)){
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				rep = xhr.responseText;
				document.getElementById("insee").value = rep;
				$("#envoi_addr").attr("disabled","");
			}
		}
				
		xhr.open("POST","./file_php/recup_insee.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
		xhr.send("codepostal="+cp+"&ville="+ville);
	}
}

//On crée un menu déroulant à partir du code postal
function cpToVille(){
	cp = $("#cp").val();
	//cp = document.getElementById("cp").value;
	getXhr();
	if((xhr != null) && (xhr != false)){
		xhr.onreadystatechange = function(){
			if(xhr.readyState == 4 && xhr.status == 200){
				var select_ville = document.getElementById("ville");
				initialize();
				rep = xhr.responseText;
				if(rep != -1) {
					var tab_array = rep.split("@");
					var nb_communes = parseInt(tab_array[0]);
					if(nb_communes > 1) {
						//select_ville.remove(0);
						for(i=1;i<=nb_communes;i++) {
							select_ville.options[i-1] = new Option(tab_array[i], tab_array[i]);
						}
					} else {
						//select_ville.remove(0);
						select_ville.options[0] = new Option(tab_array[1], tab_array[1]);
					}
				} else {
					select_ville.options[0] = new Option("Code postal ne correspondant à aucune ville", 0);
				}
				recup_insee();
			}
		}
			
		xhr.open("POST","./file_php/cp_to_ville.php",true);
		xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
		xhr.send("codepostal="+cp);
	}

}

function changeBarre(element, compteur){
	//On teste si Ã§a fait une minute en multipliant compteur x intervalle de temps
	var intervalle=150;
	var xtaille = compteur +1;
	var i; var j;
	var element2 = "";
	if(compteur!=0){
		element=element.split(",");
	}
	for(i=0;i<element.length;i++){
		if(element[i]!=""){
			//alert(element[i]);
			res = element[i].split("+");
			var img = document.getElementById(res[0]);
			if(img != null){
				var taille = res[1] * (xtaille);
				img.style.width = taille+"%";
				if(taille<100){
					element2 = element2+","+res[0]+"+"+res[1];
				}					
			}
		}
	}
	compteur++;
	//On rÃ©percute l'intervalle temps choisi dans le fichier php
	setTimeout("changeBarre('"+element2+"',"+compteur+")",intervalle)
}

function comparOffre(){
	document.getElementById("div_legende").style.display = "";
	getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			rep = xhr.responseText;
			tab= rep.split("@");
			changeBarre(tab,0);
		}
	}
	
	xhr.open("GET","/file_php/comparatif_offre.php",true);	
	xhr.send(null);
}
