//
//	TABLE
//

var CONTROL_PANEL_HEIGHT = 50;
var DIV_SPACING = 3;

//-----------------------------------------------------------------------
function Filter(name, value)
	{
	this.name = name;
	this.value = value;
	}
//-----------------------------------------------------------------------
// Table Class
//
// This class handles all user interaction (u/i) with a 
// table as well as managing the data to be displayed in cells.
// Use "SetDataStrID()" to specify the page element which who's value
// is the string containing content that will appear in cells.
//-----------------------------------------------------------------------
function Table(tblID)
	{
	this.tableID = tblID;

	this.currentRow = 0;
	this.topRow = 1;
	this.filledRows = 0;
	
	this.currentMouseOverRow = 0;
	this.currentHdgCol = 0;
	this.currentHdgOverCol = -1;
	this.rowHeight = 20;
	
	this.bgColor = '#eeddcc';
	this.hdgAlign = "center";
	this.dfltHdgSelectCol = 2;
	
	this.selectRowColor = color_medium_gray;
	this.selectMouseOverRowColor = color_very_light_gray ;
	this.headingColor = color_pale_orange;		// color_ltblue;
	this.headingSelectColor = color_rust;		// color_green;
	this.headingMouseOverColor = color_rust2;
	this.colAlign = new Array();
	this.maxChars = new Array();
	this.hdgNames = new Array();
	this.colWidths = new Array();
	this.checks = new Array();
	
	this.fontSize = 8;
	this.titleFontSize = 9;
	this.div = null;
	this.filters = new Array();
	this.controlPanel = null;
	this.mouseIsDown = false;
	this.postDataFunc = Tbl_DoNothing;
	this.leftLimitCol = -1;
	this.cellVerticalPadding = 1;
	this.cellHorizontalPadding = 4;
	this.col_delimeter = ",";
	this.incl_view_popup = false;
	this.xtra_popup_title = "";
	this.width = 0;
	this.colDataIndicies = null;
	this.parentElem = null;
	this.checkBoxCol = 0;
	this.border = 1;
	this.cellpadding = 0;
	this.cellspacing = 0;
	}

//--------------------------------------------------------------
// The DataStrID is the ID of the element who's value is the
// complete array of data that is to fill the cells.
//--------------------------------------------------------------
Table.prototype.SetDataStrID = function(id)
	{
	this.dataElem = document.getElementById(id);
	}
//--------------------------------------------------------------
Table.prototype.SetPostDataFunc = function(func)
	{
	this.postDataFunc = func;
	}
//--------------------------------------------------------------
Table.prototype.SetParentElem = function(parentElem)
	{
	this.parentElem = parentElem;
	}
//--------------------------------------------------------------
Table.prototype.SetCheckBoxCol = function(checkBoxCol)
	{
	this.checkBoxCol = checkBoxCol;
	}

//--------------------------------------------------------------
Table.prototype.SetCellBorderAttribs = function(border, cellpadding, cellspacing)
	{
	this.border = border;
	this.cellpadding = cellpadding;
	this.cellspacing = cellspacing;
	}

//--------------------------------------------------------------
// Call SetColDataIndicies with any number of args.  Each arg
// represents the column in the DATA ARRARY which will appear in
// the table.  For example, SetColDataIndicies(2, 3, 6) will 
// force  the data in column 2 of the DATA to appear in column 1
// of the TABLE, col 3 from the DATA will appear in col 2 of the 
// table, and finally, col 6 of the data will appear in col 3 of 
// the visible TABLE.
//--------------------------------------------------------------
Table.prototype.SetColDataIndicies = function()
	{
	cols = arguments[0];
	this.colDataIndicies = new Array();
	
	for (var col = 0; col < arguments.length; col++)
		{
		this.colDataIndicies[col] = arguments[col];
		}
	}

//-----------------------------------------------------
// Filters determine what rows will be displayed.
// A filter consists of a field name and a value.
// The field name is the actual name used in the
// SQL database that is generating the DATA array.
// When the Table gets refreshed, the list of filters
// will determine which rows from the database
// will get read into the DATA array.

