//******************************************************************
// Validate a Date
//******************************************************************
function valdate(date)
{
	if (date.value <= "          ")
	        return;
	var date_s = new String(date.value)
	var date_pattern = /^(\d{1,2})[\/-](\d{1,2})[\/-](\d{1,4})$/
//
// check the date for valid values and format, and unpack day, month
// and year into separate array items
	var date_match = date_s.match(date_pattern);
	if (date_match == null)
	{
		alert("Invalid Date Format\nMust be dd-mm-yyyy");
		date.focus();	//set focus to field
		date.select();	//highlight selected field
		return;
	}
//
// format and validate day and month
	if (date_match[1].length == 1)
		date_match[1] = 0 + date_match[1];
	if (date_match[2].length == 1)
		date_match[2] = 0 + date_match[2];
	if (date_match[1] < "01" || date_match[1] > "31")
	{
		alert("This date does not have a valid day")
		date.focus();	//set focus to field
		date.select();	//highlight selected field
		return
	}

// format the month
	if (date_match[2] < "01" || date_match[2] > "12")
	{
		alert("This date does not have a valid month")
		date.focus();	//set focus to field
		date.select();	//highlight selected field
		return
	}

// format the year
	if (/^0+$/.exec(date_match[3]))
		date_match[3] = 2000
	else if (date_match[3].length == 1)
		date_match[3] = 200 + date_match[3]
	else if (date_match[3].length == 2)
	{
		if (date_match[3] <= 30)
			date_match[3] = 20 + date_match[3]
		else
			date_match[3] = 19 + date_match[3]
	}
//
// validate the year
	if (date_match[3].length == 3 || (date_match[3].length == 4 && date_match[3] < "1900"))
	{
		alert("This date does not have a valid year")
		date.focus();	//set focus to field
		date.select();	//highlight selected field
		return
	}
//
// validate days in month
	var months = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31)
	if (date_match[2] == "02" && (date_match[3]%4) == 0)
	months[2] = 29
	if (date_match[2].charAt(0) == "0")
		month = date_match[2].charAt(1)
	else
		month = date_match[2]
	if (date_match[1] > months[month])
	{
		alert("This day and month are not valid")
		date.focus();	//set focus to field
		date.select();	//highlight selected field
		return
	}
//
// pass back reformatted date
	date.value = date_match[1] + "-" + date_match[2] + "-" + date_match[3]
	return
}
