TableDriver = function(id)
{
  //public
  var a=5;
  this.id=id;
  this.table='';

  //private
  this.__row='';
  this.__last_cell=undefined;
  this.__cell_new_class='';

  //activate
  if(!this.is_obj)
    tbody = document.getElementById(this.id);
  else
    tbody = this.id

  if(typeof(tbody.getElementsByTagName("TBODY")[0])=='object')
    tbody=tbody.getElementsByTagName("TBODY")[0];
  this.table=tbody;

  //methods
  this.AddRow = function(id, after_id)
  {
    if(typeof(after_id)!="undefined")
    {
      var el=this.table.insertRow(0);
      this.__row=el;
    }
    else
    {
      this.__row = document.createElement("TR");
    }
    this.__row.id=this.id+'_'+id;
    re_c =/(\d+)/;
    re_c.exec(id);
    this.__row.record_id=RegExp.$1;
    return this.__row;
  }

  this.Cell = function(text, id, id_row)
  {
    row=this.__row;
    if(typeof(id_row)!='undefined')
      if(this.GetRowByID(id_row))
        row=this.GetRowByID(id_row);
    if(document.getElementById(''+id+row.record_id)==null)
    {
      var td = document.createElement("TD");
//      td.setAttribute("id", this.id+'_'+row.record_id+'_'+id);
      td.setAttribute("id", id);
      td.innerHTML=text;
      if(this.__cell_new_class!='')
        td.className=this.__cell_new_class;
      row.appendChild(td);
      this.__last_cell=td;
      return this.__last_cell;
    }
    else
    {
      td=document.getElementById(this.id+'_'+row.record_id+'_'+id);
      td.innerHTML=text;
      this.__last_cell=td;
    }
    return this.__last_cell;
  }

  this.ConfirmRow = function(id)
  {
    if(typeof(id)!="undefined" && id!="")
    {
      row=this.GetRowByID(id);
      if(row==false)
        return false;
      this.table.appendChild(row);
    }
    else
      this.table.appendChild(this.__row);
    return true;
  }

  this.ConfirmRowAfter = function(after_id,full)
  {
        full||false
//        alert(after_id+'  '+full+'  '+this.__row)
    var el=(!full)?this.GetRowByID(after_id):this.GetRowByID(after_id,"full")            //? ломается
//        this.GetRowByID(after_id)
    this.table.insertBefore(this.__row, el);
    this.table.insertBefore(el, this.__row);
  }

  this.ListRow = function()
  {
    return this.table.childNodes
    var arr = []
    for(var i in this.table.childNodes)
      if(this.table.childNodes[i].localName=="TR")
        arr[i]=this.table.childNodes[i]
    return arr
  }

  this.GetRowByID = function(id, type)
  {
    if(typeof(type)!="undefined" && type=="full")
      text_id=id
    else
      text_id=this.id+'_'+id

    if(typeof(document.getElementById(text_id))=='object')
      return document.getElementById(text_id);
    return false;
  }

  this.SetCellClass = function(class_name)
  {
    this.__cell_new_class=class_name;
  }

  this.SetCellAttr = function(attname, attvalue)
  {
    eval("this.__last_cell."+attname+"=\""+attvalue+"\";\n");
    return this.__last_cell;
  }

  this.FreeTable = function()
  {
    var arr=this.table.getElementsByTagName("TR")
    var len=arr.length;
    for(var i=0; i<len; i++)
    {
      this.table.removeChild(arr[0])
    }
  }
  this.RemoveRow = function(id)
  {
    var row
    if(row = document.getElementById(this.id+'_'+id))
    	return this.table.removeChild(row)
    else
     return false
  }


  this.insertFirst = function()
  {
        this.table.insertBefore(this.__row, this.table.firstChild);
  }
}
