var xmlHttp;

function vytvorXMLHttpRequest(){
    if (window.ActiveXObject){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();
    }
}

function ajax(url,kam){
    
    vytvorXMLHttpRequest();
    xmlHttp.onreadystatechange = zpracujZmenuStavu;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    
    function zpracujZmenuStavu(){
        if(xmlHttp.readyState == 4){
            if (xmlHttp.status == 200){
            document.getElementById(kam).innerHTML = xmlHttp.responseText;
            }
        }
    }
}

function starsUp(num){
      for(i=1;i<=10;i++){
          var starName = "star_" + i;
          if(i<=num){
              document.getElementById(starName).src="img/star_active.png";
          } else {
              document.getElementById(starName).src="img/star_inactive.png";
          }   
      }
}

function starsOut(num){
      for(i=1;i<=10;i++){
          var starName = "star_" + i;
          if(i<=num){
              document.getElementById(starName).src="img/star_active.png";
          } else {
              document.getElementById(starName).src="img/star_inactive.png";
          } 
      }
}

function AddFavorite(linkObj,addUrl,addTitle)
    {
      if (document.all && !window.opera)
      {
        window.external.AddFavorite(addUrl,addTitle);
        return false;
      }
      else if (window.opera && window.print)
      {
        linkObj.title = addTitle;
        return true;
      }
      else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))
      {
        if (window.confirm('Přidat oblíbenou stránku jako nový panel?'))
        {
          window.sidebar.addPanel(addTitle,addUrl,'');
          return false;
        }
      }
      window.alert('Po potvrzení stiskněte CTRL-D,\n poté bude stránka přidána mezi Vaše oblíbené odkazy.');
      return false;
    }

// při prvním zobrazení stránky našeptávač 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 světa
      if (window.ActiveXObject) {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        httpRequest = new XMLHttpRequest();
      }
      var url = "naseptavac.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) {
    // š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 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ř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";
}
