/* -----------------------------------------------------------------------------------
	Please also add the helper validation file(s) (validate_helper.inc/js & 
	validate_objects.inc/js) to your *.asp/htm/html document as there are additional 
	function calls that are only located from these file(s).
   ----------------------------------------------------------------------------------- */
/* -----------------------------------------------------------------------------------
	Most (if not all) the functions called from this file can accept parameter
	arrays to further scrutinise the value/object that is being tested.
	These parameters must be passed to the called function as a string variable and 
	seperated by a semi-colon (;) so that each one can be recognised and to determine 
	what actions to take.
	
		eg.	grillTextBox(txtTextbox.value, "zerolen; noalpha:aAbBcCdD");
	
	Keys:
		[a]		-	alphanumeric characters only (a/A to z/Z and 0 to 9).
		[a..]	-	alphabetic characters only (a/A to z/Z).
		[n]		-	numbers only.
		[n..]	-	numerical characters only (0 to 9).
		[na]	-	number and single character only (eg 20y).
		[x]		-	constant (strings), only one can be used.
   ----------------------------------------------------------------------------------- */
/* -----------------------------------------------------------------------------------
	grillTextBox()
		Parameters
			value		-	Variant, Returns the value contained from TextBox.
			params		-	String [optional], Returns defined parameters on how textbox 
							is interrogated.
							Parameter options...
								zerolen			-	Determines is value should have a zero
													length string.
								min:[n]			-	Sets the minimum length of characters 
													required.
								max:[n]			-	Sets the maximum length of characters 
													required.
								noalpha			-	Determines is value should have alpha 
													characters.
								noalpha:[a..]	-	Same as using 'noalpha', additional value
													excludes those characters from check.
								nonumeric		-	Determines is value should have number 
													characters.
								nonumeric:[x]	-	[positive|negative|exponential]
													Sets the text box to not allow positive,
													negative numbers or exponential values.
								nosymbol		-	Determines is value should have non 
													alphanumeric characters.
								nosymbol:[a..]	-	Same as 'nosymbol', additional value
													excludes those characters from check.
		Returns
			0	-	value is OK and meets requirements.
			1	-	value is a zero lengthed string
			2	-	value is lower than the minimun length required.
			3	-	value is greater than the maximum length required.
			4	-	value contains alpha characters.
			5	-	value contains numeric characters.
			6	-	value contains symbol characters.
			7	-	value contains a positive number.
			8	-	value contains a negative number.
			9	-	value contains an exponential value.
   ----------------------------------------------------------------------------------- */
function grillTextBox(value, params) {
var aParams = new Array();
var aSubElement = new Array();
var min = 0;
var max = 0;
var exclude;
var lngCount;

	// Fragment parameter string into their individaul arguments.
	aParams = splitt(params, ";");

	/* Loop through parameter array to determine which argument(s) have been requested
	   and act upon them */
	for (lngCount = 0; lngCount < aParams.length; lngCount++) {
		if (aParams[lngCount] == "zerolen") {
			// Check if value contains any data.
			if (value.length == 0 || value == "" || value == null) {
				return(1);
				break
			}
		} 
		
		if (aParams[lngCount].substring(0, 3) == "min") {
			aSubElement = splitt(aParams[lngCount], ":");
			if (aSubElement.length == 2) {
				min = parseInt(aSubElement[1])

				// Check if value meets the minimum character length specified.
				if (value.length < min && value.length != 0) {
					return(2);
					break
				}
			}
		} 
		
		if (aParams[lngCount].substring(0, 3) == "max") {
			aSubElement = splitt(aParams[lngCount], ":");
			if (aSubElement.length == 2) {
				max = parseInt(aSubElement[1])

				// Check if value is within the maximum chraracter length specified.
				if (value.length > max && value.length != 0) {
					return(3);
					break
				}
			}
		} 
		
		if (aParams[lngCount].substring(0, 7) == "noalpha") {
			/* Check if value contains alphabetical characters. */
			/* Fragment parameter in order to get its value. */
			aSubElement = splitt(aParams[lngCount], ":");
			if (aSubElement.length == 1) {
				// look for all alphabetical characters.
				if (hasAlpha(value)) {
					return(4);
					break
				} 
			} else if (aSubElement.length == 2) {
				// look for all alphabetical characters excluding those specified.
				if (hasAlpha(value, aSubElement[1])) {
					return(4);
					break
				}
			}
		} 
		
		if (aParams[lngCount] == "nonumeric") {
			if (hasNumeric(value)) {
				return(5);
				break
			}
		} else if (aParams[lngCount].length > 7 && aParams[lngCount].substring(0, 7) == "numeric") {
			aSubElement = splitt(aParams[lngCount], ":");
			if (aSubElement[1] == "positive") {
				if (value > 0) return(7);
			} else if (aSubElement[1] == "negative") {
				if (value > 0) return(8);
			} else if (aSubElement[1] == "exponential") {
				if (hasAlpha(value)) return(9);
			}
		} else if (aParams[lngCount].substring(0, 8) == "nosymbol") {
			/* Check if value contains symbollic characters. */
			/* Fragment parameter in order to get its value. */
			aSubElement = splitt(aParams[lngCount], ":");
			if (aSubElement.length == 1) {
				// look for all symbollic characters.
				if (hasSymbol(value)) {
					return(6);
					break
				}
			} else if (aSubElement.length == 2) {
				// look for all symbollic characters excluding those specified.
				if (hasSymbol(value, aSubElement[1])) {
					return(6);
					break
				}
			}
		}
	}

	return(0);
}