Table.prototype.AddFilter = function(name, value)
	{
	var total = this.filters.length;
	this.filters[total] = new Filter(name, value);
	}
//-----------------------------------------------------
Table.prototype.GetFilter = function(name)
	{
	for (var cnt = 0; cnt < this.filters.length; cnt++)
		{
		if (this.filters[cnt].name == name)
			{
			return this.filters[cnt];
			}	
		}
	return null;
	}

//-----------------------------------------------------
Table.prototype.GetFilterValue = function(name)
	{
	var filter = this.GetFilter(name);
	
	if (filter == null) return null;
	return filter.value;
	}
//-----------------------------------------------------
Table.prototype.SetFilterValue = function(name, value)
	{
	var filter = this.GetFilter(name);
	
	if (filter != null)
		{
		filter.value = value;
		}
	}
	
//-----------------------------------------------------
Table.prototype.SetDfltSelectedHdg = function(col)
	{
	this.dfltHdgSelectCol = col;
	}
//-----------------------------------------------------
Table.prototype.SetColumnDelimeter = function(delimChar)
	{
	this.col_delimeter = delimChar;
	}

//---------------------------------------------------------
Table.prototype.SetPosition = function(top, left)
	{
	this.top = top;
	this.left = left;
	}

//---------------------------------------------------------
Table.prototype.SetColMaxChars = function(col, maxChars)
	{
	this.maxChars[col] = maxChars;
	}
//---------------------------------------------------------
Table.prototype.SetViewPopupVisible = function(visible)
	{
	this.incl_view_popup = visible;
	}

//---------------------------------------------------------
Table.prototype.SetTotalCols = function(cols)
	{
	this.cols = cols;
	for (var col = 0; col < arguments.length; col++)
		{
		this.colAlign[col] = "left";
		this.maxChars[col] = 0;
		}
	}
//---------------------------------------------------------
Table.prototype.SetRowHeight = function(height)
	{
	this.rowHeight = height;
	}
//---------------------------------------------------------
Table.prototype.SetHeadingName = function(col, name)
	{
	this.hdgNames[col] = name;
	}
//---------------------------------------------------------
Table.prototype.SetColWidth = function(col, width)
	{
	this.colWidths[col] = width;
	this.width += width;
	}
//---------------------------------------------------------
Table.prototype.SetColAlignment = function(col, align)
	{
	this.colAlign[col] = align;
	}

//---------------------------------------------------------
Table.prototype.SetColWidths = function()
	{
	var col;
	
	this.width = 0;

	for (col = 0; col < arguments.length; col++)
		{
		var width = arguments[col];
		this.colWidths[col] = width;
		this.width += width;
		}
	}
//---------------------------------------------------------
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;
	
	for (var col = 0; col < arguments.length; col++)
		{
		this.hdgNames[col] = arguments[col];
		}
	if (arguments[0].length == 1) leftLimitCol = 0;
	else leftLimitCol = -1;
	}

//---------------------------------------------------------
Table.prototype.SetTotalRowsPerPage = function(rows)
	{
	this.rows = parseInt(rows);
	var rowHt = parseInt(this.rowHeight);
	this.height = parseInt(rowHt * (this.rows+1));
	if (this.checkBoxCol > 0) this.height += 82;
	}
//---------------------------------------------------------
Table.prototype.SetVisRows = function(rows)
	{
	this.SetTotalRowsPerPage(rows);
	this.CreateTable();
	this.Refresh();
	this.div.style.display = "inline";
	var ctlDiv = document.getElementById(this.tableID+"_div");
	ctlDiv.style.top = 4 + parseInt(this.top)+parseInt(this.height);
	}
//---------------------------------------------------------
Table.prototype.SetRange = function(rng)
	{
	SetTopRow(this, parseInt(rng)-1)
	}

//---------------------------------------------------------
Table.prototype.SetSelectRowFunc = function(func)
	{
	this.selectRowFunc = func;
	}

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

//---------------------------------------------------------
Table.prototype.GetChkBoxCellInnerHTML = function(row,col,checked)
	{
	if (checked) s = "checked ";
	else s = "";
	return "<input id ='"+row+this.tableID+"'; type='checkbox'; "+
	" onclick='ClickedInTableCheckbox(this,'"+this.tableID+"')'; "+s+
	"/>";
	}
