//<SCRIPT> Dummy script directive so that InterDev uses appropriate colour coding

function IsUKDate(sDateString)
{
/*==================================================================================
' Description:  Tests whether a given date is a valid UK date irrespective of 
'               machine locale settings
' Inputs:       sDateString : Date string to be tested
' Outputs:      returns true if the date is valid
' Assumptions:  None
'=================================================================================*/
	// First work out what separator the user has used (accept / \ - . and space)
	var sSeparator;
	if(sDateString.indexOf("/") > -1)
	{
		sSeparator = "/";
	}
	else
	{
		if(sDateString.indexOf("\\") > -1)
		{
			sSeparator = "\\";
		}
		else
		{
			if (sDateString.indexOf("-") > -1)
			{
				sSeparator = "-";
			}
			else
			{
				if (sDateString.indexOf(".") > -1)
				{
					sSeparator = ".";
				}
				else
				{
					if (sDateString.indexOf(" ") > -1)
					{
						sSeparator = " ";
					}
					else
					{
						// Invalid seperator return false
						return false;
					}
				}	
			}
		}
	}
	
	var sDateParts;
	sDateParts = sDateString.split(sSeparator);
	if (sDateParts.length != 3)
	{
		// Invalid number of date parts
		// return false
		return false;
	}
	else
	{
		if (isNaN(ParseInteger(sDateParts[0])))
		{
			// Invalid number for the day
			// return false
			return false;
		}
		else
		{
			if (isNaN(ParseInteger(sDateParts[1])))
			{
				// Invalid number for the month
				// return false
				return false;
			}
			else
			{
				if (isNaN(ParseInteger(sDateParts[2])))
				{
					// Invalid number for the year
					// return false
					return false;
				}
				else
				{
					// All the parts are valid numbers
					var nDay;
					var nMonth;
					var nYear;

					nDay   = ParseInteger(sDateParts[0]); 
					nMonth = ParseInteger(sDateParts[1]);
					nYear  = ParseInteger(sDateParts[2]);
						
					// Don't accept dates above 9999
					// if (nYear < 100 || nYear > 9999)
					if (nYear > 9999)
					{
						return false;
					}
					else
					{
						// Don't accept months outside 1 - 12
						if (nMonth < 1 || nMonth > 12)
						{
							return false;
						}
						else
						{
							// Don't accept days less than 1
							if (nDay < 1)
							{
								return false;
							}
							else
							{
								// Finally check that there are not too many days in the month
								                        //J, F, M, A, M, J, J, A, S, O, N, D
								var nDayArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
								if (IsLeapYear(nYear) && nMonth == 2)
								{
									if (nDay > 29)
									{
										return false;
									}
									else
									{
										return true;
									}
								}
								else
								{
									if (nDay > nDayArray[nMonth-1])
									{
										return false; 
									}
									else
									{
										return true;
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

function ParseUKDate(sDateString)
{
/*==================================================================================
' Description:  Parses a date string in UK format (dd/mm/yyyy) (various other 
'               separators are also accepted) and returns a date variant This works
'               irrespective of machine locale settings
' Inputs:       sDateString : Date string to be parsed
' Outputs:      returns the date
' Assumptions:  The date is a valid date string as tested by IsUKDate
'=================================================================================*/
	// Create a default invalid date to return if there is aproblem
	var dInvalidDate = new Date(1900, 1, 1);
	
	// Next work out what separator the user has used (accept / \ - . and space)
	var sSeparator;
	
	if(sDateString.indexOf("/") > -1)
	{
		sSeparator = "/";
	}
	else
	{
		if(sDateString.indexOf("\\") > -1)
		{
			sSeparator = "\\";
		}
		else
		{
			if (sDateString.indexOf("-") > -1)
			{
				sSeparator = "-";
			}
			else
			{
				if (sDateString.indexOf(".") > -1)
				{
					sSeparator = ".";
				}
				else
				{
					if (sDateString.indexOf(" ") > -1)
					{
						sSeparator = " ";
					}
					else
					{
						// Invalid seperator return a dummy date
						// this should not occur is the date has already
						// been tested to ensure it is acceptable
						return dInvalidDate;
					}
				}	
			}
		}
	}

	var sDateParts;
	try{
		// Assuming that the date has been tested (see assumptions) 
		// this should all run without error, so if any errors occur
		//  then just return the invalid date
		sDateParts = sDateString.split(sSeparator);
	
		var nDay;
		var nMonth;
		var nYear;
		
		nDay   = ParseInteger(sDateParts[0]); 
		nMonth = ParseInteger(sDateParts[1]);
		nYear  = ParseInteger(sDateParts[2]);
		
		//Allow the user to enter 2 digit year.  Assume < year 2000 when 2 digit.
		if(nYear < 100)
		{
			nYear = nYear + 2000;
		}	
		// return the parsed date
		var dValidDate = new Date(nYear, nMonth - 1, nDay);
		return dValidDate;
	}
	catch (err)
	{
		// There was an error so return the dummy date
		return dInvalidDate;
	}
}

function ParseInteger(sIntegerString)
/*==================================================================================
' Description:  Parses an interger by removing leadingzeros and then using parseInt.
'               If leading zeros are no removed then the number is treated as octal.
' Inputs:       sIntegerString : Number string to be parsed
' Outputs:      returns number
' Assumptions:  None
'=================================================================================*/
{
	// Tidyup the input
	var sTemp = Trim(sIntegerString);
	// Remove leading zeros
	while (sTemp.charAt(0) == '0' && sTemp.length > 1)
		sTemp = sTemp.substring(1, sTemp.length);
	// Now use parseInt;
	return parseInt(sTemp);	
}

function FormatUKDateTime(dDateTime)
/*==================================================================================
' Description:  Renders a date time variable in a UK date format irrespective of any
'               other factors such as the locale of the user currently logged in to
'               the server console.
' Inputs:       dDateTime : Date time to be rendered
' Outputs:      returns a date as a string in a UK format
' Assumptions:  dDateTime is a variant of type date. If a string is passed in the 
'               results may depend on the current system locale.
'=================================================================================*/
{
	var sDateString;
	var nDatePart;
	nDatePart   =  dDateTime.getDate();
	sDateString =  ((nDatePart < 10)?"0":"") + nDatePart.toString();
	sDateString += "/";
	nDatePart   =  dDateTime.getMonth() + 1;
	sDateString += ((nDatePart < 10)?"0":"") + nDatePart.toString();
	sDateString += "/";
	sDateString += dDateTime.getFullYear().toString();
	return sDateString;
}


function IsLeapYear(nYear)
/*==================================================================================
' Description:  Test whether a given year is a leap year.
' Inputs:       nYear: year to be tested
' Outputs:      returns true if the year is a leap year otherwise returns false
' Assumptions:  None
'=================================================================================*/
{
	if ((nYear % 400) == 0)
	{
		return true;
	}
	else
	{
		if ((nYear % 100) == 0)
		{
			return false;
		}
		else
		{
			if ((nYear % 4) == 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}

function Trim(sSource)
/*==================================================================================
' Description:  Trims whitespace from the begining and end of a string
' Inputs:       sSource: string to be trimmed
' Outputs:      returns the trimmed string
' Assumptions:  None
'=================================================================================*/
{
	var sTemp = "" + sSource + "";
	return sTemp.replace(/^\s*|\s*$/g, "");
}

function LTrim(sSource)
/*==================================================================================
' Description:  Trims whitespace from the begining of a string
' Inputs:       sSource: string to be trimmed
' Outputs:      returns the trimmed string
' Assumptions:  None
'=================================================================================*/
{
	var sTemp = "" + sSource + "";
	return sTemp.replace(/^\s*/g, "");
}

function RTrim(sSource)
/*==================================================================================
' Description:  Trims whitespace from the end of a string
' Inputs:       sSource: string to be trimmed
' Outputs:      returns the trimmed string
' Assumptions:  None
'=================================================================================*/
{
	var sTemp = "" + sSource + "";
	return sTemp.replace(/\s*$/g, "");
}

function IsLettersOnly(sSource)
/*==================================================================================
' Description:  Return whether
' Inputs:       sSource: string to be tested
' Outputs:      returns true if the string only contains letters a-z, otherwise it
'               returns false
' Assumptions:  None
'=================================================================================*/
{
	var reLetters = new RegExp("[^a-z]+");
	return !reLetters.test(sSource);
}

function replace_image(picture_name)
/*==================================================================================
' Description:  Displays picture in central table cell
' Inputs:       picture_name: string
' Outputs:      None
' Assumptions:  None
'=================================================================================*/
	{
	document.all.picture_cell.innerHTML="<img src='images/indexpage/"+picture_name+"' width='400' height='400'>";
	}

function reset_image()
/*==================================================================================
' Description:  Displays default picture in central table cell
' Inputs:       None
' Outputs:      None
' Assumptions:  None
'=================================================================================*/
	{
	document.all.picture_cell.innerHTML="<img src='images/indexpage/forest_400x400.jpg' width='400' height='400'>";
	}	
	
	
	