// Calendar Popup
Calendar.prototype = new Popup();

function Calendar()
{

}

Calendar.prototype.dayCnt = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

Calendar.prototype.display = function (element, offset)
{
	this.name = "_calendar_"
	
	var date = new Date();
	if (element.value != "") {
		var s = element.value.split(" ")[0].split("/");
		date.setDate(s[0]);
		date.setMonth(s[1]-1);
		date.setFullYear(s[2]);
	}
	this.setBelow(true);
	this.setContent( this.buildCalendar(date) );
	this.setEventObj(element);
	this.allowClose = true;

	this.show();
}

Calendar.prototype.buildCalendar = function (date)
{
	var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var dayHeaders = new Array("S", "M", "T", "W", "T", "F", "S");
	
	var today = new Date();
	
	// next month
	var nMonth = date.getMonth() + 1;
	var nYear = date.getFullYear();
	if (nMonth == 12) {
		nMonth = 0;
		nYear++;
	}

	// previous month
	var pMonth = date.getMonth() - 1;
	var pYear = date.getFullYear();
	if (pMonth < 0) {
		pMonth = 11;
		pYear--;
	}
	
	// fill array; will be 6 rows with seven cols
	days = this.getDaysArray(new Date(date));
	
	var src = "<div class=\"calendar\">" +
		"<table>" +
			"<tr class=\"monthNav\">" +
				"<td><a href=\"javascript:cal.changeMonth('" + pMonth + "', '" + pYear + "');\">&lt;&lt;</a></td>" +
				"<td>" + monthNames[date.getMonth()] + " " + date.getFullYear() + "</td>" +
				"<td><a href=\"javascript:cal.changeMonth('" + nMonth + "', '" + nYear + "');\">&gt;&gt;</a></td>" +
			"</tr>" +
		"</table>" +
		"<table class=\"days\">" +
			"<tr class=\"bottomBorder\">" +
				"<td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td>" +
			"</tr>";

	for (i=0; i< days.length; ++i) {
		if (i == days.length-1)
			src += "<tr class=\"bottomBorder\">";
		else
			src += "<tr>";
		for (j=0; j<days[i].length; ++j) {
			var classes = "";
			if (date.getMonth() == days[i][j].getMonth() && date.getFullYear() == days[i][j].getFullYear() && date.getDate() == days[i][j].getDate())
				classes += "selectedDay";
			if (date.getMonth() != days[i][j].getMonth())
				classes += " otherMonth";
			src += "<td class=\"" + classes + "\"><a href=\"javascript:cal.returnDate('" + days[i][j].getFullYear() + "', '" + (days[i][j].getMonth() + 1) + "', '" + days[i][j].getDate() + "');cal.hide();\">" + days[i][j].getDate() + "</a></td>";
		}
		src += "</tr>";
	}

	src += "<tr><td colspan=\"7\" class=\"footer\"><a href=\"javascript:cal.returnDate('" + today.getFullYear() + "', '" + (today.getMonth() + 1) + "', '" + today.getDate() + "'); cal.hide();\">Today</a>";

	var tomorrow = today;
	tomorrow.setDate(tomorrow.getDate() + 1);
	if (tomorrow.getDate() > this.dayCnt[tomorrow.getMonth()]) {
		tomorrow.setMonth((tomorrow.getMonth() + 1) % 11);
		tomorrow.setDate(1);
	}

	src += "&nbsp;-&nbsp;<a href=\"javascript:cal.returnDate('" + tomorrow.getFullYear() + "', '" + (tomorrow.getMonth() + 1) + "', '" + tomorrow.getDate() + "'); cal.hide();\">Tomorrow</a></td>";
	src += "</tr></table></div>";

	return src;
}

Calendar.prototype.changeMonth = function (m, y)
{
	date = new Date();
	date.setMonth(m);
	date.setYear(y);
	this.setContent(this.buildCalendar(date));
}

Calendar.prototype.getDaysArray = function (date)
{
	date.setDate(1);

	rows = new Array(6);
	for (i = 0; i < rows.length; ++i) {
		rows[i] = new Array(7);
	}
	
	offset = date.getDay();
	//pMonth = date.getMonth() > 0 ? date.getMonth() - 1 : 11;
	//nMonth = (date.getMonth() + 1) % 11;

	if (offset > 0) {
		date.setMonth(date.getMonth() - 1);
		date.setDate(this.dayCnt[date.getMonth()] - (offset - 1));
	}

	for (i = 0; i < rows.length; ++i) {
		for (j = 0; j < 7; ++j) {
			rows[i][j] = new Date(date);
			date.setDate(date.getDate() + 1);
			if (date.getDate() > this.dayCnt[date.getMonth()]) {
				date.setMonth((date.getMonth() + 1) % 11);
				date.setDate(1);
			}
		}
	}

	return rows;	
}

Calendar.prototype.returnDate = function (year, month, day)
{
	this.getEventObj().value = day + "/" + month + "/" + year;
}

var cal = new Calendar();