// JavaScript Document
/*NASEPTAVAC BEROUCI DATA Z XML VYSTUPU
 *
 *
 */  


var pozice=-1; //aktualni pozice kurzoru
var delkanacitani = 3; //od kolikati znaku spustit naseptavac
var pocetzaznamu = 5; //pocet zobrazovanych zaznamu

    //zjistuje pocet znaku retezce
    function strlen(strVar)
    {
      return(strVar.length);
    }

    //zjisteni aktualni stisknute klavesy
    function checkKeycode(e) {
    
      var code;
      if (!e)
        var e = window.event; // nastaveni pro IE
      if (e.keyCode)
        code = e.keyCode; // IE a Mozilla
      else
        if (e.which)
          code = e.which; // NN4
      return code; 
      
    }

    function send_xmlhttprequest(obsluha, method, url, content, headers)
    {
       var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false)); 
       if (!xmlhttp)
       { //alert('chyba jak svin');
         return false; 
       }

       xmlhttp.open(method, url); 
       xmlhttp.onreadystatechange = function() 
       { 
         obsluha(xmlhttp); 
       }; 
       if (headers) { 
         for (var key in headers) 
         { 
           xmlhttp.setRequestHeader(key, headers[key]); 
         } 
       } 
       xmlhttp.send(content);
       return true;
    } 
  
    function disp(st,element) {
      self.act = st;
      if (element=="suggest") {      
        var ul = document.getElementById("suggest");
      }
      else if (element=="suggestobec") {      
        var ul = document.getElementById("suggestobec");      
      }
      if (st)
      {
        clearTimeout(self.timer);
        //ul.style.visibility = "visible";
        ul.style.display = "block";
      }
      else
      {
        //var f = function(){ul.style.visibility = "hidden";}
        var f = function(){ul.style.display = "none";}
        self.timer = setTimeout(f, 500);
      }
    }
    
    //funkce obsluhy naseptavace
    function novinky_edit(event) {
      var keycode=checkKeycode(event); 
      var hodnotavpoli = document.getElementById("q").value;
      var pocet = strlen(hodnotavpoli);
      
      if (pocet>=delkanacitani) {
      
          //akce po stisknuti klavesy dolu
          if (keycode==40) {
            pozice = pozice + 1;
            
            if (pozice>=4) pozice = 4;
            var li=document.getElementById("select"+pozice);
            var druhy=pozice-1;        
            var li2=document.getElementById("select"+druhy);            
            
            document.getElementById("q").value =li.innerHTML;
            
            li.className='pres';
            li2.className='normal';
            
            
            
          }
          //akce po stisknuti klavesy nahoru
          if (keycode==38) {      
            pozice = pozice - 1;
            if (pozice<-1) pozice = -1;
            if (pozice==-1) {
              pozice = -1;              
            }        
            var li=document.getElementById("select"+pozice);
            var druhy=pozice+1
            var li2=document.getElementById("select"+druhy);

            if (druhy!=0) li.className='pres';
            li2.className='normal';

            
            document.getElementById("q").value =li.innerHTML;
                        
            
                                        
            
          }
          
          //akce po stisknuti klavesy Enter
          if (keycode==13) {
            var li=document.getElementById("select"+pozice);
            document.getElementById("q").value =li.innerHTML;
          }
          
          
          var q = document.getElementById("q");
         // pozadavek na aktualizaci dat  
          if ((keycode!=38)&&(keycode!=40)) {         
            
           if (!send_xmlhttprequest(novinky_obsluha_edit, 'GET', 'natahni.php?search=' + q.value)) 
           { 
             return false; 
           }  
           
           return true;
           }
      
     } else disp(0,"suggest");
    } 
    
    //funkce vypsani naseptavace
    function novinky_obsluha_edit(xmlhttp) 
    {
      if (xmlhttp.readyState == 4) 
      {   
        //var id      = xmlhttp.responseXML.getElementsByTagName('id');
        var name    = xmlhttp.responseXML.getElementsByTagName('name');
        //var adresa  = xmlhttp.responseXML.getElementsByTagName('adresa');
        pozice = -1;
        var hodnota = name[0].firstChild.data;        
        if (hodnota!="false") {
        disp(1,"suggest");
            var ul = document.getElementById("suggest");
            while (ul.childNodes.length > 0)
              ul.removeChild(ul.childNodes[0]);

            for (var i=0; i <= 5; i++) {
              var li = document.createElement("LI");
              var label = name[i];
              li.id = 'select'+i;
              
      
              li.className='normal';
                          
              li.innerHTML = name[i].firstChild.data;
              ul.appendChild(li);
              
              li.onclick = function(){
                document.getElementById("q").value =
                  this.innerHTML;
              }
            }
        
        } else disp(0,"suggest");
        
      }  
    } 
    
    
