function oninit(){
  if($("form_moteurRecherche")) initMoteurGeneral();
  if($("form_moteurRechercheh")) initMoteurBandeau();
  if($("lst_commune")) actualiseCommune();
}

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

/* moteur de recherche */

function clicCalendJs(evt,jour,mois,annee,etat){
  $("inpt_dateDeb").value=jour+"/"+mois+"/"+annee;
  afficheEfface("div_calendrierMoteur");
}

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');
    	}
    }); 
  }
}

function afficheEffaceCalend(){
  if($("div_calendrierMoteur").style.display!="none") $("div_calendrierMoteur").style.display="none"; 
  else{
    $("div_calendrierMoteur").style.display="block"
		var legende = "Attention en selectionnant une date vous accédez uniquement à l'offre réservable en ligne!!";
    if(($("inpt_g")) && ($("inpt_g").checked==true)) setCalendrierGeneral("gites47","1","","G","div_calendrierMoteurContent",clicCalendJs,"inpt_dateDeb","","","lst_nbj","1","",true,legende); 
    else setCalendrierGeneral("gites47","1","","H","div_calendrierMoteurContent",clicCalendJs,"inpt_dateDeb","","","lst_nbj","1","",true,legende);
    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 gauche;
function initMoteurGeneral(){ 
  $("div_btRechercher").onclick=function(evt){lanceRecherche();};
  $("input_lno").onkeypress=function(evt){enterReference(evt);};   
  $("input_lno").onfocus=function(evt){this.value='';};
  $("a_valideNumero").onclick=function(evt){lanceRechercheReference();};        
  $("img_calendMoteur").onclick=function(evt){afficheEffaceCalend();};
  $("img_fermeCalendGauche").onclick=function(evt){afficheEffaceCalend();};
  $("inpt_dateDeb").onfocus=function(evt){afficheEfface("div_calendrierMoteur");};
  $("sp_valideDate").onclick=function(evt){ // validation de la saisie de la date
    if(($("inpt_dateDeb").value && !$("lst_nbj").value) || (!$("inpt_dateDeb").value && $("lst_nbj").value)) alertAMalibu("Vous devez définir la date et la durée de votre séjour");  
    else lanceRecherche();
  } 
}
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();
}

/*---------------Moteur bandeau------------------*/
function clicCalendJsBandeau(evt,jour,mois,annee,etat){
  $("inpt_dateDebh").value=jour+"/"+mois+"/"+annee;
  afficheEfface("div_calendrierMoteurh");
}

function afficheEffaceCalendBandeau(){
  if($("div_calendrierMoteurh").style.display!="none") $("div_calendrierMoteurh").style.display="none"; 
  else{
    $("div_calendrierMoteurh").style.display="block"
		var legende = "Attention en selectionnant une date vous accédez uniquement à l'offre réservable en ligne!!";
    if(($("inpt_gh")) && ($("inpt_gh").checked==true)) setCalendrierGeneral("gites47","1","","G","div_calendrierMoteurContenth",clicCalendJsBandeau,"inpt_dateDebh","","","lst_nbjh","1","",true,legende); 
    else setCalendrierGeneral("gites47","1","","H","div_calendrierMoteurContenth",clicCalendJsBandeau,"inpt_dateDebh","","","lst_nbjh","1","",true,legende);
    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');
    	}
    }); 
  }    
}  

function initMoteurBandeau(){    
  $("img_calendMoteurh").onclick=function(evt){afficheEffaceCalendBandeau();};
  $("img_fermeCalend").onclick=function(evt){afficheEffaceCalendBandeau();};
  $("inpt_dateDebh").onfocus=function(evt){afficheEfface("div_calendrierMoteurh");};
}



/*-------------------------actualise  la commune-------------------------------*/    
function actualiseCommune(){
  var acc = "";
  $$(".chk_acc").each(function(obj){
    if(obj.checked==true){
      if(acc=="") acc = obj.value;
      else acc += ","+obj.value;
    } 
  });
  appelAjaxToFunc("/config_v3/47/G.v3/STANDARD2.FRANCAIS/librairie/bloc_html/communes.php","REG="+$("lst_commune").value+"&ACC="+acc,function(reponseAjx){  
    var tab;
    eval("tab="+reponseAjx);
    var indice = 0;
    var selected = 0;    
    $("lst_commune").options.length=0;
    $("lst_commune").options[$("lst_commune").options.length] = new Option("Sélectionnez une commune");
    $("lst_commune").options[$("lst_commune").options.length-1].value="";
    
    $A(tab.communes).each(function(obj){
      $("lst_commune").options[$("lst_commune").options.length] = new Option(obj);
      $("lst_commune").options[$("lst_commune").options.length-1].value=obj;
      indice++;
      if(obj==$("select_commune").value) selected=indice;
    });
    $("lst_commune").selectedIndex = selected;             
  });                                        
}     
