/*
PARAMETERS:
iDay: String reference to select field with day options
iMonth: String reference to select field with month options
iYear: String reference to select field with year options
bDay, bMonth, bYear: Boolean for whether corresponding fields are required within the form
future: Number (integer) of years into the future to allow selection from
USE:
dateCheck('fm_date','fm_month','fm_year',true,true,true,2);
*/
	Element.implement({
		getSelectedValue: function(){
			if (this.tagName.toLowerCase() != 'select') 
				return null;
			return $splat(new Elements($A(this.options).filter(function(option){
				return option.selected;
			})).map(function(opt){
				return opt.value.clean();
			}));
		},
		setSelected: function(data, add){
			if (this.tagName.toLowerCase() != 'select') 
				return null;
			if (!add) 
				this.clearSelect();
			var data = $splat(data);
			Array.each(this.options, function(option, i){
				if (data.contains(option.getProperty('value')) || data.contains(i)) 
					option.set('selected', 'selected');
			});
			return;
		},
		getSelectedFull: function(){
			if (this.tagName.toLowerCase() != 'select') 
				return null;
			return $splat(new Elements($A(this.options).filter(function(option){
				return option.selected;
			})).map(function(opt){
				return {
					'index': opt.index,
					'value': opt.value.clean()
				};
			}));
		},
		
		clearSelect: function(ds){
			if (this.tagName.toLowerCase() != 'select') 
				return null;
			var ls = this.getSelectedFull();
			Array.each(this.options, function(option){
				option.set('selected', (option.defaultSelected && ds) ? 'selected' : '');
			});
			return ls;
		}
		
		
	});
function dateCheck(iDay, iMonth, iYear, bDay, bMonth, bYear, future){
	var today = new Date();
	var date = today.getDate();
	var month = today.getMonth() + 1;
	var year = takeYear(today);
	
	var dateList = $(iDay);
	var monthList = $(iMonth);
	var yearList = $(iYear);
	
	if (bDay) {
		populateDate(date, month);
		dateList.set('styles', {'width': '50px'});
	}
	if (bMonth) {
		populateMonth(month);
		monthList.set('styles', {'width': '90px'});
	}
	if (bYear) {
		populateYear(year, future);
		yearList.set('styles', {'width': '70px'});
	}
	
	if (bDay) {
		monthList.addEvent("change", function(e){
			if (monthList.getSelectedValue() > month || yearList.getSelectedValue() > year) {
				var currentDateChoice = dateList.getSelectedValue();
				dateList.empty();
				populateDate('1', monthList.getSelectedValue());
				dateList.setSelected(currentDateChoice);
			}
			else {
				var currentDateChoice = dateList.getSelectedValue();
				dateList.empty();
				populateDate(date, month);
				if (currentDateChoice >= date) 
					dateList.setSelected(currentDateChoice);
			}
		});
	}
	
	yearList.addEvent("change", function(e){
		if (yearList.getSelectedValue() > year) {
			var currentMonthChoice = monthList.getSelectedValue();
			if (bDay) {
				var currentDateChoice = dateList.getSelectedValue();
				dateList.empty();
				populateDate('1', monthList.getSelectedValue());
				dateList.setSelected(currentDateChoice);
			}
			monthList.empty();
			populateMonth('1');
			monthList.setSelected(currentMonthChoice);
		}
		else {
			if (bDay) {
				var currentDateChoice = dateList.getSelectedValue();
				dateList.empty();
				populateDate(date, month);
				if (currentDateChoice >= date) 
					dateList.setSelected(currentDateChoice);
			}
			monthList.empty();
			populateMonth(month);
		}
	});
	
	
	function populateDate(date, selectMonth){
		if (selectMonth == '1' || selectMonth == '3' || selectMonth == '5' || selectMonth == '7' || selectMonth == '8' || selectMonth == '10' || selectMonth == '12') {
			while (date <= '31') {
				//dateList.set('html', dateList.get('html') + '<option value="' + date + '">' + date + '</option>');
				var opt = new Element('option',{value:date,text:date});
				opt.inject(dateList);
				date++;
			}
		}
		else 
			if (selectMonth == '4' || selectMonth == '6' || selectMonth == '9' || selectMonth == '11') {
				while (date <= '30') {
					//dateList.set('html', dateList.get('html') + '<option value="' + date + '">' + date + '</option>');
					var opt = new Element('option',{value:date,text:date});
					opt.inject(dateList);
					date++;
				}
			}
			else 
				if (selectMonth == '2') {
					if (!isLeap(year)) {
						while (date <= '29') {
							//dateList.set('html', dateList.get('html') + '<option value="' + date + '">' + date + '</option>');
							var opt = new Element('option',{value:date,text:date});
							opt.inject(dateList);
							date++;
						}
					}
					else {
						while (date <= '28') {
							//dateList.set('html', dateList.get('html') + '<option value="' + date + '">' + date + '</option>');
							var opt = new Element('option',{value:date,text:date});
							opt.inject(dateList);
							date++;
						}
					}
				}
	}
	
	function populateMonth(month){
		while (month <= '12') {
			//monthList.set('html', monthList.get('html') + '<option value="' + month + '">' + getMonthName(month) + '</option>');
			var opt = new Element('option',{value:month,text:getMonthName(month)});
			
			opt.inject(monthList);
			month++;
		}
	}
	
	function populateYear(year, future){
		var yearEnd = year + future;
		while (year <= yearEnd) {
			//yearList.set('html', yearList.get('html') + '<option value="' + year + '">' + year + '</option>');
			var opt = new Element('option',{value:year,text:year});
			
			opt.inject(yearList);
			year++;
		}
	}
	
	function getMonthName(month){
		switch (month) {
			case 2:
				return 'February';
			case 3:
				return 'March';
			case 4:
				return 'April';
			case 5:
				return 'May';
			case 6:
				return 'June';
			case 7:
				return 'July';
			case 8:
				return 'August';
			case 9:
				return 'September';
			case 10:
				return 'October';
			case 11:
				return 'November';
			case 12:
				return 'December';
			default:
				return 'January';
		}
	}
	
	function takeYear(date){
		x = date.getYear();
		var y = x % 100;
		y += (y < 38) ? 2000 : 1900;
		return y;
	}
	
	function isLeap(year){
		if (new Date(year, 1, 29).getDate() == 29) 
			return true;
		else 
			return false;
	}
	

}