//---------------------------------------------------------
Table.prototype.GetInitCellInnerHTML = function(row,col)
	{
	if (this.checkBoxCol == (col+1))
		{
		return "<input id ='"+row+this.tableID+"'; type='checkbox'; "+
		" onclick='ClickedInTableCheckbox(this,\""+this.tableID+"\")'; checked"+
		"/>";
		}
	return "-";
	}
//-----------------------------------------------------
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='"+this.border+"'; cellpadding='"+this.cellpadding+"'; cellspacing='"+this.cellspacing+"';"+
	"style='z-index: 90; 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+"'; "+
//		"onmouseclick='TblHeading_OnMouseDownFunc(this)'; "+
		"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 nowrap " +
			"id='"+info+"'; "+
//			"onmouseclick='Tbl_OnMouseDownFunc(this)'; "+
			"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+"; "+
			"padding-left: "+this.cellHorizontalPadding+
			"px; padding-right: "+this.cellHorizontalPadding+"px; "+
			"padding-top: "+this.cellVerticalPadding+"px; "+
			"padding-bottom: "+this.cellVerticalPadding+
			"'>"+this.GetInitCellInnerHTML(row,col)+"</td>";
			}
		s=s+"</tr>";
		s=s+"</tBody>";	
		}

	s=s+"</table>";	

	if (this.div == null) 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 = 90;
	dStyle.top = this.top;
	dStyle.left = this.left;
	dStyle.width = this.width;
	dStyle.height = this.height;
	this.div.innerHTML = s;

	var parentElem;
	if (this.parentElem == null) parentElem = document.getElementsByTagName("body")[0];
	else parentElem = this.parentElem;
	parentElem.appendChild(this.div);
	this.table = document.getElementById(this.tableID);
	this.table.style.cursor = "pointer";
	this.table.onselectstart = Tbl_ReturnFalseFunc;
	this.table.obj = this;
	}
//-----------------------------------------------------
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 - 1;
	}
//-----------------------------------------------------
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.IsRowVisible = function(row)
	{
	if (row >= this.topRow)
		{
		if (row <= this.topRow + this.rows - 1)
			{
			return true;	
			}
		}
	return false;
	}
//---------------------------------------------------------
Table.prototype.SelectRow = function(row)
	{
//	if (row != this.currentRow)
//		{
		if (this.currentRow > 0)
			{
			this.SetGlobalRowBkColor(this.currentRow, this.bgColor);
			}
		if (row > 0) 
			{
			this.SetGlobalRowBkColor(row, this.selectRowColor);
			}
		this.currentRow = row;
		this.selectRowFunc(this, row);
//		}
	}
//---------------------------------------------------------
Table.prototype.SelectTopRow = function()
	{
	this.currentRow = 0;
	this.SelectRow(this.topRow);
	}
//---------------------------------------------------------
Table.prototype.LocalToGlobalRow = function(lRow)
	{
	return parseInt(this.topRow) + parseInt(lRow) - 1;	
	}
//---------------------------------------------------------
Table.prototype.GlobalToLocalRow = function(gRow)
	{
	return parseInt(gRow) - parseInt(this.topRow) + 1;	
	}

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

//-----------------------------------------------------
Table.prototype.SetGlobalRowBkColor = function(row, color)
	{
	if (this.IsRowVisible(row))
		{
		var physRow = row - this.topRow + 1;
		this.SetRowBkColor(physRow, color);
		}
	}
//-----------------------------------------------------
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.GetCellData = function(row, col)
	{
	var rowData = this.data[row].split(this.col_delimeter);				
	return rowData[col];
	}
	
//-----------------------------------------------------
Table.prototype.SetCellValue = function(row, col, value)
	{		
	var s;
	
	if (this.checkBoxCol == (col+1))
		{
		return;
//		s = GetChkBoxCellInnerHTML(row,col,value);
		}
	else	
		{
		if (value == "") value = "-";
		if (this.maxChars[col] > 0)
			{
			var maxChars = this.maxChars[col];
			if (value.length > maxChars)
				{
				s = value.substr(0,maxChars)+"...";
				}
			else s = value;
			}
		else s = value;	
		}
		
	this.GetCell(row, col).innerHTML = s;			
	}

