// LyActionScript.js
// Copyright (c) 2000-2009, Lyria-W4.
// All rights reserved.
// @version	$Id: LyActionScript.js,v 1.13.4.1 2009/10/12 16:20:23 jls Exp $

var parameters = 'scrollbars=yes,resizable=yes';
var size = 'width=640,height=480';
var smallSize = 'width=360,height=280';
var defaultDialogName = 'dialog';
var sourceParam = '_source';
var commandParam = '_command';
var hiddenParam = '_hidden';
var frameParam = '_frame';
var controllerParam = '_controller';
var pageParam = '_page';
var valueParam = '_value';
var typeParam = '_type';
var indexParam = '_index';
var extendParam = '_extend';
var submittedParam = '_submitted';
var targetControllerIdParam = '_target_controller';
var ajaxParam = '_ajax';
var positionX = '_positionX';
var positionY = '_positionY';

// This function invokes a command.
function InvokeCommand(actionId, url, noResult, isCheckField, submitMessage, fileErrorMessage, controllerId)
{
	var value;
	var targetForm;
	var mySelectionForm = findForm(null, controllerId);

	// If no form was found, try to get the first one in the current frame.
	// May happen if several controllers are used in the same frame.
	if ((mySelectionForm == null)
			&& (typeof window.document.forms[0] != "undefined"))
		mySelectionForm = window.document.forms[0];

	if ((mySelectionForm != null) && (typeof mySelectionForm.elements != "undefined") )
	{
		if ((submitMessage != null) && (mySelectionForm.elements[submittedParam] != null))
		{
			if (mySelectionForm.elements[submittedParam].value == "submitted")
			{
				alert(submitMessage);
				return;
			}
			else
			 	mySelectionForm.elements[submittedParam].value = "submitted";

		}

		// set the form action URL : usefull for struts (url may vary)
		if ((typeof url != "undefined") && (url != null) && (url != ""))
		{
			var actionUrl = "";
			if (((typeof _rootUrl) != "undefined") && (_rootUrl != null))									
				actionUrl = _rootUrl;
							
			actionUrl += url;
			actionUrl = ReplaceString(actionUrl, "&#38;","&");
				
			mySelectionForm.action=actionUrl;				
		}		

		// Set the actionId input in the form
		if (mySelectionForm.elements[sourceParam] != null)
			mySelectionForm.elements[sourceParam].value = actionId;

		// Set the actionId input in the form
		if (mySelectionForm.elements[commandParam] != null)
			mySelectionForm.elements[commandParam].value = actionId;

		// Set the actionId input in the form
		if (mySelectionForm.elements[commandParam] != null)
			mySelectionForm.elements[controllerParam].value = controllerId;

		// Set the use of the hiddenFrame
		if (mySelectionForm.elements[hiddenParam] != null)
			mySelectionForm.elements[hiddenParam].value = "true";

		// Cancel any type param that has been set
		if (mySelectionForm.elements[typeParam] != null)
			mySelectionForm.elements[typeParam].value = "";

		if (IsAjaxMode(mySelectionForm))
		{
			// Target is reload frames (LyServlet manages frames to be reloaded)
			if (mySelectionForm.elements[frameParam] != null)
				mySelectionForm.elements[frameParam].value = targetFrameName;
		
			SendAjaxRequest(mySelectionForm, url);
		}
		else
		{
			try
			{
				// And we submit the form
				mySelectionForm.submit();
			}
			catch (ex)
			{
				// When a file input is displayed in the HTML form, the submit of form may
				// launch an error. Display a message asking the user to check values of
				// file inputs.
				if ((submitMessage != null) && (mySelectionForm.elements[submittedParam] != null))
				{
					mySelectionForm.elements[submittedParam].value = "";
				}
				
				alert(fileErrorMessage);
			}
		}
	}
	else
	{
		var date = new Date();
		// If no form was found, just call the given link
		InvokeLink(url+ "&reload=" + date.getTime(), targetFrameName,"true");
	}
	
	// If close or cancel command in a popup which has no Leonardi main frame, close the popup
	if ((actionId == "_close") || (actionId == "_cancel"))
	{
		var hasMainFrame = hasLeonardiMainFrame();
		
		if (!hasMainFrame)
		{
			var topFrame = getLeonardiTopFrame();

			if ((typeof topFrame.opener) != "undefined")
				topFrame.setTimeout("close()", 1000);
		}
	}
}

