//-------------------------------------------------------------------------------------------------
// cookieHandler.js
//
// Processes the information contained in the imageInfo.js script to display the proper image
// and caption.
//
// Script should be included in the HTML file just after the "body" tag in the following
// manner:
// 
//     	<script src="cookieHandler.js" language="JavaScript"></script>
//
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// createCookie
//
// Sets a cookie.
//
// Call to set 'ppkcookie' to 'testcookie' to expire in 7 days:
//
// createCookie('ppkcookie','testcookie',7);
//
// Use 0 days to have the cookie deleted upon close of browser.
// Use negative number or call eraseCookie to remove a cookie.
//

function createCookie(name,value,days) 
{

if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	}
else var expires = "";

document.cookie = name+"="+value+expires+"; path=/";

}// end of createCookie
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// readCookie
//
// Reads a cookie.
//
// We're going to search for the name of the cookie, followed by an =. So create this new string
// and put it in nameEQ:
//
//	var nameEQ = name + "=";
//
// Then split document.cookie on semicolons. ca becomes an array containing all cookies that are set for this domain and path.
//
//	var ca = document.cookie.split(';');
//
// Then we go through the array (so through all cookies):
//
//	for(var i=0;i < ca.length;i++) {
//
//	Set c to the cookie to be checked.
//
//		var c = ca[i];
//
// If the first character is a space, remove it by using the substring() method. Continue doing
// this until the first character is not a space.
//
//		while (c.charAt(0)==' ') c = c.substring(1,c.length);
//
// Now string c begins with the name of the current cookie. If this is the name of the desired cookie
//
//		if (c.indexOf(nameEQ) == 0)
//
// we've found what we were looking for. We now only need to return the value of the cookie, which
// is the part of c that comes after nameEQ. By returning this value we also end the function: mission accomplished.
//
//		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
//
//
// If, after having gone through all cookies, we haven't found the name we're looking for, the cookie is not present. We return null.
//
//	return null;
//
//
// Call by:
//
// var x = readCookie('ppkcookie1');
// if (x) {
//	[do something with x]
//	}
//
// If the cookie does not exist, function will return null.
//

function readCookie(name) 
{

var nameEQ = name + "=";

var ca = document.cookie.split(';');

for(var i=0; i < ca.length; i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}

return null;

}// end of readCookie
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// eraseCookie
//
// Erases cookie name.
//
// NOTE: some browsers don't immediately erase a cookie but wait until the browser exits.
// To remedy this, the function also sets the cookie to "".
//

function eraseCookie(name) 
{

//change the cookie to ""
createCookie(name,"", 0); 

//erase the cookie (erasure may not actually occur until the next browser exit)
createCookie(name,"",-1);

}// end of eraseCookie
//-------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------
// displayCookie
//
// Reads value of cookie name and displays the cookie in its value in two text boxes with ids of
// 'Cookie' and 'Value'.
//

function displayCookie(name) 
{

var cookieValue = readCookie(name);

//test for value of "", which means cookie has been set to ""	
if (cookieValue == "") {

	cookieValue = "Blank";

	}

//test for value of null, which means there is not cookie
if (!cookieValue) {

	cookieValue = "Null";

	}
	
document.getElementById('Cookie').value = name;

document.getElementById('Value').value = cookieValue;

}// end of displayCookie
//-------------------------------------------------------------------------------------------------

