function filtr (a_cfg, a_tpl) 
{
    // register in global collections
	if (!window.A_FILTRS)
	    window.A_FILTRS = [];
	if (!window.A_FILTRSIDX)
	    window.A_FILTRSIDX = [];
	this.s_id = a_cfg.id ? a_cfg.id : A_FILTRS.length;
	window.A_FILTRS[this.s_id] = this;
	window.A_FILTRSIDX[window.A_FILTRSIDX.length] = this;
	 
    this.filtruj = filtruj;

    this.a_cfg = a_cfg;
    this.a_tpl = a_tpl;
    
	// zapracovani aktivniho automatickeho filtru
	automatic_active = this.a_cfg.automatic_active;
	enter_active = this.a_cfg.enter_active;
	output_html = this.a_cfg.output_html;
	if(automatic_active == true)
	{
		if(output_html == true)
		{
            html = '<input type="text" id="text_'+this.s_id+'" text="" onkeypress="return(enterStop(event));" onkeyup="A_FILTRS[\'' + this.s_id + '\'].filtruj();">';
		}
		else
		{
	        document.write('<input type="text" id="text_'+this.s_id+'" text="" onkeypress="return(enterStop(event));" onkeyup="A_FILTRS[\'' + this.s_id + '\'].filtruj();">');
        }
	}
	else if(enter_active == true)
	{
		if(output_html == true)
		{
            html = '<input type="text" id="text_'+this.s_id+'" text="" onkeypress="return(enterStop(event));" onkeyup="if(event.keyCode == 13){A_FILTRS[\'' + this.s_id + '\'].filtruj()}">';
		}
		else
		{
            document.write('<input type="text" id="text_'+this.s_id+'" text="" onkeypress="return(enterStop(event));" onkeyup="if(event.keyCode == 13){A_FILTRS[\'' + this.s_id + '\'].filtruj()}">');
		}
	}
    else
    {
		if(output_html == true)
		{
            html = '<input type="text" id="text_'+this.s_id+'" text="" onkeypress="return(enterStop(event));">';
		}
		else
		{
            document.write('<input type="text" id="text_'+this.s_id+'" text="" onkeypress="return(enterStop(event));">');
		}
    }
	if(output_html == true)
	{
        html += '<input type="button" value="Filtr" onclick="A_FILTRS[\'' + this.s_id + '\'].filtruj();">';
    	return html;
	}
	else
	{
        document.write('<input type="button" value="Filtr" onclick="A_FILTRS[\'' + this.s_id + '\'].filtruj();">');
	}
}

function filtruj()
{
	filtrText = document.getElementById('text_'+this.s_id).value;
	filtrText = filtrText.toLowerCase();
	divName = this.a_cfg.divname;
    objDiv = document.getElementById(divName);
	objDivChildren = objDiv.children;
	for(x = 0; x < objDivChildren.length; x++)
	{
        child = objDivChildren[x];
        if(child.tagName == 'TABLE')
        {
            objTable = child;
        } 
    }
    tableData = new Array();
    tableDataRow = objTable.rows.length;
    for(rowNbr = 0; rowNbr < objTable.rows.length; rowNbr++)
    {
        row = objTable.rows[rowNbr];
        rowData = new Array();
        tableDataCol = row.cells.length;
        for(cellNbr = 0; cellNbr < row.cells.length; cellNbr++)
        {
            rowData[cellNbr] = row.cells[cellNbr].innerHTML;
        }
        tableData[rowNbr] = rowData; 
    }
    j = 0;
    for(row = 1; row < tableDataRow; row++)
    {
      show = 0;
      for(col = 0; col < tableDataCol; col++)
      {
          value = tableData[row][col];
          value = value.toLowerCase();
          if(value.search(filtrText) > -1)
          {
              show = 1;
          }
      }
      if(show == 0)
      {
          objTable.rows[row].style.display = 'none';
      }
      else
      {
          objTable.rows[row].style.display = '';
          j = j + 1;
      }
    }
    //FF
    if(objDiv.firstChild.textContent && objDiv.firstChild.textContent.search('záznam') > -1)
    {
    	if(j == 1)
    	    objDiv.firstChild.textContent = "\n"+j+" odfiltrovaný záznam z "+(tableDataRow - 1);
    	else if(j > 1 && j < 5) 
    	    objDiv.firstChild.textContent = "\n"+j+" odfiltrované záznamy z "+(tableDataRow - 1);
    	else
    	    objDiv.firstChild.textContent = "\n"+j+" odfiltrovaných záznamů z "+(tableDataRow - 1);
    }
    //IE
    else if(objDiv.firstChild.nodeName == '#text' && objDiv.firstChild.nodeValue.search('záznam') > -1)
    {
    	if(j == 1)
    	    objDiv.firstChild.nodeValue = "\n"+j+" odfiltrovaný záznam z "+(tableDataRow - 1);
    	else if(j > 1 && j < 5) 
    	    objDiv.firstChild.nodeValue = "\n"+j+" odfiltrované záznamy z "+(tableDataRow - 1);
    	else
    	    objDiv.firstChild.nodeValue = "\n"+j+" odfiltrovaných záznamů z "+(tableDataRow - 1);
    }
}

function addEvent(obj, event, funct) 
{  
    if (obj.attachEvent) 
    { //IE  
	    obj['e' + event + funct] = funct;  
	    obj['x' + event + funct] = function() 
	    {  
	        obj['e' + event + funct](window.event);  
	    }  
	    obj.attachEvent('on' + event, obj['x' + event + funct]);  
	}
    else // other browser
    {
	    obj.addEventListener(event, funct, false);
    }
} 

function enterStop(event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode == 13)
	{
		return false;
	}
}