//-----------------------------------------------------
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)
	{
	if (rowStr == null) rowStr = "";
	var rowData = rowStr.split(this.col_delimeter);
	var cols = this.GetTotalColumns();
	
	if (this.colDataIndicies == null)
		{
		for (var col = 0; col < cols; col++)
			{
			this.SetCellValue(tblRow+1, col, rowData[col]);	
			}
		}
	else
		{
		for (var col = 0; col < cols; col++)
			{
			var idx = this.colDataIndicies[col];
			if (idx == this.leftLimitCol)
				{
				this.SetCellValue(tblRow+1,col,this.topRow+tblRow);
				}
			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, "-");	
		}
	if (this.checkBoxCol > 0) this.SetCheckBox(tblRow, false);
	this.SetRowBkColor(tblRow, this.bgColor);
	}
//-----------------------------------------------------
Table.prototype.ClearRows = function()
	{
	if (this.rows > 0)
		{
		for (var row = 1; row < this.rows; row++)
			{
			this.ClearRow(row);
			}
		}
	}

//-----------------------------------------------------
Table.prototype.ResetCellBkgnds = function()
	{
	if (this.rows > 0)
		{
		for (var row = 1; row < this.rows; row++)
			{
			this.SetRowBkColor(row, this.bgColor);
			}
		}
	}

//-----------------------------------------------------
Table.prototype.Refresh = function()
	{
	var currID = 0;
	var id;
	var globalRow;
	var lastVisRow;

	if (this.currentRow > 0) currID = this.GetRowDataID(this.currentRow);
	if (this.ResetData())
		{
		this.ResetCellBkgnds();
		this.InitCheckBoxes(this.filledRows);
		lastVisRow = this.filledRows - this.topRow + 1;
		if (lastVisRow > this.rows) totalRows = this.rows;
		else totalRows = lastVisRow;

		for (var row = 0; row < totalRows; row++)
			{
			globalRow = this.LocalToGlobalRow(row);
			this.FillRow(row, this.data[globalRow]);
			if (this.checkBoxCol > 0) this.UpdateCheckBox(row);
			id = this.GetRowDataID(globalRow+1);
			if (id == currID) 
				{
				this.currentRow = 0;
				this.SelectRow(globalRow+1);
				}
			}
			
		if (totalRows <= this.rows)
			{
			for (row = totalRows+1; row <= this.rows; row++)
				{
				this.ClearRow(row);
				}
			}
		}
	else this.ClearRows();
	}

//-----------------------------------------------------
Table.prototype.RefreshCheckBoxes = function()
	{
	for (var row = 0; row < this.rows; row++)
		{
		this.UpdateCheckBox(row);
		}
	}

//-----------------------------------------------------
Table.prototype.SetAllCheckBoxes = function(checked)
	{
	for (var idx = 0; idx < this.filledRows; ++idx)
		{
		this.checks[idx] = checked;
		}
	this.RefreshCheckBoxes();
	}
//-----------------------------------------------------
Table.prototype.UpdateCheckBox = function(localRow)
	{
	var globalRow = this.LocalToGlobalRow(localRow);
	var id = (localRow+1)+this.tableID;
	GetElemByID(id).checked = this.IsChecked(globalRow);
	}
//-----------------------------------------------------
Table.prototype.SetCheckBox = function(row, checked)
	{
	var id = row+this.tableID;
	GetElemByID(id).checked = checked;
	}

//-----------------------------------------------------
Table.prototype.GetCurrentRowData = function()
	{
	return this.data[this.currentRow()-1].split(this.col_delimeter);
	}
	
//-----------------------------------------------------
Table.prototype.SetColAlign = function(col, align)
	{
	var rows = this.GetTotalRows();
	for (var row = 1; row < rows; row++)
		{
		this.SetCellAlign(row,col,align);	
		}
	}
		
