// JavaScript Document
//date functions
function returnDaysInMonth(){
	if (this.currentMonth == 1){
		if ((this.currentYear % 4) == 0){
			return 29;
		}else{
			return 28;	
		}
	}else{
		return this.days[this.currentMonth];	
	}
}


function makeDaysSelect(){
	while (this.dayDrop.options.length > 0){
		this.dayDrop.remove(0);	
	}
	var amount = this.returnDaysInMonth();	
	if (this.currentDay >= amount) this.currentDay = 0;
	var loop;
	var returnValue = '';
	for (loop = 0; loop < amount; loop++){
		var o=document.createElement('option');
		o.text=(loop+1);
		try{
			this.dayDrop.add(o,null);	
		}catch(err){
			this.dayDrop.add(o);
		}
	}
	this.dayDrop.selectedIndex = this.currentDay;
}


function makeMonthsSelect(){
	while (this.monthDrop.options.length > 0){
		this.monthDrop.remove(0);	
	}
	var loop;
	var returnValue = '';
	for (loop = 0; loop < 12; loop++){
		var o=document.createElement('option');
		o.text=this.months[loop];
		try{
			this.monthDrop.add(o,null);	
		}catch(err){
			this.monthDrop.add(o);
		}
	}
}


function makeYearsSelect(){
	while (this.yearDrop.options.length > 0){
		this.yearDrop.remove(0);	
	}
	var d = new Date();
	thisYear = d.getFullYear();
	var loop;
	var returnValue = '';
	for (loop = thisYear; loop > this.baseYear; loop--){
		var o=document.createElement('option');
		o.text=loop;
		try{
			this.yearDrop.add(o,null);	
		}catch(err){
			this.yearDrop.add(o);
		}
	}
}

function setMonthVal(){
	this.currentMonth = this.monthDrop.selectedIndex;
	this.makeDaysSelect();
	this.setFinalDate();
}

function setYearVal(){
	var d = new Date();
	thisYear = d.getFullYear();
	this.currentYear = thisYear - this.yearDrop.selectedIndex;	
	this.makeDaysSelect();
	this.setFinalDate();
}

function setDayVal(){
	this.currentDay = this.dayDrop.selectedIndex;	
	this.setFinalDate();
}

function setFinalDate(){
	if (this.currentDay < 9){
		var day = '0' + (this.currentDay+1);
	}else{
		var day = this.currentDay + 1;
	}
	
	if (this.currentMonth < 9){
		var month = '0' + (this.currentMonth+1);	
	}else{
		var month = this.currentMonth+1;
	}
	var year = this.currentYear;
	this.final.value = day + '-' + month + '-' + year;
}

function setTheDate(day,month,year){
	//set the year
	var nDay = new Number(day);
	var nMonth = new Number(month);
	var nYear = new Number(year);

	var i;
	for (i = 0; i < this.yearDrop.options.length; i++){
		if (this.yearDrop.options[i].text == nYear){
			this.yearDrop.selectedIndex = i;
			break;
		}
	}

	if (this.yearDrop.selectedIndex == -1) this.yearDrop.selectedIndex = 0;
	this.setYearVal();
	//set the month
	this.monthDrop.selectedIndex = nMonth-1;
	if (this.monthDrop.selectedIndex == -1) this.monthDrop.selectedIndex = 0;
	this.setMonthVal();
	//set day
	this.dayDrop.selectedIndex = nDay-1;
	if (this.dayDrop.selectedIndex == -1) this.dayDrop.selectedIndex = 0;
	this.setDayVal();
}

function DateSelector(dayDrop,monthDrop,yearDrop,final){
	var today = new Date();
	this.months = new Array('January','Febuary','March','April','May','June','July','August','September','October','November','December');
	this.days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	this.baseYear = today.getFullYear() - 120;
	this.currentYear = today.getFullYear();
	this.returnDaysInMonth = returnDaysInMonth;
	
	this.makeYearsSelect = makeYearsSelect;
	this.makeMonthsSelect = makeMonthsSelect;
	this.makeDaysSelect = makeDaysSelect;

	this.setMonthVal = setMonthVal;
	this.setDayVal = setDayVal;
	this.setYearVal = setYearVal;
	
	this.setFinalDate = setFinalDate;

	this.setTheDate = setTheDate;
	
	this.dayDrop = document.getElementById(dayDrop);
	this.monthDrop = document.getElementById(monthDrop);
	this.yearDrop = document.getElementById(yearDrop);
	this.final = document.getElementById(final);
	
	this.makeMonthsSelect();
	this.currentMonth = this.monthDrop.selectedIndex;
	if (this.currentMonth < 0) this.currentMonth = 0;
	
	this.makeDaysSelect();
	this.currentDay = this.dayDrop.selectedIndex;
	if (this.currentDay < 0) this.currentDay = 0;
	
	this.makeYearsSelect();
	this.currentYear = today.getFullYear() - this.yearDrop.selectedIndex;	
	if (this.currentYear < 0) this.currentYear = 0;
	
	return this;
}