WOL.util.Cookies = {
   /**
    * Deletes a specific named value from the cookie.
    * 
    * @type void
    * @param {String} key The name of the value to delete.
    */
   deleteCookie: function(key) {
      var cookieObj = WOL.util.Cookies.parseCookies();
      if(cookieObj[key]) {
         /* Only write the cookie again if the named value
            existed in the cookie to begin with. */
         var lastYear = new Date();
         lastYear.setFullYear(lastYear.getFullYear() - 1);
         WOL.util.Cookies.setCookie(key, 'null', lastYear);
      };
   },

   /**
    * Parses the cookies sent with the request and returns them as an
    * object, to allow hash-table-style access to cookie data.
    *
    * @type Object
    */
   parseCookies: function() {
      var cookieObj = {};
      if(document.cookie) {
         var cookies = document.cookie.split('; ');
         for(var i=0, max=cookies.length;i<max; i++) {
            var cookiePair = cookies[i].split('=');
	    if(cookiePair[0] != 'expires') {
               cookieObj[cookiePair[0]] = cookiePair[1];
	    };
         };
      };
      return cookieObj;
   },
   
   /**
    * List of reserved names for cookie values which have special
    * functions and shouldn't ever be set manually.
    *
    * @type Array
    */
   reservedNames: ['expires', 'path', 'secure'],
   
   /**
    * Given a key and a value, stores the value in a cookie under the given
    * key.
    *
    * @type void
    * @param {String} key The key to use.
    * @param {String} value The value to store.
    * @param {Date} expires A date for the cookie to expire; will be one year
    * if not supplied.
    */
   setCookie: function(key, value, expires) {
      for(var i=0, max=WOL.util.Cookies.reservedNames.length; i<max; i++) {
	 if(key == WOL.util.Cookies.reservedNames[i]) {
	    throw new Error("'" + key + "' is reserved and can't be used as a value in a cookie");
	 };
      };
      document.cookie = key + '=' + value + ';';
      if(typeof(expires) == 'undefined') {
         // Default expiration is one year.
         var expires = new Date();
         expires.setFullYear(expires.getFullYear() + 1);
      };
      document.cookie = key + '=' + value + ';expires=' + expires.toGMTString();
   }
};