//******************************************************************************
// cookiemanager.js - Cookie Manager             (C) 2005-2007, Matthias Altmann
//******************************************************************************
// Notes:
// - Cookie names may be duplicate (same name for differing path and value!)
//------------------------------------------------------------------------------
// History:
// 2007-12-12 	Initial version
//******************************************************************************

var cookiemanager_debug=0;

// ===== OBJECT COOKIE ========================================================
// This object manages a single cookie as name and value
// -----------------------------------------------------------------------------
function COOKIE (pName, pValue) 
{
	this.Name 		= pName;
	this.Value		= pValue; 
	this.toString 	= function () 
	{
		var s =	this.Name+' => '+this.Value;
		return s;
	}
}
// ===== OBJECT COOKIEMANAGER ========================================================
function COOKIEMANAGER () 
{
	//--------------------------------------------------------------------------
	// debug() 
	// emit debugging information if debugging switched on
	//--------------------------------------------------------------------------
	this.debug = function(pMsg)
	{
		if (cookiemanager_debug==1) {document.writeln('<pre>'+pMsg+'<\/pre>');}
	}
	//--------------------------------------------------------------------------
	// listCookies() 
	// returns a string containing all available cookies formatted as name=value
	//--------------------------------------------------------------------------
	this.listCookies = function()
	{
		var result = this.parseCookies();
		this.debug('[listCookies] result: \n'+result);
		return result;
	}
	//--------------------------------------------------------------------------
	// parseCookies() 
	// parse current cookies and return an array
	//--------------------------------------------------------------------------
	this.parseCookies = function()
	{
		var aResult = new Array();
		this.debug('[parseCookies] document.cookie: '+document.cookie);
		var aCookies=document.cookie.split('; ');
		this.debug('[parseCookies] aCookies: '+aCookies);
		var reNameValue=/^([^=]+)=(.*)/; 
		for(i=0;i<aCookies.length;i++) 
		{	
			var sCookie = aCookies[i].toString();
			this.debug('[parseCookies] sCookie['+i+']: '+sCookie);
			var aNameValue=reNameValue.exec(sCookie);
			this.debug('[parseCookies] aNameValue: '+aNameValue);
			this.debug('[parseCookies] Name: '+aNameValue[1]+' Value: '+aNameValue[2]);
			if (aNameValue) 
			{
				oCookie = new COOKIE(aNameValue[1], aNameValue[2]);
				aResult.push(oCookie);
			}
		}
		this.debug('[parseCookies] Result: \n'+aResult.toString());
		return aResult;
	}

	//--------------------------------------------------------------------------
	// setCookie (name, value, days) 
	// sets a cookie
	//--------------------------------------------------------------------------
	this.setCookie = function(pName, pValue, pDays, pPath)   
	{   
		// --- set expiration
		var sExpires = ''; 
		if(!isNaN(pDays)) // if pDays is set to a number  
		{   
			var dDate = new Date();   
			dDate.setTime(dDate.getTime() + pDays * 24 * 60 * 60 * 1000);   
			sExpires = '; expires=' + dDate.toGMTString();   
		}   
		// --- set path
		var sPath = '';
		if (pPath!=null) 
		{
			sPath	 = '; path='+pPath; 
		}
		
		// --- build
		var sCookie = escape(pName) + '=' + escape(pValue) + sExpires + sPath; 
		this.debug('[setCookie] final cookie: '+sCookie);
		document.cookie = sCookie;   
	}   
	
	//--------------------------------------------------------------------------
	// getCookie(name) 
	// retrieves the first cookie with a given name
	//--------------------------------------------------------------------------
	this.getCookie = function(pName)   
	{   
		var result=this.parseCookies();
		for(var i in result)
		{
			var oCOOKIE = result[i];
			this.debug('[getCookie] ('+i+') Name: '+oCOOKIE.Name+' Value: '+oCOOKIE.Value);	
			if (oCOOKIE.Name == pName) 
			{
				return oCOOKIE.Value;
			}
		}
		return null;
	}
	//--------------------------------------------------------------------------
	// getCookies(name) 
	// retrieves all cookies with a given name
	//--------------------------------------------------------------------------
	this.getCookies = function(pName)   
	{   
		var result=this.parseCookies();
		var retval=new Array();
		for(var i in result)
		{
			var oCOOKIE = result[i];
			this.debug('[getCookie] ('+i+') Name: '+oCOOKIE.Name+' Value: '+oCOOKIE.Value);	
			if (oCOOKIE.Name == pName) 
			{
				retval.push(oCOOKIE.value);
			}
		}
		return retval;
	}
	
	//--------------------------------------------------------------------------
	// COOKIES.deleteCookie(name,path) 
	// deletes a cookie for a given path
	//--------------------------------------------------------------------------
	this.deleteCookie = function(pName,pPath)   
	{
		this.debug('[deleteCookie] Name: '+pName+' Path: '+pPath);	
		this.setCookie(pName, '', -1, pPath);   
	}   
	//--------------------------------------------------------------------------
	// COOKIES.deleteCookies(name) 
	// deletes all cookies with a given name for all paths
	//--------------------------------------------------------------------------
	this.deleteCookies = function(pName)   
	{
		var aPaths = window.location.pathname.split('/');
		this.debug('[deleteCookies] Current Path: '+aPaths);	

		var sPath = '/';
		this.debug('[deleteCookies] ('+i+') Name: '+pName+' Path: '+sPath);	
		this.setCookie(pName, '', -1, sPath);   
		for(var i=1;i<aPaths.length-1;i++)
		{
			sPath += aPaths[i];
			this.debug('[deleteCookies] ('+i+') Name: '+pName+' Path: '+sPath);	
			this.setCookie(pName, '', -1, sPath);   
			sPath += '/'
		}
	}   
}   



cookiemanager = new COOKIEMANAGER();
