// Provides a simple interface for creating, retrieving and clearing cookies.
if(typeof net255 == "undefined") var net255 = new Object();
net255.Cookie = function(life, userData) {
    this.isIE = 
        (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.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);

	this.cookieShelfLife = (life) ? life : 365;
	this.userDataForIE = (userData) ? userData : false;

	// 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.isIE && this.userDataForIE) {
		this.IECacheName = "storage";
		if (document.getElementById(this.IECacheName) == null) {
			var div = document.createElement("DIV");
			div.id = this.IECacheName;
			document.body.appendChild(div);
		}
		this.store = document.getElementById(this.IECacheName);
		this.store.style.behavior = "url('#default#userData')";
	}
}
net255.Cookie.prototype = {	
	// Returns the value of a cookie with the given name, or null if no such cookie exists.
    getCookie: function(aCookieName) {
        var result = null;
        if (this.isIE && this.userDataForIE) {
            this.store.load(this.IECacheName);
            result = this.store.getAttribute(aCookieName);
        } else {
            for (var i = 0; i < document.cookie.split('; ').length; i++) {
                var crumb = document.cookie.split('; ')[i].split('=');
                if (crumb[0] == aCookieName && crumb[1] != null) {
                    result = crumb[1];
                    break;
                }
            }
        }

        if (this.BROWSER_IS_OPERA && result != null) {
            result = result.replace(/%22/g, '"');
        }
        return result;
    },

	// Sets a cookie with the given name and value.
    setCookie: function(aCookieName, aCookieValue) {
        if (this.isIE && this.userDataForIE) {
            this.store.setAttribute(aCookieName, aCookieValue);
            this.store.save(this.IECacheName);
        } else {
            if (this.isOpera) {
                aCookieValue = aCookieValue.replace(/"/g, "%22");
            }
            var date = new Date();
            date.setTime(date.getTime() + (this.cookieShelfLife * 24*60*60*1000));
            var expires = '; expires=' + date.toGMTString();
            document.cookie = aCookieName + '=' + aCookieValue + expires + '; path=/';
        }
    },

    // Clears the cookie with the given name.
    clearCookie: function(aCookieName) {
        if (this.isIE && this.userDataForIE) {
            this.store.load(this.IECacheName);
            this.store.removeAttribute(aCookieName);
            this.store.save(this.IECacheName);
        } else {
            document.cookie = aCookieName + '=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
        }
    }
}
var Cookie = net255.Cookie;

/*

cookie.js usage:

var c = new Cookie();
var useFlash = c.getCookie("useFlash");
c.setCookie("useFlash", "no"); // set cookie
c.clearCookie("useFlash"); // delete cookie
alert(useFlash);

*/