/*****************************************************************
 * Filename: Cookie.js                                           *
 * Purpose : Class for loading,setting and removing cookies      *
 * @author   Rubayeet Islam <rubayeet@evoknow.com> Team Pegasus  *
 * Copyright © 2008                                              * 
 * Evoknow Inc.                                                  *
 *****************************************************************/
/**
 * The constructor function: creates a Cookie object for the specified
 * document, with a specified name and optional attributes.
 * 
 * @param {Object}  document The Document object for which the cookie is stored. Required.  
 * @param {String}  name     A string that specifies a name for the cookie. Required.
 * @param {integer} hours    A number that specifies the number of hours from now after 
 *                           which the cookie should expire. Optional.
 * @param {String}  path     A string that specifies the cookie path attribute. Optional.
 * @param {String}  domain   A string that specifies the cookie domain attribute. Optional.
 * @param {boolean} secure   A boolean value that, if true, requests a secure cookie.
 */

function Cookie(document, name, hours, path, domain, secure)
{
   /**
    * All the predefined properties of this object begin with '_' to distinguish
    * them from other properties, which are the values to be stored in the cookie
    */
   
   this._document = document;
   this._name     = name;
   
   this._expiration = (hours)  ? new Date((new Date()).getTime() + hours * 3600000) : null;
   this._path       = (path)   ? path   : null;
   this._domain     = (domain) ? domain : null;
   this._secure     = (secure) ? secure : null;
   
   this._cookieVal  = '';
  
   this.setPath = function(path)
   {
      this._path = path;   	
   }
   
   this.setExpiration = function(hours)
   {
      this._expiration = new Date((new Date()).getTime() + hours * 3600000);   	
   }
   
   this.setDomain = function(domain)
   {
      this._domain = domain;   	
   }
   
   this.setSecure = function()
   {
      this._secure = secure;   	
   }
   
   this.store = function()
   {
      /**
       * First, loop through the properties of the Cookie object and
       * put together the value of the cookie.
       */
      
      var cookieVal = "";

      for(var prop in this) 
      {
         /* Ignore properties with names that begin with '_' and also methods*/
         if ((prop.charAt(0) == '_') || ((typeof this[prop]) == 'function')) 
            continue;         
         
         if (cookieVal != "") cookieVal += '&';
            /*cookieVal += prop + '=' + escape(this[prop]);*/
            cookieVal += prop + '=' + this[prop];            
      }

      /**
       * Put together the complete cookie string, which includes the name
       * and the various attributes specified when the Cookie object was
       * created
       */
      
      var cookie = this._name + '=' + cookieVal;
      
      if (this._expiration) cookie += '; expires=' + this._expiration.toGMTString();
      if (this._path)       cookie += '; path='    + this._path;      
      if (this._domain)     cookie += '; domain='  + this._domain;      
      if (this._secure)     cookie += '; secure';
      
      /*Store the cookie by setting the magic Document.cookie property*/
      this._document.cookie = cookie;
   }
   
   this.isCookieExist = function(name)
   {
      var allCookies = this._document.cookie;
      if(allCookies.indexOf(name+'=') != -1) return true;
      return false;
   }
   
   this.load = function ()
   { 
      /* Get a list of all cookies that pertain to this document */
      var allCookies = this._document.cookie;
      
      if (allCookies == "") return false;
      
      /* Now extract just the named cookie from that list */
      var start = allCookies.indexOf(this._name + '=');
          
      if (start == -1) return false;   /* Cookie not defined for this page */
      
      start += this._name.length + 1;  /* Skip name and equals sign */

      var end = allCookies.indexOf(';', start);
      
      if (end == -1) end = allCookies.length;
      
      var cookieVal = allCookies.substring(start, end);
      
      /**
       * Break that value of the named cookie down into individual state variable
       * names and values. The name/value pairs are separated from each other by
       * ampersands, and the individual names and values are separated from each
       * other by colons.
       */
      
      var a = cookieVal.split('&');    /* Break it into an array of name/value pairs */
      
      for(var i=0; i < a.length; i++)  /* Break each pair into an array */
      {
         a[i] = a[i].split('=');
      } 
         
      /* Set all the names and values of the state variables in this Cookie object. */
      for(var i = 0; i < a.length; i++) 
      {
         this[a[i][0]] = unescape(a[i][1]);
      }
         
      return true;
   }
   
   this.remove = function()
   {
      var cookie;
      
      cookie = this._name + '=';
      
      if (this._path)   cookie += '; path=' + this._path;
      
      if (this._domain) cookie += '; domain=' + this._domain;
      
      cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
      
      this._document.cookie = cookie;
   }
  
  this.update = function(propertyName,value)
  {
    for(var prop in this)
    {
      if(prop == propertyName) 
      {
         this[prop] = value;
      }
    } 
    
    this.store();
  }     
}//End of Cookie class