function kitmapClickIcon(sId) {
 
 $("#id_secteur option").each(function() { 
    //recupération de la valeur
    var tabValue = $(this).attr('value').split('#');

   //secteur trouvé
   if(tabValue[0] == sId){
         $(this).attr("selected",'selected');
   }
 });
 $("#search02").submit();  
}

var flashvars = {};
var params = {loop:false, menu:false};
var attributes = {};
//taille orig 460 * 500
swfobject.embedSWF('cma17.swf', 'carte', '282', '330', '8.0.0', 'expressInstall.swf', flashvars, params, attributes);

function setCarteXML(xml) {
  $("#id_secteur option").slice(1).remove();
  i = 0; //nombre de retour
  $(xml).find("secteur").each(function() {
    $('#id_secteur').append('<option value="'+$(this).attr("id")+'#'+$(this).attr("nom")+'">'+$(this).attr("libelle")+'</option>');
    i++;
  });

  if(i > 0){
      $("#id_secteur").attr("disabled",false); 
  }
   
  //IE
  if (window.ActiveXObject) {
    var sXML = xml.xml;
  } else {
    // code for Mozilla, Firefox, Opera, etc.
    var sXML = (new XMLSerializer()).serializeToString(xml);
  }
  var oCarte=document.getElementById("carte");
  if(oCarte) oCarte.setXml(sXML);
}


function updateList( id , elem )
{ 
 $("#id_"+id+" option").slice(1).remove();
 $(elem).children().each(function() { 
        $("#id_"+id).append("<option value='"+$(this).attr("value")+"'>" + $(this).text() + "</option>");
 });
}

$(document).ready(function() {

  $.ajax({
  type: "GET",
  url: "rechercheArtisan_ajax.html?action=secteur_activite",   
  dataType: ($.browser.msie) ? "text" : "xml",
  success: function(data){
     var xml;
     if (typeof data == "string") {
       xml = new ActiveXObject("Microsoft.XMLDOM");
       xml.async = false;
       xml.loadXML(data);
     } else {
       xml = data;
     }
   // Returned data available in object "xml"
   if ( $(xml).is("erreur") )
   {
    alert( "Erreur : " + $(xml).find("erreur").text() );
   }
   else
   {
    updateList( "secteur_activite" , $(xml).find("list") );
   }
  }
 });


 $("#id_secteur_activite").change(function () {
  getMetierList( $("#id_secteur_activite option:selected").attr('value') );
  $.get("secteursSearch.xml",{'metier':'', 'activite':''} ,setCarteXML,"xml");
  $("#id_secteur").attr("disabled",true); 
 });
 
 $("#search02 input[type=reset]").click(function () {
  $("#id_metier option").slice(1).remove();
 });

 $("#id_metier").change(function() {
    $("#id_secteur").attr("disabled",false);  
    var metier = this.options[this.selectedIndex].value;
    var tabMetier = metier.split('#');  
    var code_APRM = tabMetier[0];

    $.get("secteursSearch.xml",{'metier':code_APRM, 'activite':$("#id_secteur_activite option:selected").attr("value")} ,setCarteXML,"xml");
  });
});


function getMetierList( secteur_activite )
{ 
 $.ajax({
  type: "GET",
  url: "rechercheArtisan_ajax.html?action=metier&IDmetier="+secteur_activite,   
  dataType: ($.browser.msie) ? "text" : "xml",
  success: function( data ) {
   var xml;
     if (typeof data == "string") {
       xml = new ActiveXObject("Microsoft.XMLDOM");
       xml.async = false;
       xml.loadXML(data);
     } else {
       xml = data;
     }
   // Returned data available in object "xml"

   if ( $(xml).is("erreur") )
   {
    alert( "Erreur : " + $(xml).find("erreur").text() );
   }
   else
   {
    $("#id_metier").attr("disabled", "");
    updateList( "metier" , $(xml).find("list") );
   }
  }
 });
} 

//Fonction qui rempli automatiquement le formulaire des metiers en fonction du metier cliqué
function chargeForm(metier){

   //recupération du métier
   var tabMetier = metier.split('#');
   var IDmetier = tabMetier[0];
   var code_APRM = tabMetier[1];
   
   //mise à jour des valeurs dans le formulaire
   $.ajax({
     type: "GET",
     url: "rechercheArtisan_ajax.html?action=popup&IDmetier="+IDmetier,   
      dataType: ($.browser.msie) ? "text" : "xml",
     success: function( data ) {
       var xml;
     if (typeof data == "string") {
       xml = new ActiveXObject("Microsoft.XMLDOM");
       xml.async = false;
       xml.loadXML(data);
     } else {
       xml = data;
     }
   // Returned data available in object "xml"

       if ( $(xml).is("erreur") ){
          alert( "Erreur : " + $(xml).find("erreur").text() );
       } else {
          $("#id_metier").attr("disabled", "");
          updateListPopup( $(xml).find("list"), IDmetier, code_APRM);
       }
     }
   });
}

//met à jour la liste du formulaire de recherche (appel depuis popup)
function updateListPopup( elem , IDmetier, code_APRM)
{ 

  //creation de la liste metier
  window.opener.$("#id_metier option").slice(1).remove();
  window.opener.$(elem).children().each(function() { 
       window.opener.$("#id_metier").append("<option value='"+$(this).attr("value")+"' "+$(this).attr('selected')+">" + $(this).text() + "</option>");
   });

    //recuperation du nom du secteur 
    $.ajax({
     type: "GET",
     url: "rechercheArtisan_ajax.html?action=recupActivite&IDmetier="+IDmetier,   
     dataType: ($.browser.msie) ? "text" : "xml",
     success: function( data ) {
      var xml;
      if (typeof data == "string") {
         xml = new ActiveXObject("Microsoft.XMLDOM");
         xml.async = false;
         xml.loadXML(data);
      } else {
        xml = data;
      }

       if ( $(xml).is("erreur") ){
          alert( "Erreur : " + $(xml).find("erreur").text() );
       } else {
           $secteur_activite = $(xml).find("list").text();
           window.opener.$("#id_secteur_activite").children().each(function() { 
                 if($(this).text() == $secteur_activite){
                       $(this).attr('selected','selected');

                       //si metier selectionne
                       $.get("secteursSearch.xml",{'metier':code_APRM, 'activite':IDmetier} ,setCarteXML2,"xml");
                 }
            });
        }
     }
   });
} 

function setCarteXML2(xml) {
  window.opener.$("#id_secteur option").slice(1).remove();
  $(xml).find("secteur").each(function() {
    //window.opener.$("#id_secteur").append("<option value='"+$(this).attr("nom")+"'>"+$(this).attr("libelle")+"</option>");
    window.opener.$('#id_secteur').append('<option value="'+$(this).attr("id")+'#'+$(this).attr("nom")+'">'+$(this).attr("libelle")+'</option>');

  });
  window.opener.$("#id_secteur").attr("disabled",false);    
  window.opener.$("#id_metier").attr("disabled",false);    

  //IE
  if (window.ActiveXObject) {
    var sXML = xml.xml;
  } else {
    // code for Mozilla, Firefox, Opera, etc.
    var sXML = (new XMLSerializer()).serializeToString(xml);
  }
  var oCarte=window.opener.document.getElementById("carte");
  if(oCarte) oCarte.setXml(sXML);

  //fermeture de la popup
  top.close();
}