//This function formats date and validates month/day/year at field level
function formatDateMmDdYyyytxt( obj )
{
	var str_input = obj.value;
	var chrs = "n";
	var str_msg1 = "";

	if ( str_input.length > 10 ){
	alert ("Date is wrong format.\n\nPlease re-enter in the format MM/DD/YYYY.");
	obj.focus();
	return false;
	}

	for (var i = 0; i < str_input.length; i++)
	{
		var ch = str_input.substring(i, i + 1);
		if (!((ch >= 0 && ch <= 9) || (ch == '/') || (ch == '-')))
		{
			alert("\nThe date field only accepts numbers.\n\nPlease re-enter in the format MM/DD/YYYY.");
			obj.focus();
			i = str_input.length
			chrs = "y" ;
			return false;
		}
	}
	if (chrs == "n")
	{
		//isDatetxt() is being called by this function to make use of
		//alerts available in the function.If user enters invalid
		//month/day/year,this function alert the user.If user ignores
		//this alert,this date field value will becomes spaces when
		//user finally submits form.

		str_input=isDate(str_input);
		obj.value = str_input;
		str_msg = isDateMmDdYyyy(str_input);

	if (str_msg == "F")
		{	//If date is space, isDateMmDdYyyy return a series of NaN/NaN/NaN
		if ( isNaN((obj.value).charAt(0)) )
			str_input = "";
			if ( obj.value != "" ){
				obj.value = str_input;
				alert ("Date is wrong format.\n\nPlease re-enter in the format MM/DD/YYYY..");
				return false;
			}
		}
		if (!((str_msg == 'Success') || (str_msg == "F")))
		{
			alert(str_msg);
			obj.focus();
			return false;
		}
	}
	return true;
}

/**
 * isDate	Validate if the date entered is valid or not
 *		Display appropriate error message for each situation
 *
 * @param name		String Object name ie. "name"
 *
 * @return			boolean True if date is valid/False otherwise
 *
 * @see			 replaceString
 */

function isDate(str)
{
	// var frm = self.document.frmDemographic;
	var val = str;
	var str_msg = "";
	if ( str.length < 6 ) return str;
	//replace hyphen delimiters to slashes
	if (val.indexOf("-") != -1)
	val = replaceString(val);
	var delim1 = val.indexOf("/");
	var delim2 = val.lastIndexOf("/");
	if (delim1 != -1 && delim1 == delim2) return "";
	if (delim1 != -1){	//There are delimiters: extract component values
		var mm = parseInt(val.substring(0,delim1),10);
		var dd = parseInt(val.substring(delim1 + 1, delim2),10);
		var yyyy = parseInt(val.substring(delim2 + 1, val.length),10);
	} else {
		//There are no delimters: extract component values
		var mm = parseInt(val.substring(0,2),10);
		var dd = parseInt(val.substring(2,4),10);
		var yyyy = parseInt(val.substring(4, val.length),10);
	}

	if (isNaN(mm) || isNaN(dd) || isNaN(yyyy))	return "";

	//Validate year, allowing for checks between year ranges
	//passes as parameters from other validateion functions
	if (yyyy < 1850) return str;	//Entered value is two digits, which we allow for 1950-2049

	//Make sure year is greater than 1761, SYBASE can't handle date < 1917
	if (yyyy < 1870) alert('Invalid Year');

	//If go this far, we have a formated valid date
	//	- Reset the date in the object field.
	//Convert integer to character for 2 digits date/month
	if (mm < 10) mm = "0" + mm;
	if (dd < 10) dd = "0" + dd;
	return (mm + "/" + dd + "/" + yyyy);
}

function replaceString(val)
{
	var res = ""
	for (i=0; i<val.length; i++)
	{
		var aChar = val.charAt(i);
		if ( aChar == '-' || aChar == '.') res += "/"; else res += aChar;
	}
	return res;
}

