var TXT_MAJ = "Mettre à jour";
var TXT_CREER = "Créer";
var TXT_SUPPRIMER = "Supprimer";
var TXT_1 = "correspondance";
var TXT_2 = "quai";
var TXT_3 = "actus";
var TXT_4 = "direction";
var TXT_5 = "station";
var TXT_BTN_VALIDE_1 = "Afficher";
var TXT_UNE = "une";
var TXT_LA = "la";
var TXT_UN = "un";

function afficherInfos(tab_infos) {
 var chaine = "<ul>";
 for(var val=0; val<tab_infos.length; val++) {
   chaine+= "<li>" +tab_infos[val]+ "</li>";
 }
 chaine += "</ul>";
 $("#infos").html(chaine);
}

//  Remplace toutes les occurences d'une chaine
function replaceAll(str, search, repl) {
  while (str.indexOf(search) != -1) 
    str = str.replace(search, repl);
  return str;
}
// Remplace les caractères accentués en lettre non accentuées pour formater le nom de fichier
function accentEnLettre(str) {
  var spec = new Array("é", "É", "è", "È", "ê", "Ê", "ë", "Ë", "à", "À", "ù", "Ù", "ç", "Ç", "â", "Â", "î", "Î", "ï", "Ï", "ô", "Ô", "ü", "Ü", " - ", " ", "'", ":-");
  var norm = new Array("e", "e", "e", "e", "e", "e", "e", "e", "a", "a", "u", "u", "c", "c", "a", "a", "i", "i", "i", "i", "o", "o", "u", "u", "_", "-", "-", "");
  for (var i = 0; i < spec.length; i++)
    str = replaceAll(str, spec[i], norm[i]);
  return str;
}

/* Vérifie que la valeur d'un champ est bien un entier positif */
function verifChamp(val, type) {
var exp = null;
switch(type) {
case "entier" :
exp = new RegExp('^[^-0]*[1-9]+[0-9]*$');
 break;
 case "date" :
exp = new RegExp("[0-9]{2}-[0-9]{2}-[0-9]{4}");
 break;
 }
   if(!exp.test(val)) return false;
 else return true;
}


