/**
 * This javascript class is used to control the document cookie.
 *
 * Synopsis:
 * var cookie = new Cookie('my_cookie' , 1000*60*60*24*2);
 * cook0.set('my_value');
 * cook0.get('my_default_value');
 *
 * $Rayge: rayge/js/cookie.js,v 1.2 2005/12/14 15:57:22 fhelly Exp $
 * $Id: Cookie.js 16 2006-02-10 09:59:31Z François Helly $
 *
 * $Revision: 54 $
 * $Date: 2006-04-07 15:24:11 +0200 (Fri, 07 Apr 2006) $
 * $Author: $
 *
 * Copyright 2005-2006 Francois Helly <fhelly@bebop-design.net>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @version $Revision: 54 $
 * @package rayge
 */

var cookies     = new Array();

/**
 * Constructs the variable object and sets its attributes.
 *
 * @param string id       The variable name
 * @param integer life    The life time of the cookie containing the variable
 * @param string domain   The cookie domain to set
 *                        default to '' (no domain)
 * @param string path     The cookie path to set,
 *                        default to '/' (web root path)
 * @param boolean secure  The cookie secure flag,
 *                        default to false
 */
function Cookie(id, life, domain, path, secure)
{
    this.id     = id;
    this.life   = life;
    this.domain = (typeof(domain) != 'undefined') ? domain : '';
    this.path   = (typeof(path) != 'undefined') ? path : '/';
    this.secure = (typeof(secure) != 'undefined') ? true : false;

    this.set = function(value)
    {
        var now = new Date();
        var expire = new Date();
        var cookieValue = this.id + '=' + escape(value) + ';';
        var life = (value != '') ? now.getTime() + this.life : now.getTime() - this.life;
        expire.setTime(life);
        cookieValue += 'expires=' + expire.toGMTString() + ';';
        cookieValue += 'path=' + this.path + ';';
        if (this.domain != '') {
            cookieValue += 'domain=' + this.domain + ';';
        }
        if (this.secure == true) {
            cookieValue += 'secure;';
        }
        document.cookie = cookieValue;
    }

    this.get = function(deflt)
    {
        var dc = document.cookie;
        var begin = dc.indexOf('; ' + this.id);
        if (begin == -1) {
            begin = dc.indexOf(this.id);
            if (begin != 0) {
                return deflt;
            }
        } else {
            begin += 2;
        }
        var end = dc.indexOf(';', begin);
        if (end == -1) {
            end = dc.length;
        }
        var value = dc.substring(begin + this.id.length +1, end);
        if (value == ';') {
            value = '';
        }
        return unescape(value);
    }

    this.isenabled = function()
    {
        return (navigator.cookieEnabled);
    }


}
