/* Moje js rozsireni - vyzaduje jQuery + selectboxes plugin */

/* Kontrola povinnych polozek ve formulari */
function kontrola_povinnych(povinne) {
  var first = null;
  for(var i=0;i<povinne.length;i++) {
    var element = document.getElementById(povinne[i]);
    if (element.type == 'text')
      element.style.background = element.value==''?'#ff8':'white';
    if (first==null && element.value=='')
      first = element;
  }
  if (first!=null) {
      first.focus();
      alert('Nebyla vyplněna některá povinná položka! ['+first.name+']');
      return false;
  }
  return true;
}

/* Zakaze nebo povoli pole elementu (el je pole id-cek) */
function setDisable(el,disable) {
  for(var i=0;i<el.length;i++)
    if (disable)
      $("#"+el[i]).attr("disabled","disabled");
    else
      $("#"+el[i]).removeAttr("disabled");
  return true;
}

function vyber_vse(sel) {
  var s = document.getElementById(sel);
  for(var i=0;i<s.childNodes.length;i++) {
    s.childNodes[i].selected = true;
  }
  return true;
}

/* Vrati kod stisknute klavesy */
/* Pouze pro onkeydown a onkeyup */
/* Vraci vzdy keyCode, nikdy charCode */
function GetKeyCode(e) {
  if (e) {
    return e.keyCode;
  }
  else {
    return window.event.keyCode;
  }
} 

function suggest(id, il, hid) {

  this.id  = id;     // id multiple selectu
  this.il  = il;     // id input line
  this.hid = hid;    // skryte id odesilane formularem
  this.last = '';

  this.Go = function(e, url) {
    var unicode = GetKeyCode(e);
    var inp = $("#"+this.il).val();
    var id = this.id;
    if (unicode != 38 && unicode != 40 && unicode != 13 && inp != this.last){
      this.last = inp;
      if (inp != '') {
        if (xml) {
          if (xml.readyState != 0) xml.abort();
          xml.open ("GET", url, true);
          xml.onreadystatechange = function(){
            if (xml.readyState == 4 && xml.responseText){
              eval(xml.responseText);
              $("#"+id).show();
            }
          }
          xml.send(null);
        }
      } else {
        $("#"+this.id).hide();
      }
    }
  }

  this.Done = function(results) {
    $("#"+this.id).removeOption(/./);
    for (var i=0; i<results.length; i++){
      var el = document.getElementById(this.id);
      var opt = document.createElement("option");
      opt.text = results[i]["text"];
      opt.value = results[i]["value"];
      if (results[i]["color"]!=undefined)
        opt.style.color = results[i]["color"];
      try {
        el.add(opt,null);
      }
      catch (ex) {
        el.add(opt);
      }
    }
  }

  this.getChangeHandler = function() {
    var select = document.getElementById(this.id);
    var nazev = select.options[select.selectedIndex].innerHTML;
    var hodnota = select.options[select.selectedIndex].value;
    $("#"+this.il).attr('value', nazev.replace(/\&amp;/g,'&'));
    $("#"+this.hid).attr('value', hodnota);
  }

  this.getResultClickHandler = function() {
    this.getChangeHandler();
    $("#"+this.id).hide();
  }

  this.Move = function(e) {
    var unicode = GetKeyCode(e);
    var naseptavac = document.getElementById(this.id);
    if (unicode == 40) {
      // šipka dolů
      naseptavac.options.selectedIndex =
        naseptavac.options.selectedIndex >= 0 &&
        naseptavac.options.selectedIndex < naseptavac.options.length-1 ?
        naseptavac.options.selectedIndex+1 : 0;
      this.getChangeHandler();
      return;
    }
    else if (unicode == 38) {
      // šipka nahoru
      naseptavac.options.selectedIndex =
        naseptavac.options.selectedIndex > 0 ?
        naseptavac.options.selectedIndex-1 : naseptavac.options.length-1;
      this.getChangeHandler();
      return;
    }
    else if (unicode == 13) {
      // na enter ve textovém poli nechceme odesílat formulář
      if (window.event)
        e.returnValue = false;
      else
        e.preventDefault();
      $("#"+this.id).hide();
    }
  }

}