function charger(elem) {
 var donnee = sel.val();
 var tab_infos = new Array();
switch(elem) {
case "connexion" :
$.post("charger.php", {"module": 1, "id": donnee}, function(data) {
 if($("statut",data).attr("id") == 1) {
 // récupère et formate les données avant de les afficher
  var mp3 = $("mp3",data).attr("existe");
  var ordre = $("ordre",data).text();
  var court = $("court",data).text();
  court = replaceAll(court, "\\", "");
  var detail = $("detail",data).text();
  detail = replaceAll(detail, "\\", "");
    var id_provenance = $("provenance",data).attr("id");
  var nom_provenance = replaceAll($("provenance",data).text(), "\\", "");
  var id_direction = $("direction",data).attr("id");
  var nom_direction = replaceAll($("direction",data).text(), "\\", "");
  var id_station = $("station",data).attr("id");
  var nom_station = replaceAll($("station",data).text(), "\\", "");
  var id_quai = $("quai",data).attr("id");
  // pour remplir la div infos
  tab_infos.push("id de correspondance : " +sel.val());
  tab_infos.push("Provenance : " +id_provenance +" "+ nom_provenance);
  tab_infos.push("Direction : " +id_direction +" "+ nom_direction);
  tab_infos.push("Station : " + id_station + " "+ nom_station);
  tab_infos.push("id du quai : " +id_quai);
    afficherInfos(tab_infos);
    if(mp3==1) $("#mp3:checkbox").attr("checked","checked");
    else $("#mp3:checkbox").removeAttr("checked");
$("#num_ordre").val(ordre);
  $("#court").html(court);
  $("#detail").html(detail);
 }
 else {
 alert($("erreur",data).text());
 }}); 
break;
case "quai" :
$.post("charger.php", {"option": 1, "id": donnee}, function(data) {
if($("statut",data).attr("id") == 1) {
   var index_type = $("type",data).text();
  var court = $("court",data).text();
  court = replaceAll(court, "\\", "");
  var detail = $("detail",data).text();
  detail = replaceAll(detail, "\\", "");
  var commentaire = $("commentaire",data).text();
  commentaire = replaceAll(commentaire, "\\", "");
  var id_provenance = $("provenance",data).attr("id");
  var id_station = $("station",data).attr("id");
  // pour remplir la div infos
  tab_infos.push("id de quai : " +sel.val());
  tab_infos.push("Provenance : " +id_provenance);
 tab_infos.push("Station : " + id_station);
 afficherInfos(tab_infos);
  formulaire.type[index_type].setAttribute("selected","selected");
  $("#commentaire").html(commentaire);
  $("#court").html(court);
  $("#detail").html(detail);
 }
 else {
 alert($("erreur",data).text());
 }
}); 
break;
case "direction" :
$.post("charger.php", {"option": 1, "id": donnee}, function(data) {
 if($("statut",data).attr("id") == 1) {
    var nom = $("nom",data).text();
    nom = replaceAll(nom, "\\", "");
  var ligne = $("ligne",data).text();
  ligne = replaceAll(ligne, "\\", "");
  var commentaire = $("commentaire",data).text();
  commentaire = replaceAll(commentaire, "\\", "");
  // pour remplir la div infos
  tab_infos.push("id de direction : " +sel.val());
  tab_infos.push("Direction : " + nom);
  tab_infos.push("Commentaire : " + commentaire);
  afficherInfos(tab_infos);
  $("#general_data").show();
  $("#nom").val(nom);
  $("#ligne").val(ligne);
  $("#commentaire").html(commentaire);
 }
 else {
 alert($("erreur",data).text());
 }
}); 
break;
case "station" :
$.post("charger.php", {"option": 1, "id": donnee}, function(data) {
 if($("statut",data).attr("id") == 1) {
   var nom = $("nom",data).text();
  nom = replaceAll(nom, "\\", "");
   var description = $("description",data).text();
    description = replaceAll(description, "\\", "");
     var commentaire = $("commentaire",data).text();
    commentaire = replaceAll(commentaire, "\\", "");
  
   tab_infos.push("id de station : " +sel.val());
  afficherInfos(tab_infos);
$("#general_data").show();
$("#nom").val(nom);
$("#description").html(description);
$("#commentaire").html(commentaire);
}
 else {
 alert($("erreur",data).text());
 }
});
break;
case "actus" :
$.post("charger.php", {"option": 1, "id": donnee}, function(data) {
  if($("statut",data).attr("id") == 1) {
   var index_type = $("type",data).text();
var date = $("date",data).text();
  var texte = $("texte",data).text();
  texte = replaceAll(texte, "\\", "");
$("#general_data").show();
formulaire.type[index_type-1].setAttribute("selected","selected");
$("#date").val(date);
  $("#texte").html(texte);
  }
 else {
 alert($("erreur",data).text());
 }
}); 
break;
case "voie" :
$.post("charger.php", {"option": 1, "id": donnee}, function(data) {
  if($("statut",data).attr("id") == 1) {
    var index_type = $("type",data).text();
    var nom = $("nom",data).text();
nom = replaceAll(nom, "\\", "");
var ville = $("ville",data).text();
ville = replaceAll(ville, "\\", "");

$("#general_data").show();
formulaire.type_voie[index_type-1].setAttribute("selected","selected");
$("#nom").val(nom);
$("#ville").val(ville);
 }
 else {
 alert($("erreur",data).text());
 }
});
break;
case "adresse" :
$.post("charger.php", {"option": 1, "id": donnee}, function(data) {
  if($("statut",data).attr("id") == 1) {
     var index_voie = $("voie",data).text();
  var numero = $("numero",data).text();
  var infosup = $("infosup",data).text();
  infosup = replaceAll(infosup, "\\", "");
$("#general_data").show();
formulaire.voie[index_voie-1].setAttribute("selected","selected");
$("#numero").val(numero);
  $("#infosup").html(infosup);
  }
 else {
 alert($("erreur",data).text());
 }
}); 
break;
case "bus_arret" :
$.post("charger.php", {"option": 1, "id": donnee}, function(data) {
  if($("statut",data).attr("id") == 1) {
   var type = $("type",data).text();
var nom = $("nom",data).text();
var adresse = $("adresse",data).text();
  var infosup = $("infosup",data).text();
  infosup = replaceAll(infosup, "\\", "");
$("#general_data").show();
formulaire.nom_arret[nom-1].setAttribute("selected","selected");
formulaire.adresse[adresse-1].setAttribute("selected","selected");
formulaire.type_arret[type-1].setAttribute("selected","selected");
$("#infosup").html(infosup);
  }
 else {
 alert($("erreur",data).text());
 }
}); 
break;
} // fin switch
$("#opt_infos").removeAttr("disabled");
$("#id").val(sel.val());
$("#msg").empty();
$("#titre_elem").html(sel.text());
  } // fin charger

function echangerDonnee(fichier, donnee, infos) {
  $.post(fichier, donnee, function(data) {
   var statut = $("statut",data).attr("id");
    if(statut == 0)
     alert($("message",data).text());
    if(statut == 1) {
     $("#msg").html($("message",data).text() + " " +infos);
    }
  }); // fin post
} // fin echangerDonnee


