// TODO: - FAP
// ******************************************************************************
// row       : 
// highlight : 
// ******************************************************************************
function hlRow(row, highlight)
{
	if(highlight)
	{
		if (row.className.indexOf('over') == -1)
		{
			row.oldClassName = row.className;
			row.className += 'over';
		}
	}
	else
	{
		if (row.oldClassName)
			row.className = row.oldClassName;
	}
}

// TODO: - FAP
// ******************************************************************************
// divHeader    : 
// divContentId : 
// stateTxtId   : 
// ******************************************************************************
function togglePanel(divHeader, divContentId, stateTxtId)
{
	var state;
	var divContent = document.getElementById(divContentId);
	var txtState = document.getElementById(stateTxtId);
	if (divHeader == null || divContent == null || txtState == null) return;

	if (divContent.className == 'contentclosed')
		state = 'open';
	else
		state = 'closed';

	// Remember the state
	txtState.value = state;

	divHeader.className = 'header' + state;
	divContent.className = 'content' + state;
}

// TODO: - FAP
// ******************************************************************************
// chk   : 
// divId : 
// ******************************************************************************
function togglePonyConfirm(show, divID, chkConfirmID)
{
	var div = document.getElementById(divID);
	if (!div)
		return;

	if (show)
		div.style.display = 'block';
	else
	{
		div.style.display = 'none';

		var chk = document.getElementById(chkConfirmID);
		if (chk) chk.checked = false;
	}
}

// TODO: - FAP
// ******************************************************************************
// txtCurrID : 
// txtCommID : 
// txtCompID : 
// chkID     : 
// invert    : 
// ******************************************************************************
function UpdateCompleteName(txtCurrID, txtCommID, txtCompID, chkID, invert)
{
	var txtCurr = document.getElementById(txtCurrID);
	var txtComm = document.getElementById(txtCommID);
	var txtComp = document.getElementById(txtCompID);
	var chk = document.getElementById(chkID);

	if (!txtCurr || !txtComm || !txtComp || !chk)
		return;

	var curr = trim(txtCurr.value);
	var comm = trim(txtComm.value);

	if (invert)
		chk.checked = !chk.checked;

	if (!chk.checked)
		// Commercial name - Prefix
		txtComp.value = trim(comm + ' ' + curr); //.toUpperCase(); -> toUpperCase method causes problem with some keys (e.g. arrows)
	else
		// Commercial name - Suffix
		txtComp.value = trim(curr + ' ' + comm); //.toUpperCase(); -> toUpperCase method causes problem with some keys (e.g. arrows)
}

// Toggle the debug window visibility.
// ******************************************************************************
// panelId : id of the debug control (should be a DIV element)
// ******************************************************************************
function ToggleDebugVisibility(panelId)
{
	var div = document.getElementById(panelId);
	if (!div)
		return;

	if (div.className == 'debughidden')
	{
		div.className = 'debug';
		div.style.left = '0px';
	}
	else
	{
		div.className = 'debughidden';
		div.style.left = (5 - div.clientWidth) + 'px';
	}
}

/*******************************************/
// GENERIC functions
/*******************************************/

