// LyUtilsScript.js
// Copyright (c) 2000-2009, Lyria-W4.
// All rights reserved.
// @version	$Id:

var mainFrameName = 'main_frame';
var targetFrameName = 'target_frame';
var ajaxControllerId;
var ajaxParam = '_ajax';
var questionAnswered = false;
var parentFrame = null;

// Array of characters that must be encoded in Ajax requests.
// Do not use escape method for the whole parameters string because it
// does not work with multiple relations.
// Special case for '+' character (not managed by the escape method).
var ajaxChars = new Array("%", "&", "=", "+");
var ajaxEncodedChars = new Array(escape("%"), escape("&"), escape("="), "%2B");


/**********************************************
 *     Management of components selection     *
 **********************************************/

var selectionIds = new Array();

// Adds an id to the list of selected elements for given component
function addSelectionId(componentId, selectionId)
{
	if (selectionId == "")
		return;

	var oldSelection = selectionIds[componentId];

	if ((typeof oldSelection == "undefined") || (oldSelection == null))
		oldSelection = new Array();

	var contains = false;

	for (var i = 0; i < oldSelection.length; i++)
	{
		if (oldSelection[i] == selectionId)
		{
			contains = true;
			break;
		}
	}

	if (!contains)
	{
		oldSelection[oldSelection.length] = selectionId;
		selectionIds[componentId] = oldSelection;
	}
}

// Removes an id from the list of selected elements for given component
function removeSelectionId(componentId, selectionId)
{
	if (selectionId == "")
		return;

	var oldSelection = selectionIds[componentId];

	if ((typeof oldSelection != "undefined") && (oldSelection != null))
	{
		for (var i = 0; i < oldSelection.length; i++)
		{
			if (oldSelection[i] == selectionId)
			{
				oldSelection[i] = "";
				selectionIds[componentId] = oldSelection;
				break;
			}
		}
	}
}

// Clear the list of selected elements for given component
function clearSelectionIds(componentId)
{
	selectionIds[componentId] = null;
}

// Indicates whether id is selected for given component
function hasSelectionId(componentId, selectionId)
{
	if (selectionId == "")
		return false;

	var selection = selectionIds[componentId];

	if ((typeof selection != "undefined") && (selection != null))
	{
		for (var i = 0; i < selection.length; i++)
		{
			if (selection[i] == selectionId)
				return true;
		}
	}

	return false;
}

// Gets the list of selected ids for given component
function getSelectionIds(componentId)
{
	var selection = selectionIds[componentId];
	var result = "";

	if ((typeof selection != "undefined") && (selection != null))
	{
		for (var i = 0; i < selection.length; i++)
		{
			if (selection[i] != "")
			{
				// Use the same separator as used in the getDemand() method in
				// LyStrutsSelectionEventAction.java
				if (result.length > 0)
					result += "__|__";

				result += selection[i];
			}
		}
	}

	return result;
}


/*********************************************
 *     Popup position, size and movement     *
 *********************************************/

var upFrameName = 'up_frame';
var popupFrameName = 'popup_view';
var popupDivName = 'popup_view_div';
var popupTableName = 'popup_view_table';
var popupTdName = 'popup_view_td';
var popupMovingDivName = 'popup_moving_div';
var popupShadowDivName = 'popup_view_shadow';
var modalDivName = 'popup_modal_div';
var waitDivShadowName = 'wait_window_shadow';
var waitDivPanelName = 'wait_window_panel';
var popupViewShown = true;
var popupDefaultLeftOffset = 200;
var popupDefaultTopOffset = 35;
var popupLeftOffset = popupDefaultLeftOffset;
var popupTopOffset = popupDefaultTopOffset;
var popupShadowOffset = 5;
var popupObj = null;
var popupShadowObj = null;
var popupTd = null;
var popupXPos = null;
var popupYPos = null;
var popupWidth = -1;
var popupHeight = -1;
var popupMoving = false;
var popupHadVerticalScrollbar = false;

// Starts the movement of the popup view
function startMovePopup(evt, movedName, movedShadowName, movedTd)
{
	var popupView = findTargetByContent(null, movedName);

	if (popupView == null)
		return;

	var popupViewDivId = movedName;
	var popupShadowViewId = movedShadowName;

	popupObj = popupView.document.getElementById ? popupView.document.getElementById(popupViewDivId) : popupView.document.all.popupViewDivId;
	popupShadowObj = popupView.document.getElementById ? popupView.document.getElementById(popupShadowViewId) : popupView.document.all.popupShadowViewId;
	popupTd = popupView.document.getElementById ? popupView.document.getElementById(movedTd) : popupView.document.all.movedTd;

	setMovingDivSize(false);
}

function setMovingDivSize(full)
{
	var topFrame = getLeonardiTopFrame();
	var popupMovingDiv = topFrame.document.getElementById(popupMovingDivName);

	if (popupMovingDiv == null)
		return;

	if (full)
	{
		popupMovingDiv.style.width = topFrame.document.body.clientWidth;
		popupMovingDiv.style.height = topFrame.document.body.clientHeight;
		popupMovingDiv.style.left = 0;
		popupMovingDiv.style.top = 0;
	}
	else
	{
		popupMovingDiv.style.width = cmGetWidth(popupTd) - (-1);
		popupMovingDiv.style.height = cmGetHeight(popupTd) - (-1);
		popupMovingDiv.style.left = cmGetX(popupTd);
		popupMovingDiv.style.top = cmGetY(popupTd);
		popupMovingDiv.style.visibility = "visible";
	}
}

function startPopupMoving(evt)
{
	setMovingDivSize(true);
	popupMoving = true;

	// Filter seems to change font aspect on IE7
//	if (popupObj != null)
//	{
//		popupObj.style.filter = "alpha(opacity=75)";
//		popupObj.style.MozOpacity  = 0.75;
//	}

//	if (popupShadowObj != null)
//		popupShadowObj.style.visibility = "hidden";
}

// Implements the movement of the popup view
function movePopup(evt)
{
	if (!popupMoving)
	{
		popupXPos = evt.clientX;
		popupYPos = evt.clientY;

		return;
	}

	if (document.getElementById && !(document.all))
	{
		// Firefox
		// Nothing to do
	}
	else if(document.all)
	{
		// IE
		evt = event;
	}

	if (popupXPos == null)
	{
		popupXPos = evt.clientX;
		popupYPos = evt.clientY;
	}

	var diffX = evt.clientX - popupXPos;
	var diffY = evt.clientY - popupYPos;

	var newX = parseInt(popupObj.style.left) + diffX;
	var newY = parseInt(popupObj.style.top) + diffY;

	popupXPos = evt.clientX;
	popupYPos = evt.clientY;

	if (newX <= (-0.90*parseInt(popupObj.style.width)))
		newX = -0.90*parseInt(popupObj.style.width);

	if (newY <= 0)
		newY = 0;

	if (parentFrame == null)
		parentFrame = getLeonardiTopFrame();

	if (parentFrame != null)
	{
		if (newY > (parseInt(parentFrame.document.body.clientHeight)*0.9) )
			newY = (parseInt(parentFrame.document.body.clientHeight)*0.9);

		if (newX > (parseInt(parentFrame.document.body.clientWidth)*0.9) )
			newX = (parseInt(parentFrame.document.body.clientWidth)*0.9);
	}

	popupObj.style.left = newX + "px";
	popupObj.style.top = newY + "px";

	if (popupShadowObj != null)
	{
		popupShadowObj.style.left = newX - (-popupShadowOffset) + "px";
		popupShadowObj.style.top = newY - (-popupShadowOffset) + "px";
	}
}

