/* ===================================================================================

	TITLE:			Error Handler 2003 (2.3)
	DESCRIPTION:	Stand alone client side error handler.
	FILENAME:		rmm-error.js

	AUTHOR:			Randy Maloney [randym]
	CREATED:		23 February 1999

	VERSION:		1.xx	[randym]	16 March 1999		JavaScript 1.1
					...
					2.0		[randym]	04 January 2000		JavaScript 1.2
					2.1		[randym]	10 March 1999
					2.2		[randym]	10 March 1999		JavaScript 1.4
					2.3		[randym]	10 March 1999

	----------------------------------------------------------------------------------
	HISTORY:

	23 Feb 1999		[randym]	Inital script file created.
	04 Jan 2000		[randym]	Upadate to take advantage of JavaScript 1.2.
	16 Dec 2001		[randym]	Upadate to take advantage of JavaScript 1.4.
	23 May 2002		[randym]	Fully re-useable & object oriented.

	----------------------------------------------------------------------------------
	NOTES:

		Before properties or methods can be used, the Init() function must be 
		executed!!

		------------------------------------------------------------------------------

		Please also add the common function script (rmm-err.js) to your 
		*.asp/htm/html document as there are additional function calls that are only 
		located from this file.

   =================================================================================== */

/* -- Global Javascript variables ------------------------------------------- START -- */

	var maGroups = new Array();
	var maCache = new Array();
	var lngBookMark = 0;
	var rmmError = new Object();
	var intErrRaised = false;

/* -- Global Javascript variables ---------------------------------------------------- */

/* -- initialiseValidate() -------------------------------------------------- START --
   ----------------------------------------------------------------------------------- */
	function initErr() {

		// Generate internal error messages upon initialisation
		rmmError.groups.add('IntErrMsgs');
		rmmError.groups('IntErrMsgs').messages.add(1, 'Type mismatch.');
		rmmError.groups('IntErrMsgs').messages.add(2, 'Zero arguments passed from method it was called from.');
		rmmError.groups('IntErrMsgs').messages.add(3, 'Maximum number of parameters exeeded.');
		rmmError.groups('IntErrMsgs').messages.add(4, 'Day parameter is not supported for this method.');
		rmmError.groups('IntErrMsgs').messages.add(5, 'Invalid parameter for back/forward property, range should be set \'min:max\' and not vice versa.');
	}

/* ---------------------------------------------------------------------------- END -- */

/* -- jsCoreStat() ----------------------------------------------------------- START --
	Dis-ables all script functions/methods if core script (dprjsc20.js) has not been 
	located/processed.
   ----------------------------------------------------------------------------------- */
	function jsCoreStat() {

		if (!blnCoreLocated) {
		}
	}
/* ---------------------------------------------------------------------------- END -- */
  
/* -- _AddGroup() ----------------------------------------------------------- START --
   ----------------------------------------------------------------------------------- */
	function _AddGroup(group) { 
	var blnGroupExist = new Boolean(false);
	var lngIndex = 0; lngCount = 0;

		if (arguments.length < 1) {
			// Alert developer/user.
			alert('Add() method requires group name argument.');
			// Exit method.
			return(void 0);
		} else if (arguments.length > 1) {
			// Alert developer/user.
			alert('Too many arguments passed to method.');
			// Exit method.
			return(void 0);
		} else if (typeof(group) != 'string') {
			// Alert developer/user.
			alert('Type mismatch, group argument requires alphabetic (string) characters only.');
			// Exit method.
			return(void 0);
		} else if (group.length == 0 || group == null || group == '') {
			// Alert developer/user.
			alert('Group argument cannot be null or an empty string');
			// Exit method.
			return(void 0);
		} else {
			// Check to see if group already exists.
			for (lngCount = 0; lngCount < maGroups.length; lngCount++) {
				if (maGroups[lngCount].name == group) {
					lngIndex = lngCount
					blnGroupExist = true;

					// Exit out of loop.
					break
				}
			}

			if (blnGroupExist == false) {
				// Member does not exist, add a new group.
				lngIndex = maGroups.length;

				// Add new group to array.
				maGroups[lngIndex] = new _Group(group);
			} else {
				// Member already exists.

				// Alert developer/user.
				alert('\'' + group + '\' group already exists.');
				// Exit method.
				return(void 0);
			}
		}
	}
