function lancerRecherche(){
    // Si une ville a été choisie
    if(document.recherche.IN_LOC_libelle.value!=""){
        
          document.recherche.submit() ;
          
    // Si aucune ville n'a été choisie
    }else{
          alert("Vous n'avez choisi aucune ville de recherche") ;              
    }
}
    
 function choixTransaction(){
     transactionValeur = valeurBoutonRadio(document.recherche.elements['IN_transaction']) ;
        
       // 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 = valeurBoutonRadio(document.recherche.elements['IN_transaction']) ;
       
     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.value = document.recherche.elements['IN_prix'+transactionValeur].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) ;
   
    // 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 ;
   champPrixOK = 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(!champPrixOK){
                // Champs IN_prix
             if(champ[0]=="IN_prix"){
                    initSelect("IN_prixL",champ[1]) ;
                   initSelect("IN_prixV",champ[1]) ;
                initSelect("IN_prixA",champ[1]) ;
                initSelect("IN_prixD",champ[1]) ;
                   champPrixOK = 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();
choixTypeBien() ;

