/** @fileoverview This file contains functions related to the calendars panel. */

/**
 * Loads a new day from the student event calendar into the calendars panel.
 * @param {String} direction Informs whether to load next or previous day in calendar; "next" or "previous" values only
 */
function loadStudentCalendar(direction)
{
	var month = $('student_calendar_month').value;
	var day = $('student_calendar_day').value;
	var year = $('student_calendar_year').value;

	if(direction == 'next')
		day++;
	else
		day--;

	var ajax = initAjax();
	ajax.open('GET','student_calendar.php?timeStamp='+new Date().getTime()+'&year='+year+'&month='+month+'&day='+day+'&content_type=xml',true);
	ajax.onreadystatechange = function() { loadStudentCalendarCallback(ajax) };
	ajax.send(null);
}

/**
 * Called exclusively by the loadStudentCalendar function; it follows up with the AJAX operations of that function.
 * @param {Object} ajax Ajax object
 * @see #loadStudentCalendar
 */
function loadStudentCalendarCallback(ajax)
{
	if(ajax.readyState == 4)
	{
		if(ajax.status == 200)
		{
			var student_calendar = $('student_calendar');
			var student_calendar_children = student_calendar.childNodes;

			while(student_calendar_children.length)
				student_calendar.removeChild(student_calendar_children[0]);

			var response = ajax.responseXML;

			var html = response.getElementsByTagName('html')[0];
			var student_calendar_content = html.firstChild;
			
			appendNode(student_calendar_content,student_calendar);

			var month = elementText(response.getElementsByTagName('month')[0]);
			var day = elementText(response.getElementsByTagName('day')[0]);
			var year = elementText(response.getElementsByTagName('year')[0]);
			
			$('student_calendar_month').value = month;
			$('student_calendar_day').value = day;
			$('student_calendar_year').value = year;
		}
	}
}