// LyDhmltTableScript.js
// Copyright (c) 2000-2009, Lyria-W4.
// All rights reserved.
// @version	$Id: LyDhtmlTableScript.js,v 1.17.4.10.2.1 2009/12/07 10:27:10 nha Exp $

var click = false;
var lastCellSelected = -1;
var lastRowSelected = "";

// List of cells with decorations
var cellDecorations = new Array();

// called when the mouse is over a row
function callOnMouseOver(rowId, color)
{
	var ie= (navigator.appName == "Microsoft Internet Explorer")?1:0;

	if(ie)
	{
		var trObject = document.all[rowId];

		if ( (trObject != null) && (trObject != "undefined") )
		{
			var parentTag = trObject.parentElement;

			if ( (parentTag != null) && (parentTag != "undefined") )
			{
				for (var i = 0; i < parentTag.children.length; i++)
				{
					var child = parentTag.children[i];
					var tagName = child.tagName;

					if (tagName == "TR")
					{
						if ((child.id == rowId)
							|| (child.id.indexOf(rowId+"SUB_") == 0))
							child.bgColor = color;
					}
				}
			}
		}
	}
	else
	{
		var trObject = document.getElementById(rowId);

		if ( (trObject != null) && (trObject != "undefined") )
		{
			var parentTag = trObject.parentNode;

			if ( (parentTag != null) && (parentTag != "undefined") )
			{
				for (var i = 0; i < parentTag.rows.length; i++)
				{
					var child = parentTag.rows[i];
					var tagName = child.tagName;

					if (tagName == "TR")
					{
						if ((child.id == rowId)
							|| (child.id.indexOf(rowId+"SUB_") == 0))
							child.bgColor = color;
					}
				}
			}
		}
	}
}

// called when a simple click is performed on a row
function InvokeRowClick(rowId, controllerId, tableId, selectionColor, path, singleSelection, event)
{
	var elem = (event.target) ? event.target : event.srcElement;

	if (elem.nodeName == "IMG") // Avoid selection event when clicking on an image
		return;

	var extend = event.ctrlKey;
	var shift = event.shiftKey;
	var selectedCheckbox = document.getElementById("checkboxSelection_row" + rowId);

	if (selectedCheckbox != null)
	{
		extend = true;
		shift = false;
	}

	if (singleSelection)
	{
		var wasSelected = hasSelectionId(tableId, rowId);

		clearSelectionIds(tableId);

		if (wasSelected && extend)
			removeSelectionId(tableId, rowId);
		else
			addSelectionId(tableId, rowId);
	}
	else if (!extend)
	{
		clearSelectionIds(tableId);

		if (shift)
			selectRows(tableId, lastRowSelected, rowId);
		else
			addSelectionId(tableId, rowId);
	}
	else
	{
		if (hasSelectionId(tableId, rowId))
			removeSelectionId(tableId, rowId);
		else
			addSelectionId(tableId, rowId);
	}

	if (selectedCheckbox == null)
		UpdateRowsColors(tableId, selectionColor, null);

	lastRowSelected = rowId;

	var selectedIds = getSelectionIds(tableId);

	InvokeSelection("_select", selectedIds, controllerId, (extend ? "true" : "false"), path);
}

// Called when a dblClick is performed on a row
function InvokeRowDoubleClick(tableId, controllerId, objectId, color, column, separator, path)
{
	var methodParam = ReplaceString(objectId,"\\","\\\\");

	if (column >= 0)
		methodParam = methodParam + separator + column;

	InvokeAction(tableId, path, '', controllerId, methodParam);
}

// Called when a click is performed on the status of a row
function InvokeStatusClick(rowId, controllerId, path, e)
{
	if (window.event)
		event.cancelBubble = true; // IE
	else if (e.stopPropagation)
		e.stopPropagation(); // Firefox

	InvokeAction('_status', path, '', controllerId, rowId);
}

// Called when a click is performed on a header
function InvokeHeaderClick(controllerId, index, path, event)
{
	InvokeAction('', path, '', controllerId, index, (event.ctrlKey ? 'extendSort' : 'sort'));
}

// Called when a click is performed on a HRef
function InvokeHRefClick(objectId, controllerId, path)
{
	InvokeAction(objectId, path, '', controllerId, objectId, 'href');
}

// Called when a click is performed on an expand arrow of a row
function InvokeExpandRowClick(rowId, controllerId, path)
{
	InvokeAction('', path, '', controllerId, rowId, 'expand');
}

