Date.prototype.getDays = function()
{
	return 32 - new Date(this.getFullYear(), this.getMonth(), 32).getDate();
}

Date.prototype.getFormatted = function()
{
	return this.getFullYear() + '-' + ((this.getMonth() < 9)?'0':'') + (this.getMonth() + 1) + '-' + ((this.getDate() < 10)?'0':'') + this.getDate();
}

Date.parseString = function(aString)
{
	var tempDate = Date.parse(aString.replace(/-/g, '/'));
	return new Date(tempDate);
}

function FSiCalendar(containerID, monthsNames, daysNames)
{
	this.containerID = containerID;
	this.currentDate = new Date();
	this.currentMonth = this.currentDate.getMonth();
	this.currentYear = this.currentDate.getFullYear();
	this.monthsNames = monthsNames;
	if (daysNames)
		this.daysNames = daysNames;
}

FSiCalendar.prototype.setDate = function(newDate)
{
	if (typeof newDate == 'object')
	{
		this.currentDate = newDate;
		this.currentMonth = this.currentDate.getMonth();
		this.currentYear = this.currentDate.getFullYear();
		this.rebuildCalendar();
	}
	else
		alert('bad date');
}

FSiCalendar.prototype.getDate = function()
{
	return this.currentDate;
}

FSiCalendar.prototype.setMonth = function(month)
{
	month = Math.round(month);
	if ((month > 0) && (month < 13))
	{
		this.currentMonth = month;
		this.rebuildCalendar();
	}	
}

FSiCalendar.prototype.setYear = function(year)
{
	year = Math.round(year);
	this.currentYear = year;
	this.rebuildCalendar();
}

FSiCalendar.prototype.prevMonth = function()
{
	this.currentMonth--;
	if (this.currentMonth == -1)
	{
		this.currentMonth = 11;
		this.currentYear--;
	}
	this.rebuildCalendar();
}

FSiCalendar.prototype.nextMonth = function()
{
	this.currentMonth++;
	if (this.currentMonth == 12)
	{
		this.currentMonth = 0;
		this.currentYear++;
	}
	this.rebuildCalendar();
}

FSiCalendar.prototype.rebuildCalendar = function()
{
	var currentMonth = document.getElementById(this.containerID + '_month_current');
	var days = document.getElementById(this.containerID + '_days');
	if (currentMonth && days)
	{
		days.innerHTML = '';
		this.buildMonth(currentMonth);
		this.buildDays(days);
	}
}

FSiCalendar.prototype.buildCalendar = function()
{
	var container = document.getElementById(this.containerID);
	if (container)
	{
		container.innerHTML = '';
		var months = document.createElement('div');
		months.id = this.containerID + '_months';
		months.className = 'calendar_months';
		this.buildMonths(months);
		container.appendChild(months);
		var days = document.createElement('div');
		days.id = this.containerID + '_days';
		days.className = 'calendar_days';
		this.buildDays(days);
		container.appendChild(days);
	}
}

FSiCalendar.prototype.buildMonths = function(months)
{
	var prevMonth = document.createElement('div');
	prevMonth.className = 'month_prev';
	prevMonth.id = this.containerID + '_month_prev';
	prevMonth._calendar = this;
	prevMonth.onclick = function() { this._calendar.prevMonth(); }
	months.appendChild(prevMonth);
	var currMonth = document.createElement('div');
	currMonth.className = 'month_current';
	currMonth.id = this.containerID + '_month_current';
	this.buildMonth(currMonth);
	months.appendChild(currMonth);
	var nextMonth = document.createElement('div');
	nextMonth.className = 'month_next';
	nextMonth.id = this.containerID + '_month_next';
	nextMonth._calendar = this;
	nextMonth.onclick = function() { this._calendar.nextMonth(); }
	months.appendChild(nextMonth);
}

FSiCalendar.prototype.buildMonth = function(month)
{
	month.innerHTML = this.monthsNames[this.currentMonth] + ', ' + this.currentYear;
}

FSiCalendar.prototype.buildDays = function(days)
{
	var tempDate = new Date();
	tempDate.setFullYear(this.currentYear, this.currentMonth, 1);
	var firstDay = (tempDate.getDay() + 6) % 7;
	var monthDays = tempDate.getDays();
	var weekElement;
	var dayElement;
	if (this.daysNames)
	{
		weekElement = document.createElement('div');
		weekElement.id = this.containerID + '_header';
		weekElement.className = 'header';
		for (i = 0; i < 7; i++)
		{
			var dayElement = document.createElement('div');
			dayElement.id = this.conainerID + '_weekday_' + i;
			dayElement.className = 'weekday';
			dayElement.innerHTML = this.daysNames[i];
			weekElement.appendChild(dayElement);
		}
		days.appendChild(weekElement);
	}
	for (i = 0; i < 7 * Math.floor((firstDay + monthDays + 6) / 7); i++)
	{
		if (!(i % 7))
		{
			weekElement = document.createElement('div');
			weekElement.id = this.containerID + '_week_' + (i / 7);
			weekElement.className = 'week';
		}
		if ((i >= firstDay) && (i - firstDay < monthDays))
		{
			day = i - firstDay + 1;
			if ((this.currentYear == this.currentDate.getFullYear()) && (this.currentMonth == this.currentDate.getMonth()) && (day == this.currentDate.getDate()))
				className = 'current';
			else
				className = 'normal';
			dayElement = this.buildDay(i, day, className + ' day');
			dayElement._calendar = this;
			dayElement.onclick = function() {
				this._calendar.setDate(new Date(this._calendar.currentYear, this._calendar.currentMonth, new Number(this.innerHTML)));
				if (typeof this._calendar.onchange == 'function')
					this._calendar.onchange();
			}
		}
		else
			dayElement = this.buildDay(i, '', 'empty day');
		weekElement.appendChild(dayElement);
		if ((i % 7) == 6)
			days.appendChild(weekElement);
	}
}

FSiCalendar.prototype.buildDay = function(number, day, classes)
{
	var dayElement = document.createElement('div');
	dayElement.id = this.conainerID + '_day_' + number;
	dayElement.className = classes;
	dayElement.innerHTML = day;
	return dayElement;
}