function ShowErrorForField(errorCode, minVal, maxVal, field)
{
	var errStr;
	errStr = field + ": ";

	switch (errorCode)
	{
		case 1:
		{
			errStr +=  "please fill in this field";	
			break;
		}
		case 2:
		{
			errStr += "enter at least " + minVal.toString() + " characters";
			break;
		}
		case 3:
		{	
			errStr += "enter less than " + maxVal.toString() + " characters";
			break;
		}
		case 4:
		{
			errStr += "no letters allowed in this field";
			break;
		}
		case 5:
		{	
			errStr += "no numbers allowed in this field";
			break;
		}
		
		case 6:
		{
			errStr += "no symbolic characters allowed in this field"
			break;
		}
		
	}
	
	alert(errStr);
}

/* -----------------------------------------------------------------------------------
	grillNumberBox() - grillTextBox wrapper function
		Parameters
			value		-	Variant, Returns the value contained from TextBox.
			params		-	String [optional], Returns defined parameters on how textbox 
							is interrogated.
							Parameter options...
								min:[n]			-	Sets the minimum numerical value allowed.
								max:[n]			-	Sets the maximum numerical value allowed.
								type:[x]		-	[positive|negative|exponential]
													Sets the text box to not allow positive,
													negative numbers or exponential values.
		Returns
			0	-	value is OK and meets requirements.
			1	-	value cannot be empty.
			2	-	value is lower than the minimun number needed.
			3	-	value is greater than the maximum number needed.
			4	-	value contains alpha characters.
			5	-	value contains numeric characters.
			6	-	value contains symbol characters.
			7	-	value contains a positive number.
			8	-	value contains a negative number.
			9	-	value contains an exponential value.
   ----------------------------------------------------------------------------------- */
function grillNumberBox(value, params) {
var aParams = new Array(), aSubElement = new Array();
var lngCount;
var retValue;
var strParams = "";

	// Fragment parameter string into their individaul arguments.
	aParams = splitt(params, ";");

	/* Parse through parameter string to determine what is need and add to the 
	   grillTextBox function */
	for (lngCount = 0; lngCount < aParams.length; lngCount++) {
		if (aParams[lngCount].substring(0, 3) == "min") {
			strParams += aParams[lngCount].substring + ";";
		} else if (aParams[lngCount].substring(0, 3) == "max") {
			strParams += aParams[lngCount].substring + ";";
		} else if (aParams[lngCount].substring(0, 4) == "type") {
			/* Fragment parameter in order to get its value. */
			aSubElement = splitt(aParams[lngCount], ":");
			strParams += "nonumeric:" + aSubElement[1].substring;
		}
	}

	retValue = grillTextBox(value, "zerolen; noalpha; nosymbol:." + strParams);

	if (retValue == 0) {
		return(0);
	} else {
		return(retValue);
	}
}

/* -----------------------------------------------------------------------------------
	grillOptionButton()
		Parameters
			option		-	object, Returns the object containing the array of options.
			required	-	Boolean [Optional], Determines if one of the option
							elements (greater than element 0) must be selected.
		Returns
			Boolean, True if value meets criteria else False.
   ----------------------------------------------------------------------------------- */
function grillOptionButton(option, required) {
var blnCrispOrCharred = true;
var blnOptionChosen = false;
var Count;

	if (typeof(required) != "undefined") {
		if (required) {
			for (Count = 0; Count < option.length; Count++) {
				blnOptionChosen = option[Count].checked || blnOptionChosen;
			}
		}
	}

	blnCrispOrCharred = blnOptionChosen && blnCrispOrCharred;

	return(blnCrispOrCharred);
}

/* -----------------------------------------------------------------------------------
	grillComboBox()
		Parameters
			combo		-	object, Returns the object containing the array of options.
			required	-	Boolean [Optional], Determines if one of the option elements 
							(greater than element 0) must be selected.
		Returns
			Boolean, True if value meets criteria else False.
   ----------------------------------------------------------------------------------- */
function grillComboBox(combo, required) {
var blnCrispOrCharred = true;
var blnOptionChosen = false;
var Count;

	if (typeof(required) != "undefined") {
		if (required) {
			blnOptionChosen = (combo.selectedIndex > 0) || blnOptionChosen;
		}
	}

	blnCrispOrCharred = blnOptionChosen && blnCrispOrCharred;

	return(blnCrispOrCharred);
}

/* -----------------------------------------------------------------------------------
	grillCurrency()
		Parameters
			value		-	String, Returns the object containing the array of options.
			required	-	Boolean [Optional], Determines is crncy should have a zero 
							length string.
		Returns
			0	-	value is OK and meets requirements.
			1	-	value contains nothing.
			2	-	value contains alphabetic and/or symbollic characters.
   ----------------------------------------------------------------------------------- */