function InvokeSelection(actionId, valeur, controllerId, extend, path)
{
	var selectionForm = null;
	var oldController = null;

	// For a selection, form is in the current view
	if (typeof window.document.forms[0] != "undefined")
	{
		selectionForm = window.document.forms[0];

		if (typeof selectionForm.elements[controllerParam] != "undefined")
		{
			oldController = selectionForm.elements[controllerParam].value;

			if ((typeof controllerId == "undefined") || (controllerId.length == 0))
				selectionForm.elements[controllerParam].value = _controllerId;
			else
				selectionForm.elements[controllerParam].value = controllerId;
		}
	}
	else
		selectionForm = findForm(null,_controllerId);

	if (selectionForm != null)
	{
		// Set the actionId input in the form
		if( (typeof selectionForm.elements[sourceParam] != "undefined") && (selectionForm.elements[sourceParam] != null) )
			selectionForm.elements[sourceParam].value = actionId

		if (typeof valeur!="undefined")
			selectionForm.elements[valueParam].value = valeur;

		if ((typeof extend!="undefined") && (extend.length > 0))
			selectionForm.elements[extendParam].value = extend;

		if ((typeof notify!="undefined") && (notify == true))
			selectionForm.elements[frameParam].value = context.name;

		// Cancel any type param that has been set
		if (selectionForm.elements[typeParam] != null)
			selectionForm.elements[typeParam].value = "";

		if (IsAjaxMode(selectionForm))
		{
			SendAjaxRequest(selectionForm, path);
		}
		else
		{
			// If a form was found, submit !
			selectionForm.submit();
		}

		if (oldController != null)
			selectionForm.elements[controllerParam].value = oldController;
	}
}

// This function invokes a popup demand.
function InvokePopup(sourceId, index, url, controllerId, extend, xPos, yPos)
{
	if ( (typeof controllerId == "undefined") ||
		(controllerId == ""))
		controllerId = _controllerId;

	var selectionForm = findForm(null,controllerId);
	var oldController = null;

	if (selectionForm == null)
		selectionForm = findForm(null, null);

	// If no form was found, try to get the first one in the current frame.
	// May happen if several controllers are used in the same frame.
	if ((selectionForm == null)
		&& (typeof window.document.forms[0] != "undefined"))
	{
			selectionForm = window.document.forms[0];
	}

	if ((selectionForm != null) && (typeof selectionForm.elements != "undefined") )
	{
		// set the form action URL : usefull for struts (url may vary)
		if (IsAjaxMode(selectionForm) && (typeof url != "undefined") && (url != null) && (url != ""))
		{
			var actionUrl = "";
			if (((typeof _rootUrl) != "undefined") && (_rootUrl != null))									
				actionUrl = _rootUrl;
							
			actionUrl += url;
			actionUrl = ReplaceString(actionUrl, "&#38;","&");
				
			selectionForm.action=actionUrl;				
		}		

		if (controllerId != null)
		{
			oldController = selectionForm.elements[controllerParam].value;

			if ((typeof selectionForm.elements[controllerParam] != "undefined")
					&& (selectionForm.elements[controllerParam] != null))
				selectionForm.elements[controllerParam].value = controllerId
		}

		// Set the source input in the form as _popupMenu
		if (selectionForm.elements[sourceParam] != null)
		{
			if (extend)
				selectionForm.elements[sourceParam].value = "_popupMenuExtend";
			else
				selectionForm.elements[sourceParam].value = "_popupMenu";
		}

		// Set the value input in the form
		if (selectionForm.elements[valueParam] != null)
			selectionForm.elements[valueParam].value = sourceId;

		// Set the index input in the form
		if (selectionForm.elements[indexParam] != null)
			selectionForm.elements[indexParam].value = index;

		// Set the use of the hiddenFrame
		if (selectionForm.elements[hiddenParam] != null)
			selectionForm.elements[hiddenParam].value = "true";

		// Set the position of popup menu
		if (selectionForm.elements[positionX] != null)
			selectionForm.elements[positionX].value = xPos;
		
		if (selectionForm.elements[positionY] != null)
			selectionForm.elements[positionY].value = yPos;

		// We want to use the hidden frame
		selectionForm.elements[hiddenParam].value = "true";

		// Cancel any type param that has been set
		if (selectionForm.elements[typeParam] != null)
			selectionForm.elements[typeParam].value = "";

		if (IsAjaxMode(selectionForm))
		{
			// Change the popup position to ensure popup will be displayed at the right position
			popupLeftOffset = xPos;
			popupTopOffset = yPos;

			var topFrame = getLeonardiTopFrame();
			var popupFrame = topFrame.frames[upFrameName];
			
			if (popupFrame != null)
				popupTopOffset = yPos + popupFrame.document.body.scrollHeight;

			SendAjaxRequest(selectionForm, url);
		}
		else
		{
			// And we submit the form
			selectionForm.submit();
		}

		if (oldController != null)
			selectionForm.elements[controllerParam].value = oldController;
	}
	else
	{
		var date = new Date();
		// If no form was found, just call the given link
		InvokeLink(url+ "&reload=" + date.getTime(), targetFrameName,"true");
	}
}

