function getPageSize() {	
	var xScroll, yScroll, windowWidth, windowHeight;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.scrollWidth;
		yScroll = self.innerHeight + self.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.getElementsByTagName("html").item(0).offsetWidth;
		yScroll = document.getElementsByTagName("html").item(0).offsetHeight;
		xScroll = (xScroll < document.body.offsetWidth) ? document.body.offsetWidth : xScroll;
		yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
	}
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.getElementsByTagName("html").item(0).clientWidth;
		windowHeight = document.getElementsByTagName("html").item(0).clientHeight;
		windowWidth = (windowWidth == 0) ? document.body.clientWidth : windowWidth;
		windowHeight = (windowHeight == 0) ? document.body.clientHeight : windowHeight;
	}
	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
}

function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}

function createRequestObject() {
    var tmpXmlHttpObject;
    
    //depending on what the browser supports, use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) { 
        // Mozilla, Safari would use this method ...
        tmpXmlHttpObject = new XMLHttpRequest();
	
    } else if (window.ActiveXObject) { 
        // IE would use this method ...
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return tmpXmlHttpObject;
}

function makeRequest(url, elementId) {
	var http_object = createRequestObject();

	document.getElementById(elementId).innerHTML = '<img src="/img/lytebox/loading.gif"/>';

    //make a connection to the server ... specifying that you intend to make a GET request 
    //to the server. Specifiy the page name and the URL parameters to send
    http_object.open('get', url);

    //assign a handler for the response
    http_object.onreadystatechange = function() {processResponse(http_object, elementId);};

    //actually send the request to the server
    http_object.send(null);

}

function processResponse(http_object, elementId) {
    //check if the response has been received from the server
    if(http_object.readyState == 4){
	
        //read and assign the response from the server
        var response = http_object.responseText;

        //do additional parsing of the response, if needed
		
        //in this case simply assign the response to the contents of the <div> on the page. 
        document.getElementById(elementId).innerHTML = response;
		
        //If the server returned an error message like a 404 error, that message would be shown within the div tag!!. 
        //So it may be worth doing some basic error before setting the contents of the <div>
    }
}

function makeCDRequest(url, elementId) {
	var http_object = createRequestObject();

	document.getElementById(elementId).innerHTML = '<img src="/img/lytebox/loading.gif"/>';

    //make a connection to the server ... specifying that you intend to make a GET request 
    //to the server. Specifiy the page name and the URL parameters to send
    http_object.open('get', url);

    //assign a handler for the response
    http_object.onreadystatechange = function() {processCDResponse(http_object, elementId);};

    //actually send the request to the server
    http_object.send(null);

}

function processCDResponse(http_object, elementId) {
    //check if the response has been received from the server
    if(http_object.readyState == 4){
	
        //read and assign the response from the server
        var response = http_object.responseText;

        //do additional parsing of the response, if needed
		
        //in this case simply assign the response to the contents of the <div> on the page. 
        document.getElementById(elementId).innerHTML = response;
		
		var pageSize = getPageSize();

		var useWidth = pageSize[0];
		if (useWidth > 800) {
			useWidth = 800;
		}

		document.getElementById('calendarDetailContainer').style.width = useWidth + 'px';
		//document.getElementById('calendarDetailContainer').style.height = pageSize[3] + 'px';
		
		//If the server returned an error message like a 404 error, that message would be shown within the div tag!!. 
        //So it may be worth doing some basic error before setting the contents of the <div>
    }
}


function setCalendarDate(month, year) {
	var elementId = 'calendar';
	var url = '/www/app/ajax/calendar.jsp?month=' + month + '&year=' + year;

	makeRequest(url, elementId);
}

function showCalendarDetails(id, date) {

    var elementId = 'calendarDetail';
	var url = '/www/app/ajax/calendardetails.jsp?id=' + id + '&date=' + date;

	toggleBox(elementId, true);

	makeCDRequest(url, elementId);
}
