// JavaScript Document
var days = new Array()
days[1] = 31
days[2] = 28
days[3] = 31
days[4] = 30
days[5] = 31
days[6] = 30
days[7] = 31
days[8] = 31
days[9] = 30
days[10] = 31
days[11] = 30
days[12] = 31
var monthNames = new Array()
monthNames[1] = "Jan"
monthNames[2] = "Feb"
monthNames[3] = "Mar"
monthNames[4] = "Apr"
monthNames[5] = "May"
monthNames[6] = "Jun"
monthNames[7] = "Jul"
monthNames[8] = "Aug"
monthNames[9] = "Sep"
monthNames[10] = "Oct"
monthNames[11] = "Nov"
monthNames[12] = "Dec"

function IsLeapYear(year)
{
	//Leap Year occurs every four years, except for years ending in 00, in which case only if the year is dvisible by 400.
	isLeapYear = (year%4 == 0 && ((year%100 != 0) || (year%400 ==0)))
	return isLeapYear
}


function CalculateNumDays(f)
{

	var numDays = days[f.Month.value]
	if (f.Month.value == 2 && IsLeapYear(f.Year.value))
	{
		numDays = 29
	}
	selected = f.Day.selectedIndex
	if (numDays >= f.Day.options.length)
	{
		for (counter = f.Day.options.length; counter < numDays; counter++)
		{
			f.Day.options[counter] = new Option(counter+1,counter+1,false,false)	
		}
	}
	else
	{
		for (counter = f.Day.options.length; counter >= numDays; counter--)
		{
			f.Day.options[counter] = null
		}
		if (selected >= numDays)
			f.Day.selectedIndex = numDays - 1
	}

}

function PrintDateSelect(f)
{

	document.write("<select name='Month' size='1' id='Month' onChange='CalculateNumDays(this.form)'>")
	var right_now = new Date()
	for (counter = 1; counter <= 12; counter++)
	{
		selected = ""
		if (counter == right_now.getMonth()+1)
		{
			selected = " selected "
		}
		document.write("<option " + selected + "value='" +counter + "'>" + monthNames[counter] + "</option>")
	}

	document.write("</select><select name='Day' size='1' id='Day'></select>")
  	document.write("<select name='Year' size='1' id='Year'  onChange='CalculateNumDays(this.form)'>")
	// Loop to create options for year with current year selected 
	right_now = new Date()
	for (counter = right_now.getYear()-5; counter <= right_now.getYear(); counter++)
	{
		// If the counter = the current month then 
		// change the variable date_select from null to selected
		
		var date_select="";
		
		if (counter == right_now.getYear())
		{
			date_select="selected "
		}
		
		// Write out the options for year
		document.write ("<option " + date_select + "value=" + counter +">" + counter + "</option>");

	}
	document.write("</select>")
	CalculateNumDays(f)
	f.Day.selectedIndex = right_now.getDate()-1
}
