

function ViewModel( table)
{
	this.table = table;
	this.rowIndex = null;
	this.colCount = null;
	this.setDataCb = null;
	this.subHeaderCb = null;
	
	
	//functions
//	this.setDataModel = setDataModelImp;
	this.setItem = setItemImp;
	this.setCallbacks = setVmCallbacksImp;
	this.init = initVmImp;
	this.headerCb = null;
	this.finalize = finalizeImp;
	this.setColCount = setColCountImp;
}

function setVmCallbacksImp( setData, header, subHeader)
{	
	this.setDataCb = setData;
	this.subHeaderCb = subHeader;
	this.headerCb = header;
}

function setDataModelImp()
{
}

function setItemImp( object)
{
	var table = this.table;
	
	var row = checkRow( table, this.rowIndex, this.colCount);
	
	this.setDataCb( row, object);
	this.rowIndex++;
}

function setColCountImp( colCount)
{
	this.colCount = colCount;
}

function initVmImp()
{
	var table = this.table;

	var row = checkRow( table, 0, this.colCount);

	this.headerCb( row);
	this.rowIndex = 1;
}

function finalizeImp()
{
	while ( this.table.rows.length > this.rowIndex)
	{
		this.table.deleteRow( this.rowIndex);
	}
	
	this.rowIndex = null;
}

function checkRow( table, index, colCount)
{
	var row = null;
	if ( table.rows.length <= index )
	{
		row = table.insertRow( index);
	} else
	{
		row = table.rows[ index];
	}	
	
	while ( colCount > row.cells.length)
	{
		row.insertCell(0);
	}
	
	while ( colCount < row.cells.length )
	{
		row.deleteCell( colCount);
	}
		
	return row;
}

