// JavaScript Document

/*
getDate()			1-31
getMonth()			0-11
getFullYear()
getHours()			0-23
getMinutes()		0-59

corresponding 'set' methods
*/

Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
Date.prototype.getMonthName = function(month)
{
	return Date.monthNames[this.getMonth()];
};
Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
Date.prototype.getDayName = function()
{
	return names[this.getDay()];
};
Date.prototype.getDaysInMonth = function()
{
	var month = new Date(this.getFullYear(), this.getMonth() + 1, 0);
	return month.getDate();
};

function DropdownDatePickerObject()
{
	var me = this;
	
	this.date = new Date();
	this.id = getCurrentTimestamp();
	
	var wrapperDiv = document.createElement('div');
	
	// Month
	var monthDropDown = document.createElement('select');
	for(var i = 0; i < 12; ++i)
	{
		var option = document.createElement('option');
		option.value = i;
		option.appendChild(document.createTextNode(Date.monthNames[i]));
		option.selected = (i == me.date.getMonth());
		monthDropDown.appendChild(option);
	}
	monthDropDown.onchange = function()
	{
		me.setMonth(this.value);
	};
	wrapperDiv.appendChild(monthDropDown);
	
	// Day
	var dayDropDown = document.createElement('select');
	dayDropDown.onchange = function()
	{
		me.setDay(this.value);
	};
	function refreshDayDropDown()
	{
		var days = me.date.getDaysInMonth();
		// remove extra days
		while(dayDropDown.childNodes.length > days)
		{
			dayDropDown.removeChild(dayDropDown.lastChild);
		}
		// add days if too short
		var current = dayDropDown.childNodes.length;
		for(var i = current + 1; i < days + 1; ++i)
		{
			var option = document.createElement('option');
			option.value = i;
			option.appendChild(document.createTextNode(i));
			dayDropDown.appendChild(option);
		}
		
		dayDropDown.value = me.date.getDate();
	}
	refreshDayDropDown();
	wrapperDiv.appendChild(dayDropDown);
	
	// Year
	var yearDropDown = document.createElement('select');
	var currentYear = this.date.getFullYear();
	for(var i = currentYear; i < currentYear + 5; ++i)
	{
		var option = document.createElement('option');
		option.value = i;
		option.appendChild(document.createTextNode(i));
		option.selected = (i == currentYear);
		yearDropDown.appendChild(option);
	}
	yearDropDown.onchange = function()
	{
		me.setYear(this.value);
		me.refresh();
	};
	wrapperDiv.appendChild(yearDropDown);
	
	this.setMonth = function(month)
	{
		me.date.setMonth(month);
		me.refresh();
	};
	this.setDay = function(day)
	{
		me.date.setDate(day);
		me.refresh();
	};
	this.setYear = function(year)
	{
		me.date.setYear(year);
		me.refresh();
	};
	this.setDate = function(year, month, day)
	{
		me.date.setFullYear(year, month, day);
		me.refresh();
	};
	this.refresh = function()
	{
		monthDropDown.value = me.date.getMonth();
		yearDropDown.value = me.date.getFullYear();
		refreshDayDropDown();
		me.onchange();
	};
	
	this.getTimestamp = function()
	{
		return Math.round(me.date.valueOf() / 1000);
	};
	this.setTimestamp = function(timestamp)
	{
		me.date.setTime(timestamp * 1000);
		me.refresh();
	};
	
	this.onchange = function() {};
	
	this.appendTo = function(element)
	{
		element.appendChild(wrapperDiv);
	};
}