var DateManager = Class.create({
	initialize: function(year_field, month_field, day_field) {
		this.year_selector = $(year_field);
		this.month_selector = $(month_field);
		this.day_selector = $(day_field);

		if ((this.year_selector == null) || (this.month_selector == null) || (this.day_selector == null)) {
			return;
		}

		Event.observe(this.year_selector, "change", this.yearChanged.bind(this));
		Event.observe(this.month_selector, "change", this.monthChanged.bind(this));
		Event.observe(this.day_selector, "change", this.dayChanged.bind(this));
	},

	yearChanged: function() {
		if (this.year_selector.selectedIndex != 0) {
			if (this.month_selector.selectedIndex == 0) {
				this.month_selector.selectedIndex = 1;
			}
			if (this.day_selector.selectedIndex == 0) {
				this.day_selector.selectedIndex = 1;
			}
		}
	},

	monthChanged: function() {
		if (this.month_selector.selectedIndex != 0) {
			if (this.year_selector.selectedIndex == 0) {
				this.year_selector.selectedIndex = 1;
			}
			if (this.day_selector.selectedIndex == 0) {
				this.day_selector.selectedIndex = 1;
			}
		}
	},

	dayChanged: function() {
		if (this.day_selector.selectedIndex != 0) {
			if (this.year_selector.selectedIndex == 0) {
				this.year_selector.selectedIndex = 1;
			}
			if (this.month_selector.selectedIndex == 0) {
				this.month_selector.selectedIndex = 1;
			}
		}
	}
});

function initDateManagers() {
	new DateManager("startyear", "startmonth", "startday");
	new DateManager("endyear", "endmonth", "endday");
}

Event.observe(window, "load", initDateManagers);

