function oninit(){                                 
  if($("div_moteurRecherche")) initMoteurAffine();
  if ($("inpt_envoyerMessage")) $("inpt_envoyerMessage").onclick=function (evt){checkForm($('frm_contactez_nousContactezNous'),envoiFormContactezNous);}
  defActionOngletPorteur();     
}

function afficheEfface(nomDiv){
  if($(nomDiv).showing==true){
    Effect.BlindUp(nomDiv,{"duration":0.4});       
    $(nomDiv).showing=false;
  }
  else{
    Effect.BlindDown(nomDiv,{"duration":0.4});       
    $(nomDiv).showing=true;
  }
}

function afficheEffaceListe(nomDiv){
  if($(nomDiv).style.display=="block"){   
    $(nomDiv).style.display="none";
  }
  else{
    $(nomDiv).style.display="block";
  }
}

/*----------envoie du formulaire de contact---------*/
function envoiFormContactezNous(){
  envoiMailByAjaxByForm("frm_contactez_nous","mesg_replace",true);
} 

function is_numeric(num){
	var exp = new RegExp("^[0-9-.]*$","g");
	return exp.test(num);
}

function initMoteurAffine(){
  $$(".sp_selectMoteurAcc").each(function(obj){
    obj.onclick=function(evt){
      var listeLie = obj.getAttribute("liste"); 
      afficheEffaceListe(listeLie);
    };
  });
  $$(".li_selectMoteur").each(function(obj){
    obj.onclick=function(evt){
      clicElemeListeMoteurGeneral(obj);
    };
    if(obj.getAttribute("selected")=="selected") initListeDeroulanteDefinie(obj);
  });
  $("inpt_deb").onclick=function(evt){
    afficheEffaceListe("div_calendrierMoteurAcc");
  };
  $("a_btPlusCrit").onclick=function(evt){
    afficheEfface("div_plusCritContent");
    if($("div_plusCritContent").style.display=="none") $("a_btPlusCrit").innerHTML = "Moins de critères";
    else  $("a_btPlusCrit").innerHTML = "Plus de critères";
  }
  $("img_lanceRechExpress").onclick=function(evt){
    setListByForm($("frm_rechExpress"));    
  };
  $("inpt_rechExpress").onkeypress = function(evt){enterRechExpress(evt);};
  $("inpt_rechExpress").onfocus = function(evt){$("inpt_rechExpress").value="";};
  actualiseCalendrier();
  
  if($("inpt_typeG").checked==true)
      setChmpPrixToReglette();
  else $("div_prix").style.display='none';
  
  if($("inpt_typeH").checked==true){          
    $$(".dureeG").each(function(obj){obj.style.display="none";});  
    $$(".dureeH").each(function(obj){obj.style.display="block";});
  } else {
    $$(".dureeH").each(function(obj){obj.style.display="none";});
    $$(".dureeG").each(function(obj){obj.style.display="block";});
  }
  
  $("div_fermeCalend").onclick=function(evt){afficheEffaceListe("div_calendrierMoteur");};
  
  actualiseCommune();  
  
  if(!$("div_accueil")){
    $$(".check_moteur").each(function(obj){
      obj.onclick=function(evt){
        setListByForm($("frm_moteurAffine"));
      };
    });
  }
  else{
    $$(".check_moteur").each(function(obj){
      obj.onclick=function(evt){
        initMoteurAffine();
      };
    });
  }
}

function clicElemeListeMoteurGeneral(elem){  
  var parents = elem.ancestors();
  var ul = parents[0];
  var inpt = $(ul).getAttribute("input");
  var valeur = $(elem).getAttribute("valeur");
  var span = $(ul).getAttribute("span");
  
  afficheEffaceListe(ul);
  $(inpt).value=valeur;
  $(span).innerHTML=$(elem).innerHTML;
  if(!$("div_accueil")) setListByForm($("frm_moteurAffine"));   
}

function initListeDeroulanteDefinie(elem){  
  var parents = elem.ancestors();
  var ul = parents[0];
  var inpt = $(ul).getAttribute("input");
  var valeur = $(elem).getAttribute("valeur");
  var span = $(ul).getAttribute("span");
  
  $(inpt).value=valeur;
  $(span).innerHTML=$(elem).innerHTML; 
}

