/**
 * @author ian
 * http://www.digimantra.com/tutorials/what-is-self-executing-function-in-javascript/
 *  Same as the basic session_timeout.js but this version calls a_ivert_session_timeout Ajax script on the server
 *  as it needs to remove empty iverts before killing the session, and the redirection is to the ivert_login_form.php
 * Regularly checks that the session has not timed out 
 */


function get_cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	var i;
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function process_result(req, id) {
	// Updates the DOM as to whether the session is still alive, and returns the session status
	// Return status bool (true => session is alive, false => session has expired )
	if (req.responseText == ""){
		document.getElementById(id).innerHTML = '<img title="Your session has expired."  src="images/session_dead.png"></img>';
		return false;
	}else{
		document.getElementById(id).innerHTML = req.responseText;
		return true;
	}
}
	
var timeout_delay_ms = 10000;
	
// Execute this as soon as it's defined.
(function session_timeout(session){
	// snooze, then do function...
 	setTimeout(function() {
 		// invoke php server script that checks the session timeout
		var url = 'a_ivert_session_timeout.php';
		var div = 'session_timeout';
		// Checks the SESSION has not timed out
		// Build the params java-style.
		if (session === undefined) {
			url = url;
		}else{
			url = url + "?session=\"" + session + "\"";
		}
		// Fire off the request asynchronously
		// The asynchronous request is needed because we need the result that is returned
		// process_result inspects the result from the server-side a_session_time.php 
		// and returns false if the session has expired.
		// The asynchronous version does not wait for the Ajax script to return and so 
		// the result cannot be caught.
		if (getDocSynchronous(url, process_result, div) == true){
			// The session has not timed out, so keep checking
			session_timeout(session);
		}else{
			// Request that the server kill the session
			url = url + "&kill=\"" + 1 + "\"";
			getDoc(url, process_result, div)
			
			// Should really also remove the PHPSESSID cookie
			
			// Notify the user that the session has expired
			alert("Your session has expired.");
			
			// Redirect the user to the login form, when she acknowledges the Alert
			top.location.href = 'ivert_login_form.php';
			
			// The session has timed out, so stop checking
			return;
		}
		
	},timeout_delay_ms);
})(get_cookie("PHPSESSID"))

window.onload = function() {
	// Make the initial invocation of server Ajax script that checks the session timeout
	var url = 'a_ivert_session_timeout.php';
	var div = 'session_timeout';
	var session = get_cookie("PHPSESSID");
	// Checks the SESSION has not timed out
	// Build the params java-style.
	if (session === undefined) { 
		url = url;
	}else{
		url = url + "?session=\"" + session + "\"";
	}
	// fire off asynchronous request and up-date the page div
	getDoc(url, writeHTML, div);		
}