function grillCurrency(value, required) {
var retValue = 0;
var blnHasPound = false;

	if (typeof(required) != "undefined") {
		if (required) {
			if (value.length == 0 || value == "£") return(1);
		}
	}

	// Currency field is not needed but value has been added anyway.
	if (value.length > 0) {
		/* Check for any alphabetic and/or symbollic characters 
		   (excluding £, commas and decimal). */
		if (hasAlpha(value) || hasSymbol(value, "£,.")) return(2);
	} else {
		// Nothing to look for...
		return(0);
	}

	return(retValue);
}

/* -----------------------------------------------------------------------------------
	grillCurrencyA()
		Parameters
			value		-	String, Returns the object containing the array of options.
			params		-	String [optional], Returns defined parameters on how textbox 
							is interrogated.
							Parameter options...
								required		-	Determines is value should have a 
													zero length string.
								symbol:a		-	Sets the optional currency symbol to
													to be used.
								ceilling:[n]	-	Sets the maximum value allowed.
													The default is 0 if not used.
								floor:[n]		-	Sets the minimum value allowed.
													The default is 0 if not used.
								rounded			-	Rounds the currency value to a 
													whole number.
								dad:[n]			-	Digits After Decimal, Sets the
													number of digits allowed after the
													decimal point.
													The default is 2 if not used.
								dig:[n]*		-	Digits In Group, Set the number of
													digits to group.
													The default is 3 if not used.
							
							* Assuming that the comma is used to emphasize on currency,
							  eg. n,nnn or n,nnn,nnn.
							
		Returns
			0	-	value is OK and meets requirements.
			1	-	value contains nothing or is 0/£0).
			2	-	value entered is not recognised as a valid currency.
			3	-	value is more than the limit (ceilling) specified.
			4	-	value is less than the limit (floor) specified.
   ----------------------------------------------------------------------------------- */
function grillCurrencyA(value, params) {
var acurAmount = new Array();
var aParams = new Array();
var aElement = new Array();
var blnRounded = false;
var curAmount = 0;
var curCeilling = 0;
var curFloor = 0;
var curSymbol = null;
var digitsAftDeci = 2;
var digitsInGroup = 0;
var lngCount;
var blnSymbolFound = false;

	// If any, breakdown parameter string into an array.
	aParams = splitt(params, ";");

	if (typeof(params) != "undefined") {
		for (lngCount = aParams.length; lngCount > 0; lngCount--) {

			aElement = splitt(aParams[(lngCount - 1)], ":");

			if (aElement[0] == "required") {
				// Check source is not a zero value
				if (value.length == 0 || value.length == "0" || value == curSymbol) {
					return(1);
					break
				} else if (value == curSymbol + "0" && curFloor > 0) {
					return(1);
					break
				}
			} else if (aElement[0] == "symbol") {
				if (aElement[1] != "") curSymbol = aElement[1];
			} else if (aElement[0] == "ceilling") {
				if (aElement[1] != "" || !isNaN(aElement[1])) curCeilling = parseInt(aElement[1]);
			} else if (aElement[0] == "floor") {
				if (aElement[1] != "" || !isNaN(aElement[1])) curFloor = parseInt(aElement[1]);
			} else if (aElement[0] == "rounded") {
				blnRounded = true;
			} else if (aElement[0] == "dad") {
				if (aElement[1] != "" || !isNaN(aElement[1])) digitsAftDeci = parseInt(aElement[1]);
			} else if (aElement[0] == "dig") {
				if (aElement[1] != "" || !isNaN(aElement[1])) digitsInGroup = parseInt(aElement[1]);
			}
		}
	}

	// Currency field is not needed but value has been added anyway.
	if (value.length > 0) {
		// Determine if the currency symbol is being used. If so, it must be
		// at the start of the string and not appear anywhere else.
		if (value.substring(lngCount, curSymbol.length) == curSymbol)
			blnSymbolFound = true;
		for (lngCount = curSymbol.length; lngCount < value.length - curSymbol.length; lngCount++) {
			if (value.substring(lngCount, lngCount+curSymbol.length) == curSymbol)
				return 2;
		}
		if (blnSymbolFound)
			value = strip(value, curSymbol);

		/* Check for any alphabetic and/or symbollic characters 
		   (excluding £, commas and decimal). */
		if (hasSymbol(value, ".") || hasAlpha(value)) return(2);


/*
	Checks currency format, making sure commas and decimal points are in the
	right posistions.
*/
/*
		acurAmount = splitt(value, ",");

		// Check the format of value.
		if (acurAmount.length > 1) {
			for (lngCount = 0; lngCount < acurAmount.length; lngCount++) {
				if (lngCount != 0 && lngCount != (acurAmount.length - 1)) {
					if (digitsInGroup != 0) {
						if (acurAmount[lngCount].length < digitsInGroup) {
							return(2);
							break
						}
					}
				} else if (lngCount == (acurAmount.length - 1)) {
					if (acurAmount[lngCount].length == digitsInGroup) {
						if (acurAmount[lngCount].substring(0, 1) == ".") {
							if (acurAmount[lngCount].substring(1, acurAmount[lngCount].length) == digitsAftDeci) {
								if (isNaN(acurAmount[lngCount].substring(1, 3))) {
									return(2);
									break
								}
							} else {
								return(2);
								break
							}
						} else if (isNaN(acurAmount[lngCount])) {
							return(2);
							break
						}
					}
				}
			}

			if (blnRounded) {
				curAmount = parseInt(Join(acurAmount, ""));
			} else {
				curAmount = parseFloat(Join(acurAmount, ""));
			}
		}
*/

		if (blnRounded) {
			curAmount = parseInt(value);
		} else {
			curAmount = parseFloat(value);
		}

		// Fail if currency value is less than the floor specified.
		if (curFloor != 0) {
			if (curAmount < curFloor) return(3);
		}

		// Fail if currency value is more than the ceilling specified.
		if (curCeilling != 0) {
			if (curAmount > curCeilling) return(4);
		}
	}

	return(0);
}

