
	// Note: this script requires the following script file declarations in the top page:

//    <script type="text/javascript" src="zxml.js"></script>
//    <script type="text/javascript" src="subroutines-Ajax.js"></script>
//    <script type="text/javascript" src="subroutines-cookies.js"></script>


	// Note: this script also requires the following Ajax forms in the top page:

//<!-- cookieForm -->
//
//<form id="phpCookieForm" name="phpCookieForm" method="post" action="index-phpCookie.php" onSubmit="return false">
//	<input id="cookieNames" name="cookieNames" type="hidden" value="">
//	<input id="cookieValues" name="cookieValues" type="hidden" value="">
//    <input id="cookieCallback" name="cookieCallback" type="hidden" value="">
//	<input id="cookieExpire" name="cookieExpire" type="hidden" value="">
//   	<input id="cookiePassVals" name="cookiePassVals" type="hidden" value="">
//</form>
//
//<!-- checkSessionForm -->
//
//<form id="checkSessionForm" name="checkSessionForm" method="post" action="index-phpCookie-isSessionExpired.php" onSubmit="return false">
//</form>
//
//<!-- recoverPwdForm -->
//
//<form id="renewSessionForm" name="renewSessionForm" method="post" action="index-phpCookie-renewSession.php" onSubmit="return false">
//	<input id="renewSessionID" name="renewSessionID" type="hidden" value="">
//</form>


	// Store some general variables for reference between functions

var loginDuration = 1000 * 60 * 20;					// Login expires in 20 minutes
var pageDuration  = 1000 * 60 * 60 * 12;			// Current Page memory expires in 12 hours

var timerObj = '';

	// If cookie is renewed too often, things slow down. Track last renewal and compare to desired frequency:

var lastRenewal = new Date().getTime();
var renewFreq = 115000;					// Allow renewal attempt about every 2 minutes


	// Cookies must be shared between client and server: write PHP Cookies because JS cookies can't be changed by PHP:

function phpCookie(cookieNames, cookieValues, cookieCallback, cookieDur, passVals) {	// Write, read, or delete a cookie via php

										// Multiple names and values can be passed via strings separated by commas	
										// cookieDur:      the duration of the cookie in ms. Use '-1' to delete

										// cookieCallback: optional name of a javascript function to run after reading a cookie
										//	...callback is constructed by passing the fetched value along with passVals
	if (!cookieValues) {
		cookieValues = '';
	}

	if (!cookieDur) {
		cookieDur = '';
	}

	if (!cookieCallback) {
		cookieCallback = '';
	}

	if(!passVals) {
		passVals = '';
	}
	
	if ( getObjectByID('phpCookieForm') ) {
		setValue('cookieNames', cookieNames, 'cookieValues', cookieValues, 'cookieCallback', cookieCallback);
		setValue('cookieExpire', cookieDur, 'cookiePassVals', passVals);

		sendReq('phpCookieForm', 'js');

	} else {		// Form is temporarily absent: defer execution of this request...
	
		var cmd = "phpCookie('" + cookieNames + "', '" + cookieValues + "', '" + cookieCallback + "', " + cookieDur + ", \"" + passVals + "\"); ";
		setTimeout(cmd, 1500);
	}
}



function startSessionTimer() {

	if (timerObj) {
		window.clearTimeout(timerObj);
	}

	timerObj = window.setTimeout("isSessionExpired();", renewFreq + 5000);		// Delay slightly so cookie has time to expire
}

function makeCookie(parameterName, sValue, dur) {
	
			// When should this data expire?
	
	if (!dur) {
		
		if (parameterName == 'svsCurrentPage') {		// Page is stored for 12 hours
			dur = pageDuration;
		} else {
			dur = renewFreq;							// Default cookie lifespan = renewFrequency
		}
	}

	top.phpCookie(parameterName, sValue, '', dur);
}


function readCookie(parameterName) {

	var start = document.cookie.indexOf( parameterName + "=" );

	var len = start + parameterName.length + 1;
	if ( ( !start ) && 	( parameterName != document.cookie.substring( 0, parameterName.length ) ) ) {		// Doesn't exist
		return null;
	}
	if ( start == -1 ) {
		return null;													// Contains no data
	}
	
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) {
		end = document.cookie.length;
	}

		// Cookies replace " " with "+"; fix that...

	var outputString = unescape( document.cookie.substring( len, end ) );

	outputString = outputString.replace(/\+/, " ");
	return outputString;
}


function renewSession() {					// Extends session timeout when user clicks or moves the mouse

	var now = new Date().getTime();

		// Only allow session renewals periodically to avoid database logjam


	if (now >= (lastRenewal + renewFreq) ) {		// Not renewed recently: OK to renew again

		var sessionID = readCookie('PHPSESSID');
		
		if (sessionID) {		// Cookie exists: update expiration if not renewed recently

			// Ajax request to update the record

			sendReq('renewSessionForm', '');

			window.clearTimeout(timerObj);
			lastRenewal = now;
			startSessionTimer();
		}
	}

}


function isSessionExpired() {		// If session is expired, throws an automatic logout message
									// If not expired, restarts the sessionTimer to check again 
	sendReq('checkSessionForm', 'js');
}


function delCookie(parameterName) {

	// document.cookie = parameterName + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT;";
	var cookieValues   = '';
	var cookieCallback = '';
	var cookieDur      = -9000000;
	var passVals       = '';
	phpCookie(parameterName, cookieValues, cookieCallback, cookieDur, passVals);
}


function destroyIfExists() {			// Empty specified DOM object(s) when cookie expires

	var idList = destroyIfExists.arguments;

	var lastID = idList.length;
	var objRef;

	for(i=0; i < lastID; i++) {
		objRef = getObjectByID( idList[i] );

		if ( objRef != null ) {
			objRef.innerHTML="";
		}
	}
}