// Stops the movement of the popup view
function endPopupMoving(evt)
{
//	if (popupObj != null)
//	{
//		popupObj.style.filter = "alpha(opacity=100)";
//		popupObj.style.MozOpacity  = 1;
//	}

//	if (popupShadowObj != null)
//		popupShadowObj.style.visibility = "visible";

	setMovingDivSize(false);
	popupMoving = false;
}

// Show or hides the popup view
function ShowPopupView(show, title, path)
{
	popupViewShown = show;

	var topFrame = getLeonardiTopFrame();
	var parentIframes = topFrame.document.getElementsByTagName('iframe');
	var popupFrame = topFrame.frames[popupFrameName];
	var popupIFrame = parentIframes[popupFrameName];
	var popupShadowDiv = topFrame.document.getElementById(popupShadowDivName);
	var modalDiv = topFrame.document.getElementById(modalDivName);
	var popupDiv = topFrame.document.getElementById(popupDivName);
	var popupTable = topFrame.document.getElementById(popupTableName);
	var popupTd = topFrame.document.getElementById(popupTdName);

	if ((popupIFrame == null) || ((typeof popupIFrame) == "undefined"))
		return;

	if (popupIFrame.style.visibility != "visible")
	{
		popupIFrame.style.width = 80;
		popupIFrame.style.height = 40;
		popupDiv.style.width = 80;
		popupTable.width = 80;
		popupShadowDiv.style.width = 80;
		popupShadowDiv.style.height = 40;
	}

	// Change popup title
	if ((typeof title) != "undefined")
		popupTd.innerHTML = title;

	if (show)
	{
		// Reload the content of the popup view. Do not use the reload() function as it may
		// retrieve page from the cache with old data.
		popupFrame.document.location.replace(path);
	}

	if (!show)
	{
		popupDiv.style.visibility = "hidden";
		popupIFrame.style.visibility = "hidden";
		popupTable.style.visibility = "hidden";
		popupShadowDiv.style.visibility = "hidden";

		if (modalDiv != null)
			modalDiv.style.visibility = "hidden";

		popupDiv.style.left = popupDefaultLeftOffset;
		popupDiv.style.top = popupDefaultTopOffset;
		popupShadowDiv.style.left = popupDefaultLeftOffset - (-popupShadowOffset);
		popupShadowDiv.style.top = popupDefaultTopOffset - (-popupShadowOffset);
	}
	else if ((popupLeftOffset != popupDefaultLeftOffset) || (popupTopOffset != popupDefaultTopOffset))
	{
		// If popupLeftOffset or popupTopOffset has been changed, use these new values
		// (may happen when using the popup frame from contextual event)
		popupDiv.style.left = popupLeftOffset;
		popupDiv.style.top = popupTopOffset;
		popupShadowDiv.style.left = popupLeftOffset - (-popupShadowOffset);
		popupShadowDiv.style.top = popupTopOffset - (-popupShadowOffset);
		popupLeftOffset = popupDefaultLeftOffset;
		popupTopOffset = popupDefaultTopOffset;
	}
}

// Sets the size of the popup
function setPopupDimension(width, height)
{
	popupWidth = width;
	popupHeight = height;
}

// Check the size of the popup view
function checkSizePopupView(currentWin, resetSize, noDecoration, title, modal, center)
{
	var topFrame = getLeonardiTopFrame();
	var parentIframes = topFrame.document.getElementsByTagName('iframe');
	var popupFrame = topFrame.frames[popupFrameName];
	var popupIFrame = parentIframes[popupFrameName];
	var popupDiv = topFrame.document.getElementById(popupDivName);
	var popupTable = topFrame.document.getElementById(popupTableName);
	var popupShadowDiv = topFrame.document.getElementById(popupShadowDivName);
	var modalDiv = topFrame.document.getElementById(modalDivName);
	var popupTd = topFrame.document.getElementById(popupTdName);
	var d;

	if (resetSize)
	{
		popupIFrame.style.width = 80;
		popupIFrame.style.height = 40;
		popupDiv.style.width = 80;
		popupTable.width = 80;
		popupShadowDiv.style.width = 80;
		popupShadowDiv.style.height = 40;
	}
	else if (popupHadVerticalScrollbar)
	{
		var frameOldWidth = popupIFrame.style.width.replace("px", "");
		var tableOldWidth = popupTable.width.replace("px", "");

		popupIFrame.style.width = frameOldWidth - 17;
		popupDiv.style.width = 80;
		popupTable.width = tableOldWidth - 17;
	}

	if (document.getElementById && !(document.all))
		d = popupIFrame.contentDocument;
	else
		d = popupFrame.document;

	// Change popup title
	if ((typeof title) != "undefined")
		popupTd.innerHTML = title;

	var totalWidth = topFrame.document.body.clientWidth;
	var totalHeight = topFrame.document.body.clientHeight;
	var width = d.body.scrollWidth;
	var height = d.body.scrollHeight;
	var fullWidth = true;
	var fullHeight = true;
	var xPos = popupDiv.style.left;
	var yPos = popupDiv.style.top;
	var titleWidth = cmGetWidth(popupTable);

	if (popupWidth > 0)
		width = popupWidth;

	if (popupHeight > 0)
		height = popupHeight - cmGetHeight(popupTable);

	xPos = xPos.replace("px", "");
	yPos = yPos.replace("px", "");

	if (titleWidth > width + 2)
		width = titleWidth - 2;

	if (width > totalWidth - xPos - 10)
	{
		if (noDecoration)
			popupDiv.style.left = xPos - (width - totalWidth - (-xPos) - (-10));
		else
		{
			width = totalWidth - xPos - 30;
			fullWidth = false;
		}
	}

	if (height > totalHeight - yPos - 2 * popupShadowOffset - cmGetHeight(popupTable))
	{
		if (noDecoration)
			popupDiv.style.top = yPos - (height - totalHeight - (-yPos) - (-cmGetHeight(popupTable)));
		else
		{
			height = totalHeight - yPos - 2 * popupShadowOffset - cmGetHeight(popupTable);
			fullHeight = false;
		}
	}

	popupHadVerticalScrollbar = false;

	if (fullWidth && !fullHeight)
	{
		// If there is not enough space for height, add 20 pixels to avoid
		// a useless horizontal scrollbar.
		width += 17;
		popupHadVerticalScrollbar = true;
	}
	else if (!fullWidth && fullHeight)
		// If there is not enough space for width, add 20 pixels to avoid
		// a useless horizontal scrollbar.
		height += 17;

	popupIFrame.style.width = width;
	popupIFrame.style.height = height;
	popupTable.width = width - (-2);
	popupShadowDiv.style.width = width - (-2);
	popupShadowDiv.style.height = cmGetHeight(popupDiv);

	if (center)
	{
		width = d.body.scrollWidth;
		height = d.body.scrollHeight;

		if (width > totalWidth - 10)
			width = totalWidth - 20;

		if (height > totalHeight - 10)
			height = totalHeight - 20;

		popupDiv.style.left = (totalWidth - width) / 2;
		popupDiv.style.top = (totalHeight - height) / 2;
		popupShadowDiv.style.left = (totalWidth - width) / 2 - (-popupShadowOffset);
		popupShadowDiv.style.top = (totalHeight - height) / 2 - (-popupShadowOffset);

		checkSizePopupView(currentWin, resetSize, noDecoration, title, modal, false);
		return;
	}

	if (modalDiv != null)
	{
		modalDiv.style.visibility = (modal ? "visible" : "hidden");

		if (modal)
		{
			modalDiv.style.width = totalWidth;
			modalDiv.style.height = totalHeight;
			modalDiv.style.left = 0;
			modalDiv.style.top = 0;
		}
	}

	if (popupViewShown)
	{
		if (((typeof noDecoration) != "undefined") && noDecoration)
		{
			// If popup has no decoration, place it at the right place (remove title height)
			popupDiv.style.top = cmGetY(popupDiv) - cmGetHeight(popupTable);
			popupShadowDiv.style.top = cmGetY(popupShadowDiv) - cmGetHeight(popupTable);
		}

		popupDiv.style.visibility = "visible";
		popupIFrame.style.visibility = "visible";
		popupTable.style.visibility = (noDecoration ? "hidden" : "visible");
		popupShadowDiv.style.visibility = (noDecoration ? "hidden" : "visible");
	}

	checkSizeMainView('true', 'false');
}


