function CookieManager(userData)
{
	this.BROWSER_IS_IE = (document.all && window.ActiveXObject && navigator.userAgent.toLowerCase().indexOf("msie") > -1 && navigator.userAgent.toLowerCase().indexOf("opera") == -1);

    // I hate navigator string based browser detection too, but when Opera alone
    // chokes on cookies containing double quotes...
	this.BROWSER_IS_OPERA = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
	
	this.userDataForIE = userData;

	// Internet Explorer has a cookie handling bug - if the *combined size*
	// of all cookies stored for a given domain is greater than 4096 bytes,
	// document.cookie will return an empty string. Until this is fixed, we
	// can fall back on IE's proprietary userData behaviour if necessary.
	if (this.BROWSER_IS_IE && this.userDataForIE)
	{
		this.IE_CACHE_NAME = "storage";

		if ($(this.IE_CACHE_NAME) == null)
		{
			var div = document.createElement("DIV");
			div.id = this.IE_CACHE_NAME;
			document.body.appendChild(div);
		}

		this.store = $(this.IE_CACHE_NAME);
		this.store.style.behavior = "url('#default#userData')";
	}
}

CookieManager.prototype.Get = function(cookieName)
{
	var result = null;

	if (this.BROWSER_IS_IE && this.userDataForIE)
	{
		this.store.load(this.IE_CACHE_NAME);
		result = this.store.getAttribute(cookieName);
	}
	else
	{
		for (var i = 0; i < document.cookie.split('; ').length; i++)
		{
			var crumb = document.cookie.split('; ')[i].split('=');

			if (crumb[0] == cookieName && crumb[1] != null)
			{
				result = crumb[1];
				break;
			}
		}
	}

	if (this.BROWSER_IS_OPERA && result != null)
	{
		result = result.replace(/%22/g, '"');
	}

	return result;
}


CookieManager.prototype.Set = function(cookieName, cookieValue, expireDays)
{
    if (this.BROWSER_IS_IE && this.userDataForIE)
    {
        this.store.setAttribute(cookieName, cookieValue);
        this.store.save(this.IE_CACHE_NAME);
    }
    else
    {
        if (this.BROWSER_IS_OPERA)
        {
            cookieValue = cookieValue.replace(/"/g, "%22");
        }

        if(expireDays > 0)
        {
			var date = new Date();
		    date.setTime(date.getTime() + (expireDays * 24*60*60*1000));
	        var expires = '; expires=' + date.toGMTString();

		    document.cookie = cookieName + '=' + cookieValue + expires + '; path=/';
		}
		else
		{
			document.cookie = cookieName + '=' + cookieValue + '; path=/';
		}
    }
}

CookieManager.prototype.Clear = function(cookieName)
{
	if (this.BROWSER_IS_IE && this.userDataForIE)
	{
		this.store.load(this.IE_CACHE_NAME);
		this.store.removeAttribute(cookieName);
		this.store.save(this.IE_CACHE_NAME);
	}
	else
	{
		document.cookie = cookieName + '=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
	}
}

document.CookieManager = new CookieManager();