/* -----------------------------------------------------------------------------------
	grillDate()
		Parameters
			day			-	Integer, Returns the day number.
			month		-	Integer, Returns the month number.
			year		-	Integer, Returns the year number.
			params		-	String [optional], Returns defined parameters on how 
							textbox is interrogated.
							Parameter options...
								required	-	Determines if the date entry must be 
												filled
								nopast		-	dis-allows dates before today.
								nofuture	-	dis-allows dates beyond today. 
		Returns
			0	-	parameters are OK and meets requirements.
			1	-	the date has not been filled.
			2	-	the day contains less or more digits required.
			3	-	the year contains less or more digits required.
			4	-	the day and/or year contain letters and/or symbols.
			5	-	there are 1 - 31 days in 'odd' months.
			6	-	there are 1 - 30 days in 'even' months.
			7	-	if leap year variable is true February should have 29 days.
			8	-	if leap year variable is false February should have 28 days.
			9	-	the date is before today.
			10	-	the date is after today.
   ----------------------------------------------------------------------------------- */
function grillDate(day, month, year, params) {
var blnLeapYear = false;
var dteToday = new Date();
var dteDate = new Date();
var aParams = new Array();
var lngCount;
var varReturnVal = 0;

	// If any, breakdown parameter string into an array.
	aParams = splitt(params, ";");

	// Determine if year is a leap year
	blnLeapYear = isleapYear(year);

	// Months within the Date object start with an index of 0.
	month--;

	// Assign date of birth to a reference (date) object variable.
	with (dteDate) {
		setDate(parseInt(day));
		setMonth(month);
		setYear(parseInt(year));
	}

	for (lngCount = 0; lngCount < aParams.length; lngCount++) {
		if (aParams[lngCount] == "required") {
			if (day.length == 0 && month == -1 && year.length == 0) {
				varReturnVal = 1;
				break
			}
		}

		if (aParams[lngCount] == "nopast") {
			if (dteDate > dteToday) {
				varReturnVal = 9;
				break
			}
		}

		if (aParams[lngCount] == "nofuture") {
			if (dteDate < dteToday) {
				varReturnVal = 10;
				break
			}
		}
	}
		
	if (varReturnVal != 0) return(varReturnVal);

	// Check that the month has been selected.
	if (month  == -1) return(1);

	// Check that the day is consistent to 1 or 2 characters in length.
	if (day.length  > 3 || day.length == 0) return(2);

	// Check that the year is consistent to 4 characters in length.
	if (year.length != 4) return(3);

	// Check for alpha characters within variable.
	if (hasAlpha(year) == true || hasAlpha(day) == true) {
		return(4);
	// Check for spaces within variable.
	} else if (hasSpaces(year) == true || hasSpaces(day) == true) {
		return(4);
	// Check for symbollic characters within variable.
	} else if (hasSymbol(year) == true || hasSymbol(day) == true) {
		return(4);
	}

	// Check that the date given is consistent with month.
	if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || 
		month == 9 || month == 11) {
		if ((day > 0 && day < 32) == false) return(5);
	} else if (month == 3 || month == 5 || month == 8 || month == 10) {
		if ((day > 0 && day < 31) == false) return(6);
	} else if (month == 1) {
		if (blnLeapYear && day > 29) {
			return(7);
		} else if (!blnLeapYear && day > 28) {
			return(8);
		}
	}

	return(0);
}

/* -----------------------------------------------------------------------------------
	grillDateA()
		Parameters
			day			-	Integer/String, Returns the day number.
			month		-	Integer/String, Returns the month number.
			year		-	Integer/String, Returns the year number.
			params		-	String [optional], Returns defined parameters on how 
							textbox is interrogated.
							Parameter options...
								required		-	Determines if the date entry must 
													be filled.
								nopast			-	dis-allows dates before today.
								nopast:[na]		-	Same as 'nopast', additional check
													to verify date is no less than
													(eg. 5m = "Date > (Now - 5 mths).
													Use day (d), month(m) or year(y) to 
													determine how far back to check.
								nofuture		-	dis-allows dates beyond today. 
								nofuture:[na]	-	Same as 'nofuture', additional check
													to verify date is no greater than
													(eg. 5m = "Date > (Now + 5 mths). 
													Use day (d), month(m) or year(y) to 
													determine how far forward to check.
		Returns
			0	-	parameters are OK and meets requirements.
			1	-	the date has not been filled.
			2	-	the day contains more digits than required.
			3	-	the month contains more digits required.
			4	-	the year contains less or more digits required.
			5	-	the day/month/year contain letters and/or symbols.
			6	-	there are 1 - 31 days in 'odd' months.
			7	-	there are 1 - 30 days in 'even' months.
			8	-	if leap year variable is true February should have 29 days.
			9	-	if leap year variable is false February should have 28 days.
			10	-	the date is before today.
			11	-	the date is after today.
   ----------------------------------------------------------------------------------- */