// Called when a contextual menu is performed on a row
function callOnContextMenu(rowId, controllerId, htmlTableId, tableId, selectionColor, path, selectionPath, event)
{
	var selectedCheckbox = document.getElementById("checkboxSelection_row"+ rowId);

	if (((selectedCheckbox == null) || (typeof selectedCheckbox == "undefined"))
		&& (rowId != "") && !hasSelectionId(htmlTableId, rowId))
	{
		InvokeRowClick(rowId, controllerId, htmlTableId, selectionColor, selectionPath, false, event);

		setTimeout("InvokePopup('" + tableId + "','" + ReplaceString(rowId,"\\","\\\\") +
			"_subIndexSeparator_" + lastCellSelected + "','" + path +
			"','" + controllerId + "'," + event.ctrlKey + "," +
			event.clientX + "," + event.clientY + ")", 300);
	}
	else
		InvokePopup(tableId, rowId + "_subIndexSeparator_" + lastCellSelected, path, controllerId, event.ctrlKey,
			event.clientX, event.clientY);
}


// Starts the edition of a cell
function startCellEdition(tableId, controllerId, row, column, separator, path, event)
{
	var elem = (event.target) ? event.target : event.srcElement;

	if (elem.nodeName == "INPUT")
		return;

	if (elem.nodeName == "IMG") // Tool button
		return;

	InvokeAction(tableId, path, '', controllerId, '_cellEdition' + separator + row + separator + column);
}

// Select rows from beginIndex to endIndex
function selectRows(tableId, lastRow, newRow)
{
	var ie= (navigator.appName == "Microsoft Internet Explorer")?1:0;
	var table = document.getElementById(tableId);
	var started = false;

	if(ie)
	{
		var tbody = table.children[0];

		for (var i = 0; (i < tbody.children.length); i++)
		{
			if (tbody.children[i].tagName == "TR")
			{
				// Check if sub-row of existing row
				if (tbody.children[i].id.indexOf("SUB_") > 0)
					continue;

				if ((tbody.children[i].id == lastRow) || (tbody.children[i].id == newRow))
				{
					started = !started;
					addSelectionId(tableId, tbody.children[i].id);
				}
				else if (started)
					addSelectionId(tableId, tbody.children[i].id);
			}
		}
	}
	else
	{
		var tbody = table.childNodes[1];

		for (var i = 0; (i < tbody.childNodes.length); i++)
		{
			if (tbody.childNodes[i].tagName == "TR")
			{
				// Check if sub-row of existing row
				if (tbody.childNodes[i].id.indexOf("SUB_") > 0)
					continue;

				if ((tbody.childNodes[i].id == lastRow) || (tbody.childNodes[i].id == newRow))
				{
					started = !started;
					addSelectionId(tableId, tbody.childNodes[i].id);
				}
				else if (started)
					addSelectionId(tableId, tbody.childNodes[i].id);
			}
		}
	}
}

// Select a row
function selectRow(tableId, rowId, color, frameId, updateRowColors)
{
	var frame = null;

	if (frameId != null)
		frame = findTarget(null, frameId, null);

	if (frame == null)
		frame = this;

	frame.addSelectionId(tableId, rowId);
}

// Unselect a row
function  unselectRow(tableId, rowId, frameId)
{
	var frame = null;

	if (frameId != null)
		frame = findTarget(null, frameId, null);

	if (frame == null)
		frame = this;

	frame.removeSelectionId(tableId, rowId);
}

// Select all rows
function selectAll(tableId, select, color)
{
	var ie= (navigator.appName == "Microsoft Internet Explorer")?1:0;
	var table = document.getElementById(tableId);

	if(ie)
	{
		var tbody = table.children[0];

		for (var i = 0; (i < tbody.children.length); i++)
		{
			if (tbody.children[i].tagName == "TR")
			{
				// Check if sub-row of existing row
				if (tbody.children[i].id.indexOf("SUB_") > 0)
					continue;

				if (select)
					addSelectionId(tableId, tbody.children[i].id);
				else
					removeSelectionId(tableId, tbody.children[i].id);
			}
		}
	}
	else
	{
		var tbody = table.childNodes[1];

		for (var i = 0; (i < tbody.childNodes.length); i++)
		{
			if (tbody.childNodes[i].tagName == "TR")
			{
				// Check if sub-row of existing row
				if (tbody.childNodes[i].id.indexOf("SUB_") > 0)
					continue;

				if (select)
					addSelectionId(tableId, tbody.childNodes[i].id);
				else
					removeSelectionId(tableId, tbody.childNodes[i].id);
			}
		}
	}

	UpdateRowsColors(tableId, color);
}

