/**********************************************************************************
	xCookie	namespace
					Provides some basic javascript methods to setting and getting the cookies and crumbs.
					Crumbs are name value pairs within a cookie.  generally the crumbs are delimited by the
					pipe delimiter and name value pairs delimited by #
	Author:		W.Sietz		Feb 2006

**********************************************************************************/
var xCookie = {

	//SET
	set: function(CookieName, CookieValue, ExpireDate, Path, Domain, Secure)	 {
		if (!ExpireDate) {
			ExpireDate = new Date ();		//13/06/2003 00:00:00
			ExpireDate.setTime(ExpireDate.getTime() + ((1) * 24 * 60 * 60 * 1000));
			}
		var strCookie = 
			(CookieName+'='+ (CookieValue && CookieValue!='' ? escape(CookieValue) : '') +'; ') +
			((ExpireDate) ? 'expires='+ExpireDate.toGMTString()+'; ' : '') + 
			(((Path)&&(Path!='')) ? 'path='+Path+'; ' : '') + 
			(((Domain)&&(Domain!='')) ? 'domain='+Domain+'; ' : '') +
			((Secure) ? 'secure ' : '') ;
		document.cookie=strCookie;
		},


	//SETCRUMB
	setCrumb: function(CookieName, CrumbName, CrumbValue, ExpireDate, Path, Domain, Secure)	 {
		var strCookieValue = this.get(CookieName);
		if (!CrumbName || CrumbName=='')		return;
		if (!strCookieValue || strCookieValue=='')	{
			strCookieValue = CrumbName+"#"+CrumbValue;
			this.set(CookieName, strCookieValue, ExpireDate, Path, Domain, Secure);
			return;
			}
		var Crumbs = strCookieValue.split('|');
		var fnd=false;
		var aCrumb;
		for (var j=0; j < Crumbs.length; j++)	 {
			var aCrumb = Crumbs[j].split('#');
			if (CrumbName == aCrumb[0])		{
				Crumbs[j] = CrumbName+"#"+escape(CrumbValue);
				fnd=true;
				}
			}
		if (fnd==false)	{
			Crumbs[Crumbs.length]=CrumbName+"#"+CrumbValue;
			}
		strCookieValue=Crumbs.join('|');
		this.set(CookieName, strCookieValue, ExpireDate, Path, Domain, Secure);
		},


	//GET			CookieName=CookieValue;    or	CookieName=Crumb|Crumb|Crumb;	where Crumb is  CrumbName#CrumbValue
	get: function(CookieName, CrumbName)	{
		// cookies are separated by semicolons
		if (!document || !document.cookie || document.cookie.length <= 0 || !CookieName || CookieName=='')	return null;
		var CookieNameLength = CookieName.length+1;
		var Cookies = document.cookie.split('; ');
		for (var i=0; i<Cookies.length; i++) {
			if (Cookies[i] == CookieName) return '';
			if (Cookies[i].substr(0,CookieNameLength) == CookieName+'=') {
				var CookieValue = Cookies[i].substr(CookieNameLength);
				if (!CrumbName || CrumbName=='')		return unescape(CookieValue);
				var Crumbs = CookieValue.split('|');
				for (var j=0; j < Crumbs.length; j++)	 {
					var aCrumb = Crumbs[j].split('#');
					if (CrumbName == aCrumb[0])		return unescape(aCrumb[1]);
					}
				}
			}
		return '';
		},


	//REMOVE
	remove: function(CookieName)	{
		if ( this.get(CookieName) )	{
			//document.cookie = CookieName + '=; path=' + ((!path) ? '/' : path) + '; expires=' + new Date(0).toGMTString();
			document.cookie = CookieName + '=; expires=Thu, 01-Jan-70 00:00:01 GMT';
			}
		},


	//TEST	Test for cookie support
	isSupported: function()	{
		var tf = false;
		//var savecookie = document.cookie;
		this.set('_%_','Y1');
		if ( 'Y1' == this.get('_%_') )	 {
			tf = true;
			this.remove('_%_');
			}
		return tf;
		},

	// DETERMINE IF COOKIES ARE ALLOWED
	isAllowed: function() {
		return ( navigator && navigator.cookieEnabled ? true:false );
		}

}


