﻿/* Date & time functions used by the Calendars*/

	function isValidDate(dateString)
	{
	var dateFormat = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;   //reg ex for date
	var fullDate;
	var year;
	var month;
	var day;
	var leapYearDays
	var match;
	var er=0;
	var erMsg="";
	var yearTest;
	
	fullDate=dateString.value;
	
	if (fullDate != "")  {             //big if statement holds the rest of the function		
		dateTestString=fullDate;
		
		//now test the date string versus the reg ex	
		if (!(dateFormat.test(dateTestString)))	{          //date is not in proper format 
			alert("Please enter dates using this format: m/d/yyyy.");
			return 1; 
			}			
		else {           //basic date format is ok. Check date parts next.
			//run a match against the date entered by the user to access the date parts
			match = dateTestString.match(dateFormat);
			//fill the date part vars with the resulting array values
			month	= match[1];
			day		= match[2];
			year		= match[3];
		
			// check that month is in range 
			if ((month<1) || (month>12))	{
				erMsg+= "Please enter a valid month (1-12)\n";
				er=1;
				}				
			//call the leapDays function to find out how many days February should have in the entered year
			leapYearDays = leapDays(year);			
			if ( (month==2) && ( (day<1) || (day>leapYearDays)) ){       //February, with check for leap year
				if (leapYearDays == 28) {
					erMsg += "February has 28 days in " + year + ".\n";
					}
				else {
					erMsg += "February has 29 days in " + year + ".\n";	
					}
				er=1;
				}			
			//test for months with 30 days
			if 	((month==4 || month==6 || month==9 || month==11) && (day>30) ) 	{
				erMsg+= getMonthName(month) + " has 30 days.\n";
				er=1;
				}				 
			//test for months with 31 days 
			if ((month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) && (day>31)){
				erMsg+= getMonthName(month) + " has 31 days.\n";
				er=1;
				}						
			//If we have any errors, pop up the alert, focus on the field, and block the submittal
			if (er==1)	{
				alert ("Please check the date(s) you entered.\n" + erMsg);
				//myForm.elements[con].focus();
				return 1;
				}	
			else return 0;	
		}	//end if/else		
	}	// end if !(fullDate="")
} 

//=============================

// returns max number of days in February depending on the year
function leapDays(argYear)	{
	var testYear = parseInt(argYear);

	//check the year for a leap year and return the correct # of days	
	if ( ((testYear%4 == 0) && !(testYear%100 == 0)) || (testYear%400 == 0)) 	{
		return 29;
		}
	else {return 28;}
	}		
	
//=============================		
	
function getMonthName(intMonth){
	var Months = new Array(12);
	
	Months[0]	= "January";
	Months[1]	= "February";
	Months[2]	= "March";
	Months[3]	= "April";
	Months[4]	= "May";
	Months[5]	= "June";
	Months[6]	= "July";
	Months[7]	= "August";
	Months[8]	= "September";
	Months[9]	= "October";
	Months[10]	= "November";
	Months[11]	= "December";
	
	index = intMonth-1;
	
	return Months[index];
	}
	
//=============================

function CheckDates()	{
		//make sure the date values are in valid ranges by calling isValidDate function for each date 
		var dateOne = document.frmSearch.rangeStartDate;
		var dateTwo  = document.frmSearch.rangeEndDate;
		var errOne;
		var errTwo;
		
		//make sure the From data is filled in	
		if (dateOne.value=="")
			{
			alert ("Please enter a value for the From date.");
			return false;
			}
	
		//check the dates for valid formats
		//isValidDate returns 1 if date is in bad format or out of range
				
		if (dateOne.value !="") 
		{errOne = isValidDate(dateOne);}
		
		if (dateTwo.value !="") 
			{errTwo = isValidDate(dateTwo);}
		
		if ((errOne == 1) || (errTwo==1))	{
			return false;  //bad date ranges, do not submit
			}
		else return true;
		}	