/***********************************
 *     Miscellaneous functions     *
 ***********************************/

// Id of the last div shown
var _lastDivShownId = null;

// This function switches the visibility of a div
function SwitchDivVisibility(divId)
{
	if (!document.all && !document.getElementById)
		return;

	var parentFrame = findTargetByContent(null, divId);

	if (parentFrame == null)
		return;

	var divObj = parentFrame.document.getElementById ? parentFrame.document.getElementById(divId) : parentFrame.document.all.divId;

	if (divObj.style.visibility == "visible")
		divObj.style.visibility = "hidden";
	else
	{
		divObj.style.visibility = "visible";
		_lastDivShownId = divId;
		setTimeout("document.onclick = CloseLastDivShown", 100);
	}
}

// This function switches the display of an expand bar
function SwitchExpandBar(divId, switchImgId, hideImgPath, showImgPath, pageWriterId, toolbarId)
{
	if (!document.all && !document.getElementById)
		return;

	var parentFrame = findTargetByContent(null, divId);

	if (parentFrame == null)
		return;

	var divObj = parentFrame.document.getElementById ? parentFrame.document.getElementById(divId) : parentFrame.document.all.divId;
	var imgObj = parentFrame.document.getElementById ? parentFrame.document.getElementById(switchImgId) : parentFrame.document.all.switchImgId;
	var opened = (divObj.style.display == "none");

	if (opened)
	{
		divObj.style.display = "block";
		imgObj.src = hideImgPath;
	}
	else
	{
		divObj.style.display = "none";
		imgObj.src = showImgPath;
	}

	SendAjaxRequestParams(_rootUrl + STRUTS_EVENT_URL, pageParam + '=' + pageWriterId + '&' + sourceParam + '=switchToolbarState' +
		'&' + valueParam + '=' + toolbarId);
}

// This function closes the latest div that was shown
function CloseLastDivShown()
{
	document.onclick = null;

	if (_lastDivShownId != null)
		setTimeout("SetDivVisibility('" + _lastDivShownId + "', false)", 100);

	_lastDivShownId = null;
}

// This function sets the width and height of a div
function SetDivSize(divId, width, height)
{
	if (!document.all && !document.getElementById)
		return;

	var parentFrame = findTargetByContent(null, divId);

	if (parentFrame == null)
		return;

	var divObj = parentFrame.document.getElementById ? parentFrame.document.getElementById(divId) : parentFrame.document.all.divId;

	if (width >= 0)
		divObj.style.width = width;

	if (height >= 0)
		divObj.style.height = height;
}

// This function sets the left and top positions of a div
function SetDivPosition(divId, left, top)
{
	if (!document.all && !document.getElementById)
		return;

	var parentFrame = findTargetByContent(null, divId);

	if (parentFrame == null)
		return;

	var parentHScroll = parentFrame.document.body.scrollLeft;
	var parentVScroll = parentFrame.document.body.scrollTop;

	var divObj = parentFrame.document.getElementById ? parentFrame.document.getElementById(divId) : parentFrame.document.all.divId;

	divObj.style.left = left + parentHScroll;
	divObj.style.top = top + parentVScroll;
}

// Returns the position of an object
function GetObjectPosition(object)
{
	if (object.offsetParent)
	{
		var parentResult = GetObjectPosition(object.offsetParent);

		return new Array(object.offsetLeft + parentResult[0], object.offsetTop + parentResult[1]);
	}
	else
		return new Array(object.offsetLeft, object.offsetTop);
}

// This function sets the position of a div relatively to a given anchor object
function SetDivAnchor(divId, parentObjectId)
{
	if (!document.all && !document.getElementById)
		return;

	var parentFrame = findTargetByContent(null, divId);

	if (parentFrame == null)
		return;

	var divObj = parentFrame.document.getElementById ? parentFrame.document.getElementById(divId) : parentFrame.document.all.divId;
	var anchorObj = parentFrame.document.getElementById ? parentFrame.document.getElementById(parentObjectId) : parentFrame.document.all.parentObjectId;
	var anchorPos = GetObjectPosition(anchorObj);

	divObj.style.left = anchorPos[0];

	if (typeof anchorObj.height != "undefined" && anchorObj.height != "")
		divObj.style.top = anchorPos[1] + anchorObj.height;
	else
		divObj.style.top = anchorPos[1] + anchorObj.offsetHeight;
}

