var daysInMonth = new Array(11);
daysInMonth[0] = 31;
daysInMonth[1] = 29;
daysInMonth[2] = 31;
daysInMonth[3] = 30;
daysInMonth[4] = 31;
daysInMonth[5] = 30;
daysInMonth[6] = 31;
daysInMonth[7] = 31;
daysInMonth[8] = 30;
daysInMonth[9] = 31;
daysInMonth[10] = 30;
daysInMonth[11] = 31;

var theDate = new Date();
var thisMonth = theDate.getMonth();
var thisYear = theDate.getFullYear();

var mmm = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";

var codeDelimiter = ",";

function writeMmm(group) {
	var selected = (writeMmm.arguments.length == 2) ? writeMmm.arguments[1] : thisMonth;
	
	document.write('<select name=months_'+ group +' onChange=\"updateDays(\''+ group +'\', this.form)\">');
	
	aMonths = mmm.split(codeDelimiter);
	for ( var x = 0; x < aMonths.length; x++ ) {
		if (x == selected) {
			document.write('<option value=\"'+ x + '\" selected>'+ aMonths[x]);
		} else {
			document.write('<option value=\"'+ x + '\">'+ aMonths[x]);
		}
	}
	
	document.write('</select>');
}

function writeDays(group) {
	var OKtoOmitDay = (writeDays.arguments.length == 2) ? true : false;
	var selected = (writeDays.arguments.length == 3) ? writeDays.arguments[2] : theDate.getDate();

	document.write('<select name=\"days_'+ group +'\">');

	for ( var x = 1; x < (daysInMonth[thisMonth] + 1); x++ ) {
		if (x < 10) { 
			x1 = "0"+x;
		} else {
			x1 = x;
		}
		if (x == selected) {
			document.write('<option value=\"'+ x1 + '\" selected>'+ x1);
		} else {
			document.write('<option value=\"'+ x1 + '\">'+ x1);
		}
	}
	
	document.write('</select>');
}

function writeYears(group, start, end) {
	var selected = (writeYears.arguments.length == 4) ? writeYears.arguments[3] : theDate.getFullYear();

	document.write('<select name=\"years_'+ group +'\" onChange=\"updateDays(\''+ group +'\', this.form)\">');
	
	for (x = start; x <= end; x++) {
		if (x == selected) {
			document.write('<option value=\"'+ x + '\" selected>'+ x);
		} else {
			document.write('<option value=\"'+ x + '\">'+ x);
		}
	}
	
	document.write('</select>');
}

function writeIntRange(group, start, end) {

	document.write('<select name=\"'+ group +'\" >');
	
	for (x = start; x <= end; x++) {
		document.write('<option value=\"'+ x + '\">'+ x);
	}
	
	document.write('</select>');
}

function writeInt15(group, start) {
	
	document.write('<select name=\"'+ group +'\" >');
	
	for (x = 0; x <= 23; x++) {
		if (x < 10) { 
			x1 = "0"+x;
		} else {
			x1 = x;
		}

		if (x == start) {
			document.write('<option value=\"'+ x1 + '00' +'\" selected>'+ x1 + '00');
			document.write('<option value=\"'+ x1 + '15' + '\">'+ x1 + '15');
			document.write('<option value=\"'+ x1 + '30' + '\">'+ x1 + '30');
			document.write('<option value=\"'+ x1 + '45' + '\">'+ x1 + '45');
		} else {
			document.write('<option value=\"'+ x1 + '00' + '\">'+ x1 + '00');
			document.write('<option value=\"'+ x1 + '15' + '\">'+ x1 + '15');
			document.write('<option value=\"'+ x1 + '30' + '\">'+ x1 + '30');
			document.write('<option value=\"'+ x1 + '45' + '\">'+ x1 + '45');
		}

	}
	
	document.write('</select>');
}

function daysInFebruary(year) {
        return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function updateDays(group, form) {
	var curYearField = eval(form.elements['years_' + group]);
	var curYear = curYearField.options[curYearField.selectedIndex].value;

	var curField = eval(form.elements['months_' + group]);
	var curMonth = (parseInt(curField.options[curField.selectedIndex].value) + 1);
	var totDays = (curMonth == 2) ? daysInFebruary(curYear): daysInMonth[curMonth];
	
	var curDayField = eval(form.elements['days_' + group]);
	var curSelectedDay = curDayField.options[curDayField.selectedIndex].value;
	
	var days = new Array();
	
	for (x = 0; x < (totDays + 1); x++) {
		if (x < 10) { 
			x1 = "0"+x;
		} else {
			x1 = x;
		}
		days[x] = x1;
	}
	
	curDayField.length = 0;
	for (x = 1; x < days.length; x++) {
		curDayField.options[x - 1] = new Option(days[x], days[x]);
	}
	
	var selected = (curSelectedDay <= days.length) ? (curSelectedDay - 1) : (days.length - 2);
	selected = (selected > 0 ) ? selected : 0;
	curDayField.selectedIndex = selected;
}