//-----------------------------------------------------
Table.prototype.SelectMouseOverLocalRow = function(lRow)
	{
	if (lRow > 0)
		{
		var gRow = this.LocalToGlobalRow(lRow);
		if (gRow != this.currentMouseOverRow && gRow != this.currentRow)
			{
			if (this.currentMouseOverRow > 0)
				{
				if (this.currentMouseOverRow != this.currentRow)
					{
					this.SetGlobalRowBkColor(this.currentMouseOverRow, this.bgColor);	
					}
				}
			if (gRow != this.currentRow)
				{
				this.SetGlobalRowBkColor(gRow, this.selectMouseOverRowColor);	
				this.currentMouseOverRow = gRow;
				}
			else
				{
				this.currentMouseOverRow = 0;	
				}
			}
		}
	}
//-----------------------------------------------------
Table.prototype.TurnOffMouseOverSelect = function()
	{
	if (this.currentMouseOverRow > 0)
		{
		if (this.currentMouseOverRow != this.currentRow)
			{
			this.SetGlobalRowBkColor(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;
	}

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

//-----------------------------------------------------
Table.prototype.SelectMouseOverHdgCol = function(col)
	{
	if (col > this.leftLimitCol)
		{
		if (col != this.currentHdgOverCol && col != this.currentHdgCol)
			{
			if (this.currentHdgOverCol > this.leftLimitCol)
				{
				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 = this.leftLimitCol;	
				}
			}
		}
	}

//-----------------------------------------------------
Table.prototype.TurnOffHeadingSelect = function()
	{
	if (this.currentHdgOverCol > this.leftLimitCol &&  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()
	{
	var s = this.dataElem.value;
	if (s.substr(0,5) == "empty")
		{
		this.filledRows = 0;
		return false;
		}
	else
		{
		this.data = this.dataElem.value.split("^");
		this.filledRows = this.data.length;
		this.postDataFunc(this);
//		this.scrollBar.SetMax(this.filledRows);
		return true;
		}
	}
//-----------------------------------------------------
Table.prototype.GetTotalDataRows = function()
	{
	if (this.data == null) return 0;
	return this.data.length;
	}

//-----------------------------------------------------
Table.prototype.GetRowData = function(row)
	{
	if (this.data != null)
		{
		return this.data[row-1].split(this.col_delimeter);
		}
	else 
		{
		return "";	
		}
	}

//-----------------------------------------------------
function Tbl_OnMouseDownFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	var row = tblObj.GetCellRow(cell);
	if (row > 0)
		{
		var globalRow = tblObj.LocalToGlobalRow(row);

		tblObj.SelectRow(globalRow);
		}
	tblObj.mouseIsDown = true;
	}
		
//-----------------------------------------------------
function Tbl_OnMouseUpFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	tblObj.mouseIsDown = false;
	}
		
//-----------------------------------------------------
function Tbl_OnMouseOverFunc(cell)
	{
	var tblObj = CellToTblObj(cell);
	var row = tblObj.GetCellRow(cell);

	if (row > 0)
		{
		if (tblObj.IsMouseButtonDown())
			{
			tblObj.SelectRow(tblObj.LocalToGlobalRow(row));
			}
		else
			{
			tblObj.SelectMouseOverLocalRow(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 > tblObj.leftLimitCol)
		{
		tblObj.SelectHdgCol(col);
		}
	tblObj.mouseIsDown = true;
	}

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

	if (col > tblObj.leftLimitCol)
		{
		if (tblObj.IsMouseButtonDown())
			{
			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;
		}

//---------------------------------------------------------
function ChangeVisRowsFunc(elem)
	{
	elem.obj.changeArg.SetVisRows(elem.value);
	elem.obj.SetValue(elem.value);
	var rngElem = document.getElementById('Range');
	var tbl = rngElem.obj.changeArg;
	tbl.SetRangeOptions(rngElem.obj);
	tbl.controlPanel.Show();
	}

//---------------------------------------------------------
function ChangeRangeFunc(elem)
	{
	elem.obj.changeArg.SetRange(elem.value);
	}
	
//---------------------------------------------------------
Table.prototype.SetControlForm = function()
	{
	if (this.controlid != null) return;
	
	var top = parseInt(this.top) + parseInt(this.height) + DIV_SPACING - 1;

	this.controlPanel = new Panel(this.tableID+"ctls",
							   top, this.left, CONTROL_PANEL_HEIGHT, this.width,
							   "thin", "none", color_medium_gray, "transparent", this.parentElem);
	if (this.incl_view_popup)
		{
		var viewPopup = this.controlPanel.AddObject(this.tableID+"View", TYPE_POPUP, 5, 10, 20, 40, "View", "20");
		viewPopup.AddOptions("10","15","20","25","50","100");
		viewPopup.SetChangeFunc("ChangeVisRowsFunc", this);
		}
	leftpos = 3;
	toppos = 26;
	var dblLeftBut = this.controlPanel.AddObject(this.tableID+"but_left_dbl", TYPE_IMG_BUTTON, toppos, leftpos, 0, 0, "");
	dblLeftBut.SetDblLeftButImages(ButFunc_DblLeft, this);
	var leftBut = this.controlPanel.AddObject(this.tableID+"but_left", TYPE_IMG_BUTTON, toppos, leftpos+20, 0, 0, "");
	leftBut.SetLeftButImages(ButFunc_Left, this);
	
	var rtBut = this.controlPanel.AddObject(this.tableID+"but_rt", TYPE_IMG_BUTTON, toppos, leftpos+128, 0, 0, "");
	rtBut.SetRtButImages(ButFunc_Rt, this);
	
	var dblRtBut = this.controlPanel.AddObject(this.tableID+"but_rt_dbl", TYPE_IMG_BUTTON, toppos, leftpos+148, 0, 0, "");
	dblRtBut.SetDblRtButImages(ButFunc_DblRt, this);

	var totalView = this.controlPanel.AddObject(this.tableID+"TOTAL", TYPE_TEXT_EDIT_READ_ONLY, toppos-16, leftpos+172, 18, 34, "Total");
	totalView.SetValue(this.filledRows);

	if (this.xtra_popup_title != "")
		{
		title = this.xtra_popup_title;
		var yearPopup = this.controlPanel.AddObject("id_"+this.tableID+title, TYPE_POPUP, toppos-16, leftpos+223, 20, 64, title);
		this.xtra_setup_func(yearPopup);
		}

	var rangePopup = this.controlPanel.AddObject(this.tableID+"Range", TYPE_POPUP, toppos-16, leftpos+41, 20, 85, "Range");
	this.SetRangeOptions(rangePopup);
	rangePopup.SetChangeFunc("ChangeRangeFunc", this);
	this.controlPanel.Show();
	}

//---------------------------------------------------------
Table.prototype.ResetTotal = function()
	{
	var totalVal = document.getElementById(this.tableID+"TOTAL");
	totalVal.value = this.filledRows;
	}
//---------------------------------------------------------
function ButFunc_DblLeft(obj)
	{	
	var tbl = obj.changeArg;
	var rngElem = document.getElementById(tbl.tableID+"Range");
	var opt = rngElem.obj.SetOptionFirst(rngElem);
	if (opt != "") SetTopRow(tbl, parseInt(opt));
	}
//---------------------------------------------------------
function ButFunc_Left(obj)
	{	
	var tbl = obj.changeArg;
	var rngElem = document.getElementById(tbl.tableID+"Range");
	var opt = rngElem.obj.SetOptionPrev(rngElem);
	if (opt != "") SetTopRow(tbl, parseInt(opt));
	}
//---------------------------------------------------------
function ButFunc_Rt(obj)
	{	
	var tbl = obj.changeArg;
	var rngElem = document.getElementById(tbl.tableID+"Range");
	var opt = rngElem.obj.SetOptionNext(rngElem);
	if (opt != "") SetTopRow(tbl, parseInt(opt));
	}
//---------------------------------------------------------
function ButFunc_DblRt(obj)
	{	
	var tbl = obj.changeArg;
	var rngElem = document.getElementById(tbl.tableID+"Range");
	var opt = rngElem.obj.SetOptionLast(rngElem);
	if (opt != "") SetTopRow(tbl, parseInt(opt)-1);
	}

//---------------------------------------------------------
Table.prototype.SetRangeOptions = function(popupObj)
	{
	var totalRows = this.GetTotalDataRows();
	var rowsPerPage = this.GetTotalRows();
	var pages = parseInt((totalRows + rowsPerPage - 1) / rowsPerPage);
	var option;
	var first = 1;
	var last;
	var label;

	popupObj.KillOptions();

	for (var pageCnt = 1; pageCnt <= pages; pageCnt++)
		{
		last = first + rowsPerPage - 1;
		if (last > totalRows) last = totalRows;
		label = first + " - " + last;
		popupObj.AddOption(label);
    	first = last + 1;
		}
	}

//---------------------------------------------------------
Table.prototype.GetRowDataID = function(row)
	{
	rowData = this.GetRowData(row);
	return rowData [0];
	}

//---------------------------------------------------------
Table.prototype.RowIDToRow = function(rowID)
	{
	var total = this.GetTotalDataRows();

	for (var row = 1; row <= total; row++)
		{
		var rowData = this.GetRowData(row);
		if (rowID == rowData [0]) return row;
		}
	return 0;
	}


//---------------------------------------------------------
Table.prototype.IsMouseButtonDown = function()
	{
	return this.mouseIsDown;
	}
//---------------------------------------------------------
Table.prototype.GotoFirst = function()
	{
	this.GotoRow(1);
	}
//---------------------------------------------------------
Table.prototype.GotoLast = function()
	{
	this.GotoRow(this.GetTotalDataRows());
	}
//---------------------------------------------------------
Table.prototype.GotoPrev = function()
	{
	if (this.currentRow > 1)
		{
		this.GotoRow(this.currentRow-1);	
		}
	}
//---------------------------------------------------------
Table.prototype.GotoNext = function()
	{
	if (this.currentRow < this.GetTotalDataRows())
		{
		this.GotoRow(this.currentRow+1);
		}
	}
	
//---------------------------------------------------------
Table.prototype.GotoRow = function(row)
	{
	if (row < this.topRow)
		{
		this.topRow = row;
		this.Refresh();
		}
	else
		{
		if (row > (this.topRow + this.rows - 1))
			{
			this.topRow = row - this.rows + 1;
			this.Refresh();
			}
		}
	this.SelectRow(row);
	}

//---------------------------------------------------------
Table.prototype.SetTitle = function(title)
	{
	var top = this.top - 22;
	var left = this.left;
	var ht = 22;
	var width = this.width;

	var titlePanel = new Panel(this.tableID+"_title",top,left,ht,width,
											"thin", "none", color_medium_gray, "transparent", this.parentElem);
	titleObj = titlePanel.AddObject(this.tableID+"_titlepanel", TYPE_TITLE,8,0,ht-8,width,title);
	titleObj.SetTextSize(12);
	titleObj.SetFontWeight("normal");
	titleObj.SetColor("black");
	titlePanel.Show();	
	}

//---------------------------------------------------------
Table.prototype.SetXtraPopup = function(title, setup_func)
	{
	this.xtra_popup_title = title;
	this.xtra_setup_func = setup_func;
	}
//---------------------------------------------------------
function ClickedInTableCheckbox(cbox, tableID)
	{
	var table = GetElemByID(tableID).obj;
	var row = parseInt(cbox.id);
	
	var gRow = table.LocalToGlobalRow(row);
	table.checks[gRow] = cbox.checked;
	}
//---------------------------------------------------------
Table.prototype.InitCheckBoxes = function(total)
	{
	if (total != this.checks.length)
		{
		for (var idx = 0; idx < total; idx++)
			{
			this.checks[idx] = true;
			}
		}
	}
//---------------------------------------------------------
Table.prototype.IsChecked = function(row)
	{
	if (row >= this.checks.length) return false;
	return this.checks[row];
	}
//---------------------------------------------------------
Table.prototype.SetChecked = function(row,checked)
	{
	this.checks[row-1] = checked;
	}

