/**
 * calendar.js
 *
 * copyright 2005 e-solutions, lda
 */

// the only calendar object
var calendar = null;

function Calendar() {
	var today = new Date();
	
	this.today = new Object();
	this.today.year = today.getFullYear();
	this.today.month = today.getMonth();
	this.today.day = today.getDate();
	this.current = new Date(today.getFullYear(), today.getMonth(), 1);
	this.allowMonthChange = true;
	this.allowYearChange = false;
	this.monthNames = Calendar.MONTH_NAMES;
	this.dayNames = Calendar.DAY_NAMES;
	this.title = null;
	this.firstDate = null;
	this.lastDate = null;
	this.selectedDate = null;
	this.wnd = null;
	this.css = null;
	this.callback = null;
}

Calendar.NUMBER_OF_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

Calendar.MONTH_NAMES = ["Jan", "Fev", "Mar", "Abr", "Maio", "Jun", 
						"Jul", "Ago", "Set", "Out", "Nov", "Dez"];

Calendar.DAY_NAMES = ["D", "S", "T", "Q", "Q", "S", "S"];

Calendar.prototype.setWindow = function(wnd) {
	this.wnd = wnd;
}

Calendar.prototype.setCallback = function(callback) {
	this.callback = callback;
}

Calendar.prototype.setCss = function(css) {
	this.css = css;
}

Calendar.prototype.setMonthNames = function(monthNames) {
	this.monthNames = monthNames;
}

Calendar.prototype.setDayNames = function(dayNames) {
	this.dayNames = dayNames;
}

Calendar.prototype.setAllowMonthChange = function(allowMonthChange) {
	this.allowMonthChange = allowMonthChange;
}

Calendar.prototype.setAllowYearChange = function(allowYearChange) {
	this.allowYearChange = allowYearChange;
}

Calendar.prototype.setTitle = function(title) {
	this.title = title;
}

Calendar.prototype.setFirstDate = function(firstDate) {
	this.firstDate = firstDate;
	this.firstDate.year = firstDate.getFullYear();
	this.firstDate.month = firstDate.getMonth();
	this.firstDate.day = firstDate.getDate();
}

Calendar.prototype.setLastDate = function(lastDate) {
	this.lastDate = new Object();
	this.lastDate.year = lastDate.getFullYear();
	this.lastDate.month = lastDate.getMonth();
	this.lastDate.day = lastDate.getDate();
}

Calendar.prototype.setSelectedDate = function(selectedDate) {
	this.selectedDate = new  Object();
	this.selectedDate.year = selectedDate.getFullYear();
	this.selectedDate.month = selectedDate.getMonth();
	this.selectedDate.day = selectedDate.getDate();
	
	this.current.setFullYear(this.selectedDate.year);
	this.current.setMonth(this.selectedDate.month);
}

Calendar.prototype.getSelectedDate = function() {
	return new Date(this.selectedDate.year, this.selectedDate.month, this.selectedDate.day); 
}

Calendar.prototype.selectDate = function(evt, day) {
	if(!evt) evt = window.event;
	
	this.wnd.close();
	
	this.selectedDate.year = this.current.getFullYear();
	this.selectedDate.month = this.current.getMonth();
	this.selectedDate.day = day;
	
	if(this.callback) {
		this.callback();
	} 
}

Calendar.prototype.incMonth = function(evt) {
	if(!evt) evt = window.event;
	
	var month = this.current.getMonth() + 1;
	
	if(month > 11) {
		this.current.setFullYear(this.current.getFullYear() + 1);
		this.current.setMonth(0);
	}
	else {
		this.current.setMonth(month);
	}
	
	this.show();
}

Calendar.prototype.decMonth = function(evt) {
	if(!evt) evt = window.event;
	
	var month = this.current.getMonth() - 1;
	
	if(month < 0) {
		this.current.setFullYear(this.current.getFullYear() - 1);
		this.current.setMonth(11);
	}
	else {
		this.current.setMonth(month);
	}
	
	this.show();
}

Calendar.prototype.incYear = function(evt) {
	if(!evt) evt = window.event;
	
	this.current.setFullYear(this.current.getFullYear() + 1);
	this.show();
}

Calendar.prototype.decYear = function(evt) {
	if(!evt) evt = window.event;
	
	this.current.setFullYear(this.current.getFullYear() - 1);
	this.show();
}

Calendar.prototype.show = function() {	
	this.wnd.document.open();
	this.wnd.document.write(this.display());
	this.wnd.document.close();
}

