//
//	TABLE
//

//---------------------------------------------------------
function Table(tblID)
	{
	this.tableID = tblID;

	//this.selectHdgFunc = Tbl_DoNothing;
	//this.allowSelectOnCol1Hdg = false;
	//this.selectRowFunc = Tbl_DoNothing;

	this.currentRow = 0;
	this.currentMouseOverRow = 0;
	this.currentHdgCol = 0;
	this.currentHdgOverCol = -1;
	this.rowHeight = 20;
	
	this.bgColor = '#eeddcc';
	this.hdgAlign = "center";
	this.dfltHdgSelectCol = 2;
	
	this.selectRowColor = "#ff0000";
	this.selectMouseOverRowColor = "#eeee66";
	this.headingColor = "#aaaaff";
	this.headingSelectColor = "#00ff00";
	this.headingMouseOverColor = "#bbffbb";
	this.topRow = 0;
	this.data = null;
	this.filledRows = 0;
	this.colAlign = new Array();
	this.fontSize = 8;
	this.titleFontSize = 9;
	}

//-----------------------------------------------------
Table.prototype.SetDataStrID = function(id)
	{
	this.dataElem = document.getElementById(id);
	}

//-----------------------------------------------------
Table.prototype.SetColDataIndicies = function()
	{
	cols = arguments[0];
	this.colDataIndicies = new Array();
	
	for (var col = 0; col < arguments.length; col++)
		{
		this.colDataIndicies[col] = arguments[col];
		}
	}
//-----------------------------------------------------
Table.prototype.SetDfltSelectedHdg = function(col)
	{
	this.dfltHdgSelectCol = col;
	}

//---------------------------------------------------------
Table.prototype.SetPosition = function(top, left)
	{
	this.top = top;
	this.left = left;
	}
//---------------------------------------------------------
Table.prototype.SetTotalCols = function(cols)
	{
	this.cols = cols;
	for (var col = 0; col < arguments.length; col++)
		{
		this.colAlign[col] = "left";
		}
	}
//---------------------------------------------------------
Table.prototype.SetRowHeight = function(height)
	{
	this.rowHeight = height;
	}

//---------------------------------------------------------
Table.prototype.SetColWidths = function()
	{
	var col;
	
	this.width = 0;
	this.colWidths = new Array();
	for (col = 0; col < arguments.length; col++)
		{
		var width = arguments[col];
		this.colWidths[col] = width;
		this.width += width;
		}
	this.width += 78;
	}
//---------------------------------------------------------
Table.prototype.SetColAlignments = function()
	{
	var col;
	
	for (col = 0; col < arguments.length; col++)
		{
		var align = arguments[col];
		this.colAlign[col] = align;
		}
	}

//---------------------------------------------------------
Table.prototype.SetHeadingNames = function()
	{
	var col;
	this.hdgNames = new Array();
	
	for (var col = 0; col < arguments.length; col++)
		{
		this.hdgNames[col] = arguments[col];
		}
	}

//---------------------------------------------------------
Table.prototype.SetTotalRowsPerPage = function(rows)
	{
	this.rows = rows;
	var rowHt = parseInt(this.rowHeight);
	this.height = (rowHt * rows) + 30;
	}
	
//---------------------------------------------------------
Table.prototype.SetSelectRowFunc = function(func)
	{
	this.selectRowFunc = func;
	}

//---------------------------------------------------------
Table.prototype.SetSelectHdgFunc = function(func)
	{
	this.selectHdgFunc = func;
	}

