// pri prvnim zobrazeni stranky naseptavac skryjeme
document.getElementById("naseptavacDiv").style.visibility = "hidden";
var lastSelected;

function GetKeyCode(e) {
  if (e) {
    return e.charCode ? e.charCode : e.keyCode;
  }
  else {
    return window.event.charCode ? window.event.charCode : window.event.keyCode;
  }
}

function generujNaseptavac(e) {
  var unicode = GetKeyCode(e);
  var str = document.getElementById("inputText").value;
  if (unicode != 38 && unicode != 40 && str != lastSelected) {
    if (str != "") {
      // IE/zbytek sveta
      if (window.ActiveXObject) {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        httpRequest = new XMLHttpRequest();
      }
      var url = "./naseptavacmesta.php?str=" + encodeURI(str);
      httpRequest.open("GET", url, true);
      httpRequest.onreadystatechange= function () {processRequest(); } ;
      httpRequest.send(null);
    }
    else {
      document.getElementById("naseptavacDiv").style.visibility = "hidden";
    }
  }
}

function posunNaseptavac(e) {
  var unicode = GetKeyCode(e);
  var naseptavac = document.getElementById("naseptavac");
  if (unicode == 40) {
    // sipka dolu
    naseptavac.options.selectedIndex =
      naseptavac.options.selectedIndex >= 0 &&
      naseptavac.options.selectedIndex < naseptavac.options.length-1 ?
      naseptavac.options.selectedIndex+1 : 0;
    getChangeHandler();
    return;
  }
  else if (unicode == 38) {
    // sipka nahoru
    naseptavac.options.selectedIndex =
      naseptavac.options.selectedIndex > 0 ?
      naseptavac.options.selectedIndex-1 : naseptavac.options.length-1;
    getChangeHandler();
    return;
  }
  else if (unicode == 13) {
    lastSelected = document.getElementById("inputText").value;
    // na enter ve textovem poli nechceme odesilat formular
    if (window.event)
      e.returnValue = false;
    else
      e.preventDefault();
    document.getElementById("naseptavacDiv").style.visibility = "hidden";
  }
}



function processRequest() {
  if (httpRequest.readyState == 4) {
    if(httpRequest.status == 200) {
      var response = httpRequest.responseText;
      if (response == 'EMPTY') {
        document.getElementById("naseptavacDiv").style.visibility = "hidden";
      }
      else {
        document.getElementById("naseptavacDiv").innerHTML = response;
        document.getElementById("naseptavac").size =
         document.getElementById("naseptavac").options.length;
        document.getElementById("naseptavacDiv").style.visibility = "visible";
      }
    }
    else {
      alert("Chyba p\u0159i načítání stránky" + httpRequest.status +":"+ httpRequest.statusText);
    }
  }
}

function getChangeHandler() {
  var select = document.getElementById("naseptavac");
  var nazev = select.options[select.selectedIndex].innerHTML;
  document.getElementById("inputText").value = nazev.replace(/\&amp;/g,'&');

}

function getResultClickHandler() {
  getChangeHandler();
  lastSelected = document.getElementById("inputText").value;
  document.getElementById("naseptavacDiv").style.visibility = "hidden";
}