
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 && unicode != 27 && str != lastSelected) {
    if (str != "") {
      // IE/zbytek světa
      if (window.ActiveXObject) {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        httpRequest = new XMLHttpRequest();
      }
      var url = "/ajax.php?method=naseptavacHledani&search=" + encodeURI(str);

      httpRequest.open("GET", url, true);
      httpRequest.onreadystatechange= function () {processRequestNaseptavac(); } ;
      httpRequest.send(null);
    }
    else {
      document.getElementById("naseptavacDiv").style.visibility = "hidden";
    }
  }
}

function posunNaseptavac(e) {
  var unicode = GetKeyCode(e);
  var naseptavac = document.getElementById("naseptavac");
  if(unicode == 27){
	  document.getElementById("naseptavacDiv").style.visibility = "hidden";
  }
  else if (unicode == 40) {
    // šipka dolů
    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) {
    // šipka 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 textovém poli nechceme odesílat formulář
    if (window.event)
      e.returnValue = false;
    else
      e.preventDefault();
    document.getElementById("naseptavacDiv").style.visibility = "hidden";
  }
}

function processRequestNaseptavac() {
  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ři 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";
  document.forms["vyhledavac"].submit();
}