/* HODNOCENI RESTAURACI
 *
 */ 

var vysledne_hodnoceni; //globalni promenna znazornujici vysledne hodnoceni

      function hodnoceni_hlasovat(hlas,klient) {
          // odeslání požadavku na aktualizaci dat
          if (!send_xmlhttprequest(hodnoceni_obsluha, 'GET', 'hodnoceni.php?hodnota=' + hlas + '&klient=' + klient)) {
              return false;
          }
          //document.getElementById('pocet' + hlas).innerHTML++; // zobrazení hlasu u klienta
          // znemožnění opětovného hlasování smazáním odkazů
          /*
          for (var key in document.getElementById('anketa').getElementsByTagName('td')) {
              var val = document.getElementById('anketa').getElementsByTagName('td')[key];
              if (val.className == 'odpoved') {
                  val.innerHTML = val.firstChild.innerHTML;
              }
          }
          */
          
          document.getElementById('stav-anketa').innerHTML = 'Ukládá se';
          return true;
      }
      
      function hodnoceni_obsluha(xmlhttp) {
          if (xmlhttp.readyState == 4) {
              // aktualizace odpovědí na základě aktuálního stavu
              
              var odpovedi = xmlhttp.responseXML.getElementsByTagName('hodnota');
              vysledne_hodnoceni = odpovedi[0].firstChild.data;
              /*
              for (var i=0; i < odpovedi.length; i++) {
                  document.getElementById(odpovedi[i].getAttribute('id')).innerHTML = odpovedi[i].firstChild.data;
              }
              */
              
              document.getElementById('stav-anketa').innerHTML = 'Uloženo';
          }
      }  


    
/***
 *NASEPTAVAC OBCI v REGISTRACI
 ******/    

delkanacitani = 2; //od kolikati znaku spustit naseptavac

    //funkce obsluhy naseptavace
    function obec_edit(event) {
      var keycode=checkKeycode(event); 
      var hodnotavpoli = document.getElementById("obec").value;
      var pocet = strlen(hodnotavpoli);
      
      if (pocet>=delkanacitani) {
      
          //akce po stisknuti klavesy dolu
          if (keycode==40) {
            pozice = pozice + 1;
            
            if (pozice>=4) pozice = 4;
            var li=document.getElementById("select"+pozice);
            var druhy=pozice-1;        
            var li2=document.getElementById("select"+druhy);            
            
            document.getElementById("obec").value =li.innerHTML;
            
            li.className='pres';
            li2.className='normal';
            
            
            
          }
          //akce po stisknuti klavesy nahoru
          if (keycode==38) {      
            pozice = pozice - 1;
            if (pozice<-1) pozice = -1;
            if (pozice==-1) {
              pozice = -1;              
            }        
            var li=document.getElementById("select"+pozice);
            var druhy=pozice+1
            var li2=document.getElementById("select"+druhy);

            if (druhy!=0) li.className='pres';
            li2.className='normal';

            
            document.getElementById("obec").value =li.innerHTML;
                        
            
                                        
            
          }
          
          //akce po stisknuti klavesy Enter
          if (keycode==13) {
            var li=document.getElementById("select"+pozice);
            document.getElementById("obec").value =li.innerHTML;
          }
          
          
          var q = document.getElementById("obec");
         // pozadavek na aktualizaci dat  
          if ((keycode!=38)&&(keycode!=40)) {         
            
           if (!send_xmlhttprequest(obec_obsluha_edit, 'GET', 'natahniobec.php?search=' + q.value)) 
           { 
             return false; 
           }  
           
           return true;
           }
      
     } else disp(0,"suggestobec");
    } 
    
    //funkce vypsani naseptavace
    function obec_obsluha_edit(xmlhttp) 
    {
      if (xmlhttp.readyState == 4) 
      {   
        //var id      = xmlhttp.responseXML.getElementsByTagName('id');
        var name    = xmlhttp.responseXML.getElementsByTagName('name');
        //var idpole  = xmlhttp.responseXML.getElementsByTagName('idecko');
        pozice = -1;
        var hodnota = name[0].firstChild.data;        
        if (hodnota!="false") {
        disp(1,"suggestobec");
            var ul = document.getElementById("suggestobec");
            while (ul.childNodes.length > 0)
              ul.removeChild(ul.childNodes[0]);

            for (var i=0; i <= 5; i++) {
              var li = document.createElement("LI");
              var label = name[i];
              li.id = 'select'+i;
              
      
              li.className='normal';
                          
              li.innerHTML = name[i].firstChild.data;
              ul.appendChild(li);
              
              
              
              li.onclick = function(){
                document.getElementById("obec").value =
                  this.innerHTML;
              }
            }
        
        } else disp(0,"suggestobec");
        
      }  
    } 
 