//-----------------------------------------------------
Table.prototype.CreateTable = function()
	{
	var row;
	var col;
	var rows = this.rows;
	var cols = this.cols;
	var info;
	var thisObj = this;

	// Build the table - row 0 will act as heading
	s="<table id='"+this.tableID+"';  border='1'; cellpadding='0'; cellspacing='1';"+
	"style='z-index: 100; position: absolute; cursor: pointer;"+
	"top=0; left=0; width=100%'>";
	s=s+"<tBody>"+
	"<tr>";
	for (col = 0; col < cols; col++)
		{
		info = "0,"+col;
		s=s+"<td id = '"+info+"'; "+
		"onmousedown='TblHeading_OnMouseDownFunc(this)'; "+
		"onmouseup='TblHeading_OnMouseUpFunc(this)'; "+
		"onmouseover='TblHeading_OnMouseOverFunc(this)'; "+
		"onmouseout='TblHeading_OnMouseOutFunc(this)'; "+
		"style = 'height:" + this.rowHeight + "px; width:"+this.colWidths[col]+"px; font-size:"+this.titleFontSize+"pt;"+
		"background-color:"+this.headingColor+";text-align:center'>"+
		this.hdgNames[col]+"</td>";
		}
	s=s+"</tr>";
	
	for (row = 1; row <= rows; row++)
		{	
		s=s+"<tr>";
		for (col = 0; col < cols; col++)
			{
			align = this.colAlign[col];
			info = row+","+col;
			s=s+"<td id = '"+info+"'; "+
			"onmousedown='Tbl_OnMouseDownFunc(this)'; "+
			"onmouseup='Tbl_OnMouseUpFunc(this)'; "+
			"onmouseover='Tbl_OnMouseOverFunc(this)'; "+
			"onmouseout='Tbl_OnMouseOutFunc(this)'; "+

			"style= 'font-size:"+this.fontSize+"pt; height:"+this.rowHeight+"px; width:"+this.colWidths[col]+"px; "+
			"background-color:"+this.bgColor+";text-align:"+align+"'>-</td>";
			}
		s=s+"</tr>";
		s=s+"</tBody>";	
		}
	s=s+"</table>";	
	var body = document.getElementsByTagName("body")[0];
	this.div = document.createElement("div");
	this.div.id = "TheDIV";
	this.div.tblObj = this;

	var dStyle = this.div.style;	//GetElemStyleByID("TheDIV");
	dStyle.display = "none";
	dStyle.position = "absolute";
	dStyle.zIndex = 160;
	dStyle.top = this.top;
	dStyle.left = this.left;
	dStyle.width = this.width;
	dStyle.height = this.height;
	this.div.innerHTML = s;
	body.appendChild(this.div);
	this.table = document.getElementById(this.tableID);

	this.table.style.cursor = "pointer";

	this.table.onselectstart = Tbl_ReturnFalseFunc;
	}
//-----------------------------------------------------
function CellToTblObj(cell)
	{
	var elem = cell.parentNode;

	while (elem.id != "TheDIV")
		{
		elem = elem.parentNode;	
		}
	return elem.tblObj;
	}

//---------------------------------------------------------
Table.prototype.GetCell = function(row, col)
	{
	var theRow = this.table.getElementsByTagName("tr")[row];
	return theRow.getElementsByTagName("td")[col];	
	}
//---------------------------------------------------------
Table.prototype.GetHdgCell = function(col)
	{
	var theRow = this.table.getElementsByTagName("tr")[0];
	return  theRow.getElementsByTagName("td")[col];
	}
//-----------------------------------------------------
Table.prototype.GetTotalRows = function()
	{
	return this.table.getElementsByTagName('tr').length;
	}
//-----------------------------------------------------
Table.prototype.GetTotalColumns = function()
	{
	var theRow = this.table.getElementsByTagName("tr")[0];
	return theRow.getElementsByTagName("td").length;
	}
//---------------------------------------------------------
Table.prototype.Show = function()
	{
	this.CreateTable();
/*
	this.scrollBar = new ScrollBar(this.tableID); 
	this.scrollBar.SetBounds(this.top+24, this.left + this.width+2-64, this.height-17, 17);
	this.scrollBar.SetInitValues( 1, 100, 1, this.rows);	//this.data.length
	this.scrollBar.SetHookFunc(this, SetTopRow);

	var d = "images_ui/scrollbar/";
	this.scrollBar.SetImages(
			d+"up_arrow_normal.jpg",
			d+"up_arrow_over.jpg",
			d+"up_arrow_down.jpg",
			
			d+"dn_arrow_normal.jpg",
			d+"dn_arrow_over.jpg",
			d+"dn_arrow_down.jpg",
			
			d+"thumb_normal.jpg",
			d+"thumb_over.jpg",
			d+"thumb_down.jpg",
			
			d+"thumb_bkgnd.jpg"
			);
 */
	this.SelectHdgCol(this.dfltHdgSelectCol);	
	this.div.style.display = "inline";
//	this.scrollBar.Show();
	}
