// Note: This page will work only if either /include/validation/client/alertAndRefocus.js
// or /include/banner.asp has been included.

function isValidYear(strDate)
{
	var regex = /^(19|20)?[0-9][0-9]$/;
	
	if (!regex.test(strDate))
		return false;
	else
		return true;
}


function isValidMonthYear(strDate)
{
	var regex = /^(0?[1-9]|1[0-2])[.\/ -](19|20)?[0-9][0-9]$/;
	
	if (!regex.test(strDate))
		return false;
	else
		return true;
}

function isValidDate(strDate)
{
	var tempStrDate = strDate;
	
	var regex = /^(0?[1-9]|1[0-2])[.\/ -](0?[1-9]|[12][0-9]|3[0-1])[.\/ -](19|20)?[0-9][0-9]$/;
	var altregex = /^(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[0-1])(19|20)[0-9][0-9]$/;
	var mo, dt, yr;
	
	if (altregex.test(tempStrDate))
	{
		mo = parseInt(tempStrDate.substring(0, 2), 10) - 1;
		dt = parseInt(tempStrDate.substring(2, 4), 10);
		yr = parseInt(tempStrDate.substring(4, 8), 10);
		datDate = new Date(yr, mo, dt);
	}
	else
	{	
		if (!regex.test(tempStrDate))
			return false;

		tempStrDate = tempStrDate.replace(/\./g, "/").replace(/ /g, "/").replace(/-/g, "/");
		var arr = tempStrDate.split("/");
		if (arr.length < 3)
			return false;

		mo = parseInt(arr[0], 10) - 1; // The ", 10" specifies base ten, necessary to fix an obscure quasi-bug with parseInt where numbers with leading zeroes are pased as octal numbers(!) if no base is specified.
		dt = parseInt(arr[1], 10);
		yr = parseInt(arr[2], 10);
		if (yr < 100)
			if (yr < 69)
				yr = 2000 + yr;
			else
				year = 1900 + yr;
		var datDate = new Date(yr, mo, dt);

		if (isNaN(Date.parse(datDate)))
			return false;
	}
	
	if ((mo != datDate.getMonth()) || (dt != datDate.getDate())) // We don't check the year because this logic uses a different 1900/2000 cutoff date than Javascript's built in date functions.
		return false;
	else
		return true;
}

// Assumes that isValidDate(strDate) is true, and that date is either in MMDDYYYY format, or is delimited
// by spaces, dashes, or slashes.  Results are undefined otherwise.
function parse_freeform_datestring(strDate)
{
	var obj = new Object();
	var tempStrDate = strDate.replace(/\./g, "/").replace(/ /g, "/").replace(/-/g, "/");
	var today_yr = (new Date()).getFullYear();

	if (tempStrDate.indexOf("/") > -1)
	{
		var arr = (new String(tempStrDate)).split("/");
		obj.month = parseInt(arr[0], 10) - 1;
		obj.date = parseInt(arr[1], 10);
		obj.year = parseInt(arr[2], 10);
	}
	else
	{
		obj.month = parseInt(tempStrDate.substring(0, 2), 10) - 1;
		obj.date = parseInt(tempStrDate.substring(2, 4), 10);
		obj.year = parseInt(tempStrDate.substring(4, 8), 10);
	}
	
	if (obj.year < 100)
	{
		if (2000 + obj.year > today_yr)
			obj.year = 1900 + obj.year;
		else
			obj.year = 2000 + obj.year;
	}
	
	return obj;
}

function parse_todays_date()
{
	var today = new Date();
	var obj = new Object();
	obj.month = today.getMonth();
	obj.date = today.getDate();
	obj.year = today.getFullYear();

	// Account for leap year weirdness
	if ((obj.month == 2) && (obj.date == 29))
		obj.date = 28;

	return obj;
}

function get_X_years_ago(today, years_ago)
{
	return new Date(today.year-years_ago, today.month, today.date);
}

function isValidDOB(strDate)
{
	var tempStrDate;
	var MMDDYYregex = /^(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[0-1])[0-9][0-9]$/;
	
	if ((strDate == undefined) || (strDate == null))
		return false;
	
	tempStrDate = new String(strDate);
	
	if (tempStrDate.length == 0)
		return false;
	
	if (MMDDYYregex.test(tempStrDate))
		return false;
	
	if (!isValidDate(tempStrDate))
		return false;

	var dob = parse_freeform_datestring(strDate);
	var today = parse_todays_date();
	var theDate = new Date(dob.year, dob.month, dob.date);
	
	if ((theDate > get_X_years_ago(today, 11)) || (theDate < get_X_years_ago(today, 100)))
		return false;
	
	return true;
}

function checkForValidYear(this_field, errmsg)
{
	if (!isValidYear(this_field.value))
	{
		alertAndRefocus(this_field, errmsg.replace(/&quot;/ig, '"').replace(/''/ig, '"'));
		return false;
	}
	
	return true;
}

function checkForValidMonthYear(this_field, errmsg)
{
	if (!isValidMonthYear(this_field.value))
	{
		alertAndRefocus(this_field, errmsg.replace(/&quot;/ig, '"').replace(/''/ig, '"'));
		return false;
	}
	
	return true;
}

function checkForValidDate(this_field, errmsg)
{
	if (!isValidDate(this_field.value))
	{
		alertAndRefocus(this_field, errmsg.replace(/&quot;/ig, '"').replace(/''/ig, '"'));
		return false;
	}
	
	return true;
}

function checkForValidDOB(this_field, errmsg, warnmsg)
{
	if ((!isValidDate(this_field.value)) || (!isValidDOB(this_field.value)))
	{
		alertAndRefocus(this_field, errmsg.replace(/&quot;/ig, '"').replace(/''/ig, '"'));
		return false;
	}

	var dob = parse_freeform_datestring(this_field.value);
	var today = parse_todays_date();
	var theDate = new Date(dob.year, dob.month, dob.date);

	if (theDate > get_X_years_ago(today, 18))
	{
		if (warnmsg)
		{
			if (!confirm(warnmsg.replace(/&quot;/ig, '"').replace(/''/ig, '"')))
			{
				alertAndRefocus(this_field);
				return false;
			}
		}
	} 	
	
	return true;
}