// This function shows or hides a div
function SetDivVisibility(divId, show)
{
	if (!document.all && !document.getElementById)
		return;

	var parentFrame = findTargetByContent(null, divId);

	if (parentFrame == null)
		return;

	var divObj = parentFrame.document.getElementById ? parentFrame.document.getElementById(divId) : parentFrame.document.all.divId;

	if (show)
		divObj.style.visibility = "visible";
	else
		divObj.style.visibility = "hidden";
}

// This function change the source of an iframe
function SetFrameSrc(frameId, url)
{
	var frame = findTarget(null, frameId, null);

	if (frame == null)
		return;

	frame.location.replace(url);
}

// This function checks the minimum width of a component
function CheckComponentMinWidth(compId, minWidth)
{
	var parentFrame = findTargetByContent(null, compId);

	if (parentFrame == null)
		return;

	var comp = parentFrame.document.getElementById(compId);

	if (cmGetWidth(comp) <= minWidth + 2)
		comp.style.width = minWidth;
}

var lastResizeTime = -1;

// This function checks the size of each splitter component trying to give them the size
// of their parent container (using the splitterIds var).
function checkSizeMainView(recurse, isResizing)
{
	var topFrame = getLeonardiTopFrame();

	if (topFrame != this)
	{
		topFrame.checkSizeMainView(recurse, isResizing);
		return;
	}

	if (document.getElementById && !(document.all))
	{
		// Nothing to do for Firefox
	}
	else if (document.all)
	{
		// IE needs another update at the end of a resize
		var date = (new Date().getTime());

		if (typeof recurse == "undefined" || recurse == 'true')
		{
			lastResizeTime = date;
			setTimeout("checkSizeMainView('false', '" + isResizing + "')", 200);
		}
		else if (recurse == 'false')
		{
			if (lastResizeTime > 0 && date - lastResizeTime > 300)
			{
				checkSizeMainView('stop', isResizing);
				return;
			}

			setTimeout("checkSizeMainView('false', '" + isResizing + "')", 200);
			return;
		}
		else
		{
			lastResizeTime = -1;
		}
	}

	if (document.all && (recurse == 'true'))
	{
		// On IE, when the horizontal scrollbar is displayed, main frame table with a 100% height
		// makes the vertical scrollbar appear. To avoid this, compute the main table height
		// from its frame height.
		var mainFrameFullHeightTableId = 'mainFrameFullHeightTable';
		var topFrame = findTargetByContent(null, mainFrameFullHeightTableId);

		if (topFrame != null)
		{
			var topTable = topFrame.document.getElementById(mainFrameFullHeightTableId);

			topTable.style.height = topFrame.document.body.clientHeight;
		}
	}

	for (var i = splitterIds.length - 1; i >= 0; i--)
	{
		var vars = splitterIds[i];
		var vertical = vars[0];
		var parentId = vars[1];
		var parentFrame = findTargetByContent(null, parentId);

		if (parentFrame == null)
			continue;

		var firstPartId = vars[2];
		var secondPartId = vars[3];
		var firstPartSize = vars[4];
		var setToAuto = vars[5];
		var parentDiv = parentFrame.document.getElementById(parentId);
		var firstDiv = parentFrame.document.getElementById(firstPartId);
		var secondDiv = parentFrame.document.getElementById(secondPartId);

		if ((parentDiv == null) || (typeof parentDiv == "undefined")
			|| (firstDiv == null) || (typeof firstDiv == "undefined")
		 	|| (((secondDiv == null) || (typeof secondDiv == "undefined")) && (secondPartId != '')))
			continue;

		if (isResizing == 'true')
		{
			var parentHeight = cmGetHeight(parentDiv);

			firstDiv.style.height = "50px";

			if (secondDiv != null)
				secondDiv.style.height = "50px";

			var newParentHeight = cmGetHeight(parentDiv);

			// If parent height is lower, it means that children height force parent height.
			// In that case, do not resize children.
			if (parentHeight > newParentHeight)
			{
				firstDiv.style.height = parentHeight;

				if (secondDiv != null)
					secondDiv.style.height = parentHeight;
			}
		}

		if (!vertical)
		{
			firstDiv.style.width = "50px";

			if (secondDiv != null)
				secondDiv.style.width = "50px";
		}

		var parentWidth = cmGetWidth(parentDiv);
		var parentHeight = cmGetHeight(parentDiv);

		if (vertical)
			parentHeight = parentHeight - cmGetYAt(firstDiv, parentDiv) - 8;
		else
			parentWidth = parentWidth - cmGetXAt(firstDiv, parentDiv) - 8;

		var firstSize = '';
		var secondSize = '';

		if (firstPartSize == 'full')
		{
			firstDiv.style.height = parentHeight;
			firstDiv.style.width = parentWidth;
			continue;
		}

		if (vertical)
		{
			firstDiv.style.visibility = "visible";
			secondDiv.style.visibility = "visible";

			if ((firstPartSize.indexOf('0%') == 0) || (firstPartSize == "0"))
			{
				firstDiv.style.visibility = "hidden";
				secondDiv.style.visibility = "visible";
				firstDiv.style.height = "1px";
				secondDiv.style.height = (parentHeight - 1) + "px";
				continue;
			}
			else if ((firstPartSize.indexOf('100%') == 0) || (firstPartSize == "100"))
			{
				firstDiv.style.visibility = "visible";
				secondDiv.style.visibility = "hidden";
				firstDiv.style.height = (parentHeight - 1) + "px";
				secondDiv.style.height = "1px";
				continue;
			}

			if (firstPartSize.indexOf('px') > 0)
			{
				firstSize = firstPartSize;
				secondSize = (parentHeight - firstPartSize.replace("px", "") - 1) + "px";
			}
			else if (firstPartSize == "-1" || firstPartSize == "*")
			{
				firstSize = cmGetHeight(firstDiv) + "px";
				secondSize = (parentHeight - firstSize.replace("px", "") - 1) + "px";
			}
			else
			{
				firstSize = parseInt((parentHeight * firstPartSize) / 100);
				secondSize = (parentHeight - firstSize - 1) + "px";
				firstSize += "px";

				if (setToAuto)
				{
					firstDiv.style.overflow = "auto";

					if (secondDiv != null)
						secondDiv.style.overflow = "auto";
				}
			}

			firstDiv.style.height = firstSize;

			if (secondSize.indexOf('-') == 0)
				secondSize = "50px";

			if (secondDiv != null)
				secondDiv.style.height = secondSize;
		}
		else
		{
			firstDiv.style.visibility = "visible";

			if (secondDiv != null)
				secondDiv.style.visibility = "visible";

			firstDiv.style.height = parentHeight - cmGetYAt(firstDiv, parentDiv);

			if (secondDiv != null)
				secondDiv.style.height = parentHeight - cmGetYAt(secondDiv, parentDiv);

			var newParentHeight = cmGetHeight(parentDiv);

			if (newParentHeight > parentHeight)
			{
				var oldHeight = firstDiv.style.height.replace("px", "");

				firstDiv.style.height = oldHeight - (newParentHeight - parentHeight);

				if (secondDiv != null)
					secondDiv.style.height = oldHeight - (newParentHeight - parentHeight);
			}

			if ((firstPartSize.indexOf('0%') == 0) || (firstPartSize == "0"))
			{
				firstDiv.style.visibility = "hidden";
				firstDiv.style.width = "0px";

				if (secondDiv != null)
				{
					secondDiv.style.visibility = "visible";
					secondDiv.style.width = parentWidth + "px";
				}

				continue;
			}
			else if ((firstPartSize.indexOf('100%') == 0) || (firstPartSize == "100"))
			{
				var widthOffset = (document.all ? 0 : 2);

				firstDiv.style.visibility = "visible";
				firstDiv.style.width = (parentWidth - widthOffset) + "px";

				if (secondDiv != null)
				{
					secondDiv.style.visibility = "hidden";
					secondDiv.style.width = "0px";
				}

				continue;
			}

			if (firstPartSize.indexOf('px') > 0)
			{
				firstSize = firstPartSize;
				secondSize = (parentWidth - firstPartSize.replace("px", "") - 1) + "px";
			}
			else if (firstPartSize == "-1" || firstPartSize == "*")
			{
				firstSize = cmGetWidth(firstDiv) + "px";
				secondSize = (parentWidth - firstSize.replace("px", "") - 1) + "px";
			}
			else
			{
				firstSize = parseInt((parentWidth * firstPartSize) / 100);
				secondSize = (parentWidth - firstSize - 1) + "px";
				firstSize += "px";

				if (setToAuto)
				{
					firstDiv.style.overflow = "auto";

					if (secondDiv != null)
						secondDiv.style.overflow = "auto";
				}
			}

			firstDiv.style.width = firstSize;

			if (secondDiv != null)
				secondDiv.style.width = secondSize;
		}
	}
}