//---------------------------------------------------------
function SetTopRow(tblObj, val)
	{
	tblObj.topRow = val;
	tblObj.Refresh();
	}
//---------------------------------------------------------
Table.prototype.Hide = function()
	{
	this.div.style.display = "none"
	}

//---------------------------------------------------------
Table.prototype.SetRowValues = function()
	{
	row = arguments[0] - 1;
	
	for (var col = 1; col < arguments.length; col++)
		{
		this.GetCell(row, col).innerHTML = arguments[col];
		}
	}

//---------------------------------------------------------
Table.prototype.SelectRow = function(row)
	{
	if (row != this.currentRow)
		{
		if (this.currentRow > 0)
			{
			this.SetRowBkColor(this.currentRow, this.bgColor);
			}
		if (row > 0) 
			{
			this.SetRowBkColor(row, this.selectRowColor)	
			}
		this.currentRow = row;
		var virtualRow = parseInt(row) + parseInt(this.topRow);
		this.selectRowFunc(this, virtualRow);
		}
	}

//---------------------------------------------------------
Table.prototype.SelectHdgCol = function(col)
	{
	if (col != this.currentHdgCol)
		{
		if (this.currentHdgCol > 0)
			{
			this.SetHdgColBkColor(this.currentHdgCol, this.headingColor);
			}
		if (col > 0) 
			{
			this.SetHdgColBkColor(col, this.headingSelectColor);	
			}
		this.currentHdgCol = col;
		this.selectHdgFunc(this, col);
		}
	}

//-----------------------------------------------------
Table.prototype.GetCurrentRow = function(tbl)
	{
	return this.currentRow;
	}

//-----------------------------------------------------
Table.prototype.SetRowBkColor = function(row, color)
	{
	var cols = this.GetTotalColumns();

	for (var col = 0; col < cols; col++)
		{
		this.GetCell(row,col).style.backgroundColor = color;					
		}
	}

//-----------------------------------------------------
Table.prototype.SetHdgColBkColor = function(col, color)
	{
	this.GetHdgCell(col).style.backgroundColor = color;				
	}

//-----------------------------------------------------
Table.prototype.GetCellValue = function(row, col)
	{
	return this.GetCell(row, col).innerHTML;					
	}

//-----------------------------------------------------
Table.prototype.SetCellValue = function(row, col, value)
	{
	this.GetCell(row, col).innerHTML = value;					
	}

//-----------------------------------------------------
Table.prototype.GetHdgColValue = function(col)
	{
	return this.GetHdgCell(col).innerHTML;					
	}

//-----------------------------------------------------
Table.prototype.SetHdgColValue = function(col, value)
	{
	this.GetHdgCell(col).innerHTML = value;					
	}
//-----------------------------------------------------
Table.prototype.SetCellFont = function(row, col, font)
	{
	this.GetCell(row,col).style.fontFamily = font;
	}
//-----------------------------------------------------
Table.prototype.SetCellFontSize = function(row, col, size)
	{
	this.GetCell(row,col).style.fontSize = size;
	}

//-----------------------------------------------------
Table.prototype.SetCellFontWeight = function(row, col, weight)
	{
	this.GetCell(row,col).style.fontWeight = weight;
	}
//-----------------------------------------------------
Table.prototype.SetCellTextDecoration = function(row, col, decor)
	{
	this.GetCell(row,col).style.textDecoration = decor;
	}

//-----------------------------------------------------
Table.prototype.SetRowFontWeight = function(row, weight)
	{
	var cols = this.GetTotalColumns();
	for (var col = 1; col <= cols; col++)
		{
		this.SetCellFontWeight(row,col,weight);	
		}
	}

