/*
*	File Name: polls.js
*	Methods:
*	--updatePoll() - David Montgomery 16/09/08
*   --callbackPoll() - David Montgomery 16/09/08
*	--pollAjaxError() - David Montgomery 16/09/08
*   --submitPressed() - David Montgomery 16/09/08
*   --displayJsSubmitLinks() - David Montgomery 19/09/08
*/

var key;
var modKey;
var cookieDate;
var originalSessionId;

/**
*
*	This method will construct a url to send a request to
*   retrieve an updated poll.  The method will construct a
*   XMLHttpRequest object to send the request without having
*   to refresh the form (ajax technology).
*   The method associates the function (callbackPoll) which 
*   will be called when a response is received.
*/
function updatePoll(answer) {
   //alert("in updatePoll");
   
   //construct the url
   var pollAjaxComponentPath = document.getElementById("pollAjaxComponentPath").value;
   var pollItemPath = document.getElementById("pollItem");
   
   var url = "/ajaxHelper?path=" + encodeURIComponent(pollAjaxComponentPath);
   url = url + "&pollGroup=" + encodeURIComponent(answer);
   if(pollItemPath != null)
   {
   	  url = url + "&pollItemPath=" + encodeURIComponent(pollItemPath.value);
   }
   
   //alert("url is: " + url);s
   if (typeof XMLHttpRequest != "undefined") {
       req = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("GET", url, true);
   req.onreadystatechange = callbackPoll;
   req.send(null);
   //alert("finished UpdatePoll");
}

/**
*
*	Call back function to handle the response from the request
*   made by the updatePoll function.  This function will replace
*   the existing poll 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 callbackPoll() {
	//alert("in callbackPoll: 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;
            //alert("message is: " + message);
            
            //When the client requests a page from the server a
            //session cookie with name "JSESSIONID" is stored on
            //the client side.  When the ajax component is called
            //to retrieve the poll chart a new session id is 
            //associated with the ajax request.
            //When retrieving the chart image using cewolf the session
            //id is also passed in.  This session id is compared
            //to the session id of the session which is stored in
            //the cookie.  However, the session id associated with
            //the cewolf chart image is not the same as the current
            //user's session.  This causes cewolf to return an image
            //with the text "The chart has expired".  The solution is
            //therefore to replace the user's session id with the
            //session id that was used to create the chart.  The 
            //current user session id will therefore match the session
            //id sent with the cewolf chart image and so the chart will
            //be displayed correctly.
            var startIndex = message.indexOf("jsessionid=") + 11;
            var endIndex = message.indexOf("?img=", startIndex -5);
            newSessionId = message.substring(startIndex, endIndex);
            document.cookie = "JSESSIONID" + "=" + newSessionId + "; expires=" + cookieDate +"; path=/";
            //alert("newSessionId is: " + newSessionId);
            //alert("originalSessioniId is: " + originalSessionId);      
            
            var currentPoll = document.getElementById("currentPoll");
            currentPoll.parentNode.innerHTML = message;
            
            //add a cookie to indicate that the answer has been submitted
	  		document.cookie = modKey + "=" + modKey + "; expires=" + cookieDate +"; path=/";
        }
        else
        {
        	pollAjaxError();
        }
    }
}

/**
*
*	This method is called whenever the ajax functionality fails for
*   some reason.  This method will attempt to update the poll via
*   the tradional (non-ajax) form submit (page refresh) functionality.
*/
function pollAjaxError()
{
	//alert("A problem occured when attempting to get the next month via ajax");	
	//alert("href: " + document.getElementById(savedDirection).href);
	window.location = "#";
}

/**
*
*	This method is called when the user submits an answer to the poll
*/
function submitPressed(theKey, theCookieDate, answer)
{
//alert("submit pressed" + answer);
	key = theKey;
	modKey = key.replace("'","");
	cookieDate = theCookieDate;
	originalSessionId = document.getElementById("sessionId").value;

   //get the answer selected
  /* var answer;
   var radios = document.getElementsByName("pollGroup");
   for(var i=0; i<radios.length; i++)
   {
   	  if(radios[i].checked)
   	  {
   	  	 answer = radios[i].value;
   	  }
   }*/
   
   if(answer != null)
   {
   	  updatePoll(answer);
   }
   else
   {
   	  return false;
   }
}

/**
*
*	This method sets the poll submit answer links to be displayed
*   on the page.  These are initially set to hidden to cater for
*   the user having javascript disabled and in which case a form
*   with radio buttons will be used.
*/
function displayJsSubmitLinks()
{

	var links = document.getElementsByName("jsSubmitLinks");
	for(var i=0; i<links.length; i++)
	{
		links[i].style.display = "block";
	}
	
}