function grillDateA(day, month, year, params) {
var aParams = new Array();
var aSubElement = new Array();
var blnIsLeapYear;
var dteFrwdMax = new Date(), dteBackMax = new Date(), dteToday = new Date();
var lngCount;
var objDateBackMax = new Object(), objDateChosen = new Object(), objDateFrwdMax = new Object();

	// -- Define object properties ------------------------------------------------------
	objDateBackMax.day   = dteToday.getDate();
	objDateBackMax.month = dteToday.getMonth();
	objDateBackMax.year  = dteToday.getYear();

	objDateChosen.day   = dteToday.getDate();
	objDateChosen.month = dteToday.getMonth();
	objDateChosen.year  = dteToday.getYear();

	objDateFrwdMax.day   = dteToday.getDate();
	objDateFrwdMax.month = dteToday.getMonth();
	objDateFrwdMax.year  = dteToday.getYear();
	// ----------------------------------------------------------------------------------

	// If any, breakdown parameter string into an array.
	aParams = splitt(params, ";");

	day   = day + "";
	month = month + "";
	year  = year + "";

	// If found, remove leading zeros.
	if (day.substring  (0, 1) == '0') day   = day.substring  (2, 1);
	if (month.substring(0, 1) == '0') month = month.substring(2, 1);

	/* Loop through parameter array to determine which argument(s) have been requested
	   and act upon them */
	for (lngCount = (aParams.length - 1); lngCount > -1; lngCount--) {
		if (aParams[lngCount] == "required") {
			// Check date format consistency.
			if (day.length == 0 && month == "0" && year.length == 0) {
				return(1);
			}
		}
	}

	if (day.length < 1 || day.length > 2 || parseInt(day) < 1 || parseInt(day) > 31 ) return(2);
	if (month.length < 1 || month.length > 2 || parseInt(month) < 1 || parseInt(month) > 12 ) return(3);
	if (year.length != 4 )  return(4);
	

	if (hasAlpha(day) || hasSymbol(day, " ") || hasSpaces(day) || 
		hasAlpha(month) || hasSymbol(month, " ") || hasSpaces(month) || 
		hasAlpha(year) || hasSymbol(year, " ") || hasSpaces(year)) {
		return(5);
	}

	// Assign date to a reference (date) object variable.
	objDateChosen.day   = parseInt(day);
	objDateChosen.month = parseInt(month - 1);
	
	if (dteToday.getYear() < 1900)
	  {
		year = parseInt(year) - 1900;
		year = year + "";
	  }
	objDateChosen.year  = parseInt(year);

	/* Loop through parameter array to determine which argument(s) have been requested
	   and act upon them */
	for (lngCount = (aParams.length - 1); lngCount > -1; lngCount--) {
		if (aParams[lngCount].substring(0, 6) == "nopast") {
			/* Check if value contains alphabetical characters. */
			/* Fragment parameter in order to get its value. */
			aSubElement = splitt(aParams[lngCount], ":");

			if (aSubElement.length == 1) {
				with (objDateChosen) {
					// Check that the date given is not before today.
					if (parseInt(year) < dteToday.getYear() ) {
						return(10);
					} else if (parseInt(year) == dteToday.getYear() ) {
						if (parseInt(month) < dteToday.getMonth()) {
							return(10);
						} else if (parseInt(month) == dteToday.getMonth()) {
							if (parseInt(day) < dteToday.getDate()) return(10);
						}
					}
				}
			} else if (aSubElement.length == 2) {
				if (right(aSubElement[1], 1) == "d") {
					// Set the max. limit, date into the past should not exeed.
					dteBackMax.setDate(dteToday.getDate() - parseInt(aSubElement[1]));

					// Re-adjust custom date object to reflect new date.
					objDateBackMax.day   = dteBackMax.getDate();
					objDateBackMax.month = dteBackMax.getMonth();
					objDateBackMax.year  = dteBackMax.getYear() - parseInt(aSubElement[1]);
				} else if (right(aSubElement[1], 1) == "m") {
					// Set the max. limit, date into the past should not exeed.
					dteBackMax.setMonth(dteToday.getMonth() - parseInt(aSubElement[1]));

					// Re-adjust custom date object to reflect new date.
					objDateBackMax.month = dteBackMax.getMonth();
					objDateBackMax.year = dteBackMax.getYear() - parseInt(aSubElement[1]);
				} else if (right(aSubElement[1], 1) == "y") {
					// Re-adjust custom date object to reflect new date.
					objDateBackMax.year = dteToday.getYear() - parseInt(aSubElement[1]);
				}

				with (objDateChosen) {
					// Check that the chosen date does not go beyond the range set.
					if (parseInt(year) < objDateBackMax.year) {
						return(10);
					} else if (parseInt(year) == objDateBackMax.year) {
						if (parseInt(month) < objDateBackMax.month) {
							return(10);
						} else if (parseInt(month) == objDateBackMax.month) {
							if (parseInt(day) < objDateBackMax.day) return(10);
						}
					}
				}
			}
		} else if (aParams[lngCount].substring(0, 8) == "nofuture") {
			/* Check if value contains alphabetical characters. */
			/* Fragment parameter in order to get its value. */
			aSubElement = splitt(aParams[lngCount], ":");

			if (aSubElement.length == 1) {
				with (objDateChosen) {
					// Check that the date given is not before today.
					if (parseInt(year) > dteToday.getYear() ) {
						return(11);
					} else if (parseInt(year) == dteToday.getYear() ) {
						if (parseInt(month) > dteToday.getMonth()) {
							return(11);
						} else if (parseInt(month) == dteToday.getMonth()) {
							if (parseInt(day) > dteToday.getDate()) return(11);
						}
					}
				}
			} else if (aSubElement.length == 2) {
				if (right(aSubElement[1], 1) == "d") {
					// Set the max. limit, date into the past should not exeed.
					dteFrwdMax.setDate(dteToday.getDate() + parseInt(aSubElement[1]));

					// Re-adjust custom date object to reflect new date.
					objDateFrwdMax.day   = dteFrwdMax.getDate();
					objDateFrwdMax.month = dteFrwdMax.getMonth();
					objDateFrwdMax.year  = dteFrwdMax.getYear();
				} else if (right(aSubElement[1], 1) == "m") {
					dteFrwdMax.setMonth(dteToday.getMonth() + parseInt(aSubElement[1]));

					// Re-adjust custom date object to reflect new date.
					objDateFrwdMax.month = dteFrwdMax.getMonth();
					objDateFrwdMax.year  = dteFrwdMax.getYear();
				} else if (right(aSubElement[1], 1) == "y") {
					objDateFrwdMax.year  = dteToday.getYear() + parseInt(aSubElement[1]);
				}

				with (objDateChosen) {
					// Check that the chosen date does not go beyond the range set.
					if (parseInt(year) > objDateFrwdMax.year) {
						return(11);
					} else if (parseInt(year) == objDateFrwdMax.year) {
						if (parseInt(month) > objDateFrwdMax.month) {
							return(11);
						} else if (parseInt(month) == objDateFrwdMax.month) {
							if (parseInt(day) > objDateFrwdMax.day) return(11);
						}
					}
				}
			}
		}
	}

	day   = parseInt(day);
	month = parseInt(month);
	year  = parseInt(year);

	// Check if year is a leap year.
	blnIsLeapYear = isleapYear(year);

	// Check that the date given is consistent with month.
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || 
		month == 10 || month == 12) {
		if ((day > 0 && day < 32) == false) return(6);
	} else if (month == 4 || month == 6 || month == 9 || month == 11) {
		if ((day > 0 && day < 31) == false) return(7);
	} else if (month == 2) {
		// If year is a leap year, check February has more than 29 days.
		if (blnIsLeapYear && day > 29) return(8);						
		if (!blnIsLeapYear && day > 28) return(9);
	}

	return(0);
}


