var todaysdate = new Date();
var nowday = todaysdate.getDate();
var nowmonth = todaysdate.getMonth();
var nowyear = todaysdate.getFullYear();
var monthname;

function daysInMonth(month, day) {
	var maxDays = 31;
	var selectedDay = day.selectedIndex;
	currentmonth = month[month.selectedIndex].value;
	var year = currentmonth.substring(currentmonth.indexOf('/')+1,currentmonth.length);
	currentmonth = currentmonth.substring(0,currentmonth.indexOf('/'));
	if(currentmonth == (nowmonth+1) && (selectedDay+1) < nowday)
	{
		alert("This day is in the past");
		day.selectedIndex = (nowday - 1);
		return true;
	}
	else
	{
		switch( currentmonth ) {
			case "4":
			case "6":
			case "9":
			case "11":
				maxDays = 30;
				break;
			case "2":
				maxDays = isLeapYear( year ) ? 29 : 28;
				break;
			default:
				break;
		}

		//Verify previous selection
		if ( selectedDay > maxDays ) {
			selectedDay = 0;
		}

		//Empty select list
		day.options.length = 0;

		//Create new options
		for ( i = 0; i < maxDays; i++ ) {
			day[i] = new Option(i+1);
			day[i].value = i+1;
		}

		//Pre select day
		day.selectedIndex = selectedDay;
		return true;
	}
}

function isLeapYear (year)
{
    return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}

function monthnamer(monthnum)
{
	switch(monthnum) {
		case 0:
			monthname = "Jan";
			break;
		case 1:
			monthname = "Feb";
			break;
		case 2:
			monthname = "Mar";
			break;
		case 3:
			monthname = "Apr";
			break;
		case 4:
			monthname = "May";
			break;
		case 5:
			monthname = "Jun";
			break;
		case 6:
			monthname = "Jul";
			break;
		case 7:
			monthname = "Aug";
			break;
		case 8:
			monthname = "Sep";
			break;
		case 9:
			monthname = "Oct";
			break;
		case 10:
			monthname = "Nov";
			break;
		case 11:
			monthname = "Dec";
			break;
	}
}

function populate(month, day)
{
	month.options.length = 0;
	var prevmonth = nowmonth;
	var mymonth = nowmonth;
	var myyear = todaysdate.getFullYear();
	for(i=0; i < 12; i++)
	{
		if((mymonth+i) >= 12)
		{
			mymonth -= 12;
		}
		if((mymonth+i) < prevmonth)
		{
			myyear++;
		}
		monthnamer(mymonth+i);
		prevmonth = mymonth+i;
		month[i] = new Option(monthname+" "+myyear);
		month[i].value = ((mymonth+i+1)+"/"+myyear);
	}
}

populate(document.getElementById('startDateMonthYear'), document.getElementById('startDateDay'));
document.getElementById("startDateDay").selectedIndex =  nowday - 1;
monthnamer(nowmonth);
daysInMonth(document.getElementById('startDateMonthYear'), document.getElementById('startDateDay'));
document.getElementById("startDateMonthYear").onchange = function() {daysInMonth(document.getElementById('startDateMonthYear'), document.getElementById('startDateDay'));};
document.getElementById("startDateDay").onchange = function() {daysInMonth(document.getElementById('startDateMonthYear'), document.getElementById('startDateDay'));};