/*------------------------------------------------------------------------------------
 Purpose	: Display a calendar for user to pick a date
 Parameters : dest_field:	the field to be populated once a date is chosen
			  arguments[1]: if exists, put focus on the dest_field
 Returns    : Nothing
------------------------------------------------------------------------------------*/
function Show_Calendar(dest_field, root_dir)
{
	var arr;
	var callerWin;			// caller window
	var modal_return;		// return value from the module dialog box


	if (Check_Date_Str(dest_field.value))
	{
		modal_return = window.showModalDialog("/" + root_dir + "/calendar/calendar.asp?chosen="+dest_field.value,dest_field,"dialogHeight:310px;dialogWidth:235px;center:1;status:0;help:0;scroll:0");
	
		//window.open("/" + root_dir + "/calendar/calendar.asp?chosen="+dest_field.value,"winCal","height=285,width=235,status=0,scrollbars=0");
	
		if (arguments[2])
		{
			if (modal_return != -1)	//the Cancel button was clicked
			{
				arr = arguments[2];
				callerWin = arr[0];
				callerWin.Submit_Form(callerWin.document.forms[0]);
			}
		}
	}
	else
	{
		dest_field.focus();
		dest_field.select();
		return false;
	}
}


/*----------------------------------------------------------------------
 Purpose	: Checks whether or not a string is a valid date
 Parameters	: sDate - the date string to check
 Returns	: true	- if string is a valid date (eg. 1/2/2002; 02/3/2002; 12/13/2002)
			  false - if string is not a valid date
----------------------------------------------------------------------*/
function Check_Date_Str(sDate)
{
	var date_parts;

	dateRegExp = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	if (sDate != "")
	{
		if (dateRegExp.test(sDate))
		{
			date_parts = sDate.split("/");
			
			if (Validate_Date_Vals(date_parts[0], date_parts[1], date_parts[2]))
			{
			
				if (date_parts[2] > 1900)
					return true;
				else
					return false;
			}
			else
			{
				return false;
			}
		}
		else
		{
			alert("Invalid date value.  Please re-enter.");
			return false;
		}
	}
	else
		return true;
}


/*----------------------------------------------------------------
 Purpose	: Validates a date string.
 Parameters : month_val - month val
			  day_val	- day val
			  year_val	- year val
 Returns	: true or false
----------------------------------------------------------------*/
function Validate_Date_Vals(month_val, day_val, year_val)
{
	var big_months = "'1','3','5','7','8','10','12'";
	var num_feb_days = 28;
	var num_year, num_month, num_day;
	
	num_year = Number(year_val);
	num_month = Number(month_val);
	num_day = Number(day_val);
	
	// VALIDATE MONTH
	if (num_month > 12 || num_month < 1)
	{
		alert("Invalid month value.  Please re-enter.");
		return false;
	}
	
	// VALIDATE DATE
	else if (big_months.indexOf("'" + Number(month_val) + "'") == -1)	// months with 30 days or less
	{
		if (num_month == 2)
		{
			// check for leap year
			if (year_val % 4 == 0)				// year evenly divisble by 4
				num_feb_days = 29;
			else if (year_val % 100 == 0 && year_val % 400 == 0)	// and  evenly divisible by 400
				num_feb_days = 29;		// LEAP YEAR
			
			if (day_val > num_feb_days)
			{
				alert("Februaray in year " + year_val + " has " + num_feb_days + " days only.  Please re-enter.");
				return false;
			}
			else
				return true;
		}
		else if (num_day > 30)
		{
			alert("Specified month has less than 31 days.  Please re-enter.");
			return false;
		}
		else if (num_day < 1)
			alert("Invaild date.  Please re-enter.");
		else
			return true;
	}
	else if (num_day > 31 || num_day < 1)
	{
		alert("Invalid date.  Please re-enter.");
		return false;
	}
	
	// VALIDATE YEAR (rough)
	else if (num_year < 1900)
	{
		alert("Invalid year value.  Please re-enter.");
		return false;
	}
	else
		return true;
}


