function lancerRecherche()
{
    var _msg = "";

    if(document.recherche.IN_prix_Min.value != "-1" && document.recherche.IN_prix_Max.value != "-1")
    {
      var _prixMinId = document.recherche.IN_prix_Min.value.split("_");
      var _prixMaxId = document.recherche.IN_prix_Max.value.split("_");

      //Seules 2 tranches de prix consécutives peuvent être choisies
      if(parseInt(_prixMinId[1]) > parseInt(_prixMaxId[1]))
      {
         _msg = "Le prix mini doit être inférieur au prix maxi.";
      }
    }

    // Si une ville a été choisie
    if(document.recherche.IN_LOC_libelle.value!="")
    {
          transaction = logement = prixMin = prixMax = ville = rayon = 'null';
          //var regAccent = new RegExp("[ëéèêàçùûüïî]", "gi");
          var reg = new RegExp("[^a-z0-9ëéèêàâäçùûüïîôö]", "gi");
          
          transactionForm = document.recherche.elements['IN_transaction'].value;
          if(transactionForm=="V")
          {
             transaction = "vente";
          }
          else if(transactionForm=="L")
          {
             transaction = "location";
          }
          
          if(document.recherche.IN_TYP_id.value!='')
          {
             typForm = document.recherche.elements["IN_TYP_id"+transactionForm];
             
             for(i=0;i<typForm.length;i++)
             {
                if(typForm[i].selected)
                {
                   logement = typForm[i].innerHTML.replace(reg, "_").toLowerCase();
                }
             }
             
             
             if(document.recherche.IN_CLA_id.value!='')
             {
                claForm = document.recherche.elements["IN_CLA_id"+typForm.value];
                
                for(i=0;i<claForm.length;i++)
                {
                   if(claForm[i].selected)
                   {
                      logement += "_"+claForm[i].innerHTML.replace(reg, "_").toLowerCase();
                   }
                }
             }
          }
          
          if(document.recherche.IN_prix_Min.value!='-1')
          {
             prixMin = document.recherche.IN_prix_Min.value.substring(0, document.recherche.IN_prix_Min.value.lastIndexOf("_"));
          }
          
          if(document.recherche.IN_prix_Max.value!='-1')
          {
             prixMax = document.recherche.IN_prix_Max.value.substring(0, document.recherche.IN_prix_Max.value.lastIndexOf("_"));
          }

          if(document.recherche.IN_LOC_libelle.value!='')
          {
             ville = document.recherche.IN_LOC_libelle.value.replace(reg, "_").toLowerCase();
          }
          
          if(document.recherche.IN_rayon.value!='')
          {
             rayon = document.recherche.IN_rayon.value+"km";
          }          
        
          _gaq.push(['_trackEvent', 'formulaire', 'transaction', transaction]);
          _gaq.push(['_trackEvent', 'formulaire', 'logement', logement]);
          _gaq.push(['_trackEvent', 'formulaire', 'prixMin', prixMin]);
          _gaq.push(['_trackEvent', 'formulaire', 'prixMax', prixMax]);
          _gaq.push(['_trackEvent', 'formulaire', 'ville', ville]);
          _gaq.push(['_trackEvent', 'formulaire', 'rayon', rayon]);
          
      }
      else
      {
          _msg += "\nVous n'avez choisi aucune ville de recherche";
      }

    if(_msg != "")
    {      
       alert(_msg) ;
    }
    else{
       document.recherche.submit() ;
    }
}
    
function choixTransaction(transaction)
{
    if(transaction != '')
    {
       transactionValeur = transaction ;
    }
    else
    {
       transactionValeur = 'V' ;
    }
    document.getElementById("IN_transaction").value = transactionValeur;
    
    // Liste des types de transaction (Vente, Location)                
    transaction = Array("V","L") ;

    for(i=0;i<transaction.length;i++)
    {
        document.getElementById("typeBien"+transaction[i]).style.display = "none" ;
        document.getElementById("prix"+transaction[i]).style.display = "none" ;
    }    

    document.getElementById("typeBien"+transactionValeur).style.display = "block" ;
    document.getElementById("prix"+transactionValeur).style.display = "block" ;
    
    // Mise à jour de la combo sous-type de bien
    choixTypeBien() ;

}
 
function choixTypeBien()
{     
    transactionValeur = document.recherche.IN_transaction.value;  
    champTypeBien = "IN_TYP_id" + transactionValeur ; 
    valeurTypeBien = document.recherche.elements[champTypeBien].value ;

    // Met à jour les champs cachés contenant le type de bien et le prix
    document.recherche.IN_TYP_id.value = valeurTypeBien ;
    document.recherche.IN_prix_Min.value = document.recherche.elements['IN_prix'+transactionValeur+'_Min'].value ;
    document.recherche.IN_prix_Max.value = document.recherche.elements['IN_prix'+transactionValeur+'_Max'].value ;
    
    // Effacement de toutes les combos de sous-type
    for(j=0;j<tabTypes.length;j++)
    {
        document.getElementById("ssType_"+tabTypes[j]).style.display = "none" ;
    }       
    
    // Affichage de la combo de sous type si besoin
    existeSsType = false ;
    for(i=0;i<tabTypes.length;i++)
    {
        if(tabTypes[i] == valeurTypeBien)
        {
             existeSsType = true ;
             document.getElementById("ssType_"+tabTypes[i]).style.display = "block" ;
             // Met à jour le champ caché contenant le sous-type de bien
             document.recherche.elements.IN_CLA_id.value = "" ;
             comboSsType = document.recherche.elements["IN_CLA_id"+tabTypes[i]] ; 
             for(j=0;j<comboSsType.options.length;j++){
               if(comboSsType.options[j].selected){
                 valeur = comboSsType.options[j].value ;
                 if(document.recherche.elements.IN_CLA_id.value!=""){
                  document.recherche.elements.IN_CLA_id.value = document.recherche.elements.IN_CLA_id.value + "," + valeur ;
                 }else{
                   document.recherche.elements.IN_CLA_id.value = valeur ;
                 }
               }
             }
          }
    }
    if(!existeSsType){document.recherche.elements.IN_CLA_id.value = "" ;}
}
 