// look all frame and try to find a matching name for a form
function findForm(currentFrame,controllerId,elementName)
{
	if (currentFrame == null)
	{
		// First, try to look into popup view
		var popup = findTarget(null, popupFrameName);

		if (popup != null)
		{
			var	result = findForm(popup,controllerId,elementName);

			if (result != null)
				return result;
		}
	}

	var currentForm;
	var i;
	var found = false;

	if (currentFrame == null)
		currentFrame = getLeonardiTopFrame();

	var Form;

	// To avoid a potential 'Access refused' from the navigator if a page from another server is displayed,
	// use a try / catch.
	try
	{
		// recurse into sub frame
		for (i = 0; i < currentFrame.frames.length; i++)
		{
			Form = findForm(currentFrame.frames[i], controllerId, elementName);
			if ( Form != null )
				return Form;
		}

		// in the current frame, search for the good form
		for (i = 0 ; i < currentFrame.document.forms.length; i++ )
		{
			found = false;
			currentForm = currentFrame.document.forms[i];
			if ( currentForm.elements["_controller"] != null)
			{
				if ((elementName != null) && (typeof currentForm.elements[elementName] != "undefined"))
					found = true;
				else if (((currentForm.elements["_controller"].value == controllerId)
						|| (controllerId == null))
					&& (currentForm.elements[sourceParam] != null))
					found = true;

				if (found)
				{
					currentForm.elements[frameParam].value = currentFrame.name;

					return currentForm;
				}
			}
		}
	}
	catch (ex)
	{
	}

	return null;
}

// Gives the focus to the first input of the first form found in current frame
function ResetFormFocus(selectText, timeout)
{
	if ((timeout == null) || (typeof timeout == "undefined"))
	{
		timeout = 200;

		// Firefox needs more time to ensure page has been entirely loaded before reseting focus.
		if (document.getElementById && !(document.all))
			timeout = 400;
	}

	setTimeout("ResetFormFocusImmediatly(" + selectText + ")", timeout);
}

// Gives the focus to the first input of the first form found in current frame
function ResetFormFocusImmediatly(selectText)
{
	var forms = this.document.forms;

	if ((forms != null) && (forms.length > 0))
	{
		var firstForm = forms[0];
		var formElements = firstForm.elements;

		if ((formElements != null) && (formElements.length > 0))
		{
			var i, n = formElements.length;

			for (i = 0; i < n; i++)
			{
				var formElement = formElements[i];

				if ((formElement.type != "hidden")
					&& (formElement.type != "button")
					&& !formElement.disabled
					&& !formElement.readOnly)
				{
					// Use a try catch because the focus() method may launch an error
					try
					{
						formElement.focus();
					}
					catch (ex)
					{
					}

					if (formElement.type == "text")
					{
						SetEndCaret(formElement, selectText);
					}

					return;
				}
			}
		}
	}
}

// Gives the focus to a given field in a given form
function ForceFocus(formName, fieldId, selectText)
{
	var form = findForm(null, formName, fieldId);

	if (form == null)
		return;

	var field = form.elements[fieldId];

	if ((typeof field != "undefined")
		&& (field.type != "hidden")
		&& (field.type != "button")
		&& !field.disabled
		&& !field.readOnly)
	{
		// Use a try catch because the focus() method may launch an error
		try
		{
			field.focus();
		}
		catch (ex)
		{
		}

		if (field.type == "text")
		{
			SetEndCaret(field, selectText);
		}
	}
}

// Sets the caret at the end of an input of type text
function SetEndCaret(textInput, selectText)
{
	var caretStart = textInput.value.length;
	var caretEnd = textInput.value.length;

	textInput.focus();

	if (selectText)
	{
		textInput.select();
		return;
	}

	// IE support
	if (document.selection)
	{
		var oSel = document.selection.createRange();

		oSel.moveStart ('character', -textInput.value.length);
		oSel.moveEnd ('character', -textInput.value.length);

		if (caretEnd != null)
			oSel.moveEnd('character', caretEnd);
		else
			oSel.moveEnd('character', caretStart);

		oSel.moveStart('character', caretStart);
		oSel.select();
	}
	// Firefox support
	else if (textInput.selectionStart || textInput.selectionStart == '0')
	{
		textInput.selectionStart = caretStart;

		if (caretEnd != null)
			textInput.selectionEnd = caretEnd;
		else
			textInput.selectionEnd = caretStart;
	}
}