// TODO: - FAP
// ******************************************************************************
// 
// ******************************************************************************
function ieTrueBody()
{
	return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

// Trim the given text string.
// ******************************************************************************
// text : the text to be trimmed
// ******************************************************************************
function trim(text)
{
	return (text || "").replace( /^\s+|\s+$/g, "" );
}

// Extends the Array object with a method allowing to remove a given item.
// ******************************************************************************
// index : the index of the item to remove
// ******************************************************************************
Array.prototype.remove = function(index)
{
	return this.splice(index, 1);
}

// Cross browser event handler attachment and detachment.
// ******************************************************************************
// obj    : the object to attach event to
// evName : name of the event - DON'T ADD "on", pass only "mouseover", etc
// fct    : function to call
// ******************************************************************************
function addEvent(obj, evName, fct)
{
	// IE
	if (obj.attachEvent)
	{
		var r = obj.attachEvent('on' + evName, fct);
		return r;
	}
	// Gecko / W3C
	else if (obj.addEventListener)
	{
		obj.addEventListener(evName, fct, true);
		return true;
	}
	else
	{
		//el['on' + evName] = func;
		//return true;
		return false;
	}
}
function removeEvent(obj, evName, fct)
{
	// IE
	if (obj.detachEvent)
	{
		var r = obj.detachEvent('on' + evName, fct);
		return r;
	}
	// Gecko / W3C
	else if (obj.removeEventListener)
	{
		obj.removeEventListener(evName, fct, true);
		return true;
	}
	else
	{
		//el['on' + evName] = null;
		//return true;
		alert('Handler could not be removed');
	}
}

// Cross browser (full) width/height.
// http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655
// ******************************************************************************
// n/a
// ******************************************************************************
function getViewportHeight()
{
	if (window.innerHeight != window.undefined)
		return window.innerHeight;
	if (document.compatMode == 'CSS1Compat')
		return document.documentElement.clientHeight;
	if (document.body)
		return document.body.clientHeight;
	return window.undefined;
}
function getViewportWidth()
{
	if (window.innerWidth != window.undefined)
		return window.innerWidth;
	if (document.compatMode == 'CSS1Compat')
		return document.documentElement.clientWidth;
	if (document.body)
		return document.body.clientWidth;
	return window.undefined;
}

// Get the index of an item within the given Array
// ******************************************************************************
// arr : the Array containing the item
// val : the value of the item to search the index for
// ******************************************************************************
function indexOf(arr, val)
{
	for (var i = 0; i < arr.length; i++)
	{
		if (arr[i] == val)
			return i;
	}
	return -1;
}

// TODO: - FAP
// ******************************************************************************
// 
// ******************************************************************************
function FindElementByAttribute(el, tagName, attributeName)
{
	var str = el.getAttribute(attributeName);
	while (el != null && (str == null || str == ""))
	{
		el = GetSelectedElement(el.parentNode, tagName);
		if (el != null)
			str = el.getAttribute(attributeName);
	}
	return el;
}

// Get first/last child element within the given element
// ******************************************************************************
// el : the object to attach event to
// ******************************************************************************
function GetFirstChildElement(el)
{
	for (var i = 0; i < el.childNodes.length; i++)
	{
		if (el.childNodes[i].nodeType == 1)
			return el.childNodes[i];
	}
	return null;
}
function GetLastChildElement(el)
{
	for (var i = el.childNodes.length - 1; i >= 0; i--)
	{
		if (el.childNodes[i].nodeType == 1)
			return el.childNodes[i];
	}
	return null;
}

// TODO: - FAP
// ******************************************************************************
// 
// ******************************************************************************
function GetSelectedElement(el, tagName)
{
	while(el != null && el.tagName != tagName)
		el = el.parentNode;
	return el;
}

// TODO: - FAP
// ******************************************************************************
// 
// ******************************************************************************
function GetElementPosition(el, relativeEl)
{
	var result = new Object();
	result.x = 0;
	result.y = 0;
	result.width = 0;
	result.height = 0;

	if (el.offsetParent)
	{
		var parent = el;
		while (parent != null &&
			   parent != relativeEl)
		{
			result.x += parent.offsetLeft;
			result.y += parent.offsetTop;
			// AdjustScrollPosition(parent, relativeEl, result);
			var parentTagName = parent.tagName.toLowerCase();
			if (parentTagName != "table" &&
				parentTagName != "body" &&
				parentTagName != "html" &&
				parentTagName != "div" &&
				parent.clientTop &&
				parent.clientLeft)
			{
				result.x += parent.clientLeft;
				result.y += parent.clientTop;
			}
//		  if (browseris.ie && parentTagName=="td")
//		  {
//			  if (parent.runtimeStyle.borderTopStyle != "none" ||
//				  parent.currentStyle.borderTopStyle != "none")
//			  {
//				  var shift;
//				  if (parent.runtimeStyle.borderTopWidth != "")
//					  shift = parseInt(parent.runtimeStyle.borderTopWidth);
//				  else
//					  shift = parseInt(parent.currentStyle.borderTopWidth);
//				  if (!isNaN(shift))
//					  result.y += shift;
//			  }
//			  if (parent.runtimeStyle.borderLeftStyle != "none" ||
//				  parent.currentStyle.borderLeftStyle != "none")
//			  {
//				  var shift;
//				  if (parent.runtimeStyle.borderLeftWidth != "")
//					  shift = parseInt(parent.runtimeStyle.borderLeftWidth);
//				  else
// 	 	 	 	 	  shift = parseInt(parent.currentStyle.borderLeftWidth);
// 	 	 	 	  if (!isNaN(shift))
// 	 	 	 	 	  result.x += shift;
// 	 	 	  }
// 	 	  }
 	 	 	parent = parent.offsetParent;
 	 	}
 	}
 	else if (el.left && el.top)
 	{
 	 	result.x = el.left;
 	 	result.y = el.top;
 	}
 	else
 	{
 	 	if (el.x)
 	 	 	result.x = el.x;
 	 	if (el.y)
 	 	 	result.y = el.y;
 	}
 	if (el.offsetWidth && el.offsetHeight)
 	{
 	 	result.width = el.offsetWidth;
 	 	result.height = el.offsetHeight;
 	}
 	else if (el.style && el.style.pixelWidth && el.style.pixelHeight)
 	{
 	 	result.width = el.style.pixelWidth;
 	 	result.height = el.style.pixelHeight;
 	}
 	return result;
}

// TODO: - FAP
// ******************************************************************************
// arguments (JS implicit) : the arguments passed by the caller
// ******************************************************************************
function DeferCall()
{
 	if (arguments.length==0)
 	 	return null;

 	var args = arguments;

 	var fct = null;
 	eval("if (typeof(" + args[0] + ") == 'function') { fct = " + args[0] + "; }");

 	if (fct == null)
 	 	return null;

 	if (args.length == 1) return fct();
 	else if (args.length == 2) return fct(args[1]);
 	else if (args.length == 3) return fct(args[1], args[2]);
 	else if (args.length == 4) return fct(args[1], args[2], args[3]);
 	else if (args.length == 5) return fct(args[1], args[2], args[3], args[4]);
 	else if (args.length == 6) return fct(args[1], args[2], args[3], args[4], args[5]);
 	else if (args.length == 7) return fct(args[1], args[2], args[3], args[4], args[5], args[6]);
 	else if (args.length == 8) return fct(args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
 	else if (args.length == 9) return fct(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
 	else if (args.length == 10) return fct(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
 	else
 	 	alert("Too many arguments passed to DeferCall.");

 	return null;
}