//-----------------------------------------------------
Table.prototype.SetRowFont = function(row, font)
	{
	var cols = this.GetTotalColumns();
	for (var col = 1; col <= cols; col++)
		{
		this.SetCellFont(row,col,font);	
		}
	}
//-----------------------------------------------------
Table.prototype.SetRowFontSize = function(row, size)
	{
	var cols = thisGetTotalColumns();
	for (var col = 1; col <= cols; col++)
		{
		this.SetCellFontSize(row,col,size);	
		}
	}

//-----------------------------------------------------
Table.prototype.SetRowAlign = function(row, align)
	{
	var cols = this.GetTotalColumns();
	for (var col = 1; col <= cols; col++)
		{
		this.SetCellAlign(row,col,align);	
		}
	}
//-----------------------------------------------------
Table.prototype.FillRow = function(tblRow, rowStr)
	{
	var rowData = rowStr.split(",");
	var cols = this.GetTotalColumns();
	for (var col = 0; col < cols; col++)
		{
		var idx = this.colDataIndicies[col];
		if (idx == -1)
			{
			this.SetCellValue(tblRow+1,col,this.topRow+tblRow+1);
			}
		else
			{
			this.SetCellValue(tblRow+1, col, rowData[idx]);	
			}
		}
	}

//-----------------------------------------------------
Table.prototype.ClearRow = function(tblRow)
	{
	var cols = this.GetTotalColumns();
	for (var col = 0; col < cols; col++)
		{
		this.SetCellValue(tblRow, col, "-");	
		}
	}

//-----------------------------------------------------
Table.prototype.Refresh = function()
	{
	if (this.ResetData())
		{
		if (this.filledRows < this.rows) totalRows = this.filledRows;
		else totalRows = this.rows;
		for (row = 0; row < totalRows; row++)
			{
			this.FillRow(row, this.data[this.topRow+row]);
			}
		if (this.filledRows < this.rows)
			{
			for (row = this.filledRows+1; row <= this.rows; row++)
				{
				this.ClearRow(row);	
				}	
			}
		}
	}

//-----------------------------------------------------
Table.prototype.GetCurrentRowData = function()
	{
	return this.data[this.currentRow-1].split(",");
	}
	
//-----------------------------------------------------
Table.prototype.SetColAlign = function(col, align)
	{
	var rows = this.GetTotalRows();
	for (var row = 1; row < rows; row++)
		{
		this.SetCellAlign(row,col,align);	
		}
	}
		
//-----------------------------------------------------
Table.prototype.SelectMouseOverRow = function(row)
	{
	if (row > 0)
		{
		if (row != this.currentMouseOverRow && row != this.currentRow)
			{
			if (this.currentMouseOverRow > 0)
				{
				if (this.currentMouseOverRow != this.currentRow)
					{
					this.SetRowBkColor(this.currentMouseOverRow, this.bgColor);	
					}
				}
			if (row != this.currentRow)
				{
				this.SetRowBkColor(row, this.selectMouseOverRowColor);	
				this.currentMouseOverRow = row;
				}
			else
				{
				this.currentMouseOverRow = 0;	
				}
			}
		}
	}
//-----------------------------------------------------
Table.prototype.TurnOffMouseOverSelect = function()
	{
	if (this.currentMouseOverRow > 0)
		{
		if (this.currentMouseOverRow != this.currentRow)
			{
			this.SetRowBkColor(this.currentMouseOverRow, this.bgColor);
			this.currentMouseOverRow = 0;
			}
		}
	}

//-----------------------------------------------------
Table.prototype.SelectHdgColWithNoCallback = function(col)
	{
	var savedFunc = this.selectHdgFunc;
	this.selectHdgFunc = Tbl_DoNothing;
	this.SelectHdgCol(col);
	this.selectHdgFunc = savedFunc;
	}


