/*
jbe_cookies.js

Purpose:  To provide an easy way to read/write cookies and store data inside cookies

Dependencies
- none

To Do
- None at this time

Revision History
Keys: [NEW] New Feature
      [FIX] Bug fix
      [COM] Comment
      [IMP] Improvement

Feb 3, 2003
[FIX] cookie_get_var() and cookie_set_var().   Forced sVarName to string type
	  with .toString().  Did not work if sVarName had value 0.

1.21 April 27, 2001
[NEW] set_cookie() now requires a fourth paramter, sCookiePath
      because of this, all the functions that use this function now require
      an additional parameter as well.
[FIX] Overhaul of all variable names to follow JS coding guidelines
[COM] Incompatible with version 1.20

1.20 April 2, 2001
[NEW] Added cookie_set_var(sCookieName, iExpireInDays, sVarName, sVarValue)
[NEW] Added cookie_get_var(sCookieName, sVarName)

1.10
[NEW] Added cookie_counter(sCookieName, iExpireInDays, sOperator, sCookieValue)
[FIX] get_cookie() now returns a blank string instead of undefined if cookie not found
*/

function set_cookie(sCookieName, sCookieValue, iExpireInDays, sCookiePath) {
	var oExpire = new Date();
	(iExpireInDays == 0) ? oExpire = null : oExpire.setTime(oExpire.getTime() + (60*60*24*iExpireInDays*1000));
	document.cookie = sCookieName + '=' + escape(sCookieValue) + ((oExpire == null) ? '' : '; expires=' + oExpire.toGMTString()) + '; path=' + sCookiePath;
}

function get_cookie(sCookieName) {
	var sSearch = sCookieName + '=', sReturnValue
	if (document.cookie.length > 0) { // if there are any cookies
		iOffset = document.cookie.indexOf(sSearch) 
		if (iOffset != -1) { // if cookie exists 
			iOffset += sSearch.length 
			// set index of beginning of sCookieValue
			end = document.cookie.indexOf(';', iOffset) 

			// set index of end of cookie sCookieValue
			if (end == -1) {
			   end = document.cookie.length
			}
			sReturnValue = unescape(document.cookie.substring(iOffset, end))
		} 
	}
	if (!sReturnValue) sReturnValue = '';
	return unescape(sReturnValue);
}

function parse_cookie(sCookieName, sDelimiter, sCrumbs) {
	var aCrumbs = sCrumbs.split(sDelimiter);
	return aCrumbs;
}

function parse_get_cookie(sCookieName, sDelimiter) {
	var sCrumbs = get_cookie(sCookieName);
	var aCrumbs = sCrumbs.split(sDelimiter);
	return aCrumbs;
}

function cookie_counter(sCookieName, iExpireInDays, sCookiePath, sOperator, sOperand) {
	var iValue = get_cookie(sCookieName);
	(!iValue) ? iValue = 0 : iValue = parseInt(iValue, 10);
	set_cookie(sCookieName, eval('iValue' + sOperator + sOperand), iExpireInDays, sCookiePath);
}

// set a "variable" in the cookie string, if sVarValue is empty then it will delete the variable
function cookie_set_var(sCookieName, iExpireInDays, sCookiePath, sVarName, sVarValue) {
//alert("sVarValue= " + sVarValue);
	sVarName = sVarName.toString();  //defensive programming
	if (!sVarName) {
		return false;
	}
	var sCrumbs = get_cookie(sCookieName), sNewCrumb = sVarName + ':' + sVarValue, sDelimiter = '|';
//alert("sCrumbs= " + sCrumbs);
//alert("sNewCrumb= " + sNewCrumb);
	if (sCrumbs == '') {
//alert("creating new tracker");
		// no previous tracker, create one now
		if (sVarValue) {
			set_cookie(sCookieName, sDelimiter + sNewCrumb + sDelimiter, iExpireInDays, sCookiePath);
		}
	}
	else {
		var iPos = sCrumbs.indexOf(sDelimiter + sVarName + ':');  //Including sDelimiter and ':' in the search ensures matching sVarName in its entirety, otherwise a mismatch will occur if sVarName is a substring of an existing crumb.  Adding one to iPos will ensure we don't overwrite sDelimiter when putting the cookie back together
//alert("ipos= " + iPos + "& sCrumbs= " + sCrumbs);
		if (iPos == -1) {
//alert("crumb doesn't exist");
			// crumb doesn't exist
			if (sVarValue) {
				sCrumbs += sNewCrumb + sDelimiter;
				set_cookie(sCookieName, sCrumbs, iExpireInDays, sCookiePath);
			}
		}
		else {
//alert("crumb exist");
			// crumb exist
			(sVarValue) ? sNewCrumb += sDelimiter : sNewCrumb = '';
			sCrumbs = sCrumbs.substring(0,iPos+1) + sNewCrumb + sCrumbs.substring(sCrumbs.indexOf(sDelimiter, iPos+1)+1);  //The first substring function goes to iPos+1 because we searched for the var INCLUDING the leading sDelimiter (iPos is pointing to the delimiter and not the beginning of the var)
			set_cookie(sCookieName, sCrumbs, iExpireInDays, sCookiePath);
		}
	}
	return true;
}

// get the sCookieValue of a "variable"
function cookie_get_var(sCookieName, sVarName) {
	sVarName = sVarName.toString();  //defensive programming
	var i, aCrumbs = parse_get_cookie(sCookieName,'|'), sCrumb;
	for (i = 0; i < aCrumbs.length; i++) {
		sCrumb = aCrumbs[i].split(':');
		if (sCrumb[0] == sVarName) {
			// crumb found
			return sCrumb[1];
		}
	}
	// looped through the crumbs, crumb not found, return an empty string
	return '';
}