// Indicates if a Leonardi main frame is contained in the window
function hasLeonardiMainFrame()
{
	var parentFrame = this;

	while ((parentFrame != null) && (typeof parentFrame != "undefined") && (parentFrame != top))
	{
		if (parentFrame.name == targetFrameName || parentFrame.name == popupFrameName)
			return true;

		if ((parentFrame.frames != null)
				&& (parentFrame.frames.length > 0)
				&& (parentFrame.frames[targetFrameName] != null))
			return true;

		parentFrame = parentFrame.parent;
	}

	if ((parentFrame != null)
			&& (parentFrame.frames != null)
			&& (parentFrame.frames.length > 0)
			&& (parentFrame.frames[targetFrameName] != null))
		return true;

	return false;
}

// Returns the Leonardi application top frame
function getLeonardiTopFrame()
{
	// _topFrame variable may have been set
	if ((typeof _topFrame != "undefined") && (_topFrame != null))
		return _topFrame;

	var parentFrame = this;
	var loop = 0;

	for (var loop = 0; (loop < 10) && (parentFrame != null) && (typeof parentFrame != "undefined") && (parentFrame != top); loop++)
	{
		// If target frame was found, return its parent
		if (parentFrame.name == targetFrameName || parentFrame.name == popupFrameName)
			return parentFrame.parent;

		if ((parentFrame.frames != null)
				&& (parentFrame.frames.length > 0)
				&& (parentFrame.frames[targetFrameName] != null))
			return parentFrame;

		loop++;
		parentFrame = parentFrame.parent;
	}

	return top;
}

// Returns the Leonardi application target frame
function getLeonardiTargetFrame()
{
	var topFrame = getLeonardiTopFrame();

	return topFrame.frames[targetFrameName];
}

// look all frame and try to find a matching name
function findTarget(currentWindow, name, pageWriterId)
{
	var i;
	var retour;

	if (currentWindow == null)
	{
		var topFrame = getLeonardiTopFrame();

		if (name == 'top_frame')
			return topFrame; // Got it
		currentWindow = topFrame;
	}

	// TEMPORARY FIX for consult multiple objects ans using "next" button.
	if ((pageWriterId == popupFrameName) || ((pageWriterId != null) && endsWith2(pageWriterId, '_specific_frame')))
		pageWriterId = null;

	// To avoid a potential 'Access refused' from the navigator if a page from another server is displayed,
	// use a try / catch.
	try
	{
		if (currentWindow == null || currentWindow.name == name)
		{
			return currentWindow;
		}

		for (i= 0; i<currentWindow.frames.length; i++)
		{
			retour = findTarget(currentWindow.frames[i], name, pageWriterId);

			if (retour != null)
			{
				if ((pageWriterId != null) && ((typeof retour.pageWriterId) != "undefined") && (retour.pageWriterId != pageWriterId))
				{
					return null;
				}
				return retour;
			}
		}
	}
	catch (ex)
	{
	}

	return null;
}

// look all frames and try one containing specified element
function findTargetByContent(currentWindow, contentId)
{
	var i;
	var retour;

	if (currentWindow == null)
	{
		currentWindow = getLeonardiTopFrame();
	}

	// To avoid a potential 'Access refused' from the navigator if a page from another server is displayed,
	// use a try / catch.
	try
	{
		var obj = currentWindow.document.getElementById?  currentWindow.document.getElementById(contentId) : currentWindow.document.all.contentId;

		if ((typeof (obj) != "undefined") && (obj != null))
			return currentWindow; // FOUND

		for (i= 0; i<currentWindow.frames.length; i++)
		{
			retour = findTargetByContent(currentWindow.frames[i], contentId);

			if (retour != null)
				return retour;
		}
	}
	catch (ex)
	{
	}

	return null;
}

// this function load the url in the parent of MAIN_FRAME
function loadUrlPage(url)
{
	var targetFrame = findTarget(null,targetFrameName);

	if (targetFrame != null)
	{
		if (targetFrame.parent != "undefined")
			targetFrame.parent.location.replace(url); // was location=
		else
			getLeonardiTopFrame().window.location.replace(url);// was location=
	}
	else
		this.location.replace(url);// was location=
}

function endsWith2(source, pattern)
{
	n = pattern.length;
	m = source.length;

	temp = source.substring(m-n);

	if (temp == pattern)
		return true;
	else
		return false;
}

// Used to replace the HTML content of a DIV tag such as a menu, a tool or a message
// Used in DHTML only
function replaceDivInnerHtml(frameId, divId, newInnerHTML)
{
	var frame;

	if (this.name == frameId)
		frame = this;
	else
		frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var divObj = frame.document.getElementById(divId)

		if (divObj != null)
		{
			if (typeof(this.ReplaceString) == "undefined")
				return;

			newInnerHTML = ReplaceString(newInnerHTML, '&quot;', '"');
			divObj.innerHTML = newInnerHTML;
		}
	}
	// If not found, try to find div in the target frame
	else if (frameId != targetFrameName)
		replaceDivInnerHtml(targetFrameName, divId, newInnerHTML)
}

// This function removes an entire DIV tag
function removeDiv(frameId, divId)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var divObj = frame.document.getElementById(divId)

		if (divObj != null)
		{
			var parentTag = divObj.parentElement;

			parentTag.removeChild(divObj);
		}
	}
}

// This function moves a DIV tag to a new absolute position
function moveDiv(frameId, divId, x, y)
{
	var frame = findTarget(null, frameId, null);

	if (frameId == 'null')
		frame = this;

	if (frame != null)
	{
		var divObj = frame.document.getElementById(divId)
		if (divObj != null)
		{
			divObj.style.left = x + "px";
			divObj.style.top = y + "px";
		}
	}
}

// This function adds a DIV tag to an existing DIV tag
function addDiv(frameId, parentDivId, divContent, divId)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		// Check if DIV already exists
		if ((divId != null) && (typeof divId != "undefined"))
		{
			var divObj = frame.document.getElementById(divId)

			if (divObj != null)
				return;
		}

		var parentDivObj = frame.document.getElementById(parentDivId)

		if (parentDivObj != null)
		{
			var	newDiv = document.createElement("DIV");

			parentDivObj.insertAdjacentHTML("afterBegin", divContent);
		}
	}
}

// This function resizes a DIV tag
function resizeDiv(frameId, divId, width, height)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var divObj = frame.document.getElementById(divId)

		if (divObj != null)
		{
			divObj.style.width = width;
			divObj.style.height = height;
		}
	}
}

// This function sets the clip zone and visibility of a div
function setDivClip(frameId, divId, clip, visible)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var divObj = frame.document.getElementById(divId)

		if (divObj != null)
		{
			divObj.style.clip = clip;

			if (visible)
				divObj.style.display = "block";
			else
				divObj.style.display = "none";
		}
	}
}

// This function displays or hides an element
function displayElement(frameId, elementId, display)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var element = frame.document.getElementById(elementId)

		if (element != null)
		{
			// Do not use block (may cause troubles with Firefox)
			if (display)
				element.style.display = "";
			else
				element.style.display = "none";
		}
	}
}

