
/**
 * tblName    - the name of the table
 * rowsOffset - the first rows which are excluded
 * filterName - the id/varname of the filter input text 
 */
function TableFilter (tblName, rowsOffset, filterName) {

  this.init                   = init;
  this.KeyCheckTimesheet      = KeyCheckTimesheet;
  this.update                 = update;
  this.doHideTimesheet        = doHideTimesheet;
  this.doFilterTimesheet      = doFilterTimesheet;
  this.doTimesheetFiltering   = doTimesheetFiltering;
  this.associateTableText = associateTableText;
  this.saveTableText      = saveTableText;

  this.rowsOffset             = rowsOffset;
  this.tableName              = tblName;
  this.filterBoxName          = filterName;

}

function init () {
  this.rowsText = new Array();
  this.cnt = 0;      


  this.filterBox = document.getElementById (this.filterBoxName);
  if (this.filterBox == null) return;


  this.effortTable = document.getElementById (this.tableName);

	
  this.associateTableText (this.effortTable, this.rowsOffset);

  this.initedTimesheet = true;
  this.filterBox.focus();	

  this.doTimesheetFiltering();
}

function KeyCheckTimesheet(e) {
  update();
}

function update() {

  if(!this.initedTimesheet) {
    this.init();
  }
  this.doTimesheetFiltering();

}


function doHideTimesheet() {
  for (var row in this.rowsText) {
    hideRow(this.rowsText[row]);
  }
}


var prvSearchTxt = '';
var curSearchTxt = '';

function doFilterTimesheet(str) {  

  //alert (str); 

  var vec = str.split (" ");

  //alert (vec);

  var hh = document.getElementById ("hide_headers_on_refine"); 

  for (var row in this.rowsText) {

    var fl = true;

    var  tr = this.rowsText[row];
    var tds = tr.getElementsByTagName("td");
    var s = tds[0].innerHTML;

    if (s=="") { 
      fl = ! hh.checked;    
    }else {
      for (i=0; i<vec.length; i++) {
        if (row.indexOf(vec[i]) < 0) {
          fl = false;
        }
      }

   }

   if (fl) {
      //tr = this.rowsText[row];
      showRow(tr);
   }

  }


  /*
  for (var row in this.rowsText) {
    if (row.indexOf(str) >= 0) {
      tr = this.rowsText[row];
      showRow(tr);
    }
  }
  */

  Set_Cookie('filterTimesheet',str,100);
}

function doTimesheetFiltering(){
  if (this.filterBox == null) return;

  var _str = this.filterBox.value;
  var str = _str.toLowerCase();

  curSearchTxt = str;
  
  prvSearchTxt = curSearchTxt;
  
  //alert (prvSearchTxt+':'+curSearchTxt);
	
  if(str.length < 2) {
    for (var text in this.rowsText) {
      showRow(this.rowsText[text]);
    }	
    Set_Cookie('filterTimesheet',str,100);
    //alert ("nu mai am filtru: "+prvSearchTxt);
    return;
  }
	
  this.doHideTimesheet();


  this.doFilterTimesheet(str);
}



function associateTableText(table, rowOffset) {


  var trs = table.getElementsByTagName("tr");

  for(var i = rowOffset; i <= trs.length-1; i++) {
    var row = trs[i];
    this.saveTableText(trs, i);
  }
}

function saveTableText (rows, i) {


  var tr = rows[i];
  txt = getIndexText(rows, i);
 
  txt = txt.toLowerCase();
  txt+=" " + this.cnt++;
  this.rowsText[txt] = tr;
}

function getIndexText (rows, i) {
    var tr = rows[i];
    var tds = tr.getElementsByTagName("td");
    var s = tds[0].innerHTML;

    var hh = document.getElementById ("hide_headers_on_refine"); 

    //hh = true;

    if (s=="" && !hh.checked) { 
      return "";
    }

    var txt = "";

    txt += " " + tds[1].innerHTML;
    txt += " " + tds[2].innerHTML;

    return txt;
}