// permute to style of one element
function changeStyle(element, style1, style2)
{
	if (element.className == style1)
		element.className = style2;
	else
		element.className = style1;
}

// This function returns a list of sub-elements whose foreground
// color may be updated.
function GetChildrenWithForeground(object)
{
	var ie= (navigator.appName == "Microsoft Internet Explorer")?1:0;
	var result = new Array();
	var result2;

	if (ie)
	{
		for (var i = 0; (i < object.children.length); i++)
		{
			if (object.children[i].tagName == "A"
				|| object.children[i].tagName == "TD")
				result[result.length] = object.children[i];

			result2 = GetChildrenWithForeground(object.children[i]);

			for (var j = 0; (j < result2.length); j++)
				result[result.length] = result2[j];
		}
	}
	else
	{
		for (var i = 0; (i < object.childNodes.length); i++)
		{
			if (object.childNodes[i].tagName == "A"
				|| object.childNodes[i].tagName == "TD")
				result[result.length] = object.childNodes[i];

			result2 = GetChildrenWithForeground(object.childNodes[i]);

			for (var j = 0; (j < result2.length); j++)
				result[result.length] = result2[j];
		}
	}

	return result;
}

// This function change the color on a row.
function UpdateRowColor(tableId, row, selectionColor, frame)
{
	var ie= (navigator.appName == "Microsoft Internet Explorer")?1:0;

	if (row != null)
	{
		var rowId = row.id;

		// Check if sub-row of existing row
		if (rowId.indexOf("SUB_") > 0)
			rowId = rowId.substring(0, rowId.indexOf("SUB_"));

		var isSelected =  frame.hasSelectionId(tableId, rowId);
		var wasSelected = !isSelected;

		if (ie)
		{
			for (var j = 0; (j < row.children.length); j++)
			{
				var className = row.children[j].className;

				if (className.indexOf("status")>=0 || className.indexOf("table")==0)
					continue;

				if ((row.children[j] != null) && (typeof row.children[j] != "undefined")
						&& (row.children[j].style != null) && (typeof row.children[j].style  != "undefined"))
				{
					wasSelected = (row.children[j].style != null) && (row.children[j].style.backgroundColor == selectionColor);
					break;
				}
			}
		}
		else
		{
			for (var j = 0; (j < row.cells.length); j++)
			{
				var className = row.cells[j].className;

				if (className.indexOf("status")>=0 || className.indexOf("table")==0)
					continue;

				if ((row.cells[j] != null) && (typeof row.cells[j] != "undefined")
						&& (row.cells[j].style != null) && (typeof row.cells[j].style  != "undefined"))
				{
					wasSelected = (row.cells[j].style != null) && (row.cells[j].style.backgroundColor != row.style.backgroundColor);
					break;
				}
			}
		}

		if (isSelected == wasSelected)
			return;

		if (isSelected)
		{
			if(ie)
			{
				for (var j = 0; (j < row.children.length); j++)
				{
					var className = row.children[j].className;

					if (className.indexOf("status")>=0 || className.indexOf("table")==0)
						continue;

					// Do not choose the highlighted color as oldcolor if ALREADY highlighted (case of Ctrl + click)
					if (row.children[j].style.backgroundColor != selectionColor)
					{
						row.children[j].style.oldColor = row.children[j].style.color;
						row.children[j].style.oldBackgroundColor = row.children[j].style.backgroundColor;
					}

					row.children[j].style.backgroundColor = selectionColor;
					row.children[j].style.color = "#FFFFFF";
				}
			}
			else
			{
				for (var j = 0; (j < row.cells.length); j++)
				{
					var myClassName = row.cells[j].className;

					if (myClassName.indexOf("status")>=0 || myClassName.indexOf("table")==0)
						continue;

					row.cells[j].style.backgroundColor = selectionColor;
					row.cells[j].style.color = "#FFFFFF";
					for (var l = 0; (l < row.cells[j].childNodes.length); l++)
					{
						if (row.cells[j].childNodes[l].tagName == "A")
							row.cells[j].childNodes[l].style.color = "#FFFFFF";
					}
				}
			}

			// Change the foreground color of sub-elements
			var children = GetChildrenWithForeground(row);

			if (children != null)
			{
				for (var j = 0; (j < children.length); j++)
				{
					children[j].style.color = "#FFFFFF";
				}
			}
		}
		else
		{
			var rowColor = row.getAttribute("color");

			if (rowColor == null)
				rowColor = "";

			if (ie)
			{
				for (var j = 0; (j < row.children.length); j++)
				{
					row.children[j].style.backgroundColor = row.children[j].style.oldBackgroundColor;
					row.children[j].style.color = row.children[j].style.oldColor;
				}
			}
			else
			{
				for (var j = 0; (j < row.cells.length); j++)
				{
					row.cells[j].style.backgroundColor = row.style.backgroundColor;
					row.cells[j].style.color = rowColor;
				}
			}

			// Change the foreground color of sub-elements
			var children = GetChildrenWithForeground(row);

			if (children != null)
			{
				for (var j = 0; (j < children.length); j++)
				{
					children[j].style.color = rowColor;
				}
			}
		}
	}
}