function isDateMmDdYyyy(str_input)
{
	int_month = parseInt(parseFloat(str_input.substring(0,2)));
	int_day = parseInt(parseFloat(str_input.substring(3,5)));
	int_year = parseInt(parseFloat(str_input.substring(6,10)));
	if(!isInteger(str_input.substring(0,2)) || !isInteger(str_input.substring(3,5)) || !isInteger(str_input.substring(6,10)))
		return "F"
	str_msg = 'Success';	//If no problems are found, Success will be returned to calling function

	str_separator_1 = str_input.substring(2,3)
	str_separator_2 = str_input.substring(5,6)

	if (str_input == "") return "Success";	//blank fields are not fatal errors

	if (str_input.length != 10)
	{
		str_msg = 'Invalid date. Must have 4 digit year.\n\nPlease re-enter in the format MM/DD/YYYY.'
		return str_msg; //Can't be MM/DD/YYYY unless 10 digits

	}

	if (str_separator_1 != "/" || str_separator_2 != "/")
	{
		str_msg = 'Invalid date format, user / or - to separate month, day, and year.'
		return str_msg; //Wrong kind of separator

	}

	if (!(int_month > 0 && int_month < 13))
	{
		str_msg = 'Invalid Month';
		return str_msg; //Not a valid month

	}
	if (!(int_year >= 1900 && int_year < 2079) || isNaN(int_year))
	{
		str_msg = 'Invalid Year';
		return str_msg; //Not a valid year

	}

	//If day less than zero or greater than 31 don't mess with checking for specific month
	if (!(int_day > 0 && int_day < 32) || isNaN(int_day))
	{
		str_msg = 'Invalid Day';
		return str_msg; //Not a valid day

	}

	//Check valid day for respective month including leap year case
	if (validateDay(int_day, int_month, int_year) == false)
	{
		str_msg = 'Invalid Month';
		return str_msg; //Not a valid day

	}

	//Valid Date
	return str_msg; //Sucess

}

function validateDay(str_day, int_month, str_year)
{
	//Array for valid month lengths (i.e Jan is month 1 and has 31 days so the index 1
	//of int_last_day_array array is 31) index[0] of array holds the leap year value
	int_last_day_array = new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	bln_leap_year = false;

	//check for leap year.	Any year divisible by 4 unless divisible by 100, and not divisible by 400
	if (str_year % 4 == 0)
	{
		bln_leap_year = true;
		if (str_year % 100 == 0)
		{
			bln_leap_year = false;
			if (str_year % 400 == 0) bln_leap_year = true;
		}
	}

	//If leap year is true, change int_last_day_array so int_last_day_array[2] = 29
	if (bln_leap_year == true) int_last_day_array[2] = 29;

	//Check for max Day of given month
	if (str_day > int_last_day_array[int_month]) return false;

	return true;	//Day is valid.
}

function isInteger(int_input)
{
	int_flag = false; //set to 1 for number, leave 0 if not
	if(int_input.length > 0) {
		for(x = 0;x < int_input.length; x++)
		{
			chr_value = int_input.charAt(x);
			for(y = 0; y < 10; y++)	//Loop through all digits 0 - 9
				if(chr_value == (y + '')) int_flag = true;	//if this character is a digit, set the flag to 1
			if(int_flag == false) return false;	//if the character was not a number, the flag will be 0.
			int_flag = false;	//reset for next digit
		}
		return true;	//valid int
	}
	else
		return true;
}

function isNum(theString) {
	flag = true;
	for (i = 0; i < theString.length; i++) {
		if ((theString.substring(i, i+1) != '0') &&
		(theString.substring(i, i+1) != '1') &&
		(theString.substring(i, i+1) != '2') &&
		(theString.substring(i, i+1) != '3') &&
		(theString.substring(i, i+1) != '4') &&
		(theString.substring(i, i+1) != '5') &&
		(theString.substring(i, i+1) != '6') &&
		(theString.substring(i, i+1) != '7') &&
		(theString.substring(i, i+1) != '8') &&
		(theString.substring(i, i+1) != '9'))
		{
			flag = false;
			break;
		}
	}
	return(flag);
}