// LyMessageScript.js
// Copyright (c) 2000-2009, Lyria-W4.
// All rights reserved.
// @version	$Id: LyMessageScript.js,v 1.6.4.4 2009/08/17 15:04:59 nha Exp $

var frameParam = '_frame';
var messageWindowName = 'message_window';
var messageWindowTitleName = 'message_window_td';
var messageWindowIconName = 'message_window_icon_td';
var messageWindowTextName = 'message_window_text_td';
var messageWindowCommandsName = 'message_window_commands_td';
var messageWindowShadowName = 'message_window_shadow';
var lastUrl = '';
var lastInfoId = '';


// Show a message window
function ShowMessageWindow(title, icon, text, valueArray, labelArray, infoId, url,
	leftImage, leftOverImage, rightImage, rightOverImage, target)
{
	var topFrame = getLeonardiTopFrame();
	var messageWindow = topFrame.document.getElementById(messageWindowName);
	var messageWindowTitle = topFrame.document.getElementById(messageWindowTitleName);
	var messageWindowIcon = topFrame.document.getElementById(messageWindowIconName);
	var messageWindowText = topFrame.document.getElementById(messageWindowTextName);
	var messageWindowCommands = topFrame.document.getElementById(messageWindowCommandsName);
	
	topFrame.lastUrl = url;
	topFrame.lastInfoId = infoId;
	
	messageWindowTitle.innerHTML = title;
	
	if (icon != "")
		messageWindowIcon.innerHTML = "<img src='" + icon + "'/>";
	else
		messageWindowIcon.innerHTML = "";

	messageWindowText.innerHTML = text;
	
	var commandsHtml = "";
	
	commandsHtml += "<table border='0' align='center'><tr>";
	
	for (var i = 0; i < valueArray.length; i++)
	{
		commandsHtml += "<td><table border='0' cellspacing='0' cellpadding='0'><tr>";
		commandsHtml += "<td align='right'><img src='" + leftImage + "' name='imageLeft" + valueArray[i] + "'/></td>";
		commandsHtml += "<td align='center' class='commandButton' id='imageBg" + valueArray[i] + "'";
		commandsHtml += " onMouseOver=\"rollover(document.images['imageLeft" + valueArray[i] + "'],'";
		commandsHtml += leftOverImage;
		commandsHtml += "','');rollover(document.images['imageRight" + valueArray[i] + "'],'";
		commandsHtml += rightOverImage;
		commandsHtml += "','');";
		commandsHtml += "document.getElementById('imageBg" + valueArray[i] + "').className='commandButtonOver';";
		commandsHtml += "return true;\"";
		commandsHtml += " onMouseOut=\"rollover(document.images['imageLeft" + valueArray[i] + "'],'";
		commandsHtml += leftImage;
		commandsHtml += "','');rollover(document.images['imageRight" + valueArray[i] + "'],'";
		commandsHtml += rightImage;
		commandsHtml += "','');";
		commandsHtml += "document.getElementById('imageBg" + valueArray[i] + "').className='commandButton';";
		commandsHtml += "return true;\"";
		commandsHtml += ">";
		commandsHtml += "<a id='message_button_" + valueArray[i] + "' href=\"javascript:HideMessageWindow('" + valueArray[i] + "'," + target + ");\">";
		commandsHtml += "<b><font class='commandLabel'>";
		commandsHtml += labelArray[i];
		commandsHtml += "</font></b></a>";
		commandsHtml += "</td>";
		commandsHtml += "<td align='left'><img src='" + rightImage + "' name='imageRight" + valueArray[i] + "'/></td>";
		commandsHtml += "</tr></table></td>";
	}

	commandsHtml += "</tr></table>";

	messageWindowCommands.innerHTML = commandsHtml;
	
	// Center message window
	var totalWidth = topFrame.document.body.clientWidth;
	var totalHeight = topFrame.document.body.clientHeight;
	var divWidth = cmGetWidth(messageWindow);
	var divHeight = cmGetHeight(messageWindow);
	
	messageWindow.style.left = (totalWidth - divWidth) / 2;
	messageWindow.style.top = (totalHeight - divHeight) / 2;
	messageWindowTitle.style.width = cmGetWidth(messageWindow) - 30;
	ShowMessageWindowShadow(true);
	messageWindow.style.visibility = "visible";
	
	// Give focus to the first button, to manage 'Enter' key
	if (valueArray.length > 0)
	{
		var defaultButton = topFrame.document.getElementById("message_button_" + valueArray[0]);
		
		try
		{
			defaultButton.focus();
		}
		catch (ex)
		{
		}
	}
}

