//********************************************************************************************************
// Validate text items
//********************************************************************************************************
function ValidateTextItem( item, text )
{
	if (typeof item != "undefined" )
	{
		if ( isBlank( item.value ))
		{
			if ( text != "")
			{
			  item.focus()
			  alert("Please enter " + text + ".")
			  return 0;
			}
		}
	}
	return 1;
}
//***************
// Validate text item length
//***************
function ValidateTextMaxLength( item, maxLength, text)
{
  if ( item.value.length > maxLength)
  {
    if ( text != "")
    {
      alert("Please enter " + text + " of less than " + maxLength + " characters.");
    }
    item.focus();
    return 0;
  }
  return 1;
}
//***************
// Validate text item length
//***************
function ValidateTextMinLength( item, minLength, text)
{
  if ( item.value.length < minLength)
  {
    if ( text != "")
    {
      alert("Please enter " + text + " of more than " + minLength + " characters.");
    }
    item.focus();
    return 0;
  }
  return 1;
}
//********************************************************************************************************
// Validate file items
//********************************************************************************************************
function ValidateFileItem( item )
{
	var sValue = item.value.toLowerCase();
	if ( ! isBlank( sValue ))
	{
		if ( sValue.indexOf( ".gif" ) == -1 )
		{
			if ( sValue.indexOf( ".jpg" ) == -1 )
			{
				item.focus()
				alert("The only permissible file types are .gif & .jpg");
				return 0;
			}
		}
	}
	return 1;
}
//********************************************************************************************************
// Returns the field with given name, or null if the field is not found
//********************************************************************************************************
function getField( fieldName)
{
	var field = document.forms[0][ fieldName];

	// If field not found, return null else return field
	return ( typeof field == "undefined") ? null : field;
}

//********************************************************************************************************
// Function isEmpty( item) Returns true if the passed item
// is empty.
//********************************************************************************************************
function isEmpty( item )
{
    if (( item == null ) || ( item.length == 0 ))
		return true;
    else
    		return false;
}

//********************************************************************************************************
// Function isBlank( item) Returns true if the passed item
// is empty, or only contains whitespace characters.
//********************************************************************************************************
function isBlank( item )
{
	var iIndex;
	var whitespaceCharacters = " \t\n\r";

    // Check for empty items.
    if ( isEmpty( item )) return true;

    // Search through the string array element by element.
    // If any non-whitespace characters are found return false, otherwise return true.
    for ( iIndex = 0; iIndex < item.length; iIndex++ )
    {
	    // Check that current character isn't whitespace.
     	var cCharacter = item.charAt( iIndex );
		if ( whitespaceCharacters.indexOf( cCharacter ) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}
//********************************************************************************************************
// Function isNumber( item) Returns false if the passed item
// is empty, or contains non-numeric characters other than a leading + or - and a single decimal point.
//********************************************************************************************************
function isNumber( item )
{
	var iIndex;
	var numericCharacters = "0123456789";
	var signCharacters = "+-";
	var decimalPoint = ".";
	var decimalPointFound = false;
	var numericFound = false;

    // Check for empty items.
    if ( isEmpty( item )) return false;

    // Search through the string array element by element.
    // Allow a sign in the first element, allow a single decimal point
    for ( iIndex = 0; iIndex < item.length; iIndex++ )
    {
         var cCharacter = item.charAt( iIndex );

		// Check the leading character for numerics or a sign
		if ( iIndex == 0 )
		{
			if ( numericCharacters.indexOf( cCharacter ) == -1 )
			{
				if ( signCharacters.indexOf( cCharacter ) == -1 )
				{
					if ( cCharacter == decimalPoint )
						decimalPointFound = true;
					else
						return false;
				}
			}
			else
				numericFound = true;
		}

		// Check subsequent characters for numerics, or one decimal point
		else
		{
			if ( numericCharacters.indexOf( cCharacter ) == -1)
			{
				if ( decimalPointFound )
					return false;
				else
				{
					if ( cCharacter == decimalPoint )
						decimalPointFound = true;
					else
						return false;
				}
			}
			else
				numericFound = true;
		}
    }

    // All characters are allowable in a numeric field.
    if ( numericFound )
	    return true;
	else
		return false;
}

//********************************************************************************************************
// Function isUnsignedInteger( item) Returns false if the passed item
// is empty, or contains non-numeric characters.
//********************************************************************************************************
function isUnsignedInteger( item )
{
	var iIndex;
	var numericCharacters = "0123456789";

    // Check for empty items.
    if ( isEmpty( item )) return false;

    // Search through the string array element by element.
    for ( iIndex = 0; iIndex < item.length; iIndex++ )
    {
         var cCharacter = item.charAt( iIndex );

		if ( numericCharacters.indexOf( cCharacter ) == -1)
			return false;
    }

    // All characters are numeric.
    return true;
}

//********************************************************************************************************
// Function isDate( Year,Month, Day) Returns false if the passed items
// do not constitue a valid date. Only 4-digit years are considered to be valid, therefore
// years before the year 1000 must be padded with leading zero's.
//********************************************************************************************************
function isDate( Year, Month, Day )
{
	// Check that the parameters passed are unsigned integers
	if ( !( isUnsignedInteger( Year ) && isUnsignedInteger( Month ) && isUnsignedInteger( Day )))
		return false;

	var iYear = parseInt( Year )
	var iMonth = parseInt( Month )
	var iDay = parseInt( Day )

	// Validate the year
	if ( Year.length != 4 )
		return false;

	// Validate the month
	if ( iMonth < 1 ||  iMonth > 12)
		return false;

	// Validate the day
	switch( iMonth )
	{
		// Validate 31 day months
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if ( iDay < 1 ||  iDay > 31)
				return false;
			break;

		// Validate 30 day months
		case 4:
		case 6:
		case 9:
		case 11:
			if ( iDay < 1 ||  iDay > 30)
				return false;
			break;

		// Validate February
		case 2:

			// Determine the number of days in February (leap years are where the year is divisible by 4,
			// and not divisible by 100, unless also divisible by 400).
			var iDaysInFebruary = (( Year % 4 == 0 ) && (( !( Year % 100 == 0 )) || ( Year % 400 == 0 ))) ? 29 : 28;

			if ( iDay < 1 ||  iDay > iDaysInFebruary )
				return false;
			break;
	}

	return true;
}