//alert(document.getElementById("naseptavacDiv"));
//document.getElementById("naseptavacDiv").style.visibility = "hidden";
var lastSelected;

var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

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("hledat").value;
  if (unicode != 38 && unicode != 40 && str != lastSelected) {
    if (str.length > 1) {      
      send_xmlhttprequest(odpoved_naseptavac, 'GET', '/get/get_naseptavac.php?klic='+encodeURI(str), '', '' );     
      //send_xmlhttprequest(odpoved_naseptavac, 'GET', '/get/get_naseptavac.php?klic='+str, '', '' );
    }
    else {     
      document.getElementById("naseptavacDiv").style.visibility = "hidden";
    }
  }
}

function posunNaseptavac(e) {  
  var unicode = GetKeyCode(e);
  if (unicode == 13)
  	return;
  
  var naseptavac = document.getElementById("naseptavac");
  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("hledat").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 odpoved_naseptavac() {
  if (xmlhttp.readyState == 4) {
    if(xmlhttp.status == 200) {
      var response = xmlhttp.responseText;
      if (response == 'EMPTY' || response.substr(0, 7) != '<select') {
        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"
        + xmlhttp.status +":"+ xmlhttp.statusText);
    }
  }
}

function getChangeHandler() {
  var select = document.getElementById("naseptavac");
  var nazev = select.options[select.selectedIndex].innerHTML;
  document.getElementById("hledat").value = nazev.replace(/\&amp;/g,'&');
}
 
function getResultClickHandler() {
  getChangeHandler();
  lastSelected = document.getElementById("hledat").value;  
  document.getElementById("naseptavacDiv").style.visibility = "hidden";
}
