//-------------------------------------------------------------------------------
// User messages
//-------------------------------------------------------------------------------

//-------------------------------------------------------------------------------
// Constants section
var MSG_BOCADILLO	= 10;
var MSG_DIALOGBOX	= 20;
var MSG_CONFIRM		= 30;


//-------------------------------------------------------------------------------
//	Function: msg
//	Description:
//		Mostra un missatge per pantalla
//	Parameters
//		sMsg:		missatge a mostrar
//		iType:		tipus de caixa (MSG_BOCADILLO|MSG_DIALOGBOX)
//		sBaseDir:	Directori que conté les imatges a mostrar
//		oObjectID:	nom de l'element a què apunta el missatge (o null)
//		sIconURL:	url de la icona a mostrar (o null)
//		bCenter:	indica si el missatge s'ha de centrar en pantalla
//		iXPos:		posició x de la pantalla (si no apunta a un objecte)
//		iYpos:		posiciño y de la pantalla (si no apunta a un objecte)
//		iMaxWidth:	amplada màxima de la caixa
//		iMaxHeight:	alçada màxima de la caixa
//		sReturnValueHandler: funció que rebrà el resultat del confirm (si és un CONFIRM message)
//
function msg (sMsg, iType, sBaseDir, sObjectID, sIconURL, bCenter, iXPos, iYPos, iMaxWidth, iMaxHeight, sReturnValueHandler)
{
	//Obtain layer reference. Create if not exist
	var oDiv;
	oDiv=document.getElementById('MSGDIV');
	if(!oDiv) {
		oDiv=document.createElement("<DIV ID='MSGDIV' style='visibility:hidden;filter:progid:DXImageTransform.Microsoft.Shadow(color=#777777, Direction=135, Strength=3)'></DIV>");
		document.body.appendChild(oDiv);
	}

	// Retrieve HTML Code
	if (!sReturnValueHandler)
		sReturnValueHandler="";
	sHTML = getHTML(sMsg,iType,sBaseDir,iMaxWidth,iMaxHeight,sIconURL,sObjectID,sReturnValueHandler);
	oDiv.innerHTML = sHTML;

	// Retrieve attention point
	if (sObjectID) {
		iTop = getAbsoluteTop(sObjectID);
		iLeft = getAbsoluteLeft(sObjectID)
		iTop = computeRealTop(iTop);
		iLeft = computeRealLeft(iLeft);
	}
	else {
		if (bCenter) {
			var oTable = document.getElementById('MSGTABLE');
			iTop = (document.body.clientHeight/2) - (oTable.offsetHeight/2);
			iLeft = (document.body.clientWidth/2) - (oTable.offsetWidth/2);
		}
		else {
			if (iYPos) iTop = iYPos; else iTop=0;
			if (iXPos) iLeft = iXPos; else iLeft=0;
		}
	}
	
	// Make layer visible
	oDiv.style.position="absolute";
	oDiv.style.left=iLeft;
	oDiv.style.top=iTop;
	oDiv.style.visibility="visible";
	oDiv.focus();
	
	// put focus on object, if exists
	if (document.getElementById(sObjectID))
		document.getElementById(sObjectID).focus();
}
//-------------------------------------------------------------------------------
function msgHide ()
{
	oDiv=document.getElementById('MSGDIV');
	if (oDiv) {
		oDiv.style.visibility="hidden";
		oDiv.style.position="absolute";
		oDiv.style.left=0;
		oDiv.style.top=0;
	}
}
//-------------------------------------------------------------------------------
function msgclose (oElement,iType,sReturnValueHandler)
{
	hideMessage(oElement);

	// Return false, if is a confirm message
	if (iType==MSG_CONFIRM)
		confirmReturnFalse(sReturnValueHandler);
}
//-------------------------------------------------------------------------------
function hideMessage(oElement)
{
	// Make layer invisible
	oDiv=document.getElementById('MSGDIV');
	if (oDiv) {
		oDiv.style.visibility="hidden";
		oDiv.style.position="absolute";
		oDiv.style.left=0;
		oDiv.style.top=0;
	}
	
	// Return the focus to the active element
	if ((oElement) && (oElement!="undefined") && (oElement!="null")) {
		var oObj = document.getElementById(oElement)
		if (oObj) oObj.focus();
	}
}
//-------------------------------------------------------------------------------
function getHTML (sMsg,iType,sBaseDir,iMaxWidth,iMaxHeight,sIconURL,sObjectID,sReturnValueHandler)
{
	switch (iType) {
		case MSG_BOCADILLO:
			return (getBocadilloHTML(sMsg,sBaseDir,iMaxWidth,iMaxHeight,sIconURL,sObjectID))
			break;
		case MSG_CONFIRM:
			return (getConfirmHTML(sMsg,sBaseDir,iMaxWidth,iMaxHeight,sIconURL,sObjectID,sReturnValueHandler))
			break;
		default:
			return (getBocadilloHTML(sMsg,sBaseDir,iMaxWidth,iMaxHeight,sIconURL,sObjectID))
			break;
	}
}
//-------------------------------------------------------------------------------
function getBocadilloHTML (sMsg,sBaseDir,iMaxWidth,iMaxHeight,sIconURL,sObjectID)
{
	var sHTML;
	sHTML  = "<table border='0' cellspacing='0' cellpadding='0' id='MSGTABLE'";
	if (iMaxWidth)
		sHTML += " width='" + iMaxWidth + "'"
	if (iMaxHeight)
		sHTML += " height='" + iMaxHeight + "'"
	sHTML += ">";

	sHTML += "<tr>";
	sHTML += "<td width='4' height='4'><img src='" + sBaseDir + "msg_01.gif'></td>";
	sHTML += "<td height='4' style='background-color:white' background='" + sBaseDir + "msg_02.gif'></td>";
	sHTML += "<td width='4' height='4'><img src='" + sBaseDir + "msg_03.gif'></td>";
	sHTML += "</tr>";

	sHTML += "<tr>";
	sHTML += "<td width='4'background='" + sBaseDir + "msg_04.gif'></td>";
	sHTML += "<td><table border='0' cellspacing='0' cellpadding='0'><tr>"
	if (sIconURL)
		sHTML += "<td valign='top' style='background-color:white;padding:5px'><img src='" + sIconURL + "'></td>";
	sHTML += "<td class='txtBlack' style='padding:5;cursor:default;background-color:white' width='100%'>" + sMsg + "</td>";
	sHTML += "<td valign='top' style='background-color:white;padding-left:4'><a href='javascript:msgclose(\"" + sObjectID + "\",MSG_BOCADILLO,\"\")'><img src='" + sBaseDir + "msg_btn_close.gif' border='0'></a></td>";
	sHTML += "</tr></table></td>"
	sHTML += "<td width='4' background='" + sBaseDir + "msg_05.gif'></td>";
	sHTML += "</tr>";

	sHTML += "<tr>";
	sHTML += "<td width='4' valign='top'><img src='" + sBaseDir + "msg_06.gif'></td>";
	
	if (sObjectID)
		sHTML += "<td height='4' background='" + sBaseDir + "msg_07.gif'><img src='" + sBaseDir + "msg_fl_down_01.gif'></td>";
	else
		sHTML += "<td height='4' background='" + sBaseDir + "msg_07.gif'></td>";	

	sHTML += "<td width='4' valign='top'><img src='" + sBaseDir + "msg_08.gif'></td>";
	sHTML += "</tr>";

	sHTML += "</table>";
	return sHTML;

}
//-------------------------------------------------------------------------------
function getConfirmHTML (sMsg,sBaseDir,iMaxWidth,iMaxHeight,sIconURL,sObjectID,sReturnValueHandler)
{
	var sHTML;
	sHTML  = "<table border='0' cellspacing='0' cellpadding='0' id='MSGTABLE'";
	if (iMaxWidth)
		sHTML += " width='" + iMaxWidth + "'"
	if (iMaxHeight)
		sHTML += " height='" + iMaxHeight + "'"
	sHTML += ">";

	sHTML += "<tr>";
	sHTML += "<td width='4' height='4'><img src='" + sBaseDir + "msg_01.gif'></td>";
	sHTML += "<td height='4' style='background-color:white' background='" + sBaseDir + "msg_02.gif'></td>";
	sHTML += "<td width='4' height='4'><img src='" + sBaseDir + "msg_03.gif'></td>";
	sHTML += "</tr>";

	sHTML += "<tr>";
	sHTML += "<td width='4'background='" + sBaseDir + "msg_04.gif'></td>";
	sHTML += "<td><table border='0' cellspacing='0' cellpadding='0'><tr>";
	sHTML += "<td valign='top' style='background-color:white;padding:5px'>";
	if (sIconURL)
		sHTML += "<img src='" + sIconURL + "'>";
	sHTML += "</td>";
	sHTML += "<td class='txtBlack' style='padding:5;cursor:default;background-color:white' width='100%'>" + sMsg + "</td>";
	sHTML += "<td valign='top' style='background-color:white;padding-left:4'><a href='javascript:msgclose(\"" + sObjectID + "\",MSG_CONFIRM,\"" + sReturnValueHandler + "\")'><img src='" + sBaseDir + "msg_btn_close.gif' border='0'></a></td>";
	sHTML += "</tr>";
	sHTML += "<tr><td colspan='3' align='center' width='100%' style='background-color:white'><table border='0' cellspacing='0' cellpadding='0'><tr><td class='txtBlack' style='padding:5;cursor:hand' onclick='hideMessage(\"" + sObjectID + "\");confirmReturnTrue(\"" + sReturnValueHandler + "\")'>Sí</td><td class='txtBlack' style='padding:5;cursor:hand' onclick='hideMessage(\"" + sObjectID + "\");confirmReturnFalse(\"" + sReturnValueHandler + "\")'>No</td></tr></table></td></tr>";
	sHTML += "</table></td>";
	sHTML += "<td width='4' background='" + sBaseDir + "msg_05.gif'></td>";
	sHTML += "</tr>";

	sHTML += "<tr>";
	sHTML += "<td width='4' valign='top'><img src='" + sBaseDir + "msg_06.gif'></td>";
	
	if (sObjectID)
		sHTML += "<td height='4' background='" + sBaseDir + "msg_07.gif'><img src='" + sBaseDir + "msg_fl_down_01.gif'></td>";
	else
		sHTML += "<td height='4' background='" + sBaseDir + "msg_07.gif'></td>";	

	sHTML += "<td width='4' valign='top'><img src='" + sBaseDir + "msg_08.gif'></td>";
	sHTML += "</tr>";

	sHTML += "</table>";
	return sHTML;

}
//-------------------------------------------------------------------------------
function computeRealTop (iTop)
{
	//IOM: falta el càlcul per a la ubicació de la fletxa del missatge
	var oTable = document.getElementById('MSGTABLE');
	if (oTable) {
		iTop -= oTable.offsetHeight;
	}
	return (iTop)
}

