// Does not need to be modified. Give it the page to load and the function to pass the loaded data to.
function ajax(page, FunctionPointer) {
	if(navigator.appName == "Microsoft Internet Explorer") {
	  ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
	  ajaxRequest = new XMLHttpRequest();
	}
	
	ajaxRequest.open("GET", page, true);
	
	ShowLoadingDiv();
	ajaxRequest.onreadystatechange=function() {
		if(ajaxRequest.readyState == 4) {
			FunctionPointer(ajaxRequest.responseText);
			HideLoadingDiv();
		}
	}
	ajaxRequest.send(null);
}

//On Page load
var loadingdiv=document.createElement("div");
loadingdiv.id = "loadingdiv";
loadingdiv.innerHTML = "Loading...";
document.body.appendChild(loadingdiv);

var calendaroverlay=document.createElement("div");
calendaroverlay.id = "calendaroverlay";
calendaroverlay.onclick = function () {CloseEventsCalendar(); return false;}
document.body.appendChild(calendaroverlay);

var eventscalendar=document.createElement("div");
eventscalendar.id = "eventscalendar";
document.body.appendChild(eventscalendar);

// Write functions which use the ajax() function
function OpenEventsCalendar() {
	var arrayPageSize = getPageSize();
	arrayPageScroll = getPageScroll();
	var calendaroverlay = document.getElementById("calendaroverlay");
	var eventscalendar = document.getElementById("eventscalendar");
	calendaroverlay.style.height = (arrayPageSize[1] + 'px');
	calendaroverlay.style.display = 'block';
	eventscalendar.style.display = 'block';
	eventscalendar.style.top = arrayPageScroll[1] + ((arrayPageSize[3] - 500) / 2) + "px";
	eventscalendar.style.left = arrayPageScroll[0] + ((arrayPageSize[2] - 750) / 2) + "px";
	ajax("include/calendar/calendar.asp", GetCalendarData);
}
function CloseEventsCalendar() {
	document.getElementById("calendaroverlay").style.display = "none";
	document.getElementById("eventscalendar").style.display = "none";
	document.getElementById("eventscalendar").innerHTML = "";
}

function GetCalendarData(input) {
	document.getElementById("eventscalendar").innerHTML = input;
}

function ShowLoadingDiv() {
	document.getElementById("loadingdiv").style.display = "block";
}

function HideLoadingDiv() {
	document.getElementById("loadingdiv").style.display = "none";
}

//Other functions

function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}