// Used to select : unselectAll in DHTML tables
// Used in DHTML only
function selectAllInTable(tableId, frameId, selected, color)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
		frame.selectAll(tableId, selected, color);
}

// Change the css style of an HTML object
function ChangeObjectClass(objectId, cssClass)
{
	var frame = findTargetByContent(null, objectId);

	if (frame != null)
	{
		var htmlObj = frame.document.getElementById(objectId);

		htmlObj.className = cssClass;
	}
}

// Change the image of an HTML img
function SetImageSrc(imageId, imageSrc)
{
	var frame = findTargetByContent(null, imageId);

	if (frame != null)
	{
		var imgObj = frame.document.getElementById(imageId);

		imgObj.src = imageSrc;
	}
}

// Change the image background of an HTML object
function SetObjectBackgroundImage(objectId, imageSrc, repeatMode)
{
	var frame = findTargetByContent(null, objectId);

	if (frame != null)
	{
		var obj = frame.document.getElementById(objectId);

		obj.style.background = imageSrc;
		obj.style.backgroundRepeat = repeatMode;
	}
}

// Change the tooltip of an HTML img
function SetImageTooltip(imageId, imageTooltip)
{
	var frame = findTargetByContent(null, imageId);

	if (frame != null)
	{
		var imgObj = frame.document.getElementById(imageId);

		imgObj.alt = imageTooltip;
		imgObj.title = imageTooltip;
	}
}

// Change the title of the Leonardi top frame
function setTopTitle(title)
{
	var topFrame = getLeonardiTopFrame();

	if ((topFrame != null) && (typeof topFrame != "undefined"))
		topFrame.document.title = title;
}

// Scroll down to the active row
function ShowActiveRow(rowId, frameId)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var divObj = frame.document.getElementById(rowId)

		if (divObj != null)
		{
			if (typeof(divObj.scrollIntoView) == "undefined")
				return;

			divObj.scrollIntoView(false);
		}
	}
}

// Change the color of a row in a table
function SetRowColor(rowId, color, frameId)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var trObject = frame.document.getElementById(rowId)

		if (trObject != null)
		{
			var ie= (navigator.appName == "Microsoft Internet Explorer")?1:0;

			if(ie)
			{
				for (var j = 0; j < trObject.children.length; j++)
				{
					if (trObject.childNodes[j].tagName == "TD")
					{
						var	td = trObject.children[j];
						td.style.backgroundColor = color;
					}
				}
			}
			else
			{
				for (var j = 0; (j < trObject.cells.length); j++)
				{
					var	td = trObject.cells[j];
					td.style.backgroundColor = color;
				}
			}
		}
	}
}

// Set the background color of an HTML object
function SetObjectColor(frameId, objId, color)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var obj = frame.document.getElementById(objId)

		if (obj != null)
		{
			if (color == null)
				color = "";

			// oldBackgroundColor is needed for table cells
			obj.style.oldBackgroundColor = color;
			obj.style.backgroundColor = color;
		}
	}
}

// Set the attribute value of a DIV
function SetDivAttribute(frameId, divId, name, value)
{
	var frame = findTarget(null, frameId, null);

	if (frame != null)
	{
		var obj = frame.document.getElementById(divId)

		if (obj != null)
			obj.setAttribute(name, value);
	}
}

// Update the position of the cell decorations in a given frame
function UpdateCellDecorations(frameId)
{
	var frame = findTarget(null, frameId, null);

	if ((frame != null) && (frame.cellDecorations != null) && (typeof frame.cellDecorations != "undefined"))
	{
		for (var i = 0; i < frame.cellDecorations.length; i++)
		{
			frame.UpdateCellDecoration(frame.cellDecorations[i], false, frameId);
		}
	}
}


/*******************************************************
 *     Ajax functions used to refresh page content     *
 *******************************************************/

var showWaitDivRequest = false;

// Refresh the content of a div by sending an Ajax request
function RefreshDiv(frameId, writerId, divId, frameValue, path, separator, target)
{
	var ajaxRequest;

	if (window.XMLHttpRequest)
		ajaxRequest = new XMLHttpRequest();
	else if (window.ActiveXObject)
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

	showWaitDiv(true, true);

	// Dynamic creation of a Javascript function to be sure to use the right ajax request in the response
	var ajaxCallback = function()
	{
		ProcessRefreshDiv(ajaxRequest, frameId, divId, separator);
	}

	var url = _rootUrl + path;
	var params = pageParam + "=" + writerId + "&" + frameParam + "=" + frameValue + "&" + ajaxParam + "=true";

	if ((typeof target != "undefined") && (target != null) && (target != "null"))
		params += "&target=" + target;

	ajaxRequest.open("POST", url, true);
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
	ajaxRequest.onreadystatechange = ajaxCallback;
	ajaxRequest.send(params);
}

// This function implements the process of an ajax request used to refresh the content of a div
function ProcessRefreshDiv(ajaxRequest, frameId, divId, separator)
{
	if (ajaxRequest.readyState == 4)
	{
		if (ajaxRequest.status == 200)
		{
			var text = ajaxRequest.responseText;
			var scripts = StringToArray(text, separator);
			var content = scripts[0];
			var scripts = scripts[1];

			replaceDivInnerHtml(frameId, divId, content);

			EvalScripts(scripts);

			// If div is refreshed in the popup view, check its size
			if (frameId == popupFrameName)
				checkSizePopupView(null, false);
			else if (scripts.indexOf('checkSizeMainView(') < 0)
				checkSizeMainView('true', 'false');

			showWaitDiv(false, true);
		}
	}
}

// Shows or hides the wait panel
function showWaitDiv(show, recurse)
{
	// Does nothing. Must be overridden in a specific home JSP for example.
	var topFrame = getLeonardiTopFrame();

	if (!recurse)
	{
		if (show)
		{
			showWaitDivRequest = true;
			setTimeout("RefreshWaitDiv()", 1000);
		}
		else
		{
			showWaitDivRequest = false;
		}
	}
	else
		topFrame.showWaitDiv(show, false);
}

// Check the visibility state of the wait panel
function RefreshWaitDiv()
{
	var topFrame = getLeonardiTopFrame();
	var waitDivPanel = topFrame.document.getElementById(waitDivPanelName);
	var waitDivShadow = topFrame.document.getElementById(waitDivShadowName);

	if ((waitDivPanel == null) && (waitDivShadow == null))
		return;

	if (showWaitDivRequest)
	{
		waitDivShadow.style.width = topFrame.document.body.clientWidth;
		waitDivShadow.style.height = topFrame.document.body.clientHeight;
		waitDivShadow.style.left = 0;
		waitDivShadow.style.top = 0;

		// Center wait panel
		waitDivPanel.style.left = (topFrame.document.body.clientWidth - cmGetWidth(waitDivPanel)) / 2;
		waitDivPanel.style.top = (topFrame.document.body.clientHeight - cmGetHeight(waitDivPanel)) / 2;

		waitDivShadow.style.visibility = 'visible';
		waitDivPanel.style.visibility = 'visible';

		setTimeout("RefreshWaitDiv()", 200);
	}
	else
	{
		waitDivShadow.style.visibility = 'hidden';
		waitDivPanel.style.visibility = 'hidden';
	}
}

