// date field validation (called by other validation functions)
function Val_Date(gField) {
	var inputStr = gField.value
	// convert hyphen delimiters to slashes
	while (inputStr.indexOf("-") != -1) {
		inputStr = inputStr.replace("-","/")
	}
	// convert period delimiters to slashes and update the form field
	while (inputStr.indexOf(".") != -1) {
		inputStr = inputStr.replace(".","/")
		gField.value = inputStr
	}
	var delim1 = inputStr.indexOf("/")
	var delim2 = inputStr.lastIndexOf("/")
	if (delim1 != -1) {
		// there are delimiters; extract component values
		var mm = parseInt(inputStr.substring(0,delim1),10)
		var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10)
		var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10)
	}
	if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
		// there is a non-numeric character in one of the component values
		err_msg="The date entry is not in an acceptable format.\n   You can enter dates in the following formats: mm.dd.yyyy, mm/dd/yyyy, or mm-dd-yyyy."
		gField.focus()
		gField.select()
		return false
	}
	if (mm < 1 || mm > 12) {
		// month value is not 1 thru 12
		err_msg="Date entry error.\n   Months must be entered between the range of 01 (January) and 12 (December)."
		gField.focus()
		gField.select()
		return false
	}
	if (dd < 1 || dd > 31) {
		// date value is not 1 thru 31
		err_msg="Date entry error.\n   Days must be entered between the range of 01 and a maximum of 31 (depending on the month and year)."
		gField.focus()
		gField.select()
		return false
	}

	// validate year, allowing for checks between year ranges
	// passed as parameters from other validation functions
	if (yyyy < 100) {
		// entered value is two digits, which we allow for 1950-2049
		if (yyyy >= 50) {
			yyyy += 1900
		} else {
			yyyy += 2000
		}
	}
	if (!checkMonthLength(mm,dd)) {
		gField.focus()
		gField.select()
		return false
	}
	if (mm == 2) {
		if (!checkLeapMonth(mm,dd,yyyy)) {
			gField.focus()
			gField.select()
			return false
		}
	}
	return true
}

// check the entered month for too high a value
function checkMonthLength(mm,dd) {
	var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December")
	if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30) {
		err_msg="Date entry error.\n   "+months[mm]+" has only 30 days."
		return false
	} else if (dd > 31) {
		err_msg="Date entry error.\n   "+months[mm]+" has only 31 days."
		return false
	}
	return true
}

// check the entered February date for too high a value 
function checkLeapMonth(mm,dd,yyyy) {
	if (yyyy % 4 > 0 && dd > 28) {
		err_msg="Date entry error.\n   February of " + yyyy + " has only 28 days."
		return false
	} else if (dd > 29) {
		err_msg="Date entry error.\n   February of " + yyyy + " has only 29 days."
		return false
	}
	return true
}
