/*
My cookie javascript library v 0.2
http://code.google.com/p/jscookie/
Date: 2009-11-03 15:48:12

Create new cookie:
cookie.set('test', 'test value', 2); // this example create new cookie or replace old cookie "test" with value "test value" for 2 days

Retrieve cookie:
cookie.get('test') // test value // this example return value for cookie with name "test"

Remove cookie:
cookie.remove('test'); // this example will remove cookie with name "test" 
*/

window.cookie =
{
  set: function(key, value, expires, path, domain, secure)
  {
    var sCookie = key+'='+escape(value)+'; ';

    if(expires !== undefined) {
      var date = new Date();
      date.setTime(date.getTime()+(expires*24*60*60*1000));
      sCookie+= 'expires='+date.toGMTString()+'; ';
    }

    sCookie+= (path === undefined) ? 'path=/;' : 'path='+path+'; ';
	sCookie+= (domain === undefined) ? '' : 'domain='+domain+'; '
	sCookie+= (secure === true) ? 'secure; ' : '';

    document.cookie = sCookie;
  },
  
  session: function(name, value, path)
  {
    document.cookie = name + "=" + escape (value) + '; path=' + path;
  },

  get: function(sKey)
  {
    var sValue = '';
    var sKeyEq = sKey+ '=';
    var aCookies = document.cookie.split(';');

    for(var iCounter = 0, iCookieLength = aCookies.length; iCounter < iCookieLength; iCounter++) {
      while(aCookies[iCounter].charAt(0) === ' ') {aCookies[iCounter] = aCookies[iCounter].substring(1);}
      if(aCookies[iCounter].indexOf(sKeyEq) === 0) {
        sValue = aCookies[iCounter].substring(sKeyEq.length);
      }
    }

    return unescape(sValue);
  },

  remove: function(key)
  {
    cookie.set(key, '', -1);
  },

  clear: function()
  {
    var aCookies = document.cookie.split(';');
    
    for(var iCounter = 0, iCookieLength = aCookies.length; iCounter < iCookieLength; iCounter++) {
      while(aCookies[iCounter].charAt(0) === ' ') {aCookies[iCounter] = aCookies[iCounter].substring(1);}
      var iIndex = aCookies[iCounter].indexOf('=', 1);
      if(iIndex > 0) {
        cookie.set(aCookies[iCounter].substring(0, iIndex), '', -1);
      }
    }
  },

  isEnabled: function()
  {
    cookie.set('test_cookie', 'test');

    var val = (cookie.get('test_cookie') === 'test') ? true : false;

    cookie.remove('test_cookie');

    return val;
  }
};