function InvokeStatus(url, value)
{
	InvokeAction('_status',url,'reload_frames',_controllerId,value);
}

// This function invokes an action.
function InvokeAction(actionId, url, target, controllerId, value, type)
{
	if ( (typeof controllerId == "undefined") ||
		(controllerId == ""))
		controllerId = _controllerId;

	if (controllerId.indexOf("_CONTROLLER_CHANGED_ID") > 0)
	{
		controllerId = controllerId.substring(0, controllerId.length -
			"_CONTROLLER_CHANGED_ID".length);
	}

	var selectionForm = null;
	var currentForm = this.document.forms[0];

	if ((currentForm != null)
		&& (currentForm.elements[controllerParam] != null)
		&& ((currentForm.elements[controllerParam].value == controllerId)
				|| (controllerId == null))
		&& (currentForm.elements[sourceParam] != null))
	{
			currentForm.elements[frameParam].value = this.name;
			selectionForm = currentForm;
	}

	if (selectionForm == null)
		selectionForm = findForm(null,controllerId);
	
	// If no form was found, try to get the first one in the current frame.
	// May happen if several controllers are used in the same frame.
	if ((selectionForm == null)
		&& (typeof window.document.forms[0] != "undefined"))
	{
			selectionForm = window.document.forms[0];
	}

	if (selectionForm == null)
		selectionForm = findForm(null, null);

	var oldControllerId = null;

	if (selectionForm != null)
	{
		if (controllerId != null)
		{
			if ((typeof selectionForm.elements[controllerParam] != "undefined")
					&& (selectionForm.elements[controllerParam] != null))
			{
				oldControllerId = selectionForm.elements[controllerParam].value;
				selectionForm.elements[controllerParam].value = controllerId;
			}
		}

		// Cancel any value param that has been set
		if ((typeof selectionForm.elements[valueParam] != "undefined")
				&& (selectionForm.elements[valueParam] != null))
			selectionForm.elements[valueParam].value = "";

		// Cancel any source param that has been set
		if ((typeof selectionForm.elements[sourceParam] != "undefined")
				&& (selectionForm.elements[sourceParam] != null))
			selectionForm.elements[sourceParam].value = "";

		// Cancel any type param that has been set
		if (selectionForm.elements[typeParam] != null)
			selectionForm.elements[typeParam].value = "";

		// If a value is given set the value input in the form
		if((value != null) && (typeof value != "undefined")
				&& (typeof selectionForm.elements[valueParam] != "undefined")
				&& (selectionForm.elements[valueParam] != null))
			selectionForm.elements[valueParam].value = value;

		// If a type is given set the value input in the form
		if((type != null) && (typeof type != "undefined")
				&& (typeof selectionForm.elements[typeParam] != "undefined")
				&& (selectionForm.elements[typeParam] != null))
			selectionForm.elements[typeParam].value = type;

		// Set the index if a close tab action is called
		if (((type == 'closeTab')
					|| (type == 'sort')
					|| (type == 'extendSort'))
				&& (selectionForm.elements[indexParam] != null))
			selectionForm.elements[indexParam].value = value;

		// Set the actionId input in the form
		if((typeof selectionForm.elements[sourceParam] != "undefined")
				&& (selectionForm.elements[sourceParam] != null))
			selectionForm.elements[sourceParam].value = actionId;

		// If no target is specified, use the step frame
		selectionForm.elements[hiddenParam].value = "true";
		selectionForm.elements[frameParam].value = target;

		if (IsAjaxMode(selectionForm))
		{
			SendAjaxRequest(selectionForm, url);
		}
		else
		{
			selectionForm.submit();
		}
		
		// Reset old controller id
		if (oldControllerId != null)
			selectionForm.elements[controllerParam].value = oldControllerId;
	}
	else
	{
		var date = new Date();
		// If no form was found, just call the given link
		InvokeLink(url+ "&reload=" + date.getTime(), target,"true");
	}
}

