var onprogress = false;

function init()
{
  var suggestDiv = document.getElementById("suggestDiv");
  var tfMiasta = document.getElementById("tfMiasta");

  suggestDiv.style.top = tfMiasta.style.top;
  suggestDiv.style.left = (tfMiasta.style.left/1 + tfMiasta.offsetLeft/1) + "px";
  suggestDiv.style.display = "none";
}

function keyUp()
{
  if(onprogress){
    XMLHttpRequestObject.abort();
  }
  tfMiastaText = document.getElementById('tfMiasta').value;
  if(tfMiastaText == ""){
    var suggestDiv = document.getElementById("suggestDiv");
    suggestDiv.style.display = "none";
    return;
  }
  url = "http://localhost/dane.php?text=" + tfMiastaText;
  url = encodeURI(url);

  var onprogress = true;
  startGETRequest(url, onComplete, onEnd);
}

function changeText(lista)
{
  var suggestDiv = document.getElementById("suggestDiv");
  suggestDiv.style.display = "none";

  var text = lista[lista.selectedIndex].value;
  var tfMiasta = document.getElementById("tfMiasta");
  tfMiasta.value = text;
  tfMiasta.focus();
}

function onComplete(text, xml)
{
  if(text.substr(0, 5) == "error"){
    return;
  }
  else if(text != ""){
    arr = text.split("\n");
    var listaOpcji = document.getElementById("selSuggest");
    listaOpcji.options.length = 0;
    for(i = 0; i < arr.length; i++){
      listaOpcji[i] = new Option(arr[i], arr[i]);
    }
    var suggestDiv = document.getElementById("suggestDiv");
    suggestDiv.style.display = "block";
  }
  else{
    var suggestDiv = document.getElementById("suggestDiv");
    suggestDiv.style.display = "none";
  }
}

function onEnd()
{
  var onprogress = false;
}