$(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");
    var ville = $("#ville").val();
    var cp = $("#cp").val();
    if(ville == null){
        ville = $("#selectText").val();
    //alert(ville);
    }
    
    //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(){
    var 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();
                var 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]);
                        }
                        showPullDown();
                        document.getElementById("div_commune").style.display='block';
                    }else if(nb_communes == 1){
                        document.getElementById("selectText").value =tab_array[1];
                        document.getElementById("div_commune").style.display='block';
                        document.getElementById("div_eligibilite_adr").setAttribute("style","height:240px;_height:270px");
                    }
                    else {
                        //select_ville.remove(0);
                        select_ville.options[0] = new Option(tab_array[1], tab_array[1]);
                    }
                } else {
                    select_ville.options[0] = new Option("Aucune ville", "null");
                }
                recup_insee();
            }
        }
        if(cp !=""){		
            xhr.open("POST","./file_php/cp_to_ville.php",true);
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
            xhr.send("codepostal="+cp);
        }else{
            document.getElementById("selectText").value="";
            document.getElementById("selectDiv").style.display='none';
            document.getElementById("div_eligibilite_adr").setAttribute("style","height:240px;_height:270px");
        }
    }

}

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);
}

var showSelect= false;
document.onclick = hideItems;
function hideItems(){
    if(showSelect!=true){
        var select = document.getElementById("ville");
        var colls = select.options;
        var leng = colls.length;
        //alert(leng);
        if(document.getElementById("selectDiv")!=null){
            if(leng<=0){
                document.getElementById("selectDiv").style.display='none';
                document.getElementById("div_eligibilite_adr").setAttribute("style","height:200px;_height:230px");
            }
            if(document.getElementById("ville")!=null && document.getElementById("ville").value !="null"&& document.getElementById("ville").value !=""){
                document.getElementById("selectText").value = document.getElementById("ville").value;
                document.getElementById("ville").disabled=false;
                recup_insee();
            }
        }
    }
    showSelect = false;
}
function showPullDown(){
    var select = document.getElementById("ville");
    var colls = select.options;
    var leng = colls.length;
    if(document.getElementById("selectDiv")!=null){
        var selectObj = document.getElementById("ville");
        if(selectObj!=null && selectObj.size==1){
            selectObj.size=2;
            selectObj.style.height=getPullDownHeight(selectObj.length);
            selectObj.style.width=172;
        }
        if(leng>0 && select.options[0].value !="null"){
            document.getElementById("selectDiv").style.display='block';
            showSelect = true;
            document.getElementById("div_eligibilite_adr").setAttribute("style","height:340px;_height:370px");
//            document.getElementById("div_eligibilite_adr").style = function(){                   
//                document.getElementById("div_eligibilite_adr").style.height = "330px";
//            }
           
        }
    }
}
function getPullDownHeight(length){
    switch(length){
        case 0:
            return 0;
        case 1:
            return 30;
        case 2:
            return 50;
        case 3:
            return 60;
        case 4:
            return 80;
        default:
            return 100;
   
    }
  
}


