
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var days = ['S', 'M', 'T', 'W', 'T', 'F', 'S'];
var enddays = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var calendar_id = 0;

function Calendar(name)
{
	this.activated = 0;
	this.name = name;
	this.objName = 'cal_' + name;
	this.date = new Date();
	this.d = this.date.getDate();
	this.m = this.date.getMonth();
	this.y = this.date.getFullYear();

	this.setMonth = function (m)
	{
		this.m = m;
		this.load();
	}

	this.setYear = function (y)
	{
		this.y = y;
		this.load();
	}

	this.getDay = function (d)
	{
		if (!d) {
			d = this.d;
		}

		return this.d;
	}

	this.getMonth = function (m)
	{
		if (!m) {
			m = this.m;
		}

		return months[this.m];
	}

	this.getYear = function (y)
	{
		if (!y) {
			y = this.y;
		}

		return this.y;
	}

	this.nextMonth = function()
	{
		var m;
		if (this.m == 11)
		{
			m = 0;
			this.y++;
		}
		else
		{
			m = this.m + 1;
		}

		this.setMonth(m);
	}

	this.prevMonth = function()
	{
		var m;
		if (this.m == 0)
		{
			m = 11;
			this.y--;
		}
		else
		{
			m = this.m - 1;
		}

		this.setMonth(m);
	}

	this.nextYear = function()
	{
		this.setYear(this.y + 1);
	}

	this.prevYear = function()
	{
		this.setYear(this.y - 1);
	}

	this.getLastDay = function(m, date)
	{
		if (!m) {
			m = this.m;
		}

		if (!date)
		{
			date = this.date;
		}

		var e = enddays[m];

		if (e) return e;

		var y = date.getFullYear();

		return (y % 4 == 0) ? 29 : 28;
	}

	this.setHeader = function()
	{
		var html = "<table cellspacing='0'>\n";

		html += "<tr><td colspan='2' class='prev'><a href='#' onclick='" + this.objName + ".prevMonth(); return false;'>&laquo;</a></td><td colspan='3' class='month'>" + this.getMonth() + " " + this.getYear() + "</td><td colspan='2' class='next'><a href='#' onclick='" + this.objName + ".nextMonth(); return false;'>&raquo;</a></td></tr>\n";
		html += '<tr>';

		for (i = 0; i < days.length; i++)
		{
			html += '<th>' + days[i] + '</th>';
		}

		html += '</tr>\n';

		return html;
	}
	
	this.setFooter = function()
	{
		return "<tr><td colspan='7' align='center' class='footer'><a href='#' onclick='" + this.objName + ".reset(); return false;'>Clear</a></td></tr></table>";
	}

	this.getDays = function()
	{
		var e = this.getLastDay();		
		var col = 0;		
		var html = '<tr>';

		var start = new Date();
		start.setDate(1);
		start.setMonth(this.m);
		start.setFullYear(this.y);

		var start_day = start.getDay();

		var end = new Date();
		end.setDate(e);
		end.setMonth(this.m);
		end.setFullYear(this.y);

		var end_day = end.getDay();

		var now = new Date();
		var year = now.getFullYear();
		var month = now.getMonth();
		var day = now.getDate();

		for (d = 1; d <= start_day; d++)
		{
			html += '<td>&nbsp;</td>';
			col++;
		}

		for (d = 1; d <= e; d++)
		{
			classname = (d == day && this.m == month && this.y == year) ? 'today' : 'day';
			html += "<td><a class='" + classname + "' href='#' onclick='" + this.objName + ".selectDate(" + d + "); " + this.objName + ".hide(); return false;'>" + d + '</a></td>';			

			col++;

			if (col % 7 == 0) {
				html += '</tr><tr>';
			}
		}

		for (i = 1; i <= (6 - end_day); i++)
		{
			html += '<td>&nbsp;</td>';
			col++;
		}

		html += '</tr>';		

		return html;
	}

	this.load = function()
	{
		var html = this.setHeader() + this.getDays() + this.setFooter();

		$(this.objName).innerHTML = html;
	}

	this.hide = function()
	{
		$(this.objName).hide();
	}

	this.show = function() 
	{ 
		$(this.objName).show();
	}

	this.show_and_load = function()
	{
		this.show();
		this.load();
		this.countdown();
	}

	this.toggleVisi = function()
	{
		if ($(this.objName).style.display == 'none')
		{
			this.show_and_load();
		}
		else
		{
			this.hide();
		}
	}

	this.selectDate = function(d)
	{
		m = this.m + 1;
		$(this.name).value = d + '-' + m + '-' + this.y;
	}

	this.reset = function()
	{
		$(this.name).value = 'Select';
		this.hide();
	}

	this.countdown = function()
	{
		this.active();

		_this = this;
		this.activated = setTimeout("_this.hide()", 5000);
	}

	this.active = function()
	{
		if (this.activated)
		{
			clearTimeout(this.activated);
		}
	}

}
