function g1region_to_g1departement_G(){
  // Génère le select des départements
  g1region_to_g1departement('G');

  // Sélectionne les guides à afficher
  Select_guides('region');
}

function g1departement_to_g1commune_G(){
  // Génère le select des communes
  g1departement_to_g1commune('G');

  // Sélectionne les guides à afficher
  var sel=document.getElementById('g1_departement');
  var id=sel.options[sel.selectedIndex].value;
  if (id=='0') Select_guides('region');
  else Select_guides('departement');
}

function g1commune_G(){
  // Sélectionne les guides à afficher
  var sel=document.getElementById('g1_commune');
  var id=sel.options[sel.selectedIndex].value;
  if (id=='0') Select_guides('departement');
  else Select_guides('commune');
}

function g1region_to_g1departement_E(){
  // Génère le select des départements
  g1region_to_g1departement('E');

  // Sélectionne les événements à afficher
  Select_evenements('region');
}

function g1departement_to_g1commune_E(){
  // Génère le select des communes
  g1departement_to_g1commune('E');

  // Sélectionne les événements à afficher
  var sel=document.getElementById('g1_departement');
  var id=sel.options[sel.selectedIndex].value;
  if (id=='0') Select_guides('region');
  else Select_evenements('departement');
}

function g1commune_E(){
  // Sélectionne les événements à afficher
  var sel=document.getElementById('g1_commune');
  var id=sel.options[sel.selectedIndex].value;
  if (id=='0') Select_guides('departement');
  else Select_evenements('commune');
}

function g1region_to_g1departement_A(){
  // Génère le select des départements
  g1region_to_g1departement('A');

  // Sélectionne les entreprises à afficher
  Select_entreprises('region');
}

function g1departement_to_g1commune_A(){
  // Génère le select des communes
  g1departement_to_g1commune('A');

  // Sélectionne les entreprises à afficher
  var sel=document.getElementById('g1_departement');
  var id=sel.options[sel.selectedIndex].value;
  if (id=='0') Select_entreprises('region');
  else Select_entreprises('departement');
}

function g1commune_A(){
  // Sélectionne les entreprises à afficher
  var sel=document.getElementById('g1_commune');
  var id=sel.options[sel.selectedIndex].value;
  if (id=='0') Select_entreprises('departement');
  else Select_entreprises('commune');
}

