function oninit(){
    defClicOngletsBas(); 
    if($("form_newsletter")) initNewsletter();
    if($("moteurRechercheGauche")) initMoteurGeneral();
}
function defClicOngletsBas(){
  $$(".li_ongletsBas").each(function(obj){
    obj.onclick = function(evt){
      $$(".div_ongletContentBas").each(function(obj2){
        obj2.style.display="none";        
      });
      $$(".li_ongletsBas").each(function(obj3){
        if(obj3.hasClassName("select")) obj3.removeClassName("select");        
      });
      var div = obj.getAttribute("div");
      $(div).style.display="block";
      obj.addClassName("select");      
    };    
  });
}

function viderChampNews(obj){
  obj.value="";
}

function enterNews(event){
  if(!event) var event = window.event;
  if(event && event.keyCode==13) checkForm($("form_newsletter"),valideInscNews);
}

function initNewsletter(){
  $("champ_email_newsletter").value = "Entrez votre email";
  $("form_newsletter").onsubmit = function(evt){return false;};
  $("champ_email_ok").observe("click",function(evt){checkForm($('form_newsletter'),valideInscNews);});
  $("champ_email_newsletter").onfocus = function(evt){viderChp($("champ_email_newsletter"));viderChampNews($("champ_email_newsletter"));}
  $("champ_email_newsletter").onblur = function(evt){checkByInput(this,'checkMailObligatoire');};
  $("champ_email_newsletter").onkeypress = function(evt){enterNews(evt);};
}

function valideInscNews(){
    envoiMailByAjaxByForm('form_newsletter','newsletter_header',false);
}

/* moteur de recherche */

function clicCalendJs(evt,jour,mois,annee,etat){
  $("input_dates").value=jour+"/"+mois+"/"+annee;
  afficheEfface("div_calendrierContent");
  $("inpt_pasDate").checked=false;
}

function lanceRecherche(){
  setListByForm($("frm_moteurRecherche"));
}

function afficheEfface(div){
  if($(div).style.display!="none") $(div).style.display="none"; 
  else{
    $(div).style.display="block"
    Event.observe(document, 'keypress', function(event){
    if(event.keyCode == Event.KEY_ESC){
    		$$(".div_calendrier").each(function(obj){
          obj.style.display="none";
        });
    		Event.stopObserving(document,'keypress');
    	}
    }); 
  }
}

// initialisation du moteur pour toutes les pages;
function initMoteurGeneral(){ 
  $("img_calend").onclick=function(evt){setCalendrierGeneral("gites42","1","","H","div_calendrierContent",clicCalendJs,"input_dates","","","lst_duree","1","");afficheEfface("div_calendrierContent");};
  $("input_dates").onfocus=function(evt){afficheEfface("div_calendrierContent");};
  $("a_valideDate").onclick=function(evt){ // validation de la saisie de la date
    if(($("input_dates").value && !$("input_dates").value) || (!$("input_dates").value && $("input_dates").value)) alertAMalibu("Vous devez définir la date et la durée de votre séjour");  
    else lanceRecherche();
  } 
  $("a_btRecherche").onclick=function(evt){lanceRecherche();};
  $("a_btRechercheAvacnee").onclick=function(evt){lanceRecherche();}; 
  $("input_lno").onkeypress=function(evt){enterReference(evt);};   
  $("input_lno").onfocus=function(evt){this.value='';};
  $("bt_rechercheExpress").onclick=function(evt){lanceRechercheReference();};
  appelAjaxToFunc("/lib/ajax/infosCGI.php","typeInfo=LISTALLCOMMUNE",setChmpCommuneAutoCompleter);
  //$("img_supprCommune").onclick=function(evt){$("inpt_ville").value="";$("inpt_ville_libcommune").value="";lanceRecherche();$("img_supprCommune").style.display="none";}
}

function lanceRechercheReference(){
  if($("input_lno").value && $("input_lno").value!="Référence") document.location.href="/listeheb?LNO="+$("input_lno").value;
  else alertAMalibu("Vous devez saisir une référence pour lancer la recherche");
}

function enterReference(event){
  if(!event) var event = window.event;
  if(event && event.keyCode==13) lanceRechercheReference();
}

function setCGIForLstCommune(li){
    if ( navigator.appName == 'Microsoft Internet Explorer' ){
     var commune = li.getElementsByTagName("span")[0].getAttribute("commune_reelle");
    }
    else{
      var commune = li.getElementsByTagName("span")[0].getAttribute("commune_reelle");
    }
    $("inpt_ville").value=commune;
    $("inpt_ville").onfocus=function (evt){this.value='';$("inpt_ville_libcommune").value='';}
    $("inpt_ville").onblur=function (evt){$("inpt_ville_libcommune").value=this.value;}
    
    $("inpt_ville_libcommune").value=commune;
    $("img_supprCommune").style.display="inline";
}

function setChmpCommuneAutoCompleter(reponse){
    var suffixe;
    var jsonarray=eval(reponse);
    var infos=[];
    for (var i=0;i<jsonarray.length;i++){
      (jsonarray[i].nb>1)?suffixe="s":suffixe="";
      infos.push(jsonarray[i].commune+" ("+Math.round(jsonarray[i].nb)+" réponse"+suffixe+")<span commune_reelle=\""+jsonarray[i].communeReelle+"\" />");   
    }    
    /**** champ commune ****/
    var typeahead = new Autocompleter.Local('inpt_ville', 'divLstCommunes_inpt_ville', infos, {
        frequency: 0.5,
        updateElement: setCGIForLstCommune
    });
}        
