/*
	nextNET master cookie library
	(c) Craig Davison, 1997, 1999
	last updated Mar. 4/99
*/

function jarCookie (pageName, value, shelfLife)
// ----- general function to place a cookie in the jar -----
{
	var cookie_data=escape (pageName)+'='+value;
	if (shelfLife > 0)              // if a non-temporary cookie
	{
		var today=new Date ();          // holds the current date/time
		var expires=new Date ();
		expires.setTime (today.getTime ()+1000*3600*24*shelfLife);
		cookie_data+=';expires='+expires.toGMTString ();
	}
	document.cookie=cookie_data;
}

function takeCookie (pageName)
// ----- general function to take the contents of a cookie in the jar ----
//       returns '' if no cookie is found
{
        var searchItem=escape(pageName)+'=';
        if (document.cookie.length != 0)
        {
               var startIndex=document.cookie.indexOf (searchItem);
               if (startIndex != -1)   // if the page's specific cookie exists
               {
                       startIndex+=searchItem.length;
                       var endIndex=document.cookie.indexOf (';', startIndex);      // value terminates with ';' or is at end of cookie
                       if (endIndex != -1)
                              return document.cookie.substring (startIndex, endIndex);
                       else
                              return document.cookie.substring (startIndex, document.cookie.length);
               }
               else
                       return '';
        }
        else
                return '';
}

function parseCookie (cookie_name, name)
{
// returns hash and custom values in cookie_hash and cookie_custom.
	name=escape (name);
	var cookie_data=takeCookie (cookie_name);

	if (cookie_data != '')
	{
	        var name_offset=cookie_data.indexOf (name+'|');
	        if (name_offset != -1)
	        {
			cookie_data=cookie_data.substring (name_offset+1+name.length,cookie_data.length);
			cookie_hash=cookie_data.substring (0, cookie_data.indexOf ('\\'));
			cookie_data=cookie_data.substring (cookie_data.indexOf ('\\')+1,cookie_data.length);
			cookie_custom=cookie_data.substring (0, cookie_data.indexOf ('|'));
	                return true;            // name is already stored
	        }
	        else
	                return false;           // name not found in cookie
	}
	else
		return false;
}

function addToCookie (cookie_name, name, cookie_hash, cookie_custom)
{
	name=escape (name);
	var cookie_data=takeCookie (cookie_name);
        var name_offset=cookie_data.indexOf (name+'|');
	var updated_cookie=cookie_data;

        if (name_offset != -1)
        {
		var updated_cookie=cookie_data;
		var offset=1+name.length+name_offset;
		cookie_data=cookie_data.substring (offset,cookie_data.length);
		var end=offset+cookie_data.indexOf ('|');

		updated_cookie=updated_cookie.substring (0, name_offset)+
		 updated_cookie.substring (end+1, updated_cookie.length);
	}

	updated_cookie+=name+'|'+cookie_hash+'\\'+cookie_custom+'|';
	jarCookie (cookie_name, updated_cookie, 30);
}