function ShowErrorMsgForGrillDateA(errorCode, field)
{
	var errStr;
	errStr = field + ": ";

	switch (errorCode)
	{
		case 1:
		{
			errStr +=  "please fill in this field";	
			break;
		}
		case 2:
		{
			errStr += "the date is invalid, please input in the form of dd/mm/yyyy";
			break;
		}
		case 3:
		{	
			errStr += "the date is invalid, please input in the form of dd/mm/yyyy";
			break;
		}
		case 4:
		{
			errStr += "the date is invalid, please input in the form of dd/mm/yyyy";
			break;
		}
		case 5:
		{	
			errStr += "the day/month/year contain letters and/or symbols";
			break;
		}
		case 6:
		{
			errStr += "there are 1 - 31 days in specified month"
			break;
		}
		case 7:
		{
			errStr += "there are 1 - 30 days in specified month"
			break;
		}
		case 8:
		{
			errStr += "February has 29 days in this year"
			break;
		}
		case 9:
		{
			errStr += "February has 28 days in this year"
			break;
		}
		case 10:
		{
			errStr += "date must be later than this"
			break;
		}
		case 11:
		{
			errStr += "date must be earlier than this"
			break;
		}
	}
	
	alert(errStr);
	
}

/* -----------------------------------------------------------------------------------
	grillMonthYear()
		Parameters
			month		-	Integer, Returns the month number.
			year		-	Integer, Returns the year number.
			params		-	String [optional], Returns defined parameters on how 
							textbox is interrogated.
							Parameter options...
								required		-	Determines if the date entry must 
													be filled.
								nopast			-	dis-allows dates before today.
								nopast:[na]		-	Same as 'nopast', additional check
													to verify date is no less than
													(eg. 5m = "Date > (Now - 5 mths).
													Use month(m) or year(y) to 
													determine how far back to check.
								nofuture		-	dis-allows dates beyond today. 
								nofuture:[na]	-	Same as 'nofuture', additional check
													to verify date is no greater than
													(eg. 5m = "Date > (Now + 5 mths). 
													Use month(m) or year(y) to 
													determine how far forward to check.
		Returns
			0	-	parameters are OK and meets requirements.
			1	-	the date has not been filled.
			2	-	the month contains more digits than required.
			3	-	the year contains less or more digits than required.
			4	-	the month/year contain letters and/or symbols.
			5	-	the date is before today.
			6	-	the date is after today.
   ----------------------------------------------------------------------------------- */
