var DynamicTable = Class.create(
{
	NumRows: 0,
	NumColumns: 0,
	Columns: null,
	Table: null,
	RowCounter: null,
	DataProvider: null,
	CustomColumns: null,
	TargetDiv: null,
	initialize: function() 
	{
		this.CustomColumns = new Array();
	},
	addNewRow: function()
	{
		this.addRow(this.DataProvider.length);
	},
	addRow: function(RowIndex)
	{
		var i;
		Row = document.createElement('tr');
		if( !this.DataProvider[RowIndex] )
		{
			this.createNewRow(RowIndex);
		}
		for(i=0; i<this.NumColumns; i++)
		{
			Row.appendChild( this.createColumn( this.DataProvider[RowIndex], this.Columns[i], RowIndex) );
		}
		this.Table.appendChild(Row);
		this.Table.style.visibility = 'visible';
		this.Table.style.display = 'block';
		this.RowCounter.value = this.DataProvider.length;
	},
	createNewRow: function(Index)
	{
		var i;
		this.DataProvider[Index] = new Object();
		for(i=0; i<this.NumColumns; i++)
		{
			this.DataProvider[Index][this.Columns[i]] = "";
		}
	},
	createColumn: function(Row, ColumnName, RowIndex)
	{
		var Column = document.createElement('td');
		var CellContent;
		if( this.CustomColumns[ColumnName] != null )
		{
			func = this.CustomColumns[ColumnName];
			CellContent = func(Row, ColumnName, RowIndex, this.TargetDiv);
		}
		else
		{
			CellContent = document.createTextNode(Row[ColumnName]);
		}
		Column.appendChild(CellContent);
		return Column;
	},
	build: function(Target) 
	{
		var i;
		this.TargetDiv = $(Target); 
		this.Table = document.createElement('tbody');
		this.RowCounter = document.createElement('input');
		this.RowCounter.type = "hidden";
		this.RowCounter.id = Target + "_RowCounter";
		this.RowCounter.name = Target + "_RowCounter";
		this.RowCounter.value = 0;
		this.NumRows = this.DataProvider.length;
		if( !this.NumRows )
		{
			alert("This is not an array");
			return null;
		}
		this.Columns = Object.keys(this.DataProvider[0]);
		this.NumColumns = this.Columns.length;
		for( i=0; i<this.NumRows; i++)
		{
			this.addRow(i);
		}
		var NewTable = document.createElement('table');
		NewTable.appendChild(this.Table);
		this.TargetDiv.appendChild(NewTable);
		this.TargetDiv.appendChild(this.RowCounter);
	}
});	