function valeurBoutonRadio(boutonRadio)
{
    for(i=0;i<boutonRadio.length;i++)
    {            
       if(boutonRadio[i].checked)
       {
           retour = boutonRadio[i].value ;
       }
    }
    return retour ;
}

function initForm(url)
{
    if (url == '') url = document.location.href;

    initSelect("IN_TYP_idV",defautVente) ;
    document.recherche.saisieVille.value = defautVille;
   
    // Décompose la chaine de parametres
    url = url.split("?") ;
    
    // Si des paramêtres sont fournis
    if(url.length>1){
      
          parametres = url[1].split("&") ;

          // Pour chaque parametre
          for(i=0;i<parametres.length;i++){
              champ = parametres[i].split("=") ;
              //alert("Le champ "+champ[0]+" est initialisé à "+champ[1]) ;
              // Pour les champs text
              try{document.recherche.elements[champ[0]].value = champ[1] ;}catch (e){}
              // Pour les champs select
              try{initSelect(champ[0],champ[1]);}catch (e){}
              // Pour les champs checkbox
              try{document.recherche.elements[champ[0]].checked = champ[1] ;}catch (e){}
              // Pour les champs radio
                try{for(j=0;j<document.recherche.elements[champ[0]].length;j++){
                     //alert(document.recherche.elements[champ[0]][champ[1]].value+" =? "+champ[1]) ;
                      if(document.recherche.elements[champ[0]][j].value==champ[1]){
                             //alert("on coche la valeur "+j) ;
                              document.recherche.elements[champ[0]][j].checked = true ;
                               }
                     }
             }catch (e){}
          }
          initFormChampSpeciaux(parametres) ;
   }
}

function initFormChampSpeciaux(parametres)
{
   champTypeOK = false ;
   champPrixMinOK = false ;
   champPrixMaxOK = false ;
   TYP_id = "" ;
 
   // Pour chaque parametre
    for(i=0;i<parametres.length;i++)
    {        
       champ = parametres[i].split("=") ;

       if(!champTypeOK)
       {
         // Champs IN_TYP_id
         if(champ[0]=="IN_TYP_id")
         { 
            initSelect("IN_TYP_idL",champ[1]) ;
            initSelect("IN_TYP_idV",champ[1]) ;
            initSelect("IN_TYP_idA",champ[1]) ;
            initSelect("IN_TYP_idD",champ[1]) ;
            champTypeOK = true ;
            TYP_id = champ[1] ;
         }
       }
       
       if(!champPrixMinOK)
       {
             // Champs IN_prix_Min
             if(champ[0]=="IN_prix_Min")
             {
                initSelect("IN_prixL_Min",champ[1]) ;
                initSelect("IN_prixV_Min",champ[1]) ;
                initSelect("IN_prixA_Min",champ[1]) ;
                initSelect("IN_prixD_Min",champ[1]) ;
                champPrixMinOK = true ;                      
             }
       }
       
       if(!champPrixMaxOK)
       {
             // Champs IN_prix_Max
             if(champ[0]=="IN_prix_Max")
             {
                initSelect("IN_prixL_Max",champ[1]) ;
                initSelect("IN_prixV_Max",champ[1]) ;
                initSelect("IN_prixA_Max",champ[1]) ;
                initSelect("IN_prixD_Max",champ[1]) ;
                champPrixMaxOK = true ;                      
             }
       }
       
       // Champs IN_CLA_idxxx
       if(champ[0]=="IN_CLA_id")
       {
          initSelect("IN_CLA_id"+TYP_id,champ[1]) ;
       }
    }
 
   // Champ ville
   if(document.recherche.IN_LOC_id.value!="")
   {
      document.getElementById("zoneRechercheVille").style.display = "none" ;
      document.getElementById("villeModifier").style.display = "block" ;
   }
}

function initSelect(nomSelect,valeurSelect){
 try{
    valeurs = valeurSelect.split(",") ;
    for(m=0;m<valeurs.length;m++){
     for(j=0;j<document.recherche.elements[nomSelect].length;j++){
        if(document.recherche.elements[nomSelect].options[j].value==valeurs[m]){
          if(valeurs.length>1){ 
            document.recherche.elements[nomSelect].options[j].selected = true ;
          }else{
            document.recherche.elements[nomSelect].selectedIndex = j ;
          }
        }
      }
    }
  }catch(e){} ;
}

// Au chargement, remplissage des champs avec les paramètres fournis
//initForm(document.location.href) ;
initForm(URL_init) ;

// Mise à jour des combos de type et prix
//choixTransaction(document.getElementById("IN_transaction").value);
choixTypeBien();

