//These sets of functions, cummulating in the isDate() funtion checks the user input for a valid date format.  Then converts
//what the user entered into the mm/dd/yyyy format which should be highly reconizable by all, most importantly to Notes.
//To use this function simply add onBlur="isDate(this)" or onChange="isDate(this)" to you INPUT tag.  
//This function reconized a bunch of different date formats including mm/dd/yy, mm-dd.yy, dd.mm.yy, dd mmm yy and mmm dd, yy.
//You may also enter "tomorrow" and "yesterday" in the text field and the function returns the actual date.
//All of the year input may either be 2 digit or 4 digit.
//And yes it is Y2K compliant, at least I think it is...

function convertYear( inputYear )
{
//if a user enter two characters for the year, it assumes it is refering to the four digit year that is with in the 50 year mark.
//for example if the user enters 49 (and the year is 1999) it outputs 1949
//if the user enters 48 it outputs 2048
	var today = new Date()
	var thisYear = today.getFullYear()
	var index = thisYear - (thisYear % 100)
	if ( inputYear > 99 & inputYear < 1700 ) return 0	//user input an illegal year, returns 0 to indicate
	if (inputYear >= 1870 ) return inputYear
	var outputYear = index + inputYear			//first assume century is same as current
	var difference = outputYear - thisYear			//determine the difference
	if ( difference <= -50 ) outputYear = outputYear +100	
	if ( difference > 50 ) outputYear = outputYear - 100
	return outputYear
}

function leagalDay( inputMonth, inputDate, inputYear )
{
	var leapYear = 0
	if ( 0 == (inputYear % 4) ) leapYear = 1

	if ( inputDate < 0 | inputMonth < 0 | inputMonth > 12 ) return 0
	else if ( inputMonth == 1 & inputDate > 31 ) return 0
	else if ( inputMonth == 2 & inputDate > 28 + leapYear ) return 0
	else if ( inputMonth == 3 & inputDate > 31 ) return 0
	else if ( inputMonth == 4 & inputDate > 30 ) return 0
	else if ( inputMonth == 5 & inputDate > 31 ) return 0
	else if ( inputMonth == 6 & inputDate > 30 ) return 0
	else if ( inputMonth == 7 & inputDate > 31 ) return 0
	else if ( inputMonth == 8 & inputDate > 31 ) return 0
	else if ( inputMonth == 9 & inputDate > 30 ) return 0
	else if ( inputMonth == 10 & inputDate > 31 ) return 0
	else if ( inputMonth == 11 & inputDate > 30 ) return 0
	else if ( inputMonth == 12 & inputDate > 31 ) return 0
	else return inputDate
}

function isPositiveInteger(inputNumber)
{

	var newNum = parseInt(inputNumber, 10);

	if (isNaN(newNum) | newNum <= 0)
	{
		newNum = 0;
	}

	return newNum;
}

function convertMonth(month)
{
	if ( isPositiveInteger(month) > 0 ) 
	{
		return month
	}
	else 
	{
		var myMonth = month.toString()
		myMonth = myMonth.toUpperCase()
		if ( myMonth == "JAN" | myMonth == "JANUARY" ) return 1
		else if ( myMonth == "FEB" | myMonth == "FEBUARY" ) return 2
		else if ( myMonth == "MAR" | myMonth == "MARCH" ) return 3
		else if ( myMonth == "APR" | myMonth == "APRIL" ) return 4
		else if ( myMonth == "MAY" ) return 5
		else if ( myMonth == "JUN" | myMonth == "JUNE" ) return 6
		else if ( myMonth == "JUL" | myMonth == "JULY" ) return 7
		else if ( myMonth == "AUG" | myMonth == "AUGUST" ) return 8
		else if ( myMonth == "SEP" | myMonth == "SEPTEMBER" ) return 9
		else if ( myMonth == "OCT" | myMonth == "OCTOBER" ) return 10
		else if ( myMonth == "NOV" | myMonth == "NOVEMBER" ) return 11
		else if ( myMonth == "DEC" | myMonth == "DECEMBER" ) return 12
		else return 0
	}
}

function swap(dateArray)
{
	var temp = new String()
	temp = dateArray[0]
	dateArray[0] = dateArray[1]
	dateArray[1] = temp
	return dateArray
}

function isDate(dateField)
{
//returns string in mm/dd/yyyy format

	var inputDate = dateField.value
	var dateSplit = new Array(3)
	dateSplit[0] = 0
	dateSplit[1] = 0
	dateSplit[2] = 0
	var tempDate = new Date()

	//this if..else statement block accomplises two things.  First to split the user input up into the month, day and year
	//component as well as to make sure it is in the order month day year.
	if ( inputDate.indexOf("/") != -1 )
	{
		dateSplit = inputDate.split("/")
		if ( dateSplit[0] > 12 ) swap(dateSplit)
	}
	else if ( inputDate.indexOf(".") != -1 )
	{
		//if you use the '.' as the date seperator it is assumed you are using a dd.mm.yy format
		dateSplit = inputDate.split(".")
		swap(dateSplit)
	}
	else if ( inputDate.indexOf(" ") != -1 )
	{
		dateSplit = inputDate.split(" ")
		//if the user input is in the format mm dd, yy need to take out the comma
		var reqexp = /,/
		dateSplit[1] = dateSplit[1].replace(reqexp, "")
		if ( isPositiveInteger( dateSplit[1] ) == 0 ) swap(dateSplit)
	}
	else if ( inputDate.indexOf("-") != -1 )
	{
		dateSplit = inputDate.split("-")
		if ( dateSplit[0] > 12 ) swap(dateSplit)
	}

	dateSplit[0] = convertMonth(dateSplit[0]) 
	dateSplit[0] = isPositiveInteger( dateSplit[0] ) 
	dateSplit[1] = isPositiveInteger( dateSplit[1] ) 
	dateSplit[2] = isPositiveInteger( dateSplit[2] ) 
	dateSplit[1] = leagalDay( dateSplit[0], dateSplit[1], dateSplit[2] )
	dateSplit[2] = convertYear( dateSplit[2] )		//to make this function Y2K compliant
	if ( dateSplit[0] == 0 | dateSplit[1] == 0 | dateSplit[2] == 0 )
	{
		if ( inputDate.toUpperCase() == "YESTERDAY" )
		{
			tempDate.setTime( tempDate.getTime() -  86400000 )	//set temp date to yesterday
		}
		else if ( inputDate.toUpperCase() == "TOMORROW" )
		{
			tempDate.setTime( tempDate.getTime() +  86400000 )	//set temp date to yesterday	
		}
		else
		{
			tempDate.setTime( tempDate.getTime() )	//set temp date to today
		}
		dateSplit[0] = tempDate.getMonth() + 1
		dateSplit[1] = tempDate.getDate()
		dateSplit[2] = tempDate.getFullYear()
	}
	dateField.value = dateSplit[0] + "/" + dateSplit[1] + "/" + dateSplit[2]
	
}