/**
 * @file
 * @brief JavaScript part of the calendar.
 */

/**
 * @fn calendar(url, input, showTime)
 * @brief Open the calendar in a new popup window.
 * @param[in] url URL to the HTML part of the calendar.
 * @param[in] input Name of the @<INPUT@> tag where the calendar should write the date to.
 * @param[in] showTime If true, there will be also place to write time in the calendar.
 */ 
function calendar(url, input, showTime) {
	window.calendarData = new Object();
	window.calendarData.input = input;
	window.calendarData.showTime = showTime;
	window.open(url, 'Calendar', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=350,height=' + (showTime ? '230' : '190'));
}

/**
 * @fn showCalendar(data)
 * @brief Generate HTML code of the calendar.
 * @param[in] data Data passed to the calendar.
 */
function showCalendar(data) {
	var input = data.input;
	var showTime = data.showTime;

	var nrYears = 5;

	var now = new Date();
	var year    = now.getFullYear();
	var month   = now.getMonth();
	var hours   = now.getHours();
	if (hours < 10)
		hours = '0' + hours;
	var minutes = now.getMinutes();
	if (minutes < 10)
		minutes = '0' + minutes;
	var seconds = now.getSeconds();
	if (seconds < 10)
		seconds = '0' + seconds;

	var years  = new Array();
	for (i = 0; i < nrYears; i++)
		years[i] = year - Math.floor(nrYears / 2) + i;

	var calendar = '';

	calendar +=        '<form>';
	calendar +=        '<table>';
	calendar +=        '<tr>';
	calendar +=        '<td>';
	calendar +=                '<table width="100%">';
	calendar +=                '<tr>';
	calendar +=                '<td>';
	calendar +=                '<select id="calendarMonth" onChange="changeCalendar(\'' + input + '\');">';
	for (i = 0; i < 12; i++)
		calendar += '<option value="' + i + '"' + ((i == month) ? ' selected="selected"' : '') + '>' + months[i] + '</option>';
	calendar +=                '</select>';
	calendar +=                '</td>';
	calendar +=                '<td align="right">';
	calendar +=                '<select id="calendarYear" onChange="changeCalendar(\'' + input + '\');">';
	for (i = 0; i < nrYears; i++)
		calendar += '<option value="' + years[i] + '"' + ((i == Math.floor(nrYears / 2)) ? ' selected="selected"' : '') + '>' + years[i] + '</option>';
	calendar +=                '</select>';
	calendar +=                '</td>';
	calendar +=                '</tr>';
	calendar +=                '</table>';
	calendar +=        '</td>';
	calendar +=        '</tr>';
	calendar +=        '<tr>';
	calendar +=        '<td>';
	calendar +=                '<div id="calendarDateCells">';
	calendar +=                '</div>';
	calendar +=        '</td>';
	calendar +=        '</tr>';
	if (showTime) {
		calendar +=        '<tr>';
		calendar +=        '<td>';
		calendar +=                '<div id="calendarTimeCells">';
		calendar +=                '<center>';
		calendar +=                '<table width="50%">';
		calendar +=                '<tr><td>Hours</td><td></td><td>Minutes</td><td></td><td>Seconds</td></tr>';
		calendar +=                '<tr>';
		calendar +=                '<td><input id="calendarHours" type="text" value="' + hours + '" maxlength="2" size="2"/></td>';
		calendar +=                '<td>:</td>';
		calendar +=                '<td><input id="calendarMinutes" type="text" value="' + minutes + '" maxlength="2" size="2"/></td>';
		calendar +=                '<td>:</td>';
		calendar +=                '<td><input id="calendarSeconds" type="text" value="' + seconds + '" maxlength="2" size="2"/></td>';
		calendar +=                '</tr>';
		calendar +=                '</table>';
		calendar +=                '</center>';
		calendar +=                '</div>';
		calendar +=        '</td>';
		calendar +=        '</tr>';
	}
	calendar +=        '</table>';
	calendar +=        '</form>';

	document.write(calendar);
	document.close();
	changeCalendar(input);
}

/**
 * @fn changeCalendar(input)
 * @brief Change the calendar when the user selects another month or year.
 * @param[in] input Name of the @<INPUT@> tag where the calendar should write the date to.
 */
function changeCalendar(input) {
	var year  = parseInt(document.getElementById('calendarYear').value);
	var month = parseInt(document.getElementById('calendarMonth').value);
	var firstDay = (new Date(year, month, 1)).getDay();
	if (firstDay == 0)
		firstDay = 7;
	var prevMonth = month ? (month - 1) : 11;
	var nextMonth = (month == 11) ? 0 : (month + 1);

	var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if (((year % 4 == 0) && (year % 100 != 0))
		|| (year % 400 == 0))
		daysInMonth[1] = 29;

	var rows = ((firstDay - 1 + daysInMonth[month]) > 35) ? 6 : 5;

	var cell;

	cell = 0;
	var cellValue = new Array();
	for (i = 1; i < firstDay; i++)
		cellValue[cell++] = '&nbsp;'; //daysInMonth[prevMonth] - firstDay + 1 + i;
	for (i = 1; i <= daysInMonth[month]; i++)
		cellValue[cell++] = '<a href="javascript:setDate(\'' + input + '\', \'' + year + '-' + ((month < 9) ? ('0' + (month + 1)) : (month + 1)) + '-' + ((i < 10) ? ('0' + i) : i) + '\');">' + i + '</a>';
	for (i = 1; cell < rows * 7; i++)
		cellValue[cell++] = '&nbsp;'; //i;

	cell = 0;
	var cells = '';
	cells += '<table border="1">';
	cells += '<thead><tr>';
	for (i = 0; i < 7; i++)
		cells += '<th>' + days[i] + '</th>';
	cells += '</tr></thead><tbody>';
	for (i = 0; i < rows; i++) {
		cells += '<tr>';
		for (j = 0; j < 7; j++)
			cells += '<t'+(j>4?'h':'d')+'>' + cellValue[cell++] + '</t'+(j>5?'h':'d')+'>';
		cells += '</tr>';
	}
	cells += '</tbody></table>';

	document.getElementById('calendarDateCells').innerHTML = cells;
}

/**
 * @fn setDate(input, date)
 * @brief Write the date input the destination @<INPUT@>.
 * @param[in] input Name of the @<INPUT@> tag where the calendar should write the date to.
 * @param[in] date The date which should be written to he @<INPUT@>.
 */
function setDate(input, date) {
	window.opener.document.getElementById(input).value = date;
	if (document.getElementById('calendarTimeCells') != null) {
		window.opener.document.getElementById(input).value += ' ' +
			document.getElementById('calendarHours').value + ':' +
			document.getElementById('calendarMinutes').value + ':' +
			document.getElementById('calendarSeconds').value;
	}
	window.close();
}
