function oninit(){
  initNewsletter ();
  if($("div_moteurRecherche")) initMoteur();
}

function viderChampNews(obj){
  obj.value="";
}

function enterNews(event){
  if(!event) var event = window.event;
  if(event && event.keyCode==13) checkForm($("frm_news"),valideInscNews);
}

function valideInscNews(){
    envoiMailByAjaxByForm('frm_news','div_formNews',false);
}

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','footer-form',false);
}

/*Moteur*/
function afficheEfface(nomDiv){
  if($(nomDiv).showing==true){
    Effect.BlindUp(nomDiv,{"duration":0.6});       
    $(nomDiv).showing=false;
  }
  else{
    Effect.BlindDown(nomDiv,{"duration":0.6});       
    $(nomDiv).showing=true;
  }
}


function initMoteur(){
  actualiseVilleProxCommune();
  setCalendrierGeneral("","1","","","div_calendrierMoteurContent",clicCalendJs,"inpt_deb","","","lst_nbj","1","");
  $("img_pictoCalend").onclick=function(evt){afficheEfface("div_calendrierMoteur");};
  $("img_fermeCalend").onclick=function(evt){afficheEfface("div_calendrierMoteur");};
  if($("select_villeProx").value!="") actualiseCommune();
  $("div_rechercher").onclick=function(evt){rechercheAjax();}; 
  $("sp_plusCriteres").onclick=function(evt){
    afficheEfface("div_plusCriteres"); 
    if($("div_plusCriteres").showing==true) $("sp_plusCriteres").innerHTML = "- de critères";
    else $("sp_plusCriteres").innerHTML = "+ de critères";
  };
  $$(".lstDuree").onclick=function(evt){lanceRechercheDate();};
  $("div_validDate").onclick=function(evt){lanceRechercheDate();};
  $("input_lno").onkeypress=function(evt){enterReference(evt);};   
  $("input_lno").onfocus=function(evt){this.value='';};
  $("bt_rechercheExpress").onclick=function(evt){lanceRechercheReference();};
}

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 actualiseVilleProxCommune(){
  appelAjaxToFunc("/config_v3/BOURGOGNE/G.v3/CHAMBREv4.FRANCAIS/librairie/bloc_html/villeProxEtCommunes.php","DEPARTEMENT="+$("lst_dpt").value,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("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;
    
    var indiceProx = 0;
    var selectedProx = 0;
    $("lst_chefLieu").options.length=0;
    $("lst_chefLieu").options[$("lst_chefLieu").options.length] = new Option("Ville de proximité");
    $("lst_chefLieu").options[$("lst_chefLieu").options.length-1].value="";
    $A(tab.villeprox).each(function(obj){
      $("lst_chefLieu").options[$("lst_chefLieu").options.length] = new Option(obj);
      $("lst_chefLieu").options[$("lst_chefLieu").options.length-1].value=obj;
      indiceProx++;
      if(obj==$("select_villeProx").value) selectedProx=indiceProx;
    });
    $("lst_chefLieu").selectedIndex = selectedProx;
  });
}

function actualiseCommune(){
  appelAjaxToFunc("/config_v3/BOURGOGNE/G.v3/CHAMBREv4.FRANCAIS/librairie/bloc_html/villeProxEtCommunes.php","DEPARTEMENT="+$("lst_dpt").value+"&LIBCHEFLIEU="+$("select_villeProx").value,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("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;
  });
}

function defVilleProx(){
  $("select_villeProx").value = $("lst_chefLieu").value;
}
function defCommune(){
  $("select_commune").value = $("lst_commune").value;
}

function rechercheAjax(){
  setListByForm($("frm_moteur"));
}

function clicCalendJs(evt,jour,mois,annee,etat){
  $("inpt_deb").value=jour+"/"+mois+"/"+annee;
  afficheEfface("div_calendrierMoteur");
}

function reinitCalend(){
  if($("lst_dpt").value && $("lst_dpt").value!="21,58,71,89") instance = "gites"+$("lst_dpt").value;
  setCalendrierGeneral("","1","div_calendrierMoteurContent",clicCalendJs,"inpt_deb","","","lst_nbj","1","");
}

function definitionDuree(obj){
  $("lst_nbj").value=obj.getAttribute("valeur");
}

function lanceRechercheDate(){
  if(($("inpt_deb").value!="" && $$(".lstDuree").value=="")||($("inpt_deb").value=="" && $$(".lstDuree").value!="")||($("inpt_deb").value=="" && $$(".lstDuree").value=="")){
    alertAMalibu("Merci de saisir à la fois, la date de votre arrivée, ainsi que la durée de votre séjour");
  }
  else{
    rechercheAjax();
  }
}
