function daysInMonth(nMonth, nYear) {
	var nMaxDate = 31;
	if ((nMonth == 3) || (nMonth == 5) || (nMonth == 8) || (nMonth == 10)) {
		nMaxDate = 30;
	} else if (nMonth == 1) {
		if ((nYear % 4) == 0) {
			nMaxDate = 29;
		} else {
			nMaxDate = 28;
		}
	}
	return nMaxDate;
}

//set nMonth = -1 to use current month
function initCalMonth(nMonth) {
	if (nMonth < 0) { 
		var now = new Date();
		nMonth = now.getMonth(); 
	}
	document.write(eval(nMonth == 0) ? "<option selected>Jan</option>"		: "<option>Jan</option>");
	document.write(eval(nMonth == 1) ? "<option selected>Feb</option>"		: "<option>Feb</option>");
	document.write(eval(nMonth == 2) ? "<option selected>Mar</option>"		: "<option>Mar</option>");
	document.write(eval(nMonth == 3) ? "<option selected>Apr</option>"		: "<option>Apr</option>");
	document.write(eval(nMonth == 4) ? "<option selected>May</option>"		: "<option>May</option>");
	document.write(eval(nMonth == 5) ? "<option selected>June</option>"		: "<option>June</option>");
	document.write(eval(nMonth == 6) ? "<option selected>July</option>"		: "<option>July</option>");
	document.write(eval(nMonth == 7) ? "<option selected>Aug</option>"		: "<option>Aug</option>");
	document.write(eval(nMonth == 8) ? "<option selected>Sep</option>"		: "<option>Sep</option>");
	document.write(eval(nMonth == 9) ? "<option selected>Oct</option>"		: "<option>Oct</option>");
	document.write(eval(nMonth == 10) ? "<option selected>Nov</option>"		: "<option>Nov</option>");
	document.write(eval(nMonth == 11) ? "<option selected>Dec</option>"		: "<option>Dec</option>");
}

//set nYear = -1 to use current month
function initCalYear(nYear) {
	if (nYear < 0) { 
		var now = new Date();
		nYear = now.getYear(); 
	}
	if (nYear < 1900) { nYear += 1900; }	//Netscape uses 100=2000, 101=2001, etc
	document.write(eval(nYear == 2005) ? "<option selected value='2005'>2005</option>"	: "<option value='2005'>2005</option>");
	document.write(eval(nYear == 2006) ? "<option selected value='2006'>2006</option>"	: "<option value='2006'>2006</option>");
	document.write(eval(nYear == 2007) ? "<option selected value='2007'>2007</option>"	: "<option value='2007'>2007</option>");
	document.write(eval(nYear == 2008) ? "<option selected value='2008'>2008</option>"	: "<option value='2008'>2008</option>");
	document.write(eval(nYear == 2009) ? "<option selected value='2009'>2009</option>"	: "<option value='2009'>2009</option>");
	document.write(eval(nYear == 2010) ? "<option selected value='2010'>2010</option>"	: "<option value='2010'>2010</option>");
}
		
		
//set nYear = -1 to use current day of the month
function initCalDate(nDate, nMonth, nYear) {
	if (nDate < 0) {
		var now = new Date();
		nDate = now.getDate();
		nMonth = now.getMonth();
		nYear = now.getYear();
	}
	var nDaysInMonth = daysInMonth(nMonth, nYear);
	for (var i=1; i<=nDaysInMonth; i++) {
		document.write(eval(nDate == i) ? "<option selected>" + i + "</option>" : "<option>" + i + "</option>");
	}
}

function sameDate(sFormName, sCtrlPrefix1, sCtrlPrefix2) {
	return ((document.forms[sFormName].elements[sCtrlPrefix1 + "Year"].selectedIndex == document.forms[sFormName].elements[sCtrlPrefix2 + "Year"].selectedIndex) &&
			(document.forms[sFormName].elements[sCtrlPrefix1 + "Month"].selectedIndex == document.forms[sFormName].elements[sCtrlPrefix2 + "Month"].selectedIndex) &&
			(document.forms[sFormName].elements[sCtrlPrefix1 + "Date"].selectedIndex == document.forms[sFormName].elements[sCtrlPrefix2 + "Date"].selectedIndex));
}

function addDay(sFormName, sCtrlPrefix, offset) {
	var dayIdx = document.forms[sFormName].elements[sCtrlPrefix + "Date"].selectedIndex;
	var monthIdx = document.forms[sFormName].elements[sCtrlPrefix + "Month"].selectedIndex;
	var yearIdx = document.forms[sFormName].elements[sCtrlPrefix + "Year"].selectedIndex;
	
	var nDaysInMonth = daysInMonth(monthIdx, document.forms[sFormName].elements[sCtrlPrefix + "Year"].options[yearIdx].value);
	var day = dayIdx + 1 + offset;
	
	while (day > nDaysInMonth) {
		day -= nDaysInMonth;
		monthIdx++;
		if (monthIdx > 11) {
			monthIdx = 0;
			yearIdx++;
		}
	}
	dayIdx = day-1;
	
	document.forms[sFormName].elements[sCtrlPrefix + "Year"].selectedIndex = yearIdx;
	refreshCalDateList(sFormName, sCtrlPrefix);
	document.forms[sFormName].elements[sCtrlPrefix + "Month"].selectedIndex = monthIdx;
	refreshCalDateList(sFormName, sCtrlPrefix);
	document.forms[sFormName].elements[sCtrlPrefix + "Date"].selectedIndex = dayIdx;
}
	
function revertCalDateList(sFormName, sCtrlPrefix) {
	var now = new Date();
	
	document.forms[sFormName].elements[sCtrlPrefix + "Date"].options[now.getDate()-1].selected = true;
	document.forms[sFormName].elements[sCtrlPrefix + "Month"].options[now.getMonth()].selected = true;
	document.forms[sFormName].elements[sCtrlPrefix + "Year"].options[now.getYear()-2000].selected = true;
}

function refreshCalDateList(sFormName,sCtrlPrefix) {
	var nDaysInMonth = daysInMonth(document.forms[sFormName].elements[sCtrlPrefix + "Month"].selectedIndex, 
									 document.forms[sFormName].elements[sCtrlPrefix + "Year"].options[document.forms[sFormName].elements[sCtrlPrefix + "Year"].selectedIndex].value);
	var oCtrl = document.forms[sFormName].elements[sCtrlPrefix + "Date"]
	nDate = Math.min(oCtrl.selectedIndex, nDaysInMonth-1)																		 
	while (oCtrl.length > nDaysInMonth) {
		oCtrl.options[oCtrl.length-1] = null;
	}
	while (oCtrl.length < nDaysInMonth) {
		oCtrl.options[oCtrl.length] = new Option(oCtrl.length+1, oCtrl.length+1, false, false);
	}
	if ((nDate >= 0) && (nDate < oCtrl.length)) {
		oCtrl.options[nDate].selected = true;
	}
}
