/*
   SiteComponents version:
   6.6.4.1, tag SC_6_6_4_1, created Tue Oct 27 21:28:23 +0100 2009
   $Name: HEAD $
   
   Disclaimer
   
   While we make every effort to ensure that this code is fit for its intended
   purpose, we make no guarantees as to its functionality. CoreTrek AS will
   accept no responsibility for the loss of data or any other damage or
   financial loss caused by use of this code.
   
   Copyright
   
   This programming code is copyright of CoreTrek AS. Permission to run this
   code is given to approved users of CoreTrek's publishing system CorePublish.
   
   This source code may not be copied, modified or otherwise repurposed for use
   by a third party without the written permission of CoreTrek AS.
   
   Contact webmaster@coretrek.com for information.
  
*/

/*

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype.
    ============================================================================
    
    CtCookie
    
*/    
var CtCookie = Class.create({

    /**
     * Set a named value in cookie storage. It is also possible to set how
     * long the cookie will be valid by providing the expireInDays parameter.
     *
     * @param name string
     * @param value string The value to store
     * @param expireInDays int Number of days the cookie will be available
     */
    set: function(name, value, expireInDays) {
        if(expireInDays) {
            var date = new Date();
            date.setTime(date.getTime() + (expireInDays*24*60*60*1000));
            var expires = "; expires=" + date.toGMTString();
        } else {
           expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    
    /**
     * Get a named value from cookie storage. If the value is not set, null
     * is returned.
     *
     * @param name string
     */  
    get: function(name) {
        nameRe = new RegExp("^\\s?" + name + "=.*");
        var cookieVars = document.cookie.split(';')
           .grep(nameRe)
           .collect(function(item) {
               return item.strip();
           });
           
        if(cookieVars.size() > 0) {
           return cookieVars.first().substring(name.length + 1);
        } else {
           return null;
        }
    },
    
    /**
     * Clear a value from session storage.
     *
     * @param name string
     */    
    clear: function(name) {
        var mydate = new Date();
        mydate.setTime(mydate.getTime() - 1);
        document.cookie = name + "=; expires=" + mydate.toGMTString();
    }

});

