<!-- Hide

// Function to left pad a string with a character
function leftPad(TheString, TheFill, TheWidth)
	{
	var Count, ThePad = "";

	TheString = new String(TheString);
	for (Count = 0; Count < (TheWidth - TheString.length); Count++)
		ThePad += TheFill;
	TheString = ThePad + TheString;
	return(TheString);
	}

// Function to remove whitespace from a string
function Util_WhiteSpaceRemove(TheField)
	{
	TheField.value = TheField.value.replace(/\s+/g, '');
	return(TheField.value);
	}

// Function to trim whitespace from a string
function Util_WhiteSpaceTrim(TheField)
	{
	TheField.value = TheField.value.replace(/^\s+|\s+$/g, '');
	return(TheField.value);
	}

// Function to determine if a field contains chars
function Valid_Required(TheField, MinLength, MaxLength)
	{
	var IsValid = false;
	Util_WhiteSpaceTrim(TheField);
	IsValid = ((TheField.value.length >= MinLength) && (TheField.value.length <= MaxLength));
	return(IsValid);
	}

// Function to determine if a field contains a number
function Valid_Number(TheField)
	{
	var Test, IsValid = false;
	Util_WhiteSpaceRemove(TheField);
	IsValid = ! isNaN(TheField.value);
	return(IsValid);
	}

// Function to determine if a field contains a number
function Valid_Postcode(TheField)
	{
	var Test, IsValid = false;
	Util_WhiteSpaceRemove(TheField);
	Test = TheField.value.match(/^[234567(08)]\d{3}$/g);
	IsValid = (Test != null) && (Test.length);
	return(IsValid);
	}

// Function to determine if a field contains an Email
function Valid_Email(TheField)
	{
	var Test, IsValid = false;
	Util_WhiteSpaceTrim(TheField);
	Test = TheField.value.match(/\S+@([-\w]+\.)+\w+/g);
	IsValid = (Test != null) && (Test.length);
	return(IsValid);
	}

// Function to determine if a date is valid
function Valid_Date(TheField)
	{
	var TheDay, TheMonth, TheYear;
	var TestDate;
	var IsValid = false;
	TestDate = TheField.value.split("/");
	TheDay = TestDate[0];
	TheMonth = TestDate[1];
	TheYear = TestDate[2];
	if (!isNaN(TheDay) && !isNaN(TheMonth) && !isNaN(TheYear))
		{
		TestDate = new Date(TheYear, TheMonth - 1, TheDay, 0, 0, 0);
		IsValid = ((parseInt("1" + TheDay) - 100 == TestDate.getDate()) && (parseInt(TheMonth) == TestDate.getMonth() + 1) && (parseInt(TheYear) == TestDate.getFullYear()))
		if (IsValid)
			TheField.value = leftPad(TestDate.getDate(), "0", 2) + "/" + leftPad(TestDate.getMonth()+1, "0", 2) + "/" + TestDate.getFullYear();
		}
	return (IsValid);
	}

// Function to determine if a time is valid
function Valid_Time(TheField)
	{
	var TheHours, TheMinutes;
	var TestTime;
	var IsValid = false;
	TestTime = TheField.value.split(":");
	TheHours = TestTime[0];
	TheMinutes = TestTime[1];
	if (!isNaN(TheHours) && !isNaN(TheMinutes))
		{
		TheHours = parseInt(TheHours);
		TheMinutes = parseInt(TheMinutes);
		IsValid = ((TheHours >= 0) && (TheHours < 24) && (TheMinutes >= 0) && (TheMinutes < 60))
		if (IsValid)
			TheField.value = leftPad(TheHours, "0", 2) + ":" + leftPad(TheMinutes, "0", 2);
		}
	return (IsValid);
	}

// Function to determine if a date triplet is valid
function Valid_Date_triple(TheDay, TheMonth, TheYear)
	{
	var TestDate;
	var IsValid = false;
	if (!isNaN(TheDay) && !isNaN(TheMonth) && !isNaN(TheYear))
		{
		TestDate = new Date(TheYear, TheMonth, TheDay, 0, 0, 0);
		IsValid = ((parseInt(TheDay) == TestDate.getDate()) && (parseInt(TheMonth) == TestDate.getMonth()) && (parseInt(TheYear) == TestDate.getYear()))
		}
	return (IsValid);
	}

// Function to determine if a selection field has 
function Valid_Selection(TheField)
	{
	var Test, IsValid = false;
	Util_WhiteSpaceTrim(TheField);
	Test = TheField.value.match(/([-\w]+\.)+\w+\S*/g);
	IsValid = (Test != null) && (Test.length);
	return(IsValid);
	}

// Rountine to check the validation of a field in a form
function Validate_Field(TheField, FormValidation)
	{
	var TheValidation, IsValid;
	var TheErrorMessage = "", FirstFault = null;
	TheValidation = FormValidation[TheField.name];
	if (TheValidation)
		{
		if (TheField.value.length == 0)
			{
			if (TheValidation.required)
				{
				TheErrorMessage += "The " + TheValidation.fieldname + " is required.\n";
				if (FirstFault == null) FirstFault = TheField;
				}
			}
		else
			{
			IsValid = TheValidation.verify(TheField, TheValidation.min, TheValidation.max);
			if (! IsValid)
				{
				TheErrorMessage += "The " + TheValidation.fieldname + " is not valid.\n";
				if (FirstFault == null) FirstFault = TheField;
				}
			}
		}
	if (TheErrorMessage != "")
		{
		alert(TheErrorMessage);
		FirstFault.focus();
		}

	// Retrun whether the validation was successful
	return(TheErrorMessage == "");
	}

// Rountine to check the validation of all fields in a form
function Validate_Form(TheForm, FormValidation)
	{
	var Count, TheField, TheValidation, IsValid;
	var TheErrorMessage = "", FirstFault = null;
	for (Count = 0; Count < TheForm.elements.length; Count++)
		{
		TheField = TheForm.elements[Count];
		TheValidation = FormValidation[TheField.name];
		if (TheValidation)
			{
			if (TheField.value.length == 0)
				{
				if (TheValidation.required)
					{
					TheErrorMessage += "The " + TheValidation.fieldname + " is required.\n";
					if (FirstFault == null) FirstFault = TheField;
					}
				}
			else
				{
				IsValid = TheValidation.verify(TheField, TheValidation.min, TheValidation.max);
				if (! IsValid)
					{
					TheErrorMessage += "The " + TheValidation.fieldname + " is not valid.\n";
					if (FirstFault == null) FirstFault = TheField;
					}
				}
			}
		}
	if (TheErrorMessage != "")
		{
		alert(TheErrorMessage);
		}

	// Return whether the validation was successful
	return(TheErrorMessage == "");
	}

// End hide -->