function actualiseCalendrier(){
  if($("inpt_typeH").checked) setCalendrierGeneral("gites37","1","","H","div_calendrierMoteurContent",clicCalendJs,"inpt_deb","","","inpt_duree","1","");
  else setCalendrierGeneral("gites37","1","","G","div_calendrierMoteurContent",clicCalendJs,"inpt_deb","","","inpt_duree","1","");
}

function clicCalendJs(evt,jour,mois,annee,etat){        
  $("inpt_deb").value=jour+mois+annee;
  $("sp_selectMoteurAccDate").innerHTML=jour+"/"+mois+"/"+annee; 
  afficheEffaceListe("div_calendrierMoteur");
  if( ($("inpt_duree").value!="") && (!$("div_accueil")) ) setListByForm($("frm_moteurAffine")); 
}

function enterRechExpress(event){
  if(!event) var event = window.event;
  if(event && event.keyCode==13) setListByForm($("frm_rechExpress"));
}
             
/*-----------prix réglette-----------*/
function setChmpPrixToReglette(){
  if($("inpt_typeG").checked==true){
    $("div_prix").style.display='block';
    /**** reglette de prix *****/
    var reglette = $('slider_prix');
    if (reglette){
      var vals=new Array();   
      
      for (var i=50;i<=2900;i+=50)vals.push(i);
      new Control.Slider("slider_prixMaxi", reglette, {
        range: $R(50, 2900),
        sliderValue: 2900,
        values:vals,
        onSlide: function(value) {
          $("infoPrix_prixMaxi").innerHTML=value+'&euro';          
        },
        onChange: function(value) { 
          $("inpt_pxmaxi").value=value;
          if(!$("div_accueil")) setListByForm($('frm_moteurAffine'));         
        }
      });
    }
  }  
}

/*-------------------------actualise  la commune-------------------------------*/    
function actualiseCommune(){
  var acc = "G,T,H,C";
  if($("inpt_typeG").checked==true) acc="G";
  if($("inpt_typeT").checked==true) acc="T";
  if($("inpt_typeH").checked==true) acc="H";
  if($("inpt_typeC").checked==true) acc="C";
  
  appelAjaxToFunc("/config_v3/37/G.v3/GDFv4.FRANCAIS/librairie/bloc_html/communes.php","REGION="+$("inpt_region").value+"&ACC="+acc+"&COMSELECT="+$("inpt_commune").value,function(reponseAjx){
    /*var html = "";  
    var tab = "";
    eval("tab="+reponseAjx);
    var indice = 0;
    var selected = "";    
    html='<li class="li_selectMoteur" valeur="">Indiff&eacute;rent</li>';
    
    $A(tab.communes).each(function(obj){ 
      if(obj==$("inpt_commune").value){
        selected='selected';          
        $('sp_selectMoteurAccCommune').innerHTML=obj;
      } 
      $("lst_commune").innerHTML=html+'<li class="li_selectMoteur" valeur="'+obj+'" selected="'+selected+'">'+obj+'</li>'; 
      indice++;   
      selected = "";  
      html = $("lst_commune").innerHTML;
    });   
    $$(".li_selectMoteur").each(function(obj){
      obj.onclick=function(evt){
        clicElemeListeMoteurGeneral(obj);
      };
      if(obj.getAttribute("selected")=="selected") initListeDeroulanteDefinie(obj);
    });*/
    $("lst_commune").innerHTML = reponseAjx;
    $$(".li_selectMoteur").each(function(obj){
      obj.onclick=function(evt){
        clicElemeListeMoteurGeneral(obj);
      };
      if(obj.getAttribute("selected")=="selected") initListeDeroulanteDefinie(obj);
    });
  });                                 
}  

function defActionOngletPorteur(){
  $$(".a_ongletPorteurOff").each(function(obj){
    obj.onclick=function(){if(obj.next("div").style.display=="none") obj.next("div").style.display="block";
      else obj.next("div").style.display="none";
      if(obj.hasClassName("a_ongletPorteurOn")) obj.removeClassName("a_ongletPorteurOn"); 
      else obj.addClassName("a_ongletPorteurOn");
      }
    }
  );
}
