/**
 * calendarPopup - 2006-03-06
 * ===================================================================
 * Usage:	calendarPopup(int year, int month, int day, string "name of callback function")
 * ie:		<a href="javascript:calendarPopup(2006, 3, 6, 'myCallback');">my calendar</a>
 */

var _monthNames=new Array("Januari","Februari","Mars","April","Maj","Juni","Juli","Augusti","September","Oktober","November","December");
var _dayNames = new Array("M","T","O","T","F","L","S");
var _todayText="Dagens datum";
var _currentYear=0;
var _currentMonth=-1;
var windowref = "window.opener.";
var _selectedDate;
var _instance = 0;


/**
 * Stylesheet for the calendar
 * 
 */
function getStyles() {
	var result = "<style type=\"text/css\">\n";
	result += "BODY,TD{ padding:0px; margin: 0px; font-family:Verdana, Arial, sans-serif; font-size:11px;}\n";
	result += "A, A:visited, A:active { color: black; text-decoration: none; }\n";
	result += "#calendarContainer { }\n";
	result += "TABLE.calendarTable TD { margin: 0px; padding:0px; }\n";
	result += "TABLE.calendarTable TR.days TD {border-right:1px solid #eeeeee; border-bottom:1px solid #eeeeee; width:20px; height:20px; background-color:#eeeeee; cursor: pointer;}\n";
	result += "TABLE.calendarTable TR.days TD:hover {padding-bottom: 1px;border-color: gray; background-color: white;}\n";
	result += "TABLE.calendarTable TR.days TD.selectedDate {background-color:#dde9ff; border: 1px solid gray;}\n";
	result += "TABLE.calendarTable TR.days TD.empty {border-color: white;background-color:white;}\n";
	result += "TABLE.calendarTable TR.dayNames TD { border-bottom: 1px solid gray; }\n";
	result += "TABLE.headerTable { padding:2px; background-color:#eeeeee; margin-bottom: 5px; }\n";
	result += "TABLE.headerTable TD { font-size:14px; font-weight:bold; }\n";
	result += "TABLE.headerTable TD A { font-size:16px; }\n";
	result += "TABLE.headerTable TD A:hover { color:blue; font-weight:bold; }\n";

	result += "</style>\n";
	return result;
}


/**
 * Calendar constructor
 * 
 * @param defYear
 * 		the current year value
 * @param defMonth
 * 		the current month value
 * @param defDay
 * 		the current day value
 * @param callbackFunction
 * 		the name of the callback function to pass on
 */
function calendarPopup(defYear, defMonth, defDay, callbackFunction) {
	refreshCalendar(null, defYear, defMonth, defDay, callbackFunction)
}


/**
 * Function for starting, and refreshing the calendar
 * 
 * @param caller
 * 		the (popup)window object that calls the method, null if none
 * @param defYear
 * 		the current year value
 * @param defMonth
 * 		the current month value
 * @param defDay
 * 		the current day value
 * @param callbackFunction
 * 		the name of the callback function to pass on
 */
function refreshCalendar(caller, defYear, defMonth, defDay, callbackFunction) {
	if(caller == null) {
		_instance++;
		var instanceName = "simpleCalendar" + _instance;
		var newCalendar = window.open("",instanceName,"width=200,height=185,scrollbars=0,resizable=0");
		caller = newCalendar;
	}
	caller.document.open();
	caller.document.writeln( getCalendar(defYear, defMonth, defDay, callbackFunction) );
	caller.document.close();
	caller.focus();
}


/**
 * Building the content from start to end
 * 
 * @param defYear
 * 		the current year value
 * @param defMonth
 * 		the current month value
 * @param defDay
 * 		the current day value
 * @param callbackFunction
 * 		the name of the callback function to pass on
 */
function getCalendar(defYear, defMonth, defDay, callbackFunction) {
    var result = "";

	//Check date variables 
	var now = new Date();
	if ((typeof defYear == "undefined")||(isNaN(defYear)))
		defYear = now.getFullYear();
	if ((typeof defMonth == "undefined")||(isNaN(defMonth)))
		defMonth = now.getMonth();
	else
		defMonth--;
	if ((typeof defDay == "undefined")||(isNaN(defDay)))
		defDay = now.getDate();
	
	_selectedDate = new Date(defYear, defMonth, defDay, 0, 0, 0, 0);

	//Beginning html
	result += "<html><head><title>Kalender</title>\n";
	result += getStyles();
	result += "</HEAD>\n";
	result += "<body marginwidth=0 marginheight=0 topmargin=0 rightmargin=0 leftmargin=0>\n"
	
	//Fill the calendar
	result += createCalendarContent(defYear, defMonth+1, defDay, callbackFunction);
	
	//End html
	result += "</body></html>\n";
	return result;
}


/**
 * Controlling if the current date is the one that should be marked
 * 
 * @param testDate
 * 		the date to test
 */