//-----------------------------------------------------
Table.prototype.SelectHdgCol = function(col)
	{
	if (col != this.currentHdgCol)
		{
		if (this.currentHdgCol > 0)
			{
			this.SetHdgColBkColor(this.currentHdgCol, this.headingColor);
			}
		if (col > 0)
			{
			this.SetHdgColBkColor(col, this.headingSelectColor);
			}
		this.currentHdgCol = col;
		this.selectHdgFunc(col);
		}
	}
//-----------------------------------------------------
Table.prototype.SelectMouseOverHdgCol = function(col)
	{
	if (col > 0)
		{
		if (col != this.currentHdgOverCol && col != this.currentHdgCol)
			{
			if (this.currentHdgOverCol > -1)
				{
				if (this.currentHdgOverCol != this.currentHdgCol)
					{
					this.SetHdgColBkColor(this.currentHdgOverCol, this.bgColor);	
					}
				}
			if (col != this.currentHdgCol)
				{
				this.SetHdgColBkColor(col, this.headingMouseOverColor);
				this.currentHdgOverCol = col;
				}
			else
				{
				this.currentHdgOverCol = -1;	
				}
			}
		}
	}

//-----------------------------------------------------
Table.prototype.TurnOffHeadingSelect = function()
	{
	if (this.currentHdgOverCol > 0 &&  this.currentHdgOverCol != this.currentHdgCol)
		{
		this.SetHdgColBkColor(this.currentHdgOverCol, this.headingColor);	
		this.currentHdgOverCol = -1;
		}
	}

//-----------------------------------------------------
Table.prototype.GetCellRow = function(cell)
	{
	var recRslt = cell.id.split(",");
	return recRslt[0];
	}
//-----------------------------------------------------
Table.prototype.GetCellCol = function(cell)
	{
	var recRslt = cell.id.split(",");
	return recRslt[1];
	}

//-----------------------------------------------------
Table.prototype.ResetData = function()
	{
	if (this.dataElem.value == "Empty")
		{
		this.filledRows = 0;
		return false;
		}
	else
		{
		this.data = this.dataElem.value.split("^");
		this.filledRows = this.data.length;
//		this.scrollBar.SetMax(this.filledRows);
		return true;
		}
	}
//-----------------------------------------------------
Table.prototype.GetRowData = function(row)
	{
	return this.data[row-1].split(",");
	}

//-----------------------------------------------------
function Tbl_OnMouseDownFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	var row = tblObj.GetCellRow(cell);
	if (row > 0)
		{
		tblObj.SelectRow(row);
		}
	}
		
//-----------------------------------------------------
function Tbl_OnMouseUpFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	}
		
//-----------------------------------------------------
function Tbl_OnMouseOverFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	var row = tblObj.GetCellRow(cell);

	if (row > 0)
		{
		if (Event_IsButtonDown())
			{
			tblObj.SelectRow(row);
			}
		else
			{
			tblObj.SelectMouseOverRow(row);
			}
		}
	}

//-----------------------------------------------------
function Tbl_OnMouseOutFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	var row = tblObj.GetCellRow(cell);
	tblObj.TurnOffMouseOverSelect();
	}
	
//-----------------------------------------------------
function TblHeading_OnMouseDownFunc(cell)
	{
	var tblObj = CellToTblObj(cell);	
	var col = tblObj.GetCellCol(cell);

	if (col > 0)
		{
		tblObj.SelectHdgCol(col);
		}
	}

//-----------------------------------------------------
function TblHeading_OnMouseUpFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	}
		
//-----------------------------------------------------
function TblHeading_OnMouseOverFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	var col = tblObj.GetCellCol(cell);

	if (col > 0)
		{
		if (Event_IsButtonDown())
			{
			tblObj.SelectHdgCol(col);
			}
		else
			{
			tblObj.SelectMouseOverHdgCol(col);
			}
		}
	}

//-----------------------------------------------------
function TblHeading_OnMouseOutFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	tblObj.TurnOffHeadingSelect();
//	tblObj.mouseOutFunc(cell);
	}
	//-----------------------------------------------------
	function Tbl_ReturnFalseFunc()
		{
		return false;
		}