// Indicates if Ajax mode must be used for a given form
function IsAjaxMode(form)
{
	var hasMainFrame = hasLeonardiMainFrame();

	if (!hasMainFrame)
		return false;

	// Check the AJAX_MODE param on the form
	if ((form != null) && (typeof form.elements['AJAX_MODE'] != "undefined"))
	{
		var ajaxMode = form.elements['AJAX_MODE'].value;
		return (ajaxMode == 'true');
	}

	// If the information was not found on the form, use Ajax mode from
	// current frame
	return AJAX_MODE;
}

// Send an Ajax request to the server
function SendAjaxRequest(form, path)
{
	var url = _rootUrl;

	if (((typeof path) != "undefined") && (path != null))
		url += path;
	else if (((typeof STRUTS_EVENT_URL) != "undefined") && (STRUTS_EVENT_URL != null))
		url = url + STRUTS_EVENT_URL;

	var	params = "";
	var	firstParam = true;

	for (var i = 0; i < form.elements.length; i++)
	{
		var add = true;
		var elementName = "", elementValue = "";

		if (((form.elements[i].type == "radio") || (form.elements[i].type == "checkbox"))
				&& (!form.elements[i].checked))
			add = false;

		if (add)
		{
			if (firstParam)
				firstParam = false;
			else
				params += "&";

			// Retrieve element name and value and replace special caracters
			// by their encoded string
			elementName = (form.elements[i].name);
			elementValue = (form.elements[i].value);

			for (var j = 0; j < ajaxEncodedChars.length; j++)
			{
				elementName = ReplaceString(elementName, ajaxChars[j], ajaxEncodedChars[j]);
				elementValue = ReplaceString(elementValue, ajaxChars[j], ajaxEncodedChars[j]);
			}

			params += elementName + "=" + elementValue;
		}
	}

	url = ReplaceString(url, "&#38;", "&");

	var	pos = url.indexOf('?');

	if (form == null)
		params = url.substring(pos + 1, url.length);

	if (pos > 0)
		url = url.substring(0, pos);

	if (form != null)
		ajaxControllerId = form.elements['_controller'].value;

	SendAjaxRequestParams(url, params);
}

// Send an Ajax request to the server
function SendAjaxRequestParams(url, params, showWait)
{
	var ajaxRequest;

	if (window.XMLHttpRequest)
		ajaxRequest = new XMLHttpRequest();
	else if (window.ActiveXObject)
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

	if ((showWait == null) || (typeof showWait == "undefined") || (showWait != 'false'))
		showWaitDiv(true, true);

	// Dynamic creation of a Javascript function to be sure to use the right ajax request in the response
	var ajaxCallback = function()
	{
		ProcessAjaxRequest(ajaxRequest);
	}

	ajaxRequest.open("POST", url, true);
	ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
	ajaxRequest.onreadystatechange = ajaxCallback;

	if (params.indexOf(ajaxParam) < 0)
		params += "&" + ajaxParam + "=true";

	// Specific process
	var topFrame = getLeonardiTopFrame();

	if (topFrame.beforeAjaxRequestSent)
	      topFrame.beforeAjaxRequestSent(ajaxRequest);

	ajaxRequest.send(params);
}

// This function implements the process of an ajax request
function ProcessAjaxRequest(ajaxRequest)
{
	// Specific process
	var topFrame = getLeonardiTopFrame();

	if (topFrame.beforeAjaxRequestProcessed)
	      topFrame.beforeAjaxRequestProcessed(ajaxRequest);

	if (ajaxRequest.readyState == 4)
	{
		if (ajaxRequest.status == 200)
		{
			changedField = null;
			showWaitDiv(false, true);

			var text = ajaxRequest.responseText;

			EvalScripts(text);

			var ajaxForm = findForm(null, ajaxControllerId);

			if ((ajaxForm != null) && (ajaxForm.elements['_submitted'] != null))
			{
				if (ajaxForm.elements['_submitted'].value == "submitted")
				{
					ajaxForm.elements['_submitted'].value = "";
					ajaxControllerId = null;
					return;
				}
			}
		}
	}
}

// This function calls the eval() function on a text containing several scripts
function EvalScripts(text)
{
	if ((text == null) || ((typeof text) == "undefined"))
		return;

	var sep_begin = "//<![CDATA[";
	var sep_end = "//]]>";
	var checkSizePopup = null;
	var hasShowPopup = false;
	var addScript;

	if (text.indexOf("<LEONARDI_WINDOW ") == 0)
	{
		var pos1 = text.indexOf("<LEONARDI_WINDOW ");
		var pos2 = text.indexOf(">");
		var pos3 = text.indexOf("</LEONARDI_WINDOW>");
		var windowName = text.substring(pos1 + "<LEONARDI_WINDOW ".length, pos2);
		var windowText = text.substring(pos2 + 1, pos3);

		text = text.substring(pos3 + "</LEONARDI_WINDOW>".length);

		// Check if window is registered with this name
		for (var i = 0; i < newWindows.length; i++)
		{
			if (newWindows[i][0] == windowName)
			{
				newWindows[i][1].EvalScripts(windowText);
			}
		}
	}

	if (text.indexOf("<script") >= 0)
	{
		var scripts = SplitString(text,
			"<script type='text/javascript'>", "</script>");

		text = "";

		for (var i = 0; i < scripts.length; i++)
		{
			addScript = false;

			var pos1 = scripts[i].indexOf(sep_begin);

			if (pos1 >= 0)
				scripts[i] = scripts[i].substring(pos1 + sep_begin.length);

			var pos2 = scripts[i].indexOf(sep_end);

			if (pos2 > 0)
				scripts[i] = scripts[i].substring(0, pos2);

			if (scripts[i].indexOf("checkSizePopupView") >= 0)
				checkSizePopup = scripts[i];
			else
				addScript = true;

			if (scripts[i].indexOf("showPopupView") >= 0)
				hasShowPopup = true;

			if ((scripts[i].indexOf("hidePopupView") >= 0) && hasShowPopup)
				addScript = false;

			if (addScript)
				text += scripts[i];
		}
	}

	if (checkSizePopup != null)
		text = text + checkSizePopup;

//	alert(text);

	eval(text);
}