// To update row colors in function of selection.
function UpdateRowsColors(tableId, selectionColor, frameId)
{
	var ie= (navigator.appName == "Microsoft Internet Explorer")?1:0;
	var doc = document;
	var frame = this;

	if (frameId != null)
	{
		frame = findTarget(null, frameId, null);

		if (frame != null)
			doc = frame.document;
	}

	var table = doc.getElementById(tableId);

	if ((table == null) || ((typeof table) == "undefined"))
		return;

	if(ie)
	{
		var tbody = table.children[0];

		for (var i = 0; (i < tbody.children.length); i++)
		{
			if (tbody.children[i].tagName == "TR")
				UpdateRowColor(tableId, tbody.children[i], selectionColor, frame);
		}
	}
	else
	{
		var tbody = table.childNodes[1];

		for (var i = 0; (i < tbody.rows.length); i++)
		{
			UpdateRowColor(tableId, tbody.rows[i], selectionColor, frame);
		}
	}
}

// Update the position of the cell decoration DIV
function UpdateCellDecoration(cellPosition, register, frameId)
{
	var	frame = null;

	if (frameId != null)
		frame = findTarget(null, frameId, null);

	if (frame == null)
		frame = this;

	var	cellDeco = frame.document.getElementById('cell_' + cellPosition);
	var	divDeco = frame.document.getElementById('cellDeco_' + cellPosition);

	if ((cellDeco != null) && (typeof cellDeco !="undefined")
		&& (divDeco != null) && (typeof divDeco !="undefined"))
	{
		divDeco.style.left = getXInDiv(cellDeco);
		divDeco.style.top = getYInDiv(cellDeco)
		divDeco.style.width = cmGetWidth(cellDeco);
		divDeco.style.height = cmGetHeight(cellDeco);

		if (register)
			frame.cellDecorations[frame.cellDecorations.length] = cellPosition;
	}
}

// Gets the x position of an object in its parent DIV
function getXInDiv(obj)
{
	if (!obj)
		return 0;
	var x = 0;

	do
	{
		x += obj.offsetLeft;
		obj = obj.offsetParent;

		// If parent DIV with an auto overflow is found, position must be retrieved from this DIV
		if (obj && obj.tagName == "DIV" && obj.style.overflow == "auto")
			break;
	}
	while (obj);

	return x;
}

// Gets the y position of an object in its parent DIV
function getYInDiv(obj)
{
	if (!obj)
		return 0;
	var y = 0;

	do
	{
		y += obj.offsetTop;
		obj = obj.offsetParent;

		// If parent DIV with an auto overflow is found, position must be retrieved from this DIV
		if (obj && obj.tagName == "DIV" && obj.style.overflow == "auto")
			break;
	}
	while (obj);

	return y;
}

// Enables or disables the selection in HTML page
function SetSelectionStartMode(enableSelection)
{
	if (enableSelection)
	{
		if (typeof document.onselectstart != 'undefined')
			document.onselectstart = function() {return true};
		else if (typeof document.body.style.MozUserSelect != 'undefined')
			document.body.style.MozUserSelect = null;
	}
	else
	{
		if (typeof document.onselectstart != 'undefined')
			document.onselectstart = function() {return false};
		else if (typeof document.body.style.MozUserSelect != 'undefined')
			document.body.style.MozUserSelect = 'none';
	}
}