function g1region_to_g1departement(mode){
  var xhr=getXhr();

  xhr.onreadystatechange=function(){
    if(xhr.readyState==4 && xhr.status==200){
      nouveau_select=xhr.responseText;
      // ajoute les options à la liste
      document.getElementById('l_g1_departement').innerHTML='<select id="g1_departement">'+nouveau_select+'</select>';
      document.getElementById('g1_departement').name='gp_departement';
      if (mode=='G') document.getElementById('g1_departement').onchange=function(){g1departement_to_g1commune_G();};
      else if (mode=='E') document.getElementById('g1_departement').onchange=function(){g1departement_to_g1commune_E();};
      else if (mode=='F') document.getElementById('g1_departement').onchange=function(){g1departement_to_g1commune('F');};
      else if (mode=='A') document.getElementById('g1_departement').onchange=function(){g1departement_to_g1commune_A();};
      // ré-initialise les communes
      document.getElementById('l_g1_commune').innerHTML='<select><option value="0">-----</option></select>';
      // ----- fiche -----
      if (mode=='F'){
        // ré-initialise les codes postaux
        document.getElementById('g1_cp').innerHTML='<option value="0">-----</option>';
        // efface le champ 'code_postal'
        document.getElementById('code_postal').value='';
      }
    }
  }

  xhr.open("POST","xhr_g1.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

  var sel=document.getElementById('g1_region');
  var id=sel.options[sel.selectedIndex].value;

  if (mode=='F') var maj="maj=1&";
  else var maj="";

  xhr.send(maj+"mode=get_departement&region_id="+id);
}

function g1departement_to_g1commune(mode){
  var xhr=getXhr();

  xhr.onreadystatechange=function(){
    if(xhr.readyState==4 && xhr.status==200){
      nouveau_select=xhr.responseText;
      // ajoute les options à la liste
      document.getElementById('l_g1_commune').innerHTML='<select id="g1_commune">'+nouveau_select+'</select>';
      document.getElementById('g1_commune').name='gp_commune';
      if (mode=='G') document.getElementById('g1_commune').onchange=function(){g1commune_G();};
      else if (mode=='E') document.getElementById('g1_commune').onchange=function(){g1commune_E();};
      else if (mode=='F') document.getElementById('g1_commune').onchange=function(){g1commune_to_g1cp();};
      else if (mode=='A') document.getElementById('g1_commune').onchange=function(){g1commune_A();};
      // ----- fiche -----
      if (mode=='F'){
        // ré-initialise les codes postaux
        document.getElementById('l_g1_cp').innerHTML='<select><option value="0">-----</option></select>';
        // efface le champ 'code_postal'
        document.getElementById('code_postal').value='';
      }
    }
  }

  xhr.open("POST","xhr_g1.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

  var sel=document.getElementById('g1_departement');
  var id=sel.options[sel.selectedIndex].value;

  if (mode=='F') var maj="maj=1&";
  else var maj="";

  xhr.send(maj+"mode=get_commune&departement_id="+id);
}

function g1commune_to_g1cp(){
  var xhr=getXhr();

  xhr.onreadystatechange=function(){
    if(xhr.readyState==4 && xhr.status==200){
      nouveau_select=xhr.responseText;
      // ----- nouvelle fiche -----
      // ajoute les options à la liste
      document.getElementById('l_g1_cp').innerHTML='<select id="g1_cp">'+nouveau_select+'</select>';
      document.getElementById('g1_cp').name='gp_cp';
      document.getElementById('g1_cp').onchange=function(){maj_g1cp();};
      // efface le champ 'code_postal'
      document.getElementById('code_postal').value='';
    }
  }

  xhr.open("POST","xhr_g1.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

  var sel=document.getElementById('g1_commune');
  var id=sel.options[sel.selectedIndex].value;

  xhr.send("maj=1&mode=get_cp&commune_id="+id);
}

function maj_g1cp(){
  var xhr=getXhr();

  // Efface le champ code postal
  document.getElementById('code_postal').value='';

  xhr.open("POST","xhr_g1.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

  var sel=document.getElementById('g1_cp');
  var id=sel.options[sel.selectedIndex].value;

  xhr.send("maj=1&cp_id="+id);
}

function Select_guides(mode){
  var xhr=getXhr();

  xhr.onreadystatechange=function(){
    if(xhr.readyState==4 && xhr.status==200){
      var Selection=xhr.responseText;
      // Met à jour l'affichage
      document.getElementById('div_guide').innerHTML=Selection;
    }
  }

  xhr.open("POST","xhr_g1.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

  if (mode=='region'){var sel=document.getElementById('g1_region'); var champ='region_id';}
  else if (mode=='departement'){var sel=document.getElementById('g1_departement'); var champ='departement_id';}
  else if (mode=='commune'){var sel=document.getElementById('g1_commune'); var champ='commune_id';}
  var id=sel.options[sel.selectedIndex].value;

  xhr.send("mode=get_guides&"+champ+"="+id);
}

function Select_evenements(mode){
  var xhr=getXhr();

  xhr.onreadystatechange=function(){
    if(xhr.readyState==4 && xhr.status==200){
      var Selection=xhr.responseText;
      // Met à jour l'affichage
      document.getElementById('div_calendrier').innerHTML=Selection;
    }
  }

  xhr.open("POST","xhr_g1.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

  if (mode=='region'){var sel=document.getElementById('g1_region'); var champ='region_id';}
  else if (mode=='departement'){var sel=document.getElementById('g1_departement'); var champ='departement_id';}
  else if (mode=='commune'){var sel=document.getElementById('g1_commune'); var champ='commune_id';}
  var id=sel.options[sel.selectedIndex].value;

  xhr.send("mode=get_evenements&"+champ+"="+id);
}

function Select_entreprises(mode){
  var xhr=getXhr();

  xhr.onreadystatechange=function(){
    if(xhr.readyState==4 && xhr.status==200){
      var Selection=xhr.responseText;
      // Met à jour l'affichage
      document.getElementById('div_annuaire').innerHTML=Selection;
    }
  }

  xhr.open("POST","xhr_g1.php",true);
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

  if (mode=='region'){var sel=document.getElementById('g1_region'); var champ='region_id';}
  else if (mode=='departement'){var sel=document.getElementById('g1_departement'); var champ='departement_id';}
  else if (mode=='commune'){var sel=document.getElementById('g1_commune'); var champ='commune_id';}
  var id=sel.options[sel.selectedIndex].value;

  xhr.send("mode=get_entreprises&"+champ+"="+id);
}
