﻿function $on(id)
{
  var c   = $(id );
  if(c && c.style) c.style.display = 'block';
}

function $off(id)
{
  var c   = $(id );
  if(c && c.style) c.style.display = 'none';
}


var Controls = 
{
   
    Component : function (Template)
    {
        var build = 
        {
            Array    : Template.innerHTML.split('?'),
            New      : function()
            {
               var H = "";
             
               for(var i = 0 ; i < this.Array.length ; i++)
               {
                 H += this.Array[i];
                 if(i < arguments.length) H+= arguments[i]; 
               }
               return H;
           } 
        }
     
        Template.parentNode.removeChild(Template);
        return build; 
    },
    
    Table : function(table)
    {
     
       table.Order =0 ;  
       
       table.AddColumns = function()
       {
            if(!this.controls) this.controls = new Array(); 
            for(var i = 0 , l = this.controls.length; i < arguments.length ;i++) this.controls[l++] =  arguments[i];
       }
       
       table.Add = function()
       {
           var    id = this.rows.length - 1 , tr = this.insertRow(id);
           tr.id     = this.id + '.' + (++this.Order);
           
           this.setColor(tr);
           
           for(var i = 0 , input , td  ; i < this.controls.length ; i++ ) 
           {
              (td=tr.insertCell(i)).id = tr.id + '.' + (input=this.controls[i]).id;
               td.innerHTML = input.value;
           }
           
           if (this.DeleteRow) tr.insertCell(i).innerHTML = this.DeleteRow.New(this.Order);
       };
       
       table.setColor = function(tr)
       {
          if(this.bgColors)
          {
             if(this.tc >= this.bgColors.length) this.tc = 0;
             tr.bgColor  = this.bgColors[this.tc]           ;
             this.tc++                                      ; 
          }
       },  
      
       table.Rendering = function()
       {
          
          if(arguments && arguments.length)
          {
            this.bgColors = arguments;
            this.tc       = 0        ;  
          }
       
          
          for(var i = 0 , tr , id   ; i < this.rows.length ; i++ )  if((tr = this.rows[i]).id && (id = tr.id.split('.')) && id.length && id[0] == this.id)
          {
             this.setColor(tr);
             if(this.DeleteRow && this.controls.length)
             {
                var td; if(tr.cells.length > this.controls.length) td = tr.cells[tr.cells.length - 1]; else  td = tr.insertCell(tr.cells.length);
                td.innerHTML = this.DeleteRow.New(id[1]); 
             }
          } 
         
       }, 
      
       table.Delete = function(id)
       {
          this.deleteRow ($(this.id + '.' + id).rowIndex); 
          if(this.bgColors) for(var i = this.tc = 0 , tr ; i<this.rows.length ;i++) if((tr = this.rows[i]).id &&  tr.id.indexOf(this.id)>-1) this.setColor(this.rows[i]);
       };
       
       
       table.AddResponseState = function(Container)
       {
          
          for(var i = 0 ; i < this.rows.length; i++) 
          {
             var tr = this.rows[i];
             if (tr.id && tr.id.indexOf(this.id)>-1) for(var j = 0 , td ; j < tr.cells.length ; j++) if ((td=tr.cells[j]).id && td.id.indexOf(this.id)>-1)
             {
               var input   = document.createElement('INPUT');
               input.type  = 'hidden'                       ;
               input.name  = td.id                          ;
               input.value = td.innerHTML                   ;
               Container.appendChild(input)                 ; 
             }
          } 
           
       };
       
       
    },
    
    Form : function(frm)
    {
           frm.method   = "post"     ;

           frm.onsubmit = function() {
               
               var nodes = frm.ownerDocument.getElementsByTagName('*');
               for (var i = 0, c; i < nodes.length; i++) if ((c = nodes[i]).AddResponseState) c.AddResponseState(frm);
           }
                
    } 
    
    
};