<!-- 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 Valid_Dollar(TheField)
	{
	var Test, IsValid = false;
	Util_WhiteSpaceRemove(TheField);
	IsValid = ! isNaN(TheField.value);
	if (IsValid)
		TheField.value = formatDec(TheField.value, 2);
	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(/^[1234567(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))
		{
		TestDate = new Date(TheYear, TheMonth - 1, TheDay, 0, 0, 0);
		IsValid = ((parseInt(TheDay) == TestDate.getDate()) && (parseInt("1" + TheMonth) - 100 == 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;
		if (TheWidth && TheHeight)
			IsValid = (TestImage.width == TheWidth) && (TestImage.height == TheHeight);
		else
			IsValid = true;
		}
	return (IsValid);
	}

// Function to determine if a selection field has 
function Valid_Selection(TheField)
	{
	var IsValid = false;
	IsValid = (TheField.selectedIndex != 0);
	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
var WindowValidation;
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.;";
					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;
					}
				}
			}
		}
	if (TheErrorMessage != "")
		{
		var ThePopupLocation = "/Admin/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";
		TheWindowParams += ",left=" + ((screen.Width - 400) / 2)
			+ ",top=" + ((screen.Height - 300) / 2);
		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;
	}

// Routine to set the preview of an image
function onChangeImage(TheImageNew, TheImageNewPreview, TheWidth, TheHeight)
	{
	var TheImageNewPreview;
	if (TheImageNew.value != "")
		{
		var TheNow = new Date();
		TheImageNewPreview = document.all(TheImageNewPreview);
		TheImageNewPreview.src = TheImageNew.value;
		setTimeout(function(){_setImageSize(TheImageNewPreview, TheWidth, TheHeight, TheNow)}, 500);
		}
	}

// Routine to set the size of an image after a couple of seconds - allows he browser to settle the image
function _setImageSize(TheImageNewPreview, TheWidth, TheHeight, TheNow )
	{
	var NewNow = new Date();
	TheWidth.value = TheImageNewPreview.width;
	TheHeight.value = TheImageNewPreview.height;
	}

// 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 format number into a decimal string
function formatDec(Value, Dec)
	{
	var StrOut;

	StrOut = leftPad(Math.round(Value * Math.pow(10, Dec)), "0", Dec+1);
	StrOut = StrOut.substring(0, StrOut.length - Dec)
		+ (Dec == 0 ? "" : ".") + StrOut.substring(StrOut.length - Dec);
	return (StrOut);
	}


// End hide -->