/* ---------------------------------------------------------------------------- END -- */

/* -- _CountGroup() --------------------------------------------------------- START --

	Returns the number of member within array/collection object.

   ----------------------------------------------------------------------------------- */
	function _CountGroup() { 
		return(maGroups.length);
	}
/* ---------------------------------------------------------------------------- END -- */

/* -- _ItemGroup() ---------------------------------------------------------- START --
   ----------------------------------------------------------------------------------- */
	function _ItemGroup(vntKeyIndex) {
	var lngCount;

		if (arguments.length == 0) {
			alert('This document currently contains no query/search string properties within the document URL.');
			// Exit method.
			return(void 0);
		} else {
			if (typeof(vntKeyIndex) == 'number' || typeof(vntKeyIndex) == 'string') {
				if (isNaN(vntKeyIndex)) {
					// Find associated group.
					for (lngCount = 0; lngCount < maGroups.length; lngCount++) {
						if (maGroups[lngCount].name == vntKeyIndex) {
							// Set bookmark to indicate which group to access message(s) from.
							lngBookMark = lngCount;
							// Message group found!!
							return maGroups[lngCount];
						}
					}

					// No array element of that name found, return nothing.
					alert('\'' + vntKeyIndex.toString() + '\' group does not exist or cannot be found.');
					// Exit method.
					return(void 0);
				} else {
					if (parseInt(vntKeyIndex) < maGroups.length) {
						// Set bookmark to indicate which group to access message(s) from.
						lngBookMark = parseInt(vntKeyIndex);
						// Return item value back through function.
						return maGroups[parseInt(vntKeyIndex)];
					} else {
						// Alert developer/user.
						alert('There are only ' + maGroups.length + ' group(s) defined.');
						// Exit method.
						return(void 0);
					}
				}
			} else {
				// Alert developer/user.
				alert('Type mismatch, whole numbers (integers) accepted only.');
				// Exit method.
				return(void 0);
			}
		}
	}

/* -- _Group() -------------------------------------------------------------- START --

	Construct message group element(s).

   ----------------------------------------------------------------------------------- */
	function _Group(name) {
		this.name = name;
		this.cache = new Array();	// Storage for all messages created for this group.
		this.messages = new Object();
		this.messages = _ItemMessage;
		this.messages.add = _AddMessage;
		this.messages.count = _CountMessage;
	}
/* ---------------------------------------------------------------------------- END -- */

/* -- _AddMessage() --------------------------------------------------------- START --
   ----------------------------------------------------------------------------------- */
	function _AddMessage(index, message, alt) { 
	var blnGroupExist = new Boolean(false);
	var lngIndex = 0; lngCount = 0;

		if (arguments.length < 2) {
			// Alert developer/user.
			alert('Add() method requires group name, index, message and alternative message (optional) arguments.');
			// Exit method.
			return(void 0);
		} else if (arguments.length > 3) {
			// Alert developer/user.
			alert('Too many arguments passed to method.');
			// Exit method.
			return(void 0);
		} else if (typeof(index) != 'number') {
			// Alert developer/user.
			alert('Type mismatch, number argument requires only whole numbers (long).');
			// Exit method.
			return(void 0);
		} else if (typeof(alt) != 'string' && (alt !== (void 0))) {
			// Alert developer/user.
			alert('Type mismatch, alt argument requires only string values.');
			// Exit method.
			return(void 0);
		} else if (message.length == 0 || message == null || message == '') {
			// Alert developer/user.
			alert('Message argument cannot be null or an empty string');
			// Exit method.
			return(void 0);
		} else {
			// Check to see if group already exists.
			for (lngCount = 0; lngCount < maGroups[lngBookMark].cache.length; lngCount++) {
				if (maGroups[lngBookMark].cache[lngCount].index == index) {
					lngIndex = lngCount
					blnGroupExist = true;

					// Exit out of loop.
					break
				}
			}

			if (blnGroupExist == false) {
				// Member does not exist in group, add a new message.
				lngIndex = maGroups[lngBookMark].cache.length;

				if (typeof(alt) === (void 0) || typeof(alt) == 'undefined') {
					alt = '';
				}

				// Add message to new group.
				maGroups[lngBookMark].cache[lngIndex] = new _Message(index, message, alt);
			} else {
				// Member already exists.

				// Alert developer/user.
				alert('Error #' + index + ' already exists.');
				// Exit method.
				return(void 0);
			}
		}
	}

