/**
 * JavaScript cookie management.
 *
 * @author Christian Hansen <christian@resource-it.dk>
 * @version 1.0
 * @package Resource it Modules
 * @copyright Resource it ApS
 *
 **/

    /**
     * The Cookie constructor
     * @param string name - the name for the cookie
     * @param string value - the value of the cookie
     * @param string expire [optional] - the date of expiration - in the format of Date.toGMTString();
     * @param string path [optional] - the path in which the cookie may be read.
     * @param string domain [optional] - the cookie domain
     * @param boolean secure [optional] - if true the cookie is secure.
     * @access public
     * @return void
     **/
    function Cookie(name,value,expires,path,domain,secure) {

        //this should make use of the error object in the near by future
        if(arguments.length < 1) alert("@param string name should be specified");

        for ( var c = 0; c < arguments.length; c++ ) 
            this[["name","value","expires","path","domain","secure"][c]] = arguments[c];

        if ( this.value ) this.setValue( this.value );

    }//Cookie


    /**
     * get value of the cookie
     * @access public
     * @return string|boolean - the value of the cookie or false if none was found.
     **/
    Cookie.prototype.getValue = function () {

        var cookieArray = document.cookie.split(this.name+"=");
        if(cookieArray.length > 1) {
            return unescape(cookieArray[1].split(";")[0]);
        } else {
            return false;
        }//else

    }//getCookie


    /**
     * set or update a cookie
     * @param string value - the value of the cookie
     * @access public
     * @return boolean - true on success, false on failure
     **/
    Cookie.prototype.setValue = function ( value ) {

        var cookie = this.name+"="+escape(value);

        for ( var k in {expires:0,path:0,domain:0,secure:0} )
            if ( this[k] ) cookie += ";" + k + "=" + this[k];

        document.cookie = cookie;

        if ( this.getValue() ) return true;
        else return false;

    }//setValue


    /**
     * delete the cookie
     * @return boolean - true on success false on failure
     * @access public
     * @notice even though the cookie is removed by invoking this method the object remains - calling the setValue method will re create the cookie.
     **/
    Cookie.prototype.remove = function ( ) {

        var expire = new Date(1970,1,1);
        document.cookie = this.name + "=" + 0 + "; expires=" + expire.toGMTString();

        if ( this.getValue() ) return false;
        else return true;

    }//deleteCookie