/* Javascript Check Date Selector
   
   by itao GmbH & Co. KG (26.08.2010, Markus Plauschinn)

   Script to care for valid dates using a static date selector, leap year enabled
   
   functions updateDays requires the ids of the three selects fields. day selector is manipulated by
   disabling impossible days and reseting the day to default if an invalid day was selected.
   
   day select reaches from 0 (default, i.e. "day"),1 to 31, no integration of the function on change
   month select reaches from 0 (default, i.e. "month"),1 to 12, integration of the function on change
   year select needs a default (i.e. "year") and the years you want to allow, integration of the function on change
*/
function updateDays(dateDayField, dateMonthField, dateYearField){
    // definition of maxium days per month    
	daysArr = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	// getting data of documents selects
	selectDays=document.getElementById(dateDayField);
	selectedDay=selectDays.selectedIndex;
	selectedMonth=document.getElementById(dateMonthField).selectedIndex;
	selectYears=document.getElementById(dateYearField);
	selectedYear=selectYears.options[selectYears.selectedIndex].value;
	        
	for(x=1;x<=31;x++){
		selectDays.options[x].disabled=false;
	} // enable all days
	if (selectedMonth>0){
		for(x=daysArr[selectedMonth-1]+1;x<=31;x++){
			selectDays.options[x].disabled=true;
		} // disable days except maximum days of month
		if (selectYears.selectedIndex>0 && selectedMonth==2){
			if (selectedYear % 4 != 0 || selectedYear % 100 == 0 ){
				selectDays.options[29].disabled=true;
			} else if (selectedYear % 400 > 0){ // not a leap year - disable February 29th
				selectDays.options[29].disabled=false;
			} // leap year - enable February 29th
		} // a year and february as month given - check for leap year
	} // a month is given - check the days
	if(selectDays.options[selectedDay].disabled==true && !(selectYears.selectedIndex==0 && selectedMonth==2 && selectedDay==29)){
		selectDays.options[selectedDay].selected=false;
		selectDays.options[0].selected=true;
	} // set day to default if selected day is not possible - except February 29th without year
}
