/**
 * @library BeaPortalCookieManager
 *
 * This JavaScript library implements a simple cookie manager.
 *
 * All comments in this file may be removed when optimizing for production.
 */

/**
 * Creates a new BeaPortalCookieManager instance.
 *
 * @constructor
 * @access public
 *
 * @param (useIntelligentCookies)
 *      Set this to false to avoid the overhead associated with checking if the browser has cookies enabled;
 *      defaults to true
 * @param (initMasterCookie)
 *      Initializes the master cookie upon construction of this instance;
 *      defaults to false
 */
function BeaPortalCookieManager(useIntelligentCookies, initMasterCookie)
{
    // "Public" methods
    this.setCookie = beaPortalCookieManagerSetCookie;
    this.getCookie = beaPortalCookieManagerGetCookie;
    this.deleteCookie = beaPortalCookieManagerDeleteCookie;
    this.setMasterCookie = beaPortalCookieManagerSetMasterCookie;
    this.getMasterCookie = beaPortalCookieManagerGetMasterCookie;
    this.deleteMasterCookie = beaPortalCookieManagerDeleteMasterCookie;

    // "Private" member variables
    this.useIntelligentCookies = (useIntelligentCookies ? useIntelligentCookies : true);

    // Initialization
    if (initMasterCookie)
    {
        this.setMasterCookie();
    }
}

/**
 * Sets a cookie (if possible) given the specified configuration.  Because a browser may have cookies disabled,
 * it may be a good idea to double check that the cookie was in fact set using {@link #getCookie}.
 *
 * @method BeaPortalCookieManager.setCookie
 * @access public
 *
 * @param name
 *      The name of the cookie being set
 * @param value
 *      The cookie's value
 * @param (expires)
 *      The cookie's expiration date;
 *      defaults to the end of the current browser session
 * @param (path)
 *      The request path for which the cookie is valid;
 *      defaults to the path of the originating document
 * @param (domain)
 *      The domain for which the cookie is valid;
 *      defaults to the domain of the originating document
 * @param (secure)
 *      A boolean indicating whether or not the cookie's transmission must be secure;
 *      defaults to false
 */
function beaPortalCookieManagerSetCookie(name, value, expires, path, domain, secure)
{
    // If intelligent cookies are disabled or if the master cookie exists, then write the new cookie
    if (!this.useIntelligentCookies || this.getMasterCookie() == "true")
    {
        var cookie = this.getCookie(name);

        // If the cookie doesn't exist or the value has changed, write it out
        if (!cookie || cookie != value)
        {
            var cookie
                = name + "=" + escape(value)
                + (expires ? "; expires=" + expires.toGMTString() : "")
                + (path ? "; path=" + path : "")
                + (domain ? "; domain=" + domain : "")
                + (secure ? "; secure" : "");
            document.cookie = cookie;

            if (this.useIntelligentCookies)
            {
                cookie = this.getCookie(name);

                // If the cookie didn't get written (i.e. the borwser is not accepting them), delete the master cookie
                // to prevent future write attempts; this rare case could occur if the user disabled cookies mid-session
                if (!cookie || cookie != value)
                {
                    this.deleteMasterCookie();
                }
            }
        }
    }
}

/**
 * Gets a cookie's value (if possible) with the given name, or null if the cookie does not exist.
 *
 * @method BeaPortalCookieManager.getCookie
 * @access public
 *
 * @param name
 *      The name of the cookie being fetched
 */
function beaPortalCookieManagerGetCookie(name)
{
    var cookie = document.cookie;
    name = name + "=";
    var length = cookie.length;
    var nameBegin = 0;

    while (nameBegin < length)
    {
        var valueBegin = nameBegin + name.length;

        if (cookie.substring(nameBegin, valueBegin) == name)
        {
            var valueEnd = cookie.indexOf (";", valueBegin);

            if (valueEnd == -1)
            {
                valueEnd = length;
            }

            return unescape(cookie.substring(valueBegin, valueEnd));
        }

        nameBegin = cookie.indexOf(" ", nameBegin) + 1;

        if (nameBegin == 0)
        {
            break;
        }
    }

    return null;
}

/**
 * Deletes a cookie (if possible) with the given name.
 *
 * @method BeaPortalCookieManager.deleteCookie
 * @access public
 *
 * @param name
 *      The name of the cookie being deleted
 * @param (path)
 *      The request path for which the cookie is valid; must be same as the path used to create cookie;
 *      defaults to the path of the originating document
 * @param (domain)
 *      The domain for which the cookie is valid; must be same as the domain used to create cookie;
 *      defaults to the domain of the originating document
 */
function beaPortalCookieManagerDeleteCookie(name, path, domain)
{
    if (!this.useIntelligentCookies || this.getMasterCookie() == "true")
    {
        if (this.getCookie(name))
        {
            document.cookie = name + "="
                            + (path ? "; path=" + path : "")
                            + (domain ? "; domain=" + domain : "")
                            + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    }
}

/**
 * Sets the master cookie (if possible).
 *
 * @method BeaPortalCookieManager.setMasterCookie
 * @access public
 */
function beaPortalCookieManagerSetMasterCookie()
{
    if (this.useIntelligentCookies)
    {
        if (this.getMasterCookie() != "true")
        {
            document.cookie = "beaPortalMasterCookie=true; path=/";
        }
    }
}

/**
 * Gets the master cookie (if possible).
 *
 * @method BeaPortalCookieManager.getMasterCookie
 * @access public
 */
function beaPortalCookieManagerGetMasterCookie()
{
    return this.getCookie("beaPortalMasterCookie");
}

/**
 * Deletes the master cookie (if possible).
 *
 * @method BeaPortalCookieManager.deleteMasterCookie
 * @access public
 */
function beaPortalCookieManagerDeleteMasterCookie()
{
    if (this.useIntelligentCookies)
    {
        document.cookie = "beaPortalMasterCookie=; path=/; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