//-------------------------------------------------------------------------------
function computeRealLeft (iLeft)
{
	//IOM: falta el càlcul per a la ubicació de la fletxa del missatge
	iLeft += 10;
	return (iLeft)
}

//-------------------------------------------------------------------------------
function getAbsoluteLeft(objectId) {
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oLeft += oParent.offsetLeft
		o = oParent
	}
	return oLeft
}

//-------------------------------------------------------------------------------
function getAbsoluteTop(objectId) {
	o = document.getElementById(objectId)
	oTop = o.offsetTop
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oTop += oParent.offsetTop
		o = oParent
	}
	return oTop
}

//-------------------------------------------------------------------------------
function confirmReturnTrue(sReturnValueHandler) {
	if (sReturnValueHandler!="") {
		iAux = sReturnValueHandler.indexOf('(');
		if (iAux>0) {
			sAux = sReturnValueHandler.substring(0,iAux+1) + "true," + sReturnValueHandler.substring(iAux+1,sReturnValueHandler.length+1);
			eval (sAux);
		}
		else
			eval (sReturnValueHandler+"(true)");
	}
}

//-------------------------------------------------------------------------------
function confirmReturnFalse(sReturnValueHandler) {
	if (sReturnValueHandler!="") {
		iAux = sReturnValueHandler.indexOf('(');
		if (iAux>0) {
			sAux = sReturnValueHandler.substring(0,iAux+1) + "false," + sReturnValueHandler.substring(iAux+1,sReturnValueHandler.length+1);
			eval (sAux);
		}
		else
			eval (sReturnValueHandler+"(false)");
	}
}