/* -- _CountMessage() -------------------------------------------------------- START --

	Returns the number of member within array/collection object.

   ----------------------------------------------------------------------------------- */
	function _CountMessage() { 
		if (maGroups.length > 0) {
			return maGroups[lngBookMark].cache.length;
		} else {
			return(0);
		}
	}

/* ---------------------------------------------------------------------------- END -- */

/* -- _Message() ------------------------------------------------------------ START --

	Construct message element(s).

   ----------------------------------------------------------------------------------- */
	function _Message(index, value, alt) {

		this.index = parseInt(index);
		this.value = value;
		this.alt = alt;
	}

/* ---------------------------------------------------------------------------- END -- */

/* -- _ItemMessage() -------------------------------------------------------- START --
   ----------------------------------------------------------------------------------- */
	function _ItemMessage(vntIndexKey) {
	var blnMsgExist = false, blnUseAlt = false;
	var lngCount = 0;

		if (arguments.length == 0) {
			alert('This document currently contains no query/search string properties within the document URL.');
			// Exit method.
			return(void 0);
		} else {
			if (typeof(vntIndexKey) == 'string' || vntIndexKey < 0) {
				vntIndexKey = vntIndexKey.toString();

				if (vntIndexKey.substring(0, 1) == '-') {
					// Remove the minus character and convert back to a number.
					vntIndexKey = parseInt(strip(vntIndexKey, '-'));
					blnUseAlt = true;
				} else {
					vntIndexKey = parseInt(vntIndexKey);
				}
			} else if (typeof(vntIndexKey) != 'number') {
				// Alert developer/user.
				alert('Type mismatch, whole numbers (as long or string data types) accepted only.');
				// Exit method.
				return(void 0);
			}

			if (vntIndexKey <= maGroups[lngBookMark].cache.length) {
				for (lngCount = 0; lngCount < maGroups[lngBookMark].cache.length; lngCount++) {
					if (maGroups[lngBookMark].cache[lngCount].index == vntIndexKey) {
						// Return item value back through function.
						return maGroups[lngBookMark].cache[lngCount];
						// Exit out of loop.
						break
					}
				}
			} else {
				// Alert developer/user.
				alert('There are only ' + maGroups[lngBookMark].cache.length + ' messages defined with this (' + maGroups[lngBookMark].name + ') group.');
				// Exit method.
				return(void 0);
			}
		}
	}

/* ---------------------------------------------------------------------------- END -- */

/* -- CacheErr() ------------------------------------------------------------ START --
   ----------------------------------------------------------------------------------- */
	function CacheErr(errNumber) {

		maCache[maCache.length] = new Array();
		maCache[maCache.length - 1][0] = rmmError.groups.active;
		maCache[maCache.length - 1][1] = errNumber;
	}

/* ---------------------------------------------------------------------------- END -- */

/* -- _RaiseIntMsg() -------------------------------------------------------- START --
   ----------------------------------------------------------------------------------- */
	function _RaiseIntMsg () {
	var lngCount = 0;
	var strErrMessage = new String('An internal script error has occured.\nValidation on this page cannot be carried out for the following reasons:\n');

		// Check to see if any internal JavaScript errors occurred.
		for (lngCount = 0; lngCount < maCache.length ; lngCount++) {
			// Raise ONLY internally generated error messages.
			if (intErrRaised && (maCache[lngCount][0] == 'IntErrMsgs')) {
				strErrMessage += '\n  -  ' + rmmError.groups(maCache[lngCount][0]).messages(maCache[lngCount][1].toString()).value;
			}
		}

		intErrRaised = false;

		// Show error messages raised.
		alert(strErrMessage);
	}

