function SF_ChildGridController(sname, smode){
 
 this.name = sname;
 this.mode = smode;
 this.activeRow=0;
 this.maxRowNum=1;
 this.rowCount=1;
 this.lastAutoNumber=-1;
 
 this.columns=new Array();
 
 this.addColumn = function(colname, stype, collabel, obligatory, autoinc)
 {
  var col = new ChildColumn(colname, stype);
  col.label=collabel;
  col.obligatory=obligatory;
  col.autoincrement=autoinc;
  this.columns.push(col);
 }
 
 this.setActiveRow=function(rownum)
 {
  this.activeRow=rownum;
 }
 
 this.checkRow=function(rownum)
 {
  //alert('check'+ rownum.toString());
  var retval = true;
  var c, col;
  for(c=0;c<this.columns.length;c++)
  {
   col = this.columns[c];
   if(this.columns[c].obligatory && col.type.search(/radio/g)==-1 && col.type!="checkbox")
   {
    //alert('check ctrl ' + col.name + "_" + rownum.toString());
    //alert(document.getElementById(col.name + "_" + rownum.toString()).value);
    if(document.getElementById(col.name + "_" + rownum.toString()).value=="")
     retval=false;
   }
  }
  //alert(retval.toString());
  return retval;
 }
 
 this.addRowOld = function() //Deprecated
 {
    document.getElementById( this.name + "_tb").insertRow(document.getElementById(this.name + "_tb").rows.length);
    var len1=document.getElementById(this.name + "_tb").rows.length-1;
    var r1 = document.getElementById(this.name + "_tb").rows[len1];
    var r2 = document.getElementById(this.name + "_tb").rows[len1 - 1]; 
    var c;
    for(c=0;c < r2.cells.length; c++)
    {
      var newcontent = r2.cells[c].innerHTML;
      newcontent = newcontent.replace(/_[\d]+/g, '_' + (len1-1).toString());
      if(newcontent.search(/hidden/g))
      {
       newcontent = newcontent.replace(/value=[\d]/g, 'value=' + (len1+1).toString());
       newcontent = newcontent.replace(/^[\d]/, (len1+1).toString());
      }
       
      r1.insertCell(c);  
      r1.cells[c].innerHTML=newcontent;
      r1.cells[c].style.borderTop= '0px solid';
      r1.cells[c].style.borderLeft= r2.cells[c].style.borderLeft;
      r1.cells[c].style.borderRight= r2.cells[c].style.borderRight;
      r1.cells[c].style.borderBottom= r2.cells[c].style.borderBottom;
      
      var fchild=r1.cells[c].firstChild;
      
      if(fchild.nodeName.toLowerCase()=='input' && (fchild.getAttribute('TYPE')=='text' || fchild.getAttribute('TYPE')=='password'))
        fchild.value='';
      if(fchild.nodeName.toLowerCase()=='input' && fchild.getAttribute('TYPE')=='checkbox')
        fchild.checked=false; 
      else if(fchild.nodeName.toLowerCase()=='select')
        fchild.selectedIndex=0;
      this.rowCount++;
      if(this.maxRowNum<this.rowCount)
       this.maxRowNum=this.rowCoun;
  }
 }
 
 this.enableRow = function(rownum)
 {
  //alert('enable' + rownum.toString());
  var i,c;
  for(i=0;i<this.maxRowNum;i++)
  {
   for(c=0;c<this.columns.length;c++)
   {
    if(this.columns[c].autoincrement)
     continue;
    if(i<=rownum)
     document.getElementById(this.columns[c].name + "_" + i.toString()).disabled=false;
    else
     document.getElementById(this.columns[c].name + "_" + i.toString()).disabled=true; 
   }
  }
 }
 
 this.setFocus = function(rownum)
 {
  //alert('focus' + rownum.toString());
  var c;
  for(c=0;c<this.columns.length;c++)
  {
    if(this.columns[c].autoincrement)
     continue;
    else
    {
     document.getElementById(this.columns[c].name + "_" + rownum.toString()).focus();
     break;
    }
     
  }
  this.activeRow=rownum;  
 }
 
 this.setAutoNumber = function(rownum)
 {
  //alert('autonum' + rownum.toString());
  var c;
  for(c=0;c<this.columns.length;c++)
  {
    if(this.columns[c].autoincrement)
    {
     var newcontent = document.getElementById(this.name + "_tb").rows[rownum-1].cells[c].innerHTML;
     //alert(newcontent);
     document.getElementById(this.columns[c].name + "_" + rownum.toString()).value=(rownum+1).toString(); 
     //alert(document.getElementById(this.columns[c].name + "_" + rownum.toString()).value);
     //alert(this.columns[c].name + "_" + rownum.toString());
     document.getElementById(this.name + "_tb").rows[rownum].cells[c].innerHTML = (rownum+1).toString() + ' ' + document.getElementById(this.name + "_tb").rows[rownum].cells[c].innerHTML;
     //alert(document.getElementById(this.name + "_tb").rows[rownum].cells[c].innerHTML);
    }
     
  }
 }
 
 this.addRow = function()
 {
    //alert(this.rowCount.toString());
    document.getElementById( this.name + "_tb").insertRow(this.rowCount);
    var len1=this.rowCount;
    var r1 = document.getElementById(this.name + "_tb").rows[len1];
    var r2 = document.getElementById(this.name + "_tb").rows[len1 - 1]; 
    var c;
    for(c=0;c < r2.cells.length; c++)
    {
      var newcontent = r2.cells[c].innerHTML;
      newcontent = newcontent.replace(/_[\d]+/g, '_' + (len1).toString());
      newcontent = newcontent.replace(/ActiveRow\([\d]+\)/g, 'ActiveRow(' + (len1).toString() + ')');
      if(newcontent.search(/hidden/g)!=-1)
      {
       var autonum = len1+1;
       if(this.lastAutoNumber>=0)
        autonum = ++this.lastAutoNumber;
      
       if(navigator.appName == 'Microsoft Internet Explorer')
       {
        newcontent = newcontent.replace(/value=[\d]+/g, 'value=' + autonum.toString());
        newcontent = newcontent.replace(/^[\d]+/, autonum.toString());
       }
       else
       {
        newcontent = newcontent.replace(/value=\"[\d]+\"/g, "value=\"" + autonum.toString() + "\"");
        newcontent = newcontent.replace(/[\d]+/, autonum.toString());
       }
      }
       
      r1.insertCell(c);  
      r1.cells[c].innerHTML=newcontent;
      //alert(r1.cells[c].innerHTML);
      r1.cells[c].style.borderTop= '0px solid';
      r1.cells[c].style.borderLeft= r2.cells[c].style.borderLeft;
      r1.cells[c].style.borderRight= r2.cells[c].style.borderRight;
      r1.cells[c].style.borderBottom= r2.cells[c].style.borderBottom;
      r1.cells[c].style.backgroundColor= r2.cells[c].style.backgroundColor;
      
      var fchild=r1.cells[c].firstChild;
      /*if(fchild.nodeName.toLowerCase() == "#text")
      {
       fchild=r1.cells[c].lastChild;
      }*/
       
      //alert(fchild.nodeName.toLowerCase());
      if(fchild.nodeName.toLowerCase()=='input' && (fchild.getAttribute('TYPE')=='text' || fchild.getAttribute('TYPE')=='password'))
        fchild.value='';
      if(fchild.nodeName.toLowerCase()=='input' && fchild.getAttribute('TYPE')=='checkbox')
        fchild.checked=false; 
      else if(fchild.nodeName.toLowerCase()=='select')
        fchild.selectedIndex=0;
        
      
    
  }
      this.rowCount++;
      //alert(this.rowCount.toString());
      if(this.maxRowNum<this.rowCount)
       this.maxRowNum=this.rowCount;
 }
 
 this.inputAction=function(action)
 {
 // alert('inputaction=' + action + this.activeRow.toString());
  if(!this.checkRow(this.activeRow))
  {
   //alert('failed');
   return false;
  }
   
  if(action=="add")
  {
   this.addRow();
   this.setFocus(this.rowCount-1);
  // this.setAutoNumber(this.rowCount-1);
  }
  else if(action=="move")
  {
   if(this.mode=='insert')
    this.enableRow(this.activeRow + 1);
   this.setFocus(this.activeRow + 1);
   if(this.mode=='insert'){
    this.setAutoNumber(this.activeRow);
    this.rowCount++;
   }
   
  }
  //return false;
 }
 
 this.keyPress = function(ev, lastcell)
 {
  var kcode=0;
  if(navigator.appName == 'Microsoft Internet Explorer')
   kcode=window.event.keyCode;
  else
   kcode=ev.keyCode; 

   if(kcode==13 || (lastcell && kcode==9))
   {
    if(this.activeRow==this.maxRowNum-1)
     this.inputAction("add");
    else
     this.inputAction("move");
    return false; 
   }
   else
    return true;
 
 }
 
 return this;
}

function ChildColumn(sname,stype)
{
 this.name=sname;
 this.type=stype;
 this.label="";
 this.obligatory="";
 this.autoincrement=false;
 return this;
}