Calendar.prototype.display = function() {
	var month = this.current.getMonth();
	var year = this.current.getFullYear();
	var start = this.current.getDay();
	var numberOfDays = Calendar.NUMBER_OF_DAYS[month];
	
	if(month == 1) {
		var dt = new Date(year, 1, 29);
		
		if(dt.getMonth() == 1) {
			numberOfDays = 29;
		}
	}
	
	var html = "<html><head><title>";
	
	if(this.title) {
		html += this.title;
	}
	else {
		html += this.monthNames[month] + " " + year;
	}
	
	html += "</title>";
	
	if(this.css) {
		html += "<link href=\"" + this.css + "\" rel=\"stylesheet\"/>";
	}
	
	html += "</head><body><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"calTopRow\">";
	
	if(this.allowYearChange) {
		html += "<a class=\"calIncDec\" href=\"\" onClick=\"window.opener.calendar.decYear(); return false\">&lt;&lt;</a>&nbsp;&nbsp;";
	}
	
	if(this.allowMonthChange) {
		html += "<a class=\"calIncDec\" href=\"\" onClick=\"window.opener.calendar.decMonth(); return false\">&lt;&lt;</a>&nbsp;&nbsp;";
	}
	
	html += "<span class=\"calLabelMonth\">" + this.monthNames[month] + "</span>&nbsp;<span class=\"calLabelYear\">" + year + "</span>";
	
	if(this.allowMonthChange) {
		html += "&nbsp;&nbsp;<a class=\"calIncDec\" href=\"\" onClick=\"window.opener.calendar.incMonth(); return false\">&gt;&gt;</a>";
	}
	
	if(this.allowYearChange) {
		html += "&nbsp;&nbsp;<a class=\"calIncDec\" href=\"\" onClick=\"window.opener.calendar.incYear(); return false\">&gt;&gt;</a>";
	}
	
	html += "</td></tr><tr><td><table border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"100%\"><tr>";
	
	var day, col, row;
	
	for(col = 0; col < 7; col++) {
		html += "<td class=\"calLabelWeekday\">" + this.dayNames[col] + "</td>";
	}
	
	html += "</tr><tr>";
	
	for(col = 0; col < start; col++) {
		html += "<td></td>";
	}
	
	for(day = 1, row = 1; day <= numberOfDays; day++, col++) {
		if(col == 7) {
			html += "</tr><tr>";
			col = 0;
			row++;
		}
		
		if(this.firstDate && 
			(year < this.firstDate.year || 
			(year == this.firstDate.year && month < this.firstDate.month) || 
			(year == this.firstDate.year && month == this.firstDate.month && day < this.firstDate.day))) {
			
			html += "<td class=\"calStrikedDay\">" + day + "</td>";
		}
		else if(this.lastDate && 
				(year > this.lastDate.year || 
				(year == this.lastDate.year && month > this.lastDate.month) || 
				(year == this.lastDate.year && month == this.lastDate.month && day > this.lastDate.day))) {

			html += "<td class=\"calStrikedDay\">" + day + "</td>";
		}
		else if(this.selectedDate && 
				year == this.selectedDate.year && 
				month == this.selectedDate.month && 
				day == this.selectedDate.day) {
			
			html += "<td class=\"calSelectedDay\" onClick=\"window.opener.calendar.selectDate(event, " + day + ")\">" + day + "</td>";
		}
		else if(year == this.today.year && 
				month == this.today.month && 
				day == this.today.day) {
			
			html += "<td class=\"calToday\" onClick=\"window.opener.calendar.selectDate(event, " + day + ")\">" + day + "</td>";
		}
		else {
			html += "<td class=\"calDay\" onClick=\"window.opener.calendar.selectDate(event, " + day + ")\">" + day + "</td>";
		}
	}
	
	html += "</tr>";
	
	if(row < 6) {
		html += "<tr>";
		
		for(col = 0; col < 7; col++) {
			html += "<td class=\"calDay\"></td>";
		}
		
		html += "</tr>";
	}
	
	html += "</table></td></tr><tr><td class=\"calBottomRow\">";
	html += "<a href=\"\" onClick=\"self.close(); return false\">";
	html += "<img border=\"0\" height=\"11\" src=\"/commons/images/button-close-popup.gif\" width=\"11\"/></a>&nbsp;";
	html += "</td></tr></table></body></html>";
	
	return html;
}
