// JavaScript Document

function xautocomplete(suggest_id, data_sloupec, data_table, data_where, data_limit, xform_fulltext, xform_input_id, e, xform_maxlength, xform_align, xform_onselect, xform_formular_id, xform_color) {

   xform_input_obj = document.getElementById(xform_input_id);
   xform_formular = document.getElementById(xform_formular_id);
   klavesa = kod_klavesy(e);

   var ms = new Date();
   var msx = ms.getTime();
   document.body.tag = msx;
   window.setTimeout("xautocomplete_run('"+suggest_id+"','"+data_sloupec+"','"+data_table+"','"+data_where+"','"+data_limit+"','"+xform_fulltext+"', "+klavesa+", '"+xform_maxlength+"','"+xform_align+"','"+xform_onselect+"','"+xform_color+"')", 300);
   };


function xautocomplete_run(suggest_id, data_sloupec, data_table, data_where, data_limit, xform_fulltext, klavesa, xform_maxlength, xform_align, xform_onselect, xform_color) {

   //posledni stisknuta klavesa byla pred sekundou
   var ms = new Date();
   var msx = ms.getTime();
   //alert(document.body.tag + " > " + (msx-500));
	if (!((msx - 280) > document.body.tag && (msx - 320) < document.body.tag)) return false;

   var el = document.getElementById(suggest_id);
   if ((klavesa == 38 && el.style.display == "block") ||
       (klavesa == 40 && el.style.display == "block") ||
       klavesa == 13 ||
       klavesa == 37 ||
       klavesa == 39 ||
       klavesa == 27) return;


   if (xform_input_obj.value.length > 0) {

      var xmlhttp=getHTTPObject();
      var docname = "ajax_global.php?akce=autocomplete";
      if (xmlhttp) {
         xmlhttp.open("POST", docname, false);
			var content = "suggest_id=" + encodeURIComponent(suggest_id) + "&data_sloupec=" + encodeURIComponent(data_sloupec) + "&data_table=" + encodeURIComponent(data_table) + "&data_where=" + encodeURIComponent(data_where) + "&data_limit=" + encodeURIComponent(data_limit) + "&fulltext=" + encodeURIComponent(xform_fulltext) + "&retezec=" + encodeURIComponent(xform_input_obj.value) + "&maxlength=" + encodeURIComponent(xform_maxlength) + "&color=" + encodeURIComponent(xform_color); 
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=windows-1250");
			xmlhttp.setRequestHeader("Content-length", content.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(content);

         var text = xmlhttp.responseText;
			//alert(text);
         if ((text.length > 0) && (xform_input_obj.getAttribute("id") == document.activeElement.getAttribute("id"))) {  //pokud je fokus na input prvku a text neco vrati
            pole = text.split("@@@");
            //odstranit stare radky
            while (el.rows[0]) el.deleteRow(0);
            //vytvorit nove radky
            for (i=0; i < pole.length - 1; i++) {
               var pole2 = pole[i].split("|||");
               var pole3 = pole2[0].split("###");
					tr = el.insertRow(i);
               td = tr.insertCell(0);
               td.innerHTML = pole3[1];
               td.tag = pole[i];
               tr.onmousemove = function(){
                  if (el.tag > -1) el.rows[el.tag].style.backgroundColor = "";
                  this.style.backgroundColor = xform_color;
                  el.tag = this.rowIndex;
                  };
               tr.onmouseout = function(){
                  this.style.backgroundColor = "";
                  el.tag = -1;
                  };
               td.onclick = function(){
                  var pole4 = this.tag.split("|||");
                  for (i=0; i < pole4.length - 1; i++) {
                     var pole5 = pole4[i].split("###");
                     if (document.getElementById(pole5[0])) document.getElementById(pole5[0]).value = pole5[2];
                     };
                  el.style.display = "none";
                  window.setTimeout("xform_input_obj.focus();", 100);
                  //spusteni dalsiho uzivatelskeho skriptu
						if (xform_onselect.length > 0) eval(xform_onselect);
                  window.setTimeout("xform_formular.onsubmit = function(){};", 100);  //povolit odeslani formulare
                  };
               };

            el.style.display = "block";  //zobrazit suggest


            //nastavit pozici pole
				var input_left = getLeftPos(xform_input_obj);
				var input_top = getTopPos(xform_input_obj);
				var novy_top = input_top + xform_input_obj.offsetHeight; 
				var novy_left = input_left; 
				if (xform_align.indexOf("top") != -1) {
					var novy_top = input_top - el.offsetHeight;
					};
				if (xform_align.indexOf("right") != -1) {
					var novy_left = input_left - el.offsetWidth + xform_input_obj.offsetWidth;
					};
				el.style.left = novy_left + "px";
				el.style.top = novy_top + "px";
				
            el.tag = -1;
            } else {
            el.style.display = "none";
            window.setTimeout("xform_formular.onsubmit = function(){};", 100);  //povolit odeslani formulare
            }
         }
         
      } else {
      el.style.display = "none";
      window.setTimeout("xform_formular.onsubmit = function(){};", 100);  //povolit odeslani formulare
      }

   };


function xautocomplete_key(suggest_id, xform_input_obj, e, xform_color, xform_onselect) {
   var klavesa = kod_klavesy(e);
   var el = document.getElementById(suggest_id);
   switch(klavesa) {
      case 40:
      if (el.style.display == "block") {
         xform_formular.onsubmit = function(){  //zabranit odeslani formulare
            return false;
            };
         if (el.tag == -1) {
            el.rows[0].style.backgroundColor = xform_color;
            el.tag = 0;
            } else {
            if (el.tag > -1) {
               //alert(el.tag);
               if (el.tag == el.rows.length - 1) {
                  el.rows[el.tag].style.backgroundColor = "";
                  el.rows[0].style.backgroundColor = xform_color;
                  el.tag = 0;
                  } else {
                  el.rows[el.tag].style.backgroundColor = "";
                  el.rows[el.tag + 1].style.backgroundColor = xform_color;
                  el.tag = el.tag + 1;
                  };
               };
            };
         };
      return;
      case 38:
      if (el.style.display == "block") {
         xform_formular.onsubmit = function(){  //zabranit odeslani formulare
            return false;
            };
         if (el.tag == -1) {
            el.rows[el.rows.length - 1].style.backgroundColor = xform_color;
            el.tag = el.rows.length - 1;
            } else {
            if (el.tag > -1) {
               //alert(el.tag);
               if (el.tag == 0) {
                  el.rows[el.tag].style.backgroundColor = "";
                  el.rows[el.rows.length - 1].style.backgroundColor = xform_color;
                  el.tag = el.rows.length - 1;
                  } else {
                  el.rows[el.tag].style.backgroundColor = "";
                  el.rows[el.tag - 1].style.backgroundColor = xform_color;
                  el.tag = el.tag - 1;
                  };
               };
            };
         };
      return;
      case 13:
      if (el.style.display == "block") {
         var pole4 = el.rows[el.tag].cells[0].tag.split("|||");
         for (i=0; i < pole4.length - 1; i++) {
            var pole5 = pole4[i].split("###");
            if (document.getElementById(pole5[0])) document.getElementById(pole5[0]).value = pole5[2];
            };
         el.style.display = "none";
         window.setTimeout("xform_input_obj.focus();", 100);
         //spusteni dalsiho uzivatelskeho skriptu
         if (xform_onselect.length > 0) eval(xform_onselect);
         window.setTimeout("xform_formular.onsubmit = function(){};", 100);  //povolit odeslani formulare
         };
      return;
      case 27:
      if (el.style.display == "block") {
         el.style.display = "none";
         window.setTimeout("xform_formular.onsubmit = function(){};", 100);  //povolit odeslani formulare
         };
      return;
      };
   };

function xautocomplete_blur(suggest_id) {
   window.setTimeout("document.getElementById('"+suggest_id+"').style.display = 'none'", 100);
   if (window.xform_formular !== undefined)
      window.setTimeout("xform_formular.onsubmit = function(){};", 100);  //povolit odeslani formulare
   };


