<!--//
/**
 * Javascript class to manage clientside page error messages
 */
function PageErrors() {
	var pageErrorsArr		= new Array();
	var msgIconLayersArr	= new Array();
	var highlightFieldsArr = new Array();
	var numErrors			= 0;
	this.addError 			= addError;
	this.clearError			= clearError;
	this.layoutErrors 		= layoutErrors;
	this.hasErrors			= hasErrors;
	this.clearErrors		= clearErrors;
	this.setMsgIcon			= setMsgIcon;
	this.showConfirm		= showConfirm;
	this.showAlert			= showAlert;
	this.getText			= getText;
	this.removeHightlight	= removeHighlight;
	this.highlightField		= highlightField;

	/**
	 * Add the error to the page errors array
	 * @param key
	 * @param value
	 */
	function addError(key, value) {
		
		// clear any existing errors with this key
		clearError(key);
	
		// create the new error aray
		errorArr 			= new Array();
		errorArr['key'] 	= getText(key);
		errorArr['value'] 	= getText(value);

		// add the error arr
		pageErrorsArr.push(errorArr);

		// increment the error
		numErrors++;

		// layout the errors
		layoutErrors();
	}

	/**
	 * Sets a message icon
	 */
	function setMsgIcon(layer,icon,message) {
		var obj;

		// if we are setting a message
		if (message != '' && message != 'undefined') { 

			// translate
			message = getText(message);

			if ((obj=findObj(layer)) != null) { 

				// add it to the array if it does not already exist
				msgIconLayersArr[layer] = layer;

				// set the innter html
				obj.innerHTML = '&nbsp;<img src="images/admin_icons/' + icon + '.gif" alt="' + message + '" title="' + message + '" style="vertical-align:middle;" />';

				// display the layer
				displayLayer(layer, '');
			} 
		} else { 
		
			// hide the layer
			displayLayer(layer, 'none');

			// remove it from the array
			try {
				msgIconLayersArr[layer] = layer;
			} catch (error) {}
		}
	}

	/**
 	 * Clears the page errors but does not relayout the page
	 * Clears the array of msgIcons
	 */
	function clearErrors() {
		numErrors 		= 0;
		pageErrorsArr 	= Array();

		// clear the message icons
		for (var layer in msgIconLayersArr) { 

			// clear the msg icon
			setMsgIcon(layer, '', '');

			// delete the property
			delete msgIconLayersArr[layer];
		}

		// clear any field highlighting
		for (i=0;i<highlightFieldsArr.length;i++) {
			removeHighlight(highlightFieldsArr[i]);
		}

		// 
		layoutErrors();
	}

	/**
	 *
	 */
	function removeHighlight(field) {

		var obj;

		if ((obj = findObj(field)) != null) {
			obj.className = '';
		}

		// remove this entry
		delete highlightFieldsArr[field];
	}

	/**
	 * Add highlighting (red) to a field
	 */
	function highlightField(field) {
	
		// clear any existing errors with this key
		removeHighlight(field);
	
		// add the error arr
		highlightFieldsArr.push(field);

		var obj;

		if ((obj = findObj(field)) != null) {
			obj.className = 'highlightField';
		}
	}


	/**
	 * Clear the error value
	 */
	function clearError(key) {
		tmpErrorsArr = Array();

		// translate the key
		key = getText(key);
	
		for(i=0;i<pageErrorsArr.length;i++) {
			if (pageErrorsArr[i]['key'] == key) {
				continue;
			} else {
				tmpErrorsArr.push(pageErrorsArr[i]);
			}
		}

		// set the erro number
		numErrors 		= tmpErrorsArr.length;
		pageErrorsArr 	= tmpErrorsArr;

		// layout the errors
		layoutErrors();
	}

	/**
	 * Print out the errors
	 */
	function layoutErrors() {
	
		var pageErrorsListLayerObj;

		// if we have no errors - hide the box and return
		if (numErrors <= 0) {
			displayLayer('pageErrorsLayer', 'none');
			return true;
		}

		// start the list html
		listHTML = '<ul style="list-style-type:square;">'
		
		// for each error
		for(i=0;i<pageErrorsArr.length;i++) {

			// assign the current error
			err = pageErrorsArr[i];

			// check we have still got a value or skip this error
			if (err["value"] == '') { continue; }
		
			// add the page error to the list
			listHTML += '<li><b>' + err['key'] + '</b> - ' + err['value'] + '</li>';
		}

		// end the list html
		listHTML += '</ul>';

		if ((pageErrorsListLayerObj = findObj('pageErrorsListLayer')) != null) {
			pageErrorsListLayerObj.innerHTML = listHTML;
			displayLayer('pageErrorsLayer', '');
		} 
	}

	/**
	 * Returns true if a errors exist
	 */
	function hasErrors() {
		return (numErrors > 0);
	}

	/**
	 * Wrapper to standard js alert message
	 */
	function showAlert(msg) {
		return alert(getText(msg));
	}

	/**
	 * Wrapper to standard js alert message
	 */
	function showConfirm(msg) {
		return confirm(getText(msg));
	}

	/**
	 * Get text wrapper
	 */
	function getText(msg) {

		try {
			// check in the gettext
			if (l != null && l[msg] != null) {
				return l[msg];
			} else {
				return msg;
			}
		} catch (error) {
			return msg;
		}
	}
}	

// create an instance of the errors object
pageErrors = new PageErrors();
//-->
