/*
Description: JavaScript for AJAX calendar
Author: Brian Toms and Jeremy "Gub" Johnson
Date: 16 May 2006
Filename: calendar_ajax.js
*/

var request_calendar_file = 'plugins/calendar/requestcal.php';

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 requestCalendar(month, year, type) {
	if(type == 'display') {
		http.open('get', 'plugins/calendar/requestcal.php?type=' + type + '&month=' + month + '&year=' + year);
	}
	else {
		var textareas = document.getElementsByTagName("div");
		for (var i = 0; i < textareas.length; i++) {
			tinyMCE.execCommand('mceRemoveControl', true, textareas[i].id);
		}	
		http.open('get', '../plugins/calendar/requestcal.php?type=' + type + '&month=' + month + '&year=' + year);
	}
	http.onreadystatechange = updateCalendar;
	http.send(null);
}

function calMakeAllMCEs() {
	var textareas = document.getElementsByTagName("div");
	for (var i = 0; i < textareas.length; i++) {
		if(textareas[i].className == 'calTextarea') {
			tinyMCE.execCommand('mceAddControl', false, textareas[i].id);
		}
	}	
}

function calMakeTMCE(elemname) {
	globalvar.elemattrs = '';
	globalvar.bluract = '';
	toggleTextarea(elemname, 'div', true, true);
	//toggleEditor(elemname);
}

function updateCalendar() {  //handles response for requestCalendar
  if(http.readyState == 4 && http.status == 200) { 
    var response = http.responseText;
    if(response) {
      document.getElementById('cal-container').innerHTML = response;
	  calMakeAllMCEs()
   }
  }
	if(http.readyState == 1) {
		loading(document.getElementById('cal-container'));
	}
}

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 = "<div style=\"text-align: center;\"><em>Click any date to display activities</em></div>";
		}
	} 
}

function initCal() { //initial onLoad function
	if(document.getElementById('calHolder')) {
		http.open('get', request_calendar_file + '?action=showcalendar');
		http.onreadystatechange = handleShowCalResponse;
		http.send(null);
	}
}

window.onload = initCal;