//
// Basic cookie class
//

// ==============================================
// OBJECT
// ----------------------------------------------
function Cookie( name ){
   this.name = name;
   this.domain = null;
   this.path = null;
   this.expireDays = 30;
   this.isRed = false;
   this.value = "";
   this.data = new Array();

   this._Read();
}

// ==============================================
// PUBLIC METHODS
// ----------------------------------------------
Cookie.prototype.GetAttribute = function( name, value ){
   var ret = null;
   if ( this.value.match( name + "=([^&]+)&" ) ){
      ret = RegExp.$1;
   }
   
   return ret;
}

// ----------------------------------------------
Cookie.prototype.Save = function(){
   var expires = new Date();
   expires.setDate( expires.getDate() + this.expireDays );

   this._BuildValue();
   var szCookie = this.name + "=" + escape( this.value ) + "; expires=" + expires.toGMTString();
   
   if( this.domain != null ){
      szCookie += "; domain=" + this.domain;
   }
   if( this.path != null){
      szCookie += "; path=" + this.path;
   }

   document.cookie = szCookie;
}

// ----------------------------------------------
Cookie.prototype.SetAttribute = function( name, value ){
// NOTE: this.value.replace doesnt work
   var reg = new RegExp( name + "=[^&]+" );
   
   if ( this.value.match( reg ) ){
      //Debug("Cookie::SetAttribute","attribute " + name + " already exists and have been set to: " + value);

      // NOTE: its essential to build a regexp here, do not use string concat directly in the replace method
      //Debug("Cookie::SetAttribute","this.value (before): " + this.value );
      this.value = this.value.replace( reg, name + "=" + value );
      //Debug("Cookie::SetAttribute","this.value (after): " + this.value );
   }
   else{
      //Debug("Cookie::SetAttribute","adding attribute: " + name );
      var pair = new Array();
      pair[0] = name;
      pair[1] = value;

      this.data.push( pair );
   }

}

// ----------------------------------------------
Cookie.prototype.SetDomain = function( value ){
   this.domain = value;   
}

// ----------------------------------------------
Cookie.prototype.SetExpireDays = function( value ){
   this.expireDays = value;   
}

// ----------------------------------------------
Cookie.prototype.SetPath = function( value ){
   this.path = value;   
}

// ==============================================
// PRIVATE METHODS
// ----------------------------------------------
Cookie.prototype._BuildValue = function(){
   // append new attributes
   for ( var i=0; i<this.data.length; i++){
      this.value += this.data[i][0] + "=" + this.data[i][1] + "&";
   }
}

// ----------------------------------------------
Cookie.prototype._Read = function(){
   if ( document.cookie) { 
      var index = document.cookie.indexOf( this.name );
      
      if ( index != -1) {
         nDeb = (document.cookie.indexOf( "=", index) + 1);
         nFin = document.cookie.indexOf( ";", index);
         if (nFin == -1) {nFin = document.cookie.length;}
         
         this.value = unescape(document.cookie.substring(nDeb, nFin));
      } 
   }
}


