/*
*	File Name: calendar.js
*	Methods:
*	--updateCal() - David Montgomery 09/09/08
*   --callbackCal() - David Montgomery 09/09/08
*	--calendarAjaxError - David Montgomery 09/09/08
*/

var saveDirection;

/**
*
*	This method will construct a url to send a request to
*   retrieve an updated calendar based on the request
*   parameters passed in.  The method will construct a
*   XMLHttpRequest object to send the request without having
*   to refresh the form (ajax technology).
*   The method associates the function (callbackCal) which 
*   will be called when a response is received.
*/
function updateCal(direction, requestPath) {
   //alert("in UpdateCal");
   savedDirection = direction;
   
   //get the year and month values
   var monthField = document.getElementById("hiddenMonth");
   var yearField = document.getElementById("hiddenYear");
   var filter = document.getElementById("hiddenFilter");
   
   //construct the url
   var url = "/ajaxHelper?month=" + encodeURIComponent(monthField.value);
   url = url + "&year=" + encodeURIComponent(yearField.value);
   url = url + "&direction=" + encodeURIComponent(direction);
   url = url + "&path=" + encodeURIComponent(requestPath)
   url = url + "&view=Calendar"
   url = url + "&eventsFilter=" + encodeURIComponent(filter.value);
   
   //alert("url is: " + url);
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("GET", url, true);
   req.onreadystatechange = callbackCal;
   req.send(null);
   //alert("finished UpdateCal");
}

/**
*
*	Call back function to handle the response from the request
*   made by the updateCal function.  This function will replace
*   the existing calendar html code with the html received.
*   If a status other than 200 eg 404 if the target server cannot
*   be found then the method will call an error handling function.
*/
function callbackCal() {
	//alert("in callbackCal: readystate:" + req.readyState + ", status:" + req.status);
    if (req.readyState == 4) {
        if (req.status == 200) {
            // update the HTML DOM based on whether or not message is valid
            var message = req.responseText;
            var eventCalendar = document.getElementById("eventCalendar");
            eventCalendar.innerHTML = message;
        }
        else
        {
        	calendarAjaxError();
        }
    }
}

/**
*
*	This method is called whenever the ajax functionality fails for
*   some reason.  This method will attempt to update the calendar via
*   the tradional (non-ajax) form submit (page refresh) functionality.
*/
function calendarAjaxError()
{
	//alert("A problem occured when attempting to get the next month via ajax");	
	//alert("href: " + document.getElementById(savedDirection).href);
	window.location = document.getElementById(savedDirection).href;
}