<!-- 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 field contains an URL
function Valid_URL(TheField)
	{
	var Test, IsValid = false;
	Util_WhiteSpaceTrim(TheField);
	Test = TheField.value.match(/([-\w]+\.)+\w+\S*/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))
		{
		TheDay = leftPad(TheDay, "0", 2);
		if (TheDay == "08")
			TheDay = 8;
		else if (TheDay == "09")
			TheDay = 9;
		else
			TheDay = parseInt(TheDay);
		TheMonth = leftPad(TheMonth , "0", 2);
		if (TheMonth == "08")
			TheMonth = 8;
		else if (TheMonth == "09")
			TheMonth = 9;
		else
			TheMonth = parseInt(TheMonth);
		TestDate = new Date(TheYear, TheMonth - 1, TheDay, 0, 0, 0);
		IsValid = ((TheDay == TestDate.getDate()) && (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 an image is valid
function Valid_Image(TheField, TheWidth, TheHeight)
	{
	var TestImage;
	var IsValid = true;
	if (TheField.value != "")
		{
		TestImage = new Image();
		TestImage.src = TheField.value;
		IsValid = (TestImage.width == TheWidth) && (TestImage.height == TheHeight);
		}
	return (IsValid);
	}

// Function to determine if an image is valid
function Valid_Checkbox(TheField, TheWidth, TheHeight)
	{
	var Count, IsUnchecked = true;
	var TheCheckList;
	TheCheckList = FormWishList.FormFieldWishItems;
	for (Count = 0; Count < TheCheckList.length & IsUnchecked; Count++)
		IsUnchecked = ! TheCheckList[Count].checked;
	return (!IsUnchecked);
	}

// 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) && (TheValidation != Valid_Checkbox))
				{
				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
var WindowValidation;
function Validate_Form(TheForm, FormValidation)
	{
	var Count, TheField, TheValidationName, TheValidation, IsValid;
	var TheErrorMessage = "", FirstFault = null;
	for (TheValidationName in FormValidation)
		{
		TheValidation = FormValidation[TheValidationName];
		TheField = TheForm.elements[TheValidationName];
		if (TheValidation && TheField)
			{
			if (TheValidation.required)
				{
				if (TheField.value)
					IsValid = (TheField.value.length != 0);
				else
					IsValid = TheValidation.verify(TheField, TheValidation.min, TheValidation.max);
				if (! IsValid)
					{
					TheErrorMessage += "The " + TheValidation.fieldname + " is required.;";
					if (FirstFault == null) FirstFault = TheField;
					}
				}
			else
				{
				IsValid = TheValidation.verify(TheField, TheValidation.min, TheValidation.max);
				if (! IsValid)
					{
					TheErrorMessage += "The " + TheValidation.fieldname + " is not valid.;";
					if (FirstFault == null) FirstFault = TheField;
					}
				}
			}
		}
// alert("TheErrorMessage =" + TheErrorMessage );
	if (TheErrorMessage != "")
		{
		var ThePopupLocation = "../Global/Popup/Error.html";
		var TheWindowParams = "toolbar=no,location=no,menubar=no,scrollbars=yes,status=no,statusbar=no,resizable=yes,title=no,titlebar=no,width=400,height=300";
		if (FirstFault.length)
			FirstFault[0].focus();
		else
			FirstFault.focus();
		ThePopupLocation += "?FormName=" + TheForm.name;
		TheForm.ValidationError.value = TheErrorMessage;
		if (WindowValidation && !WindowValidation.closed)
			WindowValidation.location = ThePopupLocation;
		else
			WindowValidation = window.open(ThePopupLocation,"ModalPopup",TheWindowParams);
//alert("WindowValidation.document.all.TheTitle=" + WindowValidation.document.all.TheTitle);
//alert("WindowValidation.document.all.TheTitle.innerHTML =" + WindowValidation.document.all.TheTitle.innerHTML);
//		WindowValidation.document.all.TheTitle.innerHTML = "Error";
//		WindowValidation.document.all.TheContent.innerHTML = TheErrorMessage;
		WindowValidation.focus();
		}

	// Retrun whether the validation was successful
	return(TheErrorMessage == "");
	}


// Rountine to check the validation of all fields in a form
function Validate_Setup(TheForm, FormValidation)
	{
	var Count, TheField, TheValidation, IsValid;
	var TheErrorMessage = "", FirstFault = null;
	for (Count = 0; Count < TheForm.elements.length; Count++)
		{
		TheField = FormDiaryList.elements[Count];
		TheValidation = FormValidation[TheField.name];
		if (TheValidation)
			TheField.onblur = "Validate_Field(" + TheField + ", " + FormValidation + ");";
		}
	}

// Rountine to limit the max length of a field
function Validate_TextLimit(TheField, TheDisplay, MaxLength)
	{
	if (TheField.value.length > MaxLength)
		TheField.value = TheField.value.substr(0, MaxLength);
	TheDisplay = document.all ? document.all[TheDisplay] : document.getElementById(TheDisplay);
	TheDisplay.innerText = MaxLength - TheField.value.length;
	}

// End hide -->

