/*
Description: JavaScript for AJAX calendar
Author: Brian Toms and Jeremy "Gub" Johnson
Date: 16 May 2006
Filename: calendar_ajax.js
*/

var request_calendar_file = 'calendar/calendar.php';

function createRequestObject() { //generic function to create AJAX request
	var req; 

	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest(); 
	} else if(window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP"); 
	} else {
		alert('Problem creating the XMLHttpRequest object');
	} 

	return req;
} 

var http = createRequestObject();

function calClick(day, month, year) { //when clicking on a day in calendar
	var allTDs=document.getElementById('calBody').getElementsByTagName("*"); //unbold all dates
	for (i=0; i<allTDs.length; i++) {
		if (allTDs[i].className=='activeDay') {
			allTDs[i].className='regularDay';
		}
		else if (allTDs[i].className=='activeToday') {
			allTDs[i].className='today';
		}
	}
	var theDay = document.getElementById("cal_" + year + "-" + month + "-" + day); //set clicked date to active (bold)
	if(theDay.className == 'regularDay') {
		theDay.className = 'activeDay';
	}
	else if(theDay.className == 'today') {
		theDay.className = 'activeToday';
	}
	//AJAX Stuff
	http.open('get', request_calendar_file + '?action=getevents&date=' + day + '&month=' + month + "&year=" + year);
	http.onreadystatechange = handleCalEventResponse;
	http.send(null);
}

function handleCalEventResponse() {  //handles response for calClick()
  if(http.readyState == 4 && http.status == 200) { 
    var response = http.responseText;

    if(response) {
      document.getElementById('calTextBox').innerHTML = response;
   }
  } 
}

function calMonth(month, year) { //for next/previous arrows
	http.open('get', request_calendar_file + '?action=showcalendar&month=' + month + "&year=" + year);
	http.onreadystatechange = handleShowCalResponse;
	http.send(null);
}

function handleShowCalResponse() { //handles response for calMonth() and initCal()
	if(http.readyState == 4 && http.status == 200) { 
		var response = http.responseText;
		if(response) {
			document.getElementById('calWrapper').innerHTML = response;
			document.getElementById('calTextbox').innerHTML = "<center><em>Click any date to display activities</em></center>";
		}
	} 
}

function initCal() { //initial onLoad function
	http.open('get', request_calendar_file + '?action=showcalendar');
	http.onreadystatechange = handleShowCalResponse;
	http.send(null);
}

window.onload = initCal;