/**
 * Determine the proper number of days for the selected month and year (taking leap years
 * into account), and update the day listbox accordingly. Also, populate the hidden control
 * with the complete date.
 *
 * @param lstMonth				Month listbox
 * @param lstDay				Day listbox
 * @param lstYear				Year listbox
 * @param hdnDate				Complete date hidden control
 */ 
function buildDayList( lstMonth, lstDay, lstYear, hdnDate ) 
{
	var arrDaysPerMonth = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
	var intSelMonth = parseInt( lstMonth.options[lstMonth.selectedIndex].value );
	var intSelDay = parseInt( lstDay.options[lstDay.selectedIndex].value );
	var intSelYear = parseInt( lstYear.options[lstYear.selectedIndex].value );

	//If a blank is selected, blank out the date hidden field
	if(lstMonth.selectedIndex == 0 || lstDay.selectedIndex == 0 || lstYear.selectedIndex == 0) 
	{
		hdnDate.value = "";
	} 
	else 
	{
		var intDays = arrDaysPerMonth[intSelMonth - 1];	// Array is zero-based
		var intCount;

		if( intSelMonth == 2 && intSelYear%4 == 0 )
		{
			intDays++;
		}

		if( intDays > lstDay.length - 1 ) 
		{
			for( intCount = lstDay.length; intCount < intDays + 1; intCount++ )
			{
				lstDay.options[intCount] = new Option( intCount, intCount );  // Option list is zero-based
			}
		} 
		else if( intDays < lstDay.length - 1 ) 
		{
			for( intCount = lstDay.length; intCount >= intDays + 1; intCount-- )
			{
				lstDay.options[intCount] = null;
			}
		}
	
		hdnDate.value = intSelMonth.toString( ) + "/" + intSelDay.toString( ) + "/" + intSelYear.toString( );
	}
}

/**
 * Construct a set of listboxes that are used to build a month, day and year date parts, as well as a 
 * hidden field that will contain the complete, concactenated date
 *
 * @param listPrefix			Prefix used to uniquely identify the listbox form controls
 * @param hiddenName			Name of the hidden field
 * @param startYear				First year to be listed in the year listbox
 * @param years					Number of years to be displayed in the year listbox
 * @param matchDateString		Optional date to be selected when constructing the listboxes
 */
function buildDateControls( listPrefix, hiddenName, startYear, years, matchDateString )
{
	var months = new Array( "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" );
	var i;
	var month = -1;
	var day = -1;
	var year = -1;
	
	// Create a date object, if the matchDate was provided (this assumes a format of: mm/dd/yyyy)
	if( matchDateString != "" )
	{
		var dateArray = matchDateString.split("/");
		month = dateArray[0];
		day = dateArray[1];
		year = dateArray[2];
	}

	// Construct the month listbox
	document.write( "<select name=\"" + listPrefix + "Month\" onChange='buildDayList(this, " + listPrefix + "Day, " + listPrefix + "Year, " + hiddenName + ");'>\n" );
	document.write( "<option value=\"\">\n" );
	for( i = 0; i < months.length; i++ )
	{
		document.write( "<option value=\"" + ( i + 1 ) + "\"" );

		// Determine if the month matches
		if( month == ( i + 1 ) )
		{
			document.write (" selected" );
		}

		document.write( ">" + months[ i ] + "\n" );
	}
	document.write( "</select>\n" );
	
	// Construct the day listbox
	document.write( "&nbsp;/&nbsp;<select name=\"" + listPrefix + "Day\" onChange='buildDayList(" + listPrefix + "Month, this, " + listPrefix + "Year, " + hiddenName + ");'>\n" );
	document.write( "<option value=\"\">\n" );
	for( i = 1; i < 31; i++ )
	{
		document.write( "<option value=\"" + i + "\"" );

		// Determine if the day matches
		if( day == i )
		{
			document.write (" selected" );
		}

		document.write( ">" + i + "\n" );
	}
	document.write( "</select>\n" );
	
	// Construct the year listbox
	document.write( "&nbsp;/&nbsp;<select name=\"" + listPrefix + "Year\" onChange='buildDayList(" + listPrefix + "Month, " + listPrefix + "Day, this, " + hiddenName + ");'>\n" );
	document.write( "<option value=\"\">\n" );
	for( i = 0; i < years - 1; i++ )
	{
		document.write( "<option value=\"" + ( startYear + i ) + "\"" );

		// Determine if the year matches
		if( year == ( startYear + i ) )
		{
			document.write (" selected" );
		}

		document.write( ">" + ( startYear + i ) + "\n" );
	}
	document.write( "</select>\n" );
	
	// Construct the complete date hidden field
	document.write( "<input type=\"hidden\" name=\"" + hiddenName + "\" value=\"" + matchDateString + "\">\n" );
}