function grillMonthYear(month, year, params) {
var aParams = new Array();
var aSubElement = new Array(), blnIsLeapYear, blnDateSet = true;
var dteFrwdMax = new Date(), dteBackMax = new Date(), dteToday = new Date();
var dateNow = dteToday.toString();
var lngCount;
var objDateBackMax = new Object(), objDateChosen = new Object(), objDateFrwdMax = new Object();

	// -- Define object properties ------------------------------------------------------
	objDateBackMax.day   = 1;
	objDateBackMax.month = dteToday.getMonth();
	objDateBackMax.year  = dteToday.getYear();

	objDateChosen.day   = 1;
	objDateChosen.month = dteToday.getMonth();
	objDateChosen.year  = dteToday.getYear();

	objDateFrwdMax.day   = 1;
	objDateFrwdMax.month = dteToday.getMonth();
	objDateFrwdMax.year  = dteToday.getYear();
	// ----------------------------------------------------------------------------------


	// If any, breakdown parameter string into an array.
	aParams = splitt(params, ";");

	if (month == 0) {
		month = "";
	} else {
		month = month + "";
	}
	year = year + "";

	// If found, remove leading zeros.
	if (month.substring(0, 1) == '0') month = month.substring(2, 1);

	/* Loop through parameter array to determine which argument(s) have been requested
	   and act upon them */
	for (lngCount = (aParams.length - 1); lngCount > -1; lngCount--) {
		if (aParams[lngCount] == "required") {
			// Check date format consistency.
			if (month.length == 0 && year.length == 0) {
				return(1);
			}
		}
	}

	if (hasAlpha(month) || hasSymbol(month, " ") || hasSpaces(month) || 
		hasAlpha(year) || hasSymbol(year, " ") || hasSpaces(year)) {
		return(4);
	}

	if (month.length < 1 || month.length > 2 || parseInt(month) < 1 || parseInt(month) > 31 ) return(2);
	if (year.length != 4) return(3);

	// Assign date of birth to a reference (date) object variable.
	objDateChosen.month = parseInt(month) - 1;
	objDateChosen.year  = parseInt(year);

	/* Loop through parameter array to determine which argument(s) have been requested
	   and act upon them */
	for (lngCount = (aParams.length - 1); lngCount > -1; lngCount--) {
		if (aParams[lngCount].substring(0, 6) == "nopast") {
			/* Check if value contains alphabetical characters. */
			/* Fragment parameter in order to get its value. */
			aSubElement = splitt(aParams[lngCount], ":");

			if (aSubElement.length == 1) {
				with (objDateChosen) {
					// Check that the date given is not before today.
					if (parseInt(year) < dteToday.getYear()) {
						return(5);
					} else if (parseInt(year) == dteToday.getYear()) {
						if (parseInt(month) < dteToday.getMonth()) return(5);
					}
				}
			} else if (aSubElement.length == 2) {
				if (right(aSubElement[1], 1) == "m") {
					// Set the max. limit, date into the past should not exeed.
					dteBackMax.setMonth(dteToday.getMonth() - parseInt(aSubElement[1]));

					// Re-adjust custom date object to reflect new date.
					objDateBackMax.month = dteBackMax.getMonth();
					objDateBackMax.year  = dteBackMax.getYear() - parseInt(aSubElement[1]);
				} else if (right(aSubElement[1], 1) == "y") {
					// Re-adjust custom date object to reflect new date.
					objDateBackMax.year = dteToday.getYear() - parseInt(aSubElement[1]);
				}

				with (objDateChosen) {
					// Check that the chosen date does not go beyond the range set.
					if (parseInt(year) < objDateBackMax.year) {
						return(5);
					} else if (parseInt(year) == objDateBackMax.year) {
						if (parseInt(month) < objDateBackMax.month) return(5);
					}
				}
			}
		} else if (aParams[lngCount].substring(0, 8) == "nofuture") {
			/* Check if value contains alphabetical characters. */
			/* Fragment parameter in order to get its value. */
			aSubElement = splitt(aParams[lngCount], ":");

			if (aSubElement.length == 1) {
				with (objDateChosen) {
					// Check that the date given is not before today.
					if (parseInt(year) > dteToday.getYear()) {
						return(6);
					} else if (parseInt(year) == dteToday.getYear()) {
						if (parseInt(month) > dteToday.getMonth()) return(6);
					}
				}
			} else if (aSubElement.length == 2) {
				if (right(aSubElement[1], 1) == "m") {
					// Set the max. limit, date into the past should not exeed.
					dteFrwdMax.setMonth(dteToday.getMonth() + parseInt(aSubElement[1]));

					// Re-adjust custom date object to reflect new date.
					dteFrwdMax.month = dteFrwdMax.getMonth();
					dteFrwdMax.year  = dteFrwdMax.getYear();
				} else if (right(aSubElement[1], 1) == "y") {
					dteFrwdMax.Year = dteToday.getYear();
				}

				with (objDateChosen) {
					// Check that the chosen date does not go beyond the range set.
					if (parseInt(year) > objDateFrwdMax.year) {
						return(6);
					} else if (parseInt(year) == objDateFrwdMax.year) {
						if (parseInt(month) > objDateFrwdMax.month) {
							return(6);
						}
					}
				}
			}
		}
	}

	return(0);
}

