function whisper(obj, e, table, field, fkey, focus, minChars)
{
  var key=e.keyCode;
  
  
  if ( (13==key) || (40==key)  || (38==key) )
  {
    return false;
  }
  
  //alert(focus);

  if (focus)
  {
    //alert('a');
//  document.body.removeChild(selectBox);

  }
//  obj.parentNode.parentNode.parentNode.onsubmit=insertValues;

  input=obj;
//  input.onblur="whisperHide();" ;
  //curtain.style.width=screen.width+'px';

  if (!selectBox)
  {
    valuesList[input]=document.createElement('div');
    valuesList[input].className='values-list';
    valuesListClear[input]=document.createElement('div');
    valuesListClear[input].style.clear='both';
    obj.parentNode.appendChild(valuesList[input]);
    obj.parentNode.appendChild(valuesListClear[input]);


  }
  else
  {
//    selectBox.style.display='block';
  }


  
//    values[obj.name]=new Array();

  if ( (focus) && (!blocked) )
  {
    blocked=true;
    
    selectBox=document.createElement('div');
    selectBox.values=new Array();
    selectBox.className='multireference-selectbox';
    //selectBox.onmouseout='selectBoxShow(0, true)';
    var position=findPos(obj);
    selectBox.style.left=(position[0])+'px';
    selectBox.style.top=(position[1]+20)+'px';
    document.body.appendChild(selectBox);
    input.value='';
  }
  

  if (obj.value.length<minChars) 
  {
    selectBox.values=new Array();
    selectBox.innerHTML='Začněte psát požadované slovo.<br />Napište alespoň '+minChars+' písmena a poté vyberte ze seznamu.';
    selectBox.active=false;
    return false;
  }
  
  
  if ( (undefined!=key) || (focus) )
  {

  selectBox.innerHTML='Načítám položky seznamu';

//alert('/ijarmark/ajax/lists.php?t='+table+'&f='+field+'&k='+fkey+'&v='+input.value);
  xmlhttpList=null;
  if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Mozilla, etc.
    xmlhttpList=new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {// code for IE5, IE6
    xmlhttpList=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlhttpList!=null)
  {
    xmlhttpList.onreadystatechange=onResponseList;
    
    xmlhttpList.open("GET",'/ajax/lists.php?t='+table+'&f='+field+'&k='+fkey+'&v='+input.value, true);
    xmlhttpList.send(null);
  }
  else
  {
    return false;
  }  
  }

  
}





function onResponseList()
{


    
//    input.value='1';
 //   input.text='aaa';


    //selectBox.options[selectBox.options.length]=option;


  if(xmlhttpList.readyState!=4) return;

  if(xmlhttpList.status!=200)
  {
    return false;
  }
  
selectBox.values=new Array();
//selectBox.values[selectBox.values.length]=new Array('- Vyberte -', 0);

  x=xmlhttpList.responseXML.documentElement.getElementsByTagName("ITEM");
  
  for (i=0; i<x.length; i++)
  {
    xx=x[i].getElementsByTagName("ID");
    var value=xx[0].firstChild.nodeValue;
    xx=x[i].getElementsByTagName("TEXT");
    var text=xx[0].firstChild.nodeValue;


    selectBox.values[selectBox.values.length]=new Array(text, value);
//    selectBox.options[selectBox.options.length]=new Option(text, value);
  }

    

  if ( ( (!selectBox.active) || (selectBox.active>x.length) ) && (x.length>0) )
  {
    selectBox.active=1;
  }
  

  
  selectBoxShow(selectBox.active, true);  

  blocked=false;

}






function selectBoxShow(offset, reload)
{
  if ( (offset==selectBox.active) && (!reload) )
  {
    return false;
  }
  
  var style;
  var header;
  selectBox.innerHTML='';
  for (i=0; i<selectBox.values.length; i++)
  {
    if (i+1==offset)
    {
      style=' selected';  
      selectBox.active=offset;
    }
    else
    {
      style='';
    }
    selectBox.innerHTML+='<div onmouseover="selectBoxShow('+(i+1)+', false);" onclick="whisperHide();" class="selectbox-value'+style+'" id="selectbox-value-'+i+'">'+selectBox.values[i][0]+'</div>';
  }
  selectBox.active=offset;
  selectBox.style.display='block';

  if (''==selectBox.innerHTML)
  {
    selectBox.style.display='none';
  }

}



function selectBoxSetValue(offset)
{
  if (values[input.name])
  {

  //input.onblur=null;
  //alert('a');
//  input.value=selectBox.values[offset][0];
  if (!offset)
  {
    return false;
  }
  
  selectBoxShow(0);
//  
  input.focus();
  whisperHide();
  if (!inArray(values[input.name], selectBox.values[offset-1]))
  {
    values[input.name][values[input.name].length]=selectBox.values[offset-1];
  }
  input.value='';



//  input.onblur=whisperHide;
  showValues();

  }
  
  else
  {
    if (selectBox.values[selectBox.active-1])
    {
     input.value=selectBox.values[selectBox.active-1][0];
    }
    if (selectBox)
    {
      selectBox.style.display='none';
    }
  }
  

}




function removeValue(offset, name)
{ 
  values[name]=removeFromArray(values[name], offset);
  input=document.getElementById(name);
  showValues();
}


function removeFromArray(array, index)
{
  var result=new Array();
  
  
  for (i=0; i<index; i++)
  {
    result[result.length]=array[i];
  }

  for (i=index; i<array.length-1; i++)
  {
    result[result.length]=array[i+1];
  }
  
  return result;
   
}


