//==================================================================
//	gccCookie JavaScript Object
//	Usage:	var myCookie = new gccCookie();
//	
//	Description: This JavaScript Object adds functionality to cookies.
//				 By grouping cookies on a single line, the gccCookie 
//				 object can get rid of the 20 cookies per page limit.
//
//	variables:
//			none
//	methods:
//	2002/10/02 - Created by Patrice Thibeault.
//	2002/10/07 - Modified by Patrice Thibeault.
//					- Added obsolete cookies and obsolete groups 
//					  concepts.	
//  2003/06/19 - Modified by Patrice Thibeault.
//					- Added the 'remove' function.
//==================================================================//
function gccCookie()
{
	this.setCookie		= _setCookie;	
	this.getCookie		= _getCookie;
	this.setPath		= _setPath;
	this.getPath		= _getPath;
	this.getCount		= _getCount;
	this.getExpireDate	= _getExpireDate;
	this.setGroupName   = _setGroupName;
	this.getGroupName	= _getGroupName;
	this.toString		= _toString;
	this.remove			= _remove;
	this.setObsoleteCookieList = _setObsoleteCookieList;
	this.getObsoleteCookieList = _getObsoleteCookieList;
	this.setObsoleteGroupList  = _setObsoleteGroupList;
	this.getObsoleteGroupList  = _getObsoleteGroupList;

	//------------------------------------
	// Those variables are protected
	//------------------------------------
	var m_strPath		= "";
	var m_strGroupName	= "undefined";		
	var m_dtExpire		= new Date();
	var m_arrayObsoleteCookies = null;
	var m_arrayObsoleteGroups  = null;
			
	m_dtExpire.setYear(m_dtExpire.getYear()+1); // Keep the cookie for a one year period.
	
	//------------------------------------
	// Those functions are public
	//------------------------------------
	function _setCookie(strVariableName,strVariableValue)
	{
		var strGroup = new String(getGroup());
		if (strGroup == "null")
			strGroup = "";	
		var arrayVariables = strGroup.split(";;");
		var bVariableSet = false;
		for (var i=0; i < arrayVariables.length; i++)
		{			
			var iPos = arrayVariables[i].indexOf("=");
			if (iPos != -1)
			{
				var strCurrentVariableName = arrayVariables[i].substr(0,iPos);
				if (strCurrentVariableName == strVariableName)
				{
					arrayVariables[i] = strVariableName+"="+pack(new String(strVariableValue));
					bVariableSet = true;
					break;
				}
			}
		}
		if (!bVariableSet)
			arrayVariables[arrayVariables.length]=	strVariableName+"="+pack(new String(strVariableValue));
		strGroup = arrayVariables.join(";;");
		
		//------------------------------------
		// TODO: Setting the path 
		//------------------------------------
		document.cookie = m_strGroupName + "=" + escape(strGroup) +"; expires="+m_dtExpire.toGMTString() + "; path="+m_strPath+";";
	}

	function _getCookie(strVariableName)
	{
		var strGroup = new String(getGroup());
		if (strGroup == "null")
			strGroup = "";
		var arrayVariables = strGroup.split(";;");

		for (var i=0; i < arrayVariables.length; i++)
		{			
			var iPos = arrayVariables[i].indexOf("=");
			if (iPos != -1)
			{
				var strCurrentVariableName = arrayVariables[i].substr(0,iPos);
				if (strCurrentVariableName == strVariableName)
					return unpack(arrayVariables[i].substr(iPos+1));
			}
		}
		return "undefined";
	}
	function _getCount()		{return 0;				}
	function _getPath()			{return m_strPath;		}	
	function _getExpireDate()	{return m_dtExpire;		}
	function _getGroupName()	{return m_strGroupName;	}
	function _setGroupName(strNewName)	{m_strGroupName	= strNewName;}
	function _setPath(strNewPath)		{m_strPath		= strNewPath;}
	function _remove()
	{
		var arrayCookies = document.cookie.split("; ");
		for (var i=0; i < arrayCookies.length; i++)
		{		
			var strGroup = arrayCookies[i].split("=");
			deleteGroup(strGroup[0]);
		}
		return null;
	}
	function _toString()
	{	
		var strResult = "<PRE>";
		strResult = strResult + "\nCookie length = "+document.cookie.length+"\n";		
		
		var arrayCookies = document.cookie.split("; ");
		strResult = strResult + "Number of groups = "+arrayCookies.length+"\n\n";
		
		for (var i=0; i < arrayCookies.length; i++)
		{		
			var iPos = arrayCookies[i].indexOf("=");
			var strGroup = arrayCookies[i].substr(0,iPos);
			strResult = strResult + "\n"+strGroup + " : ";
			var strValue = unescape(arrayCookies[i].substr(iPos+1));
			if (strValue.indexOf(";;")!=-1)
			{
				var arrayVariables = strValue.split(";;");

				for (var j=0; j < arrayVariables.length; j++)
				{			
					var iPos = arrayVariables[j].indexOf("=");
					if (iPos != -1)
					{
						var strCurrentVariableName = arrayVariables[j].substr(0,iPos);
						strResult = strResult + "\n      "+strCurrentVariableName + " : "+ unpack(arrayVariables[j].substr(iPos+1));
					}
				}
			}
			else
				strResult = strResult + strValue;
		}
				
		strResult = strResult + "\n\n\nAvailable length = "+(4096-document.cookie.length)+"\n";
		strResult = strResult + "Available groups = "+(20-arrayCookies.length)+"\n";

		strResult = strResult + "</PRE>";
		return strResult;
	}
	function _setObsoleteCookieList(arrayObsoleteCookies)
	{
		m_arrayObsoleteCookies = arrayObsoleteCookies; 
		var strGroup = new String(getGroup());
		if (strGroup == "null")
			strGroup = "";	
		var arrayVariables = strGroup.split(";;");		
		for (var i=0; i < arrayVariables.length; i++)
		{			
			var iPos = arrayVariables[i].indexOf("=");
			if (iPos != -1)
			{
				var strCurrentVariableName = arrayVariables[i].substr(0,iPos);
				for (var j=0; j < m_arrayObsoleteCookies.length; j++)
				{				
					if (strCurrentVariableName == m_arrayObsoleteCookies[j])
					{
						delete arrayVariables[i];
						arrayVariables = arrayVariables.slice(0, i).concat(arrayVariables.slice(i+1));
						i--
						break;
					}
				}
			}
		}		
		strGroup = arrayVariables.join(";;");
		//------------------------------------
		// TODO: Setting the path
		//------------------------------------
		document.cookie = m_strGroupName + "=" + escape(strGroup) +"; expires="+m_dtExpire.toGMTString() + "; path="+m_strPath+";";
	}
	function _setObsoleteGroupList (arrayObsoleteGroups )
	{
		m_arrayObsoleteGroups  = arrayObsoleteGroups;
		for (var i = 0; i < m_arrayObsoleteGroups.length; i++)
			deleteGroup(m_arrayObsoleteGroups[i]);
	}
	function _getObsoleteCookieList(){return m_arrayObsoleteCookies};
	function _getObsoleteGroupList() {return m_arrayObsoleteGroups };

	//------------------------------------
	// Those functions are protected
	//------------------------------------
	function getGroup(strGroupName)
	{
		var arrayCookies = document.cookie.split("; ");
		for (var i=0; i < arrayCookies.length; i++)
		{		
			var strGroup = arrayCookies[i].split("=");
			if (typeof(strGroupName) != "string")
			{
				if (m_strGroupName == strGroup[0])
				{
					if (strGroup[1]!="OBSOLETE")
						return unescape(strGroup[1]);
				}
			}
			else
			{
				if (strGroupName == strGroup[0]) 
				{
					if (strGroup[1]!="OBSOLETE")
						return unescape(strGroup[1]);
				}
			}
		}
		return null;
	}
	function deleteGroup(strGroupName) 
	{
		if (getGroup(strGroupName))
			document.cookie = strGroupName + "=" +"; expires="+(new Date(0)).toGMTString() + "; path="+m_strPath+";";		
	}
	function pack(strPack)	{return strPack.replace(/;/g,";.");		}
	function unpack(strPack){return strPack.replace(/;[.]/g,";");	}
	
}	


