/**
	The errors module allows to write and reset error messages in a 
	DIV with a given ID. This DIV is usually defined in the layout such
	as jsp/layouts/classicLayout.jsp
	
	@copyright Copyright &copy; 2004, 2005, 2006, Umbrella Software AG 
	@author sniederb
	@version $Id: errors.js 4902 2010-07-30 15:36:54 +0200 (Fri, 30 Jul 2010) simon $	

*/

var ERROR_CONTAINER_DIV_ID = "error_container_div_id";
var ERROR_MESSAGE_DIV_ID = "error_message_div_id";
var ERROR_CLOSE_DIV_ID = "error_close_div_id";

var errors_is_initialized = false;

/*
	Attach to the window.onload event
*/
if (window.attachEvent) {
	window.attachEvent("onload", errors_init);
}


/* ------------------------------------------------------------------------
	Function:  errors_init

	Synopsis:  Initialize the error message handler. If the forms library
			   has been loaded, assign itself to that library as error
			   handler.

	Arguments: none

	Returns:   void

	Notes:     none
	------------------------------------------------------------------------*/
	function errors_init() {

		if (errors_is_initialized) {
			return;
		}
		errors_is_initialized = true;


		try {
			forms_setErrorOutputFunction(errors_setError);
			forms_setErrorClearFunction(errors_clearError);
		}
		catch (e) {
			// forms module is probably not loaded		
		}
		
		var errContainerDiv = document.getElementById(ERROR_CONTAINER_DIV_ID);
		var errMsgDiv = document.getElementById(ERROR_MESSAGE_DIV_ID);
		
		if ((errContainerDiv != null) &&
			(errMsgDiv != null)) {
			
			if (errMsgDiv.innerHTML == "") {
				errContainerDiv.style.visibility = "hidden";
			}
			else {
				errContainerDiv.style.visibility = "visible";
			}
		}		
		
		var errClrDiv = document.getElementById(ERROR_CLOSE_DIV_ID);
		if (errClrDiv != null) {
			errClrDiv.onclick = errors_clearError;
			errClrDiv.style.cursor = "hand";
		}
	}

/* ------------------------------------------------------------------------
	Function:  errors_setError

	Synopsis:  Display an error message.

	Arguments: none

	Returns:   void

	Notes:     none
	------------------------------------------------------------------------*/
	function errors_setError(msg, exception) {
		var errContainerDiv = document.getElementById(ERROR_CONTAINER_DIV_ID);
		var errMsgDiv = document.getElementById(ERROR_MESSAGE_DIV_ID);
		
		if ((errContainerDiv != null) &&
			(errMsgDiv != null)) {
			errMsgDiv.innerHTML = msg;
			
			// For an IE filter to run, the DIV needs "layout", i.e. a given width (not in %)
			errContainerDiv.style.visibility = "visible";
			errContainerDiv.scrollIntoView();
		}		
		
		if (exception) {
			window.status = exception.description;
		}
	}
	
/* ------------------------------------------------------------------------
	Function:  errors_clearError

	Synopsis:  Reset all errors.

	Arguments: none

	Returns:   void

	Notes:     none
	------------------------------------------------------------------------*/	
	function errors_clearError() {
		var errContainerDiv = document.getElementById(ERROR_CONTAINER_DIV_ID);
		var errMsgDiv = document.getElementById(ERROR_MESSAGE_DIV_ID);
		
		if ((errContainerDiv != null) &&
			(errMsgDiv != null)) {
			errMsgDiv.innerHTML = "";
			errContainerDiv.style.visibility = "hidden";
		}		
	}