function inArray(array, value)
{
  var result=false;
  
  if ( (!value) || (!array) )
  {
    return true;
  }
  
  for (i=0; i<array.length; i++)
  {
    if (array[i])
    if (array[i][0]==value[0])
    {
      result=true;
    }
  }
  
  return result;
   
}



function checkWhisper(e)
{
  var key=e.keyCode;
  
  

  if (9==key)
  {
    selectBox.active=0;
  }


  if (13==key)
  {
    selectBoxSetValue(selectBox.active);
    return false;
  }

  if (40==key)
  {
    if (selectBox.active<=(selectBox.values.length-1))
    {
      selectBox.active++;
    }
    selectBoxShow(selectBox.active, true);
    selectBox.scrollTop=(selectBox.active-1)*18;
  }

  if (38==key)
  {
    if (selectBox.active>1)
    {
      selectBox.active--;
    }
    selectBoxShow(selectBox.active, true);
    selectBox.scrollTop=(selectBox.active-1)*18;
  }

  

}



function whisperHide()
{

  if (values[input.name])
  {

  if ( (selectBox.active) )
  {
    if ( (!inArray(values[input.name], selectBox.values[selectBox.active-1])) && (values) )
    {
      values[input.name][values[input.name].length]=selectBox.values[selectBox.active-1];
    }
  }



//  input.value=selectBox.values[selectBox.active][0];
  input.value='';
  if (selectBox)
  {
    selectBox.style.display='none';
  }
  
  showValues();
  }
  
  else
  {
    if (selectBox.values[selectBox.active-1])
    {
     input.value=selectBox.values[selectBox.active-1][0];
    }
    if (selectBox)
    {
      selectBox.style.display='none';
    }
  }
  
}



function agetMrValues(id, name, table, field, key, mrtable, mrkey, mrfkey)
{
  
  setTimeout('getMraValues(\''+id+'\', \''+name+'\', \''+table+'\', \''+field+'\', \''+ key+'\', \''+mrtable+'\', \''+mrkey+'\', \''+mrfkey+'\')', 100);
  return true;
}





function getMrValues(i)
{

  if (inputs[i])  
  {
  
  inc=i;
  
  var id=inputs[i][0];
  var name=inputs[i][1];
  var table=inputs[i][2];
  var field=inputs[i][3];
  var key=inputs[i][4];
  var mrtable=inputs[i][5];
  var mrkey=inputs[i][6];
  var mrfkey=inputs[i][7];
  
  //alert(name);
  obj=document.getElementById(name);
  input=obj;
  input.value='';
  if (!valuesList[input.name])
  {
    valuesList[input.name]=document.createElement('div');
    valuesList[input.name].className='values-list';
    valuesListClear[input.name]=document.createElement('div');
    valuesListClear[input.name].style.clear='both';
    selectBox=document.createElement('div');
    selectBox.style.display='none';
    selectBox.values=new Array();
    values[input.name]=new Array();
    selectBox.className='multireference-selectbox';
    var position=findPos(obj);
    selectBox.style.left=(position[0])+'px';
    selectBox.style.top=(position[1]+20)+'px';
    obj.parentNode.appendChild(valuesList[input.name]);
    obj.parentNode.appendChild(valuesListClear[input.name]);
    document.body.appendChild(selectBox);
  }



  if ( (id) || (forced[input.name]) )
  {  

  xmlhttpList=new Array();
  xmlhttpList=null;
  if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Mozilla, etc.
    xmlhttpList=new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {// code for IE5, IE6
    xmlhttpList=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlhttpList!=null)
  {
    xmlhttpList.onreadystatechange=onResponseListInit;
    if (id)
    {
      xmlhttpList.open("GET",'/ajax/lists.php?t='+table+'&f='+field+'&k='+key+'&kv='+id+'&mrt='+mrtable+'&mrk='+mrkey+'&mrfk='+mrfkey, true);
    }
    else
    {
      xmlhttpList.open("GET",'/ajax/lists.php?t='+table+'&f='+field+'&k='+key+'&forced='+forced[input.name], true);
    }
    xmlhttpList.send(null);
  }
  else
  {
    return false;
  }  

  }
  else
  {
  getMrValues(inc+1);
  }
  
  
  }

  
}


function onResponseListInit()
{



  if(xmlhttpList.readyState!=4) return;

  if(xmlhttpList.status!=200)
  {
    return false;
  }

//alert(input.name);
  x=xmlhttpList.responseXML.documentElement.getElementsByTagName("ITEM");
  
  for (i=0; i<x.length; i++)
  {
    xx=x[i].getElementsByTagName("ID");
    var value=xx[0].firstChild.nodeValue;
    xx=x[i].getElementsByTagName("TEXT");
    var text=xx[0].firstChild.nodeValue;

    var item=new Array(text, value);
    if (!inArray(values[input.name], item))
    {
      values[input.name][values[input.name].length]=item;
    }


  }



  showValues();
  getMrValues(inc+1);

}





function showValues()
{
 if (valuesList[input.name])
 {

  valuesList[input.name].innerHTML='';
  for (i=0; i<values[input.name].length; i++)
  {
    if (values[input.name][i])
    {
      valuesList[input.name].innerHTML+='<div class="values-list-item">'+values[input.name][i][0]+'<span onclick="removeValue('+i+', \''+input.name+'\');">x</span><input type="hidden" name="'+input.name+'['+i+']" value="'+values[input.name][i][1]+'" /></div>';
    }
  }
  
}
}




var selectBox;
var list;
var values=new Array();
var valuesList=new Array();
var valuesListClear=new Array();
var input;
var inputs=new Array();
var ok=false;
var inc=0;
var blocked=false;
var forced=new Array();