/* -- _RaiseMessage() ------------------------------------------------------- START --
   ----------------------------------------------------------------------------------- */
	function _RaiseMessage() {
	var lngCount = new Number(0);
	var strErrMessage = new String(this.groups.header + '\n');

		// Check to see if any internal errors has been raised.
		if (intErrRaised) {
			alert("1");
			_RaiseIntMsg();

			/* Because of internal error, we do not want to prevent the user from 
			   going to the next page. */
			return(true);
		}

/*
		if (arguments.length == 0 && maCache.length == 0) {
			alert(maCache.toString());
			// Alert developer/user.
			alert('Cannot display raised errors without indicating which error(s) to show.');
			// Exit method.
			return(false);
		} else 
*/

		if (arguments.length > 0) {
			for (lngCount = 0; lngCount <= arguments.length ; lngCount++) {
				if (typeof(arguments[lngCount]) == 'number') {
					if (this.groups(this.groups.active).messages(arguments[lngCount]) == (void 0)) {
						return(false);
					} else {
						switch (typeof(arguments[lngCount])) {
							case 'number':
								if (parseInt(arguments[lngCount]) > 0) {
									strErrMessage += '\n  -  ' + this.groups(this.groups.active).messages(arguments[lngCount]).value;
								} else if (parseInt(arguments[lngCount]) < 0) {
									strErrMessage += '\n  -  ' + this.groups(this.groups.active).messages(arguments[lngCount]).alt;
								}
 
								break
							case 'string':
								strErrMessage += '\n  -  ' + this.groups(this.groups.active).messages(arguments[lngCount]).value;
						}
					}
				}
			} 

			// Show error messages.
			alert(strErrMessage);
			// Remove/reset list of all errors raised.
			maCache = new Array();
			// Exit method.
			return(true);
		} else if (maCache.length > 0) {
			for (lngCount = 0; lngCount < maCache.length ; lngCount++) {
				if (!this.groups.alt) {
					strErrMessage += '\n  -  ' + this.groups(maCache[lngCount][0]).messages(maCache[lngCount][1].toString()).value;
				} else if (parseInt(arguments[lngCount]) < 0) {
					strErrMessage += '\n  -  ' + this.groups(maCache[lngCount][0]).messages(maCache[lngCount][1].toString()).alt;
				}
			}

			// Show error messages.
			alert(strErrMessage);
			// Remove/reset list of all errors raised.
			maCache = new Array();
			// Exit method.
			return(true);
		} else {
			// Exit method.
			return(false);
		}
	}

/* ---------------------------------------------------------------------------- END -- */

	// Assign methods and properties to top level object. ----------------------------

	rmmError.raise = _RaiseMessage;

/* -----------------------------------------------------------------------------------

	rmmError.groups()
	rmmError.groups().messages()
		Arguments
			vntKeyIndex			-	Syntax: rmmError.messages(key$|position&)
									Long|String, Returns a message from collection base 
									on the member name or position.

		Additional properties/methods below can be used to refine how the value 
		argument can be validated.

			add					-	Syntax: rmmError.messages.add(group$, number%, message$)
									Determines which characters 
			count				-	Long, Returns/Sets the number of members within 
									collection.
			clear				-	Removes all members.
			activeGroup			-	Long|String, Determines which message group to 
									generate error messages from.
			raise				-	Syntax: rmmError.messages.remove(aParam)
									Array, Displays error message(s) raise.
			remove				-	Syntax: rmmError.messages.remove(key$|position%)
									Removes a specified member from collection.
	   ------------------------------------------------------------------------------- */

	rmmError.groups = new Object();
	rmmError.groups = _ItemGroup;
	rmmError.groups.add = _AddGroup;
	rmmError.groups.header = '';
	rmmError.groups.active = '';
	rmmError.groups.alt = false;
	rmmError.groups.count = _CountGroup;

/* ----------------------------------------------------------------------------------- */

/* =================================================================================== */