/* -----------------------------------------------------------------------------------
	grillCompareDates()
		Parameters
			strComp1	-	String, Returns the date applicant is currently resides at.
			strComp2	-	String, Returns the date applicant is previously lived.
 
		Returns
			0	-	dates check out ok and met requirements.
			1	-	inconsistency between given dates.
   ----------------------------------------------------------------------------------- */
function grillCompareDates(strDatePrev, strDateCurr) { 
var avntDate = new Array();
var dteToday = new Date();
var lngCount;
var objDateComp = new Array(1);
	objDateComp[0] = new Object();
	objDateComp[1] = new Object();

	// -- Define object properties ------------------------------------------------------
	objDateComp[0].day   = dteToday.getDate();
	objDateComp[0].month = dteToday.getMonth();
	objDateComp[0].year  = dteToday.getYear();

	objDateComp[1].day   = dteToday.getDate();
	objDateComp[1].month = dteToday.getMonth();
	objDateComp[1].year  = dteToday.getYear();
	// ----------------------------------------------------------------------------------

	for (lngCount = 0; lngCount < 2; lngCount++) {
		// Fragment date object into their individual elements.
		if (lngCount == 0) {
			avntDate[lngCount] = splitt(strDatePrev, "/");
		} else if (lngCount == 1) {
			avntDate[lngCount] = splitt(strDateCurr, "/");
		}

		if (avntDate[lngCount].length == 3) {
			objDateComp[lngCount].day = parseInt(avntDate[lngCount][0]);
			objDateComp[lngCount].month = parseInt(avntDate[lngCount][1]) - 1;
			objDateComp[lngCount].year = parseInt(avntDate[lngCount][2]);
		} else if (avntDate[lngCount].length == 2) {
			objDateComp[lngCount].day = 1;
			objDateComp[lngCount].month = parseInt(avntDate[lngCount][0]) - 1;
			objDateComp[lngCount].year = parseInt(avntDate[lngCount][1]);
		}
	}

	// Check that the chosen date does not go beyond the range set.
	if (objDateComp[0].year > objDateComp[1].year) {
		return(1);
	} else if (objDateComp[0].year == objDateComp[1].year) {
		if (objDateComp[0].month > objDateComp[1].month) {
			return(1);
		} else if (objDateComp[0].month == objDateComp[1].month) {
			if (objDateComp[0].day > objDateComp[1].day) return(1);
		}
	}
}

/* -----------------------------------------------------------------------------------
	grillMinAge()
		Parameters
			day			-	String, Returns the day number.
			month		-	String, Returns the month number.
			year		-	String, Returns the year number.
			age			-	Integer, Returns the minimum age required.
 
		Returns
			0	-	applicant 
			1	-	returns 1 if false.
   ----------------------------------------------------------------------------------- */
function grillMinAge(day, month, year, age) {
var aParams = new Array();
var aSubElement = new Array();
var dteBackMax = new Date(), dteToday = new Date();
var lngCount;
var objDateBackMax = new Object(), objDateChosen = new Object();

	// -- Define object properties ------------------------------------------------------
	objDateBackMax.day   = dteToday.getDate();
	objDateBackMax.month = dteToday.getMonth();
	objDateBackMax.year  = dteToday.getYear();

	objDateChosen.day   = dteToday.getDate();
	objDateChosen.month = dteToday.getMonth();
	objDateChosen.year  = dteToday.getYear();
	// ----------------------------------------------------------------------------------

	// Assign date of birth to a reference (date) object variable.
	objDateChosen.day = parseInt(day);
	objDateChosen.month = parseInt(month - 1);
	objDateChosen.year = parseInt(year);

	// Re-adjust custom date object to reflect new date.
	objDateBackMax.year = dteToday.getYear() - parseInt(age);

	with (objDateChosen) {
		// Check that the chosen date does not go beyond the range set.
		if (parseInt(year) > objDateBackMax.year) {
			return(1);
		} else if (parseInt(year) == objDateBackMax.year) {
			if (parseInt(month) > objDateBackMax.month) {
				return(1);
			} else if (parseInt(month) == objDateBackMax.month) {
				if (parseInt(day) > objDateBackMax.day) {
					return(1);
				} else {
					return(0);
				}
			} else {
				return(0);
			}
		} else {
			return(0);
		}
	}
}							

/////////////////////////////////////

function CheckForExcludedChars(text, fieldname)
{
	var re;
	var errmsg;
	
	re = /[<>¬£]/g;
	if (re.test(text))
	{
		errmsg = fieldname + ': the following characters are not allowed in this field: < > ¬ £'
		alert(errmsg);
		return(false);
	}

	return(true)

}

function CheckForApostrophe(text, fieldname)
{
	var errmsg;
	
	if (text.indexOf('"') >= 0)
	{
		errmsg = fieldname + ': the following characters are not allowed in this field: < > ¬ £'
		alert(errmsg);
		return(false);
	}

	return(true);
}