function InvokeDef(url)
{
	InvokeLink(url,'reload_frame','true');
}

function InvokeHref(url)
{
	InvokeLink(url,'','true');
}


// This function opens a link in a given target
function InvokeLink(url, target, hiddenFrame, newSize)
{
	var newUrl = "";
	var isRootUrl = false;
	
	if (newSize != null)
	{
		if (newSize == 'small')
			size = smallSize;
	}

	if (target.length == 0)
		target = targetFrameName;
	
	
	
	if (url != null && url != "")
	{
		if (url.indexOf("http:"))
			isRootUrl = true;
	}

	if (((typeof _rootUrl) != "undefined") && (_rootUrl != null)
		&& isRootUrl)
	{
		// Compute the partial rootUrl: i.e.  the part of the url from the beginning to ?
		var pos = _rootUrl.indexOf("?");
		var root = _rootUrl;
		if (pos >= 0)
		{
			root = _rootUrl.substring(0, pos);
		}

		if (url.indexOf(root) != 0)
		{
			newUrl = _rootUrl;
		}
	}


	newUrl += url;
	newUrl = ReplaceString(newUrl, "&#38;","&");
	var finalTarget;

	if ( ((typeof hiddenFrame) != "undefined") && (hiddenFrame=="true") )
	{
	}
	else
	{
		// We are in the hiddenFrame
		// the target frame must get its page
		finalTarget = findTarget(null, target);
		
		// first we have to find main_frame or target_frame
		if (finalTarget == null)
		{
			finalTarget = findTarget(null, targetFrameName);
			/*if (target=='popup_view')
			{
				//If popup not exist create a iframe
				var tempTarget = findTargetByContent(null, 'savePopUp');
				var myScript = tempTarget.addIframe;
				
				replaceDivInnerHtml(tempTarget.name,'savePopUp',myScript);				
				tempTarget = findTarget(tempTarget, target);
				
				if (tempTarget != null)
					finalTarget = tempTarget;
			}*/
		}
			
		// if main_frame or target_frame are undefined, we have to construct main_frame
		// else the target is certainly target_frame
		if (finalTarget == null)
		{
			finalTarget = findTarget(null,mainFrameName);
			// We need to rebuild MainFrame structure (frameset)
			if (finalTarget != null)
				newUrl += "&_source=" + finalTarget.name;
		}
		else
		{
			newUrl += "&_frame=" + finalTarget.name;
		}
	}

	if (finalTarget != null)
		finalTarget.location.replace(newUrl);
	else if (AJAX_MODE == 'force')
		SendAjaxRequest(null, newUrl);

	return;
}

// This function invokes a print job
function InvokePrint(url, target, areaId, actionId)
{
	var newUrl ='';

	newUrl += ReplaceString(url, "&#38;","&");
	newUrl += "&" + hiddenParam + "=true";

	// Add area id in source
	var toolbar = "";

	newUrl = ReplaceString(newUrl, actionId, "_print");

	if (((typeof areaId) != "undefined") && (areaId != null))
		newUrl = ReplaceString(newUrl, "_print","_print" + areaId);

	if (this.print == "undefined")
		toolbar = ",toolbar=yes";

	open(newUrl, null, parameters + toolbar + ',width=600,height=500');
}

// This function opens a print job in a new window
function OpenPrintPopup(url)
{
	var toolbar = "";

	if (this.print == "undefined")
		toolbar = ",toolbar=yes";

	open(url, null, parameters + toolbar + ',width=600,height=500');
}

