var cboCal_MonthCap = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var cboCal_MonthVal = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var cboCal_YearS    = 1900;
var cboCal_YearE    = 2039;

function cboCalendarTime(oHour, oMinute, dHour, dMinute) {
	oHour.options.length = 24;
	for (var i = 0; i < 24; i++) {
		oHour.options[i].value = i;
		oHour.options[i].selected = (i == dHour + 0);
		var t = i + ''; 
		oHour.options[i].text = t.length == 1?'0'+i:i;
	}
	
	oMinute.options.length = 60;
	for (var i = 0; i < 60; i++) {
		oMinute.options[i].value = i;
		oMinute.options[i].selected = (i == dMinute + 0);
		var t = i + ''; 
		oMinute.options[i].text = t.length == 1?'0'+i:i;
	}
}

function cboCalendar(varname, oDate, oMonth, oYear, dDate, dMonth, dYear) {
	this.isLeap = cboCal_IsLeapYear;
	
	this.oDate  = oDate;
	this.oMonth = oMonth;
	this.oYear  = oYear;
	
	dYear  = dYear > cboCal_YearE?cboCal_YearE:dYear;
	dYear  = dYear < cboCal_YearS?cboCal_YearS:dYear;
	dMonth = dMonth > 12?12:dMonth;
	dMonth = dMonth < 1?1:dMonth;
	
	monthDayCount = this.isLeap(dYear) && dMonth == 2?cboCal_MonthVal[dMonth - 1] + 1:cboCal_MonthVal[dMonth - 1];
	dDate  = dDate > monthDayCount?monthDayCount:dDate;
	dDate  = dDate < 1?1:dDate;

	this.dDate  = dDate;
	this.dMonth = dMonth;
	this.dYear  = dYear;
	
	this.changeDate  = cboCal_ChangeDate;
	this.changeMonth = cboCal_ChangeMonth;
	this.changeYear  = cboCal_ChangeYear;
	
	this.returnMDY = cboCal_ReturnMDY;
	this.returnYMD = cboCal_ReturnYMD;
	this.returnDMY = cboCal_ReturnDMY;
	
	this.oDate.onchange = new Function(varname + ".changeDate(this.options[this.selectedIndex].value);");
	this.oMonth.onchange = new Function(varname + ".changeMonth(this.options[this.selectedIndex].value);");
	this.oYear.onchange  = new Function(varname + ".changeYear(this.options[this.selectedIndex].value);");
	
	this.oYear.options.length = cboCal_YearE - cboCal_YearS + 1;
	for (var i = cboCal_YearS; i <= cboCal_YearE; i++) {
		this.oYear.options[i-cboCal_YearS].value = i;
		this.oYear.options[i-cboCal_YearS].text = i;
	}
	this.oYear.selectedIndex = this.dYear - cboCal_YearS;
	
	this.oMonth.options.length = cboCal_MonthCap.length;
	for (var i = 1; i <= cboCal_MonthCap.length; i++) {
		this.oMonth.options[i-1].value = i;
		this.oMonth.options[i-1].text = cboCal_MonthCap[i-1];
	}
	this.oMonth.selectedIndex = this.dMonth - 1;
	
	this.oDate.options.length = monthDayCount;
	for (var i = 1; i <= this.oDate.options.length; i++) {
		this.oDate.options[i-1].value = i;
		this.oDate.options[i-1].text = i;
	}
	this.oDate.selectedIndex = this.dDate - 1;
}

function cboCal_IsLeapYear(year) {
	return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
}

function cboCal_ChangeDate(val) {
	this.dDate = val;
}
	
function cboCal_ChangeMonth(val) {
	this.dMonth = val;
	var monthDayCount = this.isLeap(this.dYear) && this.dMonth == 2?cboCal_MonthVal[this.dMonth - 1] + 1:cboCal_MonthVal[this.dMonth - 1];
	if (monthDayCount <= this.oDate.options.length) {
		this.oDate.selectedIndex  = this.oDate.selectedIndex > monthDayCount - 1?monthDayCount - 1:this.oDate.selectedIndex;
		this.oDate.options.length = monthDayCount;
	} else {
		var prevDayCount = this.oDate.options.length;
		var selMonthDayCount = monthDayCount - this.oDate.options.length;
		this.oDate.options.length = monthDayCount;
		for (var i = 0; i < selMonthDayCount; i++) {
			this.oDate.options[i + prevDayCount].value = i + 1 + prevDayCount;
			this.oDate.options[i + prevDayCount].text = i + 1 + prevDayCount;
		}
	}
}
	
function cboCal_ChangeYear(val) {
	this.dYear = val;
	if (this.dMonth == 2) this.changeMonth(this.dMonth);
}

function cboCal_ReturnMDY(spt) {
	if (spt == "") spt = "/";
	return this.oMonth.options[this.oMonth.selectedIndex].value + spt + this.oDate.options[this.oDate.selectedIndex].value + spt + this.oYear.options[this.oYear.selectedIndex].value;
}

function cboCal_ReturnYMD(spt) {
	if (spt == "") spt = "-";
	return  this.oYear.options[this.oYear.selectedIndex].value + spt + this.oMonth.options[this.oMonth.selectedIndex].value + spt + this.oDate.options[this.oDate.selectedIndex].value;
}

function cboCal_ReturnDMY(spt) {
	if (spt == "") spt = "-";
	return this.oDate.options[this.oDate.selectedIndex].value + spt + this.oMonth.options[this.oMonth.selectedIndex].value + spt + this.oYear.options[this.oYear.selectedIndex].value;
}