function isSelectedDate( testDate ) {
	return (_selectedDate.toString() == testDate.toString());
}


/**
 * The main calendar content
 * 
 * @param year
 * 		the current year value
 * @param month
 * 		the current month value
 * @param callbackFunction
 * 		the name of the callback function to pass on
 */
function createCalendarContent(year, month, day, callbackFunction) {
	var result = "<div id=calendarContainer align=center>\n";
	var _currentYear = year;
	var _currentMonth = month-1;
	var daysCount=DaysInMonth(year, month);
	var currentDay=1;
	var firstDay = new Date(_currentYear, _currentMonth, currentDay).getDay();

	//Build the page header with the next/previuos links
	result += buildPageHeader(_currentYear, _currentMonth, callbackFunction);

	result += "<table class=calendarTable>\n";
	result += "<tr class=dayNames>\n";
	for (var d=0; d<7; d++) {
		result += "	<td align=center>";
		result += _dayNames[d];
		result += "</td>\n";
	}
	var cellsWritten = 1;
	
	// fix for months starting on a sunday
	if (firstDay == 0) {
		firstDay = 7;
	}
	while (currentDay <= daysCount) {
		if (((cellsWritten-1)%7) == 0) {
			result += "</tr>\n<tr class=days>\n";
		}
		for (var i=1; i<=7; i++) {
			if(i < firstDay) {
				//Write out empty cells
				result += "	<td class=empty>&nbsp;</td>\n";
			}else{
				firstDay = -1;
				if (currentDay > daysCount)
					break;
				result += "	<td align=center";
				if( isSelectedDate( new Date(_currentYear, _currentMonth, currentDay, 0, 0, 0, 0) ) ) {
					result += " class=selectedDate";
				}
				result += ">";
				
				result += '<a href="javascript:' + windowref + callbackFunction + '('+_currentYear+','+(_currentMonth+1)+','+currentDay+');window.close();">'
				result += currentDay + "</a>";

				result += "</td>\n"

				currentDay++;
			}
			cellsWritten++;
		}
	}
	
	result += "</tr>\n</table>";
	
	//Write shortcut to current date
	var now = new Date();
	result += '<br /><a href="javascript:' + windowref + callbackFunction + '('+now.getFullYear()+','+(now.getMonth()+1)+','+now.getDate()+');window.close();" style="text-decoration:underline;">'+_todayText+'</a>'
	
	result += "</div>\n";
	return result;
}


/**
 * The header containing the month name and the next/prev buttons
 * 
 * @param year
 * 		the current year value
 * @param month
 * 		the current month value
 * @param callbackFunction
 * 		the name of the callback function to pass on
 */
function buildPageHeader(year, month, callbackFunction) {
	var result = "";
	result += "<table width='100%' border=0 cellpadding=0 cellspacing=0 class=headerTable>\n";
	result += "	<td width=30 align=left>\n";
	result += "<a href='javascript:" + windowref + prevMonthClick(year, month, callbackFunction) + "'>";
	result += "&nbsp;&laquo;";
	result += "</a></td>\n";
	
	result += "	<td align=center>" + _monthNames[month] + " " + year + "</td>\n";
	
	result += "	<td width=30 align=right>";
	result += "<a href='javascript:" + windowref + nextMonthClick(year, month, callbackFunction) + "'>";
	result += "&raquo;&nbsp;";
	result += "</a></td>\n";
	result += "</table>\n";
	return result;
}


/**
 * Generates link for the previous month
 * 
 * @param year
 * 		the current year value
 * @param month
 * 		the current month value
 * @param callbackFunction
 * 		the name of the callback function to pass on
 */
function prevMonthClick(year, month, callbackFunction) {
	month--;
	if (month < 0) {
		month = 11; year--;
	}
	return "refreshCalendar(this, " + year + ", " +  (month+1) + ", 1, \"" + callbackFunction + "\");";
}


/**
 * Generates link for the next month
 * 
 * @param year
 * 		the current year value
 * @param month
 * 		the current month value
 * @param callbackFunction
 * 		the name of the callback function to pass on
 */
function nextMonthClick(year, month, callbackFunction) {
	month++;
	if (month > 11) {
		month = 0; year++;
	}
	return "refreshCalendar(this, " + year + ", " + (month+1) + ", 1, \"" + callbackFunction + "\");";
}


/**
 * Calculate number of days in the month
 * 
 * @param year
 * 		the current year value
 * @param month
 * 		the current month value
 */
function DaysInMonth(year, month) {
	var date=new Date();
	var result=0;
	date.setFullYear(year, month-1, 1);
	while ((date.getFullYear() <= year)&&(date.getMonth() <= (month-1))) {
		result++;
		if (result > 31) {
			alert("error getting days in month!\nyear: "+year+", month: "+month);
			return 0;
		}
		date.setFullYear(year, month-1, date.getDate()+1);
	}
	return result;
}