// This function returns a valid name for a new Window
function GetWindowName(newUrl)
{
	var targetName;

	targetName = ReplaceString(newUrl, ':', '_');
	targetName = ReplaceString(targetName, '\\', '_');
	targetName = ReplaceString(targetName, '\/', '_');
	targetName = ReplaceString(targetName, '?', '_');
	targetName = ReplaceString(targetName, '&', '_');
	targetName = ReplaceString(targetName, '.', '_');
	targetName = ReplaceString(targetName, '=', '_');

	return targetName;
}

// This function selects a tab in a tabs view
function SelectTab(url, index, controllerId, pageId, frameName, targetFormId, targetControllerId, type)
{
	var targetForm;
	var mySelectionForm;
	var oldController = null;

	mySelectionForm = findForm(null, controllerId);

	if ((mySelectionForm != null) && (typeof mySelectionForm.elements != "undefined"))
		targetForm = findTarget(null, mySelectionForm.elements[frameParam].value);

	// If no form was found, try to get the first one in the current frame.
	// May happen if several controllers are used in the same frame.
	if ((mySelectionForm == null)
		&& (typeof window.document.forms[0] != "undefined"))
	{
			mySelectionForm = window.document.forms[0];

			if (typeof mySelectionForm.elements[controllerParam] != "undefined")
			{
				oldController = mySelectionForm.elements[controllerParam].value;

				if ((typeof controllerId == "undefined") || (controllerId.length == 0))
					mySelectionForm.elements[controllerParam].value = _controllerId;
				else
					mySelectionForm.elements[controllerParam].value = controllerId;
			}
	}

//	if (mySelectionForm == null)
//		mySelectionForm = findForm(null, null);

	if ((mySelectionForm != null) && (typeof mySelectionForm.elements != "undefined") )
	{
		// Set the actionId input in the form
		if (mySelectionForm.elements[sourceParam] != null)
			mySelectionForm.elements[sourceParam].value = "_tabs";

		// set the form action URL : usefull for struts (url may vary)
		if ((typeof url != "undefined") && (url != null) && (url != ""))
		{
			var actionUrl = "";
			if (((typeof _rootUrl) != "undefined") && (_rootUrl != null))									
				actionUrl = _rootUrl;
							
			actionUrl += url;
			actionUrl = ReplaceString(actionUrl, "&#38;","&");
				
			mySelectionForm.action=actionUrl;				
		}		

		// Reset any value param that has been set
		if (mySelectionForm.elements[valueParam] != null)
			mySelectionForm.elements[valueParam].value = "";

		// Set the target
		if (mySelectionForm.elements[pageParam] != null)
			mySelectionForm.elements[pageParam].value = pageId;

		// Set the target
		if (mySelectionForm.elements[indexParam] != null)
			mySelectionForm.elements[indexParam].value = index;

		// Set the target
		if ((targetControllerId != null) && (mySelectionForm.elements[targetControllerIdParam] != "undefined"))
			mySelectionForm.elements[targetControllerIdParam].value = targetControllerId;

		// Cancel any type param that has been set
		if (mySelectionForm.elements[typeParam] != null)
			mySelectionForm.elements[typeParam].value = type;

		if (IsAjaxMode(mySelectionForm))
		{
			SendAjaxRequest(mySelectionForm, url);
		}
		else
		{
			// And we submit the form
			mySelectionForm.submit();
		}

		if (oldController != null)
			mySelectionForm.elements[controllerParam].value = oldController;
	}
	else
	{
		// If no form was found, just call the given link
		var date = new Date();

		if (AJAX_MODE)
		{
			SendAjaxRequest(null, url + "&" + typeParam + "=" + type + "&reload=" + date.getTime());
		}
		else
		{
			InvokeLink(url + "&reload=" + date.getTime(), frameName, "true");
		}
	}
}

// This function opens or closes a toolbar
function OpenToolbar(url)
{
	var newUrl = _rootUrl + url;
	var date = new Date();

	newUrl = ReplaceString(newUrl , "&#38;" , "&");
	newUrl = newUrl + "&reload=" + date.getTime() + "&_frame=" + this.name;

	this.location.href = newUrl;
}

//Those functions set Image backgrounds of a command button whether the mouse is over it or not
function rollover(img, path, source) {
	img.src = path + source;
}

function rollout(img, path, source) {
	img.src = path + source;
}

function changeLineBg(line, path, source)
{
	line.background = path + source;
}