/*
Date Validation & Format Prompt
Adapted by Ricq Pattay (May 2002)
from JavaScript Bible (Danny Goodman, 3rd Edition, Chapter 37)

Any date value from a text box can be passed to these routines.
Greyed-back date format prompt is mm/dd/yyyy.
Validation occurs when text box loses focus (onBlur event).
Tested in IE 5.5, 6.0, and Netscape 6.2
Validation works in NS 4.7 but date format prompt is black, not grey.

Validates for:
- Dates must be entered in one of the following formats:
	- mmddyy or mmddyyyy (requires leading zeroes in days/months)
    - mm-dd-yy, mm-dd-yyyy, mm/dd/yy or mm/dd/yyyy (does not require leading zeroes)
- Converts hyphen delimiters to slashes  
- Requires numeric values in entry, no characters
- Month value is 1 - 12
- Day value is 1 -31
  (Day value does not exceed available number of days in the specified month;
  allows for February in leap years).
- Year value can be entered as 2 or 4 digits
  (If 2 digit year is entered and value is >= 30, value is converted to 20th-century;
  otherwise value is converted to 21st-century. 2-digit years are expanded to 4 digits.)
- Adds leading zero to single-digit days and months.

Text box should be created as:
<input type="text" name="your_field_name"  
	onFocus="ClearDatePrompt(this)" 
	onBlur="isDate(this)" 
	value="mm/dd/yyyy" style="{color='Silver'}">
	
If text box may have existing data populated by ColdFusion, the text box creation should be modified as:
<input type="text" name="your_field_name"  
	onFocus="ClearDatePrompt(this)" 
	onBlur="isDate(this)" 
	<cfif #your_query.your_date_field# is "">
		value="mm/dd/yyyy" style="{color='Silver'}"
	<cfelse>
		value="<cfoutput>#dateformat(your_query.your_date_field,"mm/dd/yyyy")#</cfoutput>"
	</cfif>
>
	
*/


// clear format prompt from textbox
function ClearDatePrompt(field) {
if (field.value == 'mm/dd/yyyy') {
	field.value = '';
	field.style.color = 'Black'}
}

// extract front part of string prior to searchString 
function getFront(mainStr,searchStr){ 
	foundOffset = mainStr.indexOf(searchStr) 
	if (foundOffset == -1) { 
		return null 
	} 
	return mainStr.substring(0,foundOffset) 
} 

// extract back end of string after searchString 
function getEnd(mainStr,searchStr) { 
	foundOffset = mainStr.indexOf(searchStr) 
	if (foundOffset == -1) { 
		return null 
	} 
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length) 
} 

// replace searchString with replaceString
function replaceString(mainStr,searchStr,replaceStr) {
var front = getFront(mainStr,searchStr)
var end = getEnd(mainStr,searchStr)
if (front != null && end != null) {
	return front + replaceStr + end
	}
	return null
}

// check the entered month for too high a value function 
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) { 
alert(months[mm] + " has only 30 days.") 
return false } 
else if (dd > 31) { alert(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) { alert("February of " + yyyy + " has only 28 days.") 
return false } 
else if (dd > 29) { alert("February of " + yyyy + " has only 29 days.") 
return false } 
return true } 


// date field main validation function
function isDate(field) { 

var inputStr = field.value

if (inputStr.length < 1) {
	field.value = 'mm/dd/yyyy';
	field.style.color = 'Silver';
	return false
}
	
// convert hyphen delimiters to slashes 
while (inputStr.indexOf("-") != -1) { 
	inputStr = replaceString(inputStr,"-","/") } 
	
var delim1 = inputStr.indexOf("/") 
var delim2 = inputStr.lastIndexOf("/") 

if (delim1 != -1 && delim1 == delim2) { 
// there is only one delimiter in the string 
alert("Please enter dates in the format mm/dd/yyyy.") 
field.focus() 
//field.select() 
field.value = ''
return false } 

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) 
} 
else { // there are no delimiters; extract component values 
var mm = parseInt(inputStr.substring(0,2),10) 
var dd = parseInt(inputStr.substring(2,4),10) 
var yyyy = parseInt(inputStr.substring(4,inputStr.length),10) 
} 

if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) { 
// there is a non-numeric character in one of the component values 
alert("Please enter dates in the numeric format mm/dd/yyyy.") 
field.focus() 
//field.select() 
field.value = ''
return false } 

if (mm < 1 || mm > 12) { 
// month value is not 1 thru 12 
alert("Please enter dates in the format mm/dd/yyyy.\n\nMonths must be entered between the range of 01 (January) and 12 (December).") 
field.focus() 
//field.select() 
field.value = ''
return false } 

if (dd < 1 || dd > 31) { 
// date value is not 1 thru 31 
alert("Please enter dates in the format mm/dd/yyyy.\n\nDays must be entered between the range of 01 and a maximum of 31 (depending on the month and year).") 
field.focus() 
//field.select() 
field.value = ''
return false } 

// validate year
if (yyyy > 99 && yyyy < 1000) {
alert("Please enter dates in the format mm/dd/yyyy.\n\nYears must be entered as two or four digits.") 
field.focus() 
//field.select() 
field.value = ''
return false } 

if (yyyy < 100) { // entered value is two digits, which we allow for 1930-2029 
	if (yyyy >= 30) { 
	yyyy += 1900 } 
else { yyyy += 2000 } 
} 

if (mm == 2) { 
	if (!checkLeapMonth(mm,dd,yyyy)) { 
	field.focus() 
	//field.select() 
	field.value = ''
	return false } 
} 

if (!checkMonthLength(mm,dd)) {
	field.focus()
	//field.select() 
	field.value = ''
	return false }

// add leading 0 to single-digit months
if (mm < 10) {
	mm = "0" + mm;
}
// add leading 0 to single-digit days
if (dd < 10) {
	dd = "0" + dd
}

field.value = mm + "/" + dd + "/" + yyyy
return true 
} 