// Show the message window shadow
function ShowMessageWindowShadow(force)
{
	var topFrame = getLeonardiTopFrame();
	var parentIframes = topFrame.document.getElementsByTagName('iframe');
	var popupShadowObject = parentIframes[messageWindowShadowName];
	
	if (popupShadowObject == null)
		popupShadowObject = topFrame.document.getElementById(messageWindowShadowName);

	if ((popupShadowObject != null)
		&& (force || (popupShadowObject.style.visibility == "visible")))
	{
		var totalWidth = topFrame.document.body.clientWidth;
		var totalHeight = topFrame.document.body.clientHeight;
		
		popupShadowObject.style.width = totalWidth;
		popupShadowObject.style.height = totalHeight;
		popupShadowObject.style.left = 0;
		popupShadowObject.style.top = 0;
		popupShadowObject.style.visibility = "visible";
	}
}

// Hide the message window shadow
function HideMessageWindow(value, target)
{
	var params = "_message=" + lastInfoId + "&_answer=" + value;
	
	if ((typeof target != "undefined") && (target != null) && (target != "null"))
		params += "&target=" + target;

	SendAjaxRequestParams(lastUrl, params);
		
	var topFrame = getLeonardiTopFrame();
	var messageWindow = topFrame.document.getElementById(messageWindowName);
	var messageWindowTitle = topFrame.document.getElementById(messageWindowTitleName);
	var messageWindowIcon = topFrame.document.getElementById(messageWindowIconName);
	var messageWindowText = topFrame.document.getElementById(messageWindowTextName);
	var messageWindowCommands = topFrame.document.getElementById(messageWindowCommandsName);
	var parentIframes = topFrame.document.getElementsByTagName('iframe');
	var popupShadowObject = parentIframes[messageWindowShadowName];
	
	if (popupShadowObject == null)
		popupShadowObject = topFrame.document.getElementById(messageWindowShadowName);
	
	topFrame.lastUrl = '';
	topFrame.lastInfoId = '';
	messageWindowTitle.innerHTML = "";
	messageWindowIcon.innerHTML = "";
	messageWindowText.innerHTML = "";
	messageWindowCommands.innerHTML = "";
	messageWindowTitle.style.width = 20;
	messageWindow.style.visibility = "hidden";

	if (popupShadowObject != null)
	{
		popupShadowObject.style.width = 80;
		popupShadowObject.style.height = 40;
		popupShadowObject.style.visibility = "hidden";
	}
}

// This function shows a message.
function ShowAlert(message)
{
	alert(message);
}

// This function shows a message and try to put it in the hidden frame.
function ShowMessage(message, infoId)
{
	// If possible, message should appear in hidden_frame
	// Do *not* Loop
	var mainFrame = findTarget(null, "main_frame");
	var hiddenFrame = findTarget(null, "hidden_frame");

	if (((typeof AJAX_MODE == "undefined") || !AJAX_MODE)
		&& (mainFrame!=null) && (hiddenFrame != null))
	{
		// Redirect to pilot step if possible
		if (hiddenFrame != this)
		{
			hiddenFrame.location.href = this.location.href;
			
			alert("ShowMessage in LyMessageScript");
			
			this.history.go(-1);
		}
		else
			alert(message);
	}
	else
		alert(message);
}

