/***********************************************************************************
	AUTHOR: JASON SHEPARD <jshepard@fullsail.com>
	CREATED: Friday, July 19, 2002
	PURPOSE: This script is designed to utilize 3 selection objects to build
				   a proper date, and check those dates for anything out of range,
					 ie: only 28 days in Feb, 30 days in Apr, etc.  This script also 
					 accounts for leap years. The only variables needed for this 
					 script to operate are the name of the form that is being checked, 
					 and the prefix of the date selection objects.  The requirements
					 of the date selection objects is that they must be properly named
					 prefix + mon (stmon) | prefix + day (stday) | prefix + year (styear)					 
***********************************************************************************/

function isLeapYear(x){
	// not perfect, but if the month selected has no remainder when diveded by 4
	// the month is a leap year.
	if(x%4==0) return true;
	else return false;
}
function isLeapMonth(x){
	// Leap month is 2 for February. If the month selected is 2 then return true.
	if(x==2){return true}
	else{return false}
}
function setDay(tday,x,m,fr,f,am,ad,ay){
	monthNamesAR=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	with(f){
		// if tday is greater than the day 
		if(tday > x){
			// alert the user to the status.
			alert("There are only "+x+" days in "+monthNamesAR[m-1]+".\nResetting your days to match...");
			// reset the day to the last day of the month.
			for(d=0;d<eval(ad+".options.length");d++){
				if( eval(ad+".options[d].value==x") ){
					eval(ad+".options[d].selected=true");
				} // end if value=29
			} // end for
		} // end if(tday > 29)
	} // end with
}
function errMsg(x){alert(x);}
function checkDate(fr,ab){	
	var f=eval("document."+fr); //evaluate the name of the form for dynamics.
	var aMon=ab+"mon"; //evaluate the name of the month select object.
	var aDay=ab+"day"; //evaluate the name of the day select object.
	var aYear=ab+"year"; //evaluate the name of the year select object.	
	buildChecks(f,aMon,aDay,aYear,fr); //call the function to start the checking
}
function buildChecks(f,am,ad,ay,fr){ 
	//new to v2.0. This function is separate from the initial function that checks the
	//selects.  The purpose is to separate, and eventually create a checkDate object
	//to cut down on the amount of code.  We'll see when that happens.
	with(f){
		// set up some local variables.
		var tday="";
		var tmon="";
		var tyear="";
		//set up an array for all months with 31 days.
		days30Ar=new Array(4,6,9,11);
		// read in the options selected.
		tmon=eval(am+"["+am+".selectedIndex].value");
		tday=eval(ad+"["+ad+".selectedIndex].value");
		tyear=eval(ay+"["+ay+".selectedIndex].value");
		// First we'll check to see if the month selected is February
		if(isLeapMonth(tmon)){ 
			// Now we'll check to see if it's a leap year
			// If it is, then if the date is greater than the last day of the month, reset it.
			if(isLeapYear(tyear)){if(tyear!=""){setDay(tday,29,tmon,fr,f,am,ad,ay);}
			else{setDay(tday,28,tmon,fr,f,am,ad,ay);}} // end if(LeapYear())
			else {setDay(tday,28,tmon,fr,f,am,ad,ay);} // end if(isLeapYear())								
		} // end if(isLeapMonth())
		else {
			if(tday > 30){ // if the day selected is 31
				for(i=0;i<days30Ar.length;i++){ // check to see if the month selected only has 30 days.
					if(tmon==days30Ar[i]){ // if it does, reset the days back to 30.
						setDay(tday,30,tmon,fr,f,am,ad,ay);
						break;
					} // end if
				} // end for
			} // end if								
		}	
	} // end with
} // end function
