﻿var TableBuilder = function()
{
    this.columns = [{'Name' : '' , 'Field' : '', 'Style' : ''}];
    this.dataSource = null;
    this.table =document.createElement('table');
    this.dataRowClassName = '';
    this.spaceRowClassName = '';
    this.HeadCellClassName = '';
    this.CellClassName = '';
    this.Sorter = null;
    
    this.Build = function()
    {
        this.InitDataRegion();
        this.InitHead (this.Sorter);
    }
    
    this.InitHead = function(sorter)
    {
        var head = this.table.insertRow(0);
        var cell = null;
        
        for (var i = 0; i < this.columns.length; i++)
        {
            cell =  head.insertCell(i);
            cell.className = this.HeadCellClassName;
            
            if (this.columns[i].Sort == true && this.Sorter != null)
            {
                var f = this.columns[i].SortExpression;
                var sa = document.createElement('a');
                sa.href = f;
                
                sa.onclick = function()
                {
                    sorter(this.href);
                    return false;
                }
                
                sa.innerHTML = this.columns[i].Name; 
                
                cell.appendChild(sa);
            }
            else
            {
                cell.innerHTML = this.columns[i].Name; 
            }
            
            if (this.columns[i].Visible == false)
            {
                cell.style.display = 'none';
            }
            
            if (this.columns[i].Width != undefined && this.columns[i].Width != null && this.columns[i].Width != '')
            {
                cell.style.width = this.columns[i].Width;
            }
        }
    }
    
    this.InitDataRegion = function()
    {
        var row;
        
        for (var i = 0; i < this.dataSource.length; i++)
        {
            row = this.table.insertRow(i);
            
            if (i % 2 == 0)
            {
                row.className = this.dataRowClassName;
            }
            else
            {
                row.className = this.spaceRowClassName;
            }
            
            this.InitDataRow(row,this.dataSource[i]);
        }
    }
    
    this.InitDataRow = function(row,data)
    {
        var cell = null;
        
        for (var i = 0; i < this.columns.length; i++)
        {
            cell =  row.insertCell(i);
            cell.className = this.CellClassName;
            
            this.InitCell(cell, data, this.columns[i]);
        }
    }
    
    this.InitCell = function(cell, data, cellData)
    {
        if (cellData.Type == undefined || cellData.Type == 'Text')
        {
            this.InitTextCell(cell, data, cellData);
        }
        else if (cellData.Type == 'RadioButton')
        {
            this.InitRadioButtonCell(cell, data, cellData);
        }
    }
    
    this.InitTextCell = function(cell, data, cellData)
    {
        var value = data[cellData.Field];
            
        if (cellData.Render != undefined)
        {
            cell.innerHTML = cellData.Render(data,value);
        }
        else
        {
            cell.innerHTML = value;
        }

        if (cellData.Visible == false)
        {
            cell.style.display = 'none';
        }
        
        if (cell.innerHTML == null || cell.innerHTML == '')
        {
            cell.innerHTML = '&nbsp;';
        }
    }
    
    this.InitRadioButtonCell = function(cell, data, cellData)
    {
        var rd = null;
        
        if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
        {
            var isChecked = data[cellData.Attribute.ValueField] == cellData.Attribute.SelectedValue;
            var elestring = '';
            
            if (isChecked)
            {
                elestring = '<input name = "' + cellData.Attribute.Name + '" checked = "checked" />';
            }
            else
            {
                elestring = '<input name = "' + cellData.Attribute.Name + '" />';
            }
            
            rd = document.createElement(elestring);
            
            rd.type = 'radio';
            rd.value = data[cellData.Attribute.ValueField];
        }
        else
        {
            rd = document.createElement('input');
            
            rd.setAttribute('type','radio');
            rd.setAttribute('name',cellData.Attribute.Name);
            rd.setAttribute('value',data[cellData.Attribute.ValueField]);
            rd.setAttribute('checked','checked');
        }
       
        if (rd != null)
        {
            rd.onclick = function()
            {
                cellData.Attribute.Click(data,this.value);
            }
        }
        
        cell.appendChild(rd);
    }
}
