var txt_destination = document.getElementById("txt_destination")
var lst_destination = document.getElementById("lst_destination")
var img_loading = document.getElementById("img_loading")




function txt_destination_onKeyUp()
{
  var xhr = null
  // INITIALISATION
  // on regarde quel navigateur est utilisé pour instancier correctement l'objet (en cours de validation sur Opéra)
  if (window.XMLHttpRequest)  // Firefox, Safari, Mozilla, etc.
    xhr = new XMLHttpRequest()
  else
    if (window.ActiveXObject) // Internet Explorer
      xhr = new ActiveXObject("Microsoft.XMLHTTP")
    else
      alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...")
  if (xhr != null) {
    xhr.onreadystatechange = function() {traiter_reponse_auto(xhr)}

    // appel au fichier php et mode de transmission par la méthode POST (fonctionne aussi en GET)
    xhr.open("POST", "saisi_auto.php", true) // true pour asynchronous
    // création des données à transférer via un entête HTTP
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded") // pour prendre en compte données POST
    var data = "txt_destination="+txt_destination.value
    // envoie la requête (données)
    xhr.send(data)
    // alert("Envoyé " + txt_destination.value)
//    alert("Etat " + xhr.readyState)
  } //if (xhr != null)
}

function traiter_reponse_auto (xhr) {
  // lorsqu'il y a résultat (==4 données complètes)
  if (xhr.readyState == 4)
  {
    if (xhr.status == 200)
    { // pas d'erreur de chargement
      eval(xhr.responseText)
      tester_visible();
      // alert(xhr.responseText)
    } // if (xhr.status == 200)
    else
      alert("Le serveur a retourné l'erreur " + xhr.status)
  }
  else
  {
    if(txt_destination.value.length >= 3)
      img_loading.style.display = "block"
  }  
}

function tester_visible()
{
  if((lst_destination.innerHTML != "") && (txt_destination.value.length >= 3))
  {
    img_loading.style.display = "none"
    lst_destination.style.display = "block"
  }
  else if((lst_destination.innerHTML == "") || (txt_destination.value.length < 3))
  {
    img_loading.style.display = "none"
    lst_destination.style.display = "none"
  }
}

function remplir_destination(dest)
{
  txt_destination.value = dest
}

function cacher_chargement() {
  img_loading.style.display = "none"
  lst_destination.style.display = "none"  
}

txt_destination.onkeyup = txt_destination_onKeyUp
txt_destination.onclick = tester_visible
txt_destination.onblur = cacher_chargement
cacher_chargement()