var monthNames = new Array("--Month--","January","February","March","April","May","June","July","August","September","October","November","December");

function changeDate(sel) {
	// parent form of everything
	var form = sel.form;

	// get current year, month, day for use later:
	var now = new Date( );
	var curYear = now.getFullYear( );
	var curMonth = now.getMonth() + 1; // js months are 0 to 11
	var curDay = now.getDate();

	// analyze source of the change
	var toFrom = sel.name.charAt(0); // always f(rom) or t(0)
	var unit = sel.name.charAt(4); // Y(ear) or M(onth)

	// set up the sel references to appropriate set...
	//var selYear = toFrom == "f" ? form.fYear : form.tYear;
	//var selMonth = toFrom == "f" ? form.fMonth : form.tMonth;
	//var selDay = toFrom == "f" ? form.fDay : form.tDay;

	var selYear = form._INYY;
	var selMonth = form._INMMM;	
	var selDay = form._INDD;

	// need selected year to calculate Feb days and how many months
	var year = parseInt(selYear.options[selYear.selectedIndex].text)

	// if year changed, then must rebuild months:
	if ( unit == "Y" ) {
		selMonth.options.length = 1;
		//only show upto current month: for ( var m = 0; m <= (year == curYear ? curMonth : 12); ++m ) {
		for ( var m = 0; m <= 12; ++m ) {
			selMonth.options[m] = new Option(monthNames[m],m);
		}
		selMonth.selectedIndex = 0;
		selDay.options.length = 1;
		selDay.selectedIndex = 0;
	} 
	else {
		// change of month, so rebuild the days:
		selDay.options.length = 1;
		var lastDay = 31;
		switch ( selMonth.selectedIndex ) {
			case 0:
				lastDay = 0; break;
			case 4: case 6: case 9: case 11: /* april, june, sept, nov */
				lastDay = 30; break;
			case 2:
				lastDay = ( year % 4 == 0 ? 29 : 28 );
		}
		// but if current year and month selected, last day is today
		//if ( year == curYear && selMonth.selectedIndex == curMonth ) {
		//	lastDay = curDay;
		//}
		for ( var d = 1; d <= lastDay; ++d ) {
			selDay.options[d] = new Option(d,d);
		}
	}
}

function setupPickers( ) {
	// get current year, month, day for use later:
	var now = new Date( );
	var curYear = now.getFullYear( );
	var curMonth = now.getMonth() + 1; // js months are 0 to 11
	var curDay = now.getDate();

	var form = document.resform;
	var selT = form._INYY;
	//alert(selT);
	// allow 3 years to be chosen (easy to change this)
	for ( var i = 0; i <= 3; i++ ) {
		var y = curYear + i;
		selT.options[i] = new Option(y,y);
	}
	// for the TO side, we force feed to current day
	// So first fake a change of year to this year:
	//selT.selectedIndex = selT.options.length - 1; // always last in list
	selT.selectedIndex = selT.options[0];
	changeDate( selT );
	// and then fake a change of month to this month:
	form._INMMM.selectedIndex = curMonth;
	changeDate( form._INMMM );
	// and finally select today:
	form._INDD.selectedIndex = curDay;
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
//addLoadEvent(changeDate);
//addLoadEvent(setupPickers);

