function swapDaysInMonth(key,form) {
	yearfield =  eval("form." + key + "yearfield");
	monthfield = eval("form." + key + "monthfield");
	dayfield =   eval("form." + key + "dayfield");
	month = monthfield.selectedIndex;
	year = yearfield.options[yearfield.selectedIndex].text;
	numberOfDays = numberOfDaysIn(month,year);
	day = dayfield.selectedIndex + 1;
	dayfield.options.length = numberOfDays;
	if( day > numberOfDays ) {
		dayfield.selectedIndex = numberOfDays - 1;
	}
	for(i=27;i<numberOfDays;i++) {
		dayfield.options[i].text = i + 1;
	}
	day = dayfield.selectedIndex + 1;
	eval("form." + key).value = year + "-" + (month+1) + "-" + day;
}
function numberOfDaysIn(month,year) {
	if( month == 8 || month == 3 || month == 5 || month == 10 ) return 30;
	if( month != 1 ) return 31;
	if( isleapyear(year) ) {
		return 29;
	}
	return 28;
}
function isleapyear( year ) {
	if(((year % 4 == 0 ) && (year % 100 != 0)) || (year % 400 == 0 ))
		return true;
	else
		return false;
}
function getFullYear(theDate) {
	year = theDate.getYear( );
	if( year < 1000 ) year += 1900;
	return year;
}
var cur_date = new Date();
monthNames = new Array("","January","February","March","April","May","June","July","August","September","October","November","December");
//monthAbbreviations = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");


function getFormPrivate( key, day, month, year, addtoyear, startyear ) {
	
	day   == "-1" ? day   = cur_date.getDate( )      : day = day;
	month == "-1" ? month = cur_date.getMonth( ) + 1     : month = month;
	year  == "-1" ? year  = getFullYear( cur_date )  : year = year;

	if(!addtoyear) {
		addtoyear = 0;
	}

	// day
	document.write("<select name=\""+key+"dayfield\" size=\"1\" onChange='swapDaysInMonth( \""+key+"\", this.form )'>\n" );
	for ( i = 1; i <= numberOfDaysIn( month - 1, year ); i++ ) {
		document.write( "<option" + (day == i ? " selected" : "") + ">" + i + "</option>\n" );
	}
	document.write("</select>\n");
	
	// month
	document.write("<select name=\""+key+"monthfield\" size=\"1\" onChange='swapDaysInMonth( \""+key+"\", this.form )'>\n" );
	for( i = 1; i <= 12; i++ ) {
		document.write("<option value=\"" + i + "\"" + ( month == i ? " selected" : "" ) + ">" + monthNames[i] + "</option>\n");
	}
	document.write("</select>\n");
	
	// year
	document.write("<select name=\""+key+"yearfield\" size=\"1\" onChange='swapDaysInMonth( \""+key+"\",this.form )'>\n" );
	for ( i = startyear; i < (getFullYear( cur_date ) + 1 + addtoyear); i++ ) {
		document.write( "<option" + ( year == i ? " selected" : "" ) + ">" + i + "</option>\n" );
	}

	document.write("</select>");
	if(month < 10) {
		month = '0' + eval(month);
	}
	if(day < 10) {
		day = '0' + eval(day);
	}
	document.write("<input name=\""+key+"\" type=\"hidden\" value=\"" + year + "-" + month + "-" + day + "\" />");
}

function writeForm( key, day, month, year, addtoyear ) {
	
	getFormPrivate( key, day, month, year, addtoyear, 1901 );
}


function writeFormTimeSheet( key, day, month, year, addtoyear ) {
	
	getFormPrivate( key, day, month, year, addtoyear, 2003 );
}

function writeFormFuture( key, day, month, startYear, endYear) {
	
	day   == "-1" ? day   = cur_date.getDate( )      : day = day;
	month == "-1" ? month = cur_date.getMonth( ) + 1     : month = month;
	startYear  == "-1" ? startYear = getFullYear( cur_date )  : startYear = startYear;
	var year = getFullYear( cur_date );
	if (endYear > getFullYear( cur_date ))	{
		day = 0;
		month = 0;
		year = 0;
	}

	// day
	document.write("<select name=\""+key+"dayfield\" size=\"1\" onChange='swapDaysInMonth( \""+key+"\", this.form )'>\n" );
	
	if (day == 0)
	{
		document.write( "<option" + (day == 0 ? " selected" : "") + ">-</option>\n" );
	}
	for ( i = 1; i <= numberOfDaysIn( cur_date.getMonth( ), getFullYear( cur_date ) ); i++ ) {
		document.write( "<option" + (day == i ? " selected" : "") + ">" + i + "</option>\n" );
	}
	document.write("</select>\n");
	
	// month
	document.write("<select name=\""+key+"monthfield\" size=\"1\" onChange='swapDaysInMonth( \""+key+"\", this.form )'>\n" );
	if (month == 0)
	{
		document.write( "<option" + (month == 0 ? " selected" : "") + ">-</option>\n" );
	}
	for( i = 1; i <= 12; i++ ) {
		document.write("<option value=\"" + i + "\"" + ( month == i ? " selected" : "" ) + ">" + monthNames[i] + "</option>\n");
	}
	document.write("</select>\n");
	
	// startYear
	document.write("<select name=\""+key+"yearfield\" size=\"1\" onChange='swapDaysInMonth( \""+key+"\",this.form )'>\n" );
	if (year == 0)
	{
		document.write( "<option" + (year == 0 ? " selected" : "") + ">-</option>\n" );
	}
	if (endYear == '-1') {
		endYear = startYear;
	}
	for ( i = startYear; i <= endYear; i++ ) {
		document.write( "<option" + ( year == i ? " selected" : "" ) + ">" + i + "</option>\n" );
	}

	document.write("</select>");
	document.write("<input name=\""+key+"\" type=\"hidden\" value=\"" + year + "-" + month + "-" + day + "\" />");
}