// This function asks a question.
function AskQuestion(message, infoId, baseUrl)
{
	var params;

	if (confirm(message))
		params = "_message=" + infoId + "&_answer=true";
	else
		params = "_message=" + infoId + "&_answer=false";

	SendAjaxRequestParams(baseUrl, params);

	// If a question has been answered, there is no need to perform the update process since
	// servlet will be notified by the answer request.
	questionAnswered = true;
	
	setTimeout("questionAnswered = false;", 500);
}

function ShowLocation(url)
{
	var mainFrame = findTarget(null, "main_frame");
	var targetFrame = findTarget(null, "target_frame");

	if (targetFrame != null )//typeof top.frames["main_frame"].frames["target_frame"] != "undefined")
	{
		//top.frames["main_frame"].frames["target_frame"].location.href = url;
		targetFrame.location.href = url;
	}
	else
		this.location.href = url;
}

var newWindows = new Array();

function RegisterNewWindow(parameters)
{
	var name = parameters[0];
	var newWindow = NewWindow(parameters);

	// Check if window is already registered
	for (var i = 0; i < newWindows.length; i++)
	{
		if (newWindows[i][0] == name)
			return;
	}

	newWindows[newWindows.length] = new Array(name, newWindow);
}

function CloseWindow(windowName)
{
	for (var i = 0; i < newWindows.length; i++)
	{
		if (newWindows[i][0] == windowName)
		{
			newWindows[i][1].close();
			break;
		}
	}
}

function CloseAllWindows()
{
	for (var i = 0; i < newWindows.length; i++)
	{
		newWindows[i][1].close();
	}
}

// Opens a new window with an array of parameters.
// The list of parameters must be :
//  - parameters[0] : name
//  - parameters[1] : url
//  - parameters[2] : parameters
//  - parameters[3] : x
//  - parameters[4] : y
//  - parameters[5] : width
//  - parameters[6] : height
//  - parameters[7] : fullscreen
function NewWindow(parameters)
{
	var name = parameters[0];
	var url = parameters[1];
	var windowParameters = parameters[2];
	var x = parameters[3];
	var y = parameters[4];
	var width = parameters[5];
	var height = parameters[6];
	var fullscreen = parameters[7];

	if (fullscreen == "true")
	{
		width = (screen.width - 5) + "";
		height = (screen.height - 30) + "";
	}
	
	if (x != '')
		windowParameters += ',left=' + x;
	
	if (y != '')
		windowParameters += ',top=' + y;
	
	if (width != '')
		windowParameters += ',width=' + width;
	
	if (height != '')
		windowParameters += ',height=' + height;
	
	return window.open(url, name, windowParameters);
}

function ShowFile(file, title, message, click, alternate)
{
	var newWin = window.open("_download","Download", 'width=500,height=100,menubar=yes,resizable=yes');

	// If the window was already open, give him the focus and clear the current document
	// Clear document
	newWin.document.clear();
	newWin.focus();
	newWin.document.writeln("<HTML>");
	newWin.document.writeln("<HEAD>");
	newWin.document.writeln("<TITLE>");
	newWin.document.writeln(title);
	newWin.document.writeln("</TITLE><SCRIPT LANGUAGE='JavaScript1.1'>function saveAs(sFilename){  if(document.queryCommandSupported && document.queryCommandSupported('SaveAs')){    var win = self.open((event.srcElement.href || location.href), '', 'top=1000,left=300,width=1000,height=100');    win.document.execCommand('SaveAs', true, sFilename);    win.close();    } return false  }</SCRIPT>");
	newWin.document.writeln("</HEAD>");
	newWin.document.writeln("<BODY>");
	newWin.document.writeln(message);
	newWin.document.writeln("<A href=\""+file+"\">");
	newWin.document.writeln(click);
	newWin.document.writeln("</A>");

	if (alternate != null && alternate != "")
	{
		newWin.document.writeln("<BR/>");
		newWin.document.writeln(alternate);
		newWin.document.writeln("<A href=\""+file+"\" onclick='return saveAs()'>");
		newWin.document.writeln(click);
		newWin.document.writeln("</A>");
	}
	newWin.document.writeln("</BODY>");
	newWin.document.writeln("</HTML>");
}
