// Cookie singleton - gets/sets/deletes user cookies

var Cookie = {
  /* get - does what it says on the tin
       name (string) - name of the cookie to fetch */
  get:function(name) {
    var reg = new RegExp(name+"=([^;]*)");
    
    try {
      return unescape(reg.exec(document.cookie)[1]);
    } catch(e) { return false }
  },
  
  /* set - sets a cookie
       name (string)              - name of the new cookie
       value (string)             - value of the new cookie
       expires (number)           - number of days before the cookie expires
       path (string optional)     - limits the cookie to only be readable within that dir
       domain (string optional)   - allows the cookie to be read by any hostname in that domain
       secure (boolean optional)  - reads cookie only over a secure connection */
  set:function(name, value, expires, path, domain, secure) {
    expires = expires ? expires * 1000 * 60 * 60 * 24 : 0;
    
    var expires_date = new Date(Date.now() + expires).toString();
    
    document.cookie = name+'='+escape(value) +
      (expires > 0 ? ';expires='+expires_date : '') +
      (path ? ';path=' + path : '') +
      (domain ? ';domain=' + domain : '') +
      (secure ? ';secure' : '');
  },
  
  /* del - manually expires a cookie. truncated name due to 'delete' being a reserved word
       name, path, domain - same as arguments of the same name used by Cookie.set */
  del:function(name, path, domain) {
    if (this.get(name)) document.cookie = name + '=' +
        (path ? ';path=' + path : '') +
        (domain ? ';domain=' + domain : '') +
        ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
  }
}
