/************************************************************
 * AUTHOR: Royston Tong
 ************************************************************
 * Requires: HashMap.js
 ************************************************************/

function Cookies() {
  // PUBLIC VARIABLE    
  this.accept = true;

  // PUBLIC FUNCTIONS
  this.get = function(name) {
    if (!this.accept || name == null) return null;
    var cookies = new HashMap(document.cookie, ';', '=');  
    return cookies.get(name);
  }

  this.set = function(name, value, date, path, domain, isSecure) {
    if (!this.accept || name == null) return false;
    var cookie = new HashMap(null, ';', '=');
    cookie.set(name, value);
    if (date != null)
      cookie.set('expires', date.toGMTString());
    if (path == null) path = '/';
    cookie.set('path', path);
    if (domain == null) domain = window.location.host.match(/\.[^\.]*\.[^\.]*$/);
    cookie.set('domain', domain);
    if (isSecure != null) cookie.set('secure', isSecure);
    document.cookie = cookie.toString();
    return true;
  }

  this.kill = function(name, path, domain) {
    if (!this.accept || name == null) return false;
    return this.set(name, null, new Date(0), path, domain);
  }

  this.toString = function() {
    var cookies = new HashMap(document.cookie, ';', '=');  
    return cookies.toString();
  }

  // CONSTRUCTOR
  this.set('accept', 1);
  if (this.get('accept') == 1) { 
    this.kill('accept'); 
  } else {
    this.accept = false;
  }
}
