/* <gslb_version version="V1.0.0" /> */
/* Copyright © GalaSoft Laurent Bugnion 2005 - 2007 */

//*****************************************************************************
//* gslb.Cookie.js
//*****************************************************************************
//* Project Name            : Utilities
//* Class Name              : Cookie
//* Tested Targets          : Firefox 2, Internet Explorer 7
//* Language/Compiler       : ECMAScript V3
//* Author                  : Laurent Bugnion (LBu)
//* Created                 : 15.04.2005
//*****************************************************************************
//* Description:
//* Cookie functions.
//* Last Base Level: BL0002
//*****************************************************************************

//*****************************************************************************
//* Imports *******************************************************************
//*****************************************************************************

// using none

// Create namespace
if ( !window.gslb )
{
  window.gslb = {};
}

//*****************************************************************************
//* Constructor ***************************************************************
//*****************************************************************************

// ----------------------------------------------------------------------------
/// <summary>
/// Dummy constructor. Use gslb.Cookie.methodName()
/// for "static" methods.
/// </summary>
gslb.Cookie = function()
{
}

//*****************************************************************************
//* Constants *****************************************************************
//*****************************************************************************

/// <summary type="String">
/// Path for the cookies. When set to '/', makes the cookie valid
/// for the whole domain.
/// </summary>
gslb.Cookie.PATH = '/';

//*****************************************************************************
//* Static methods ************************************************************
//*****************************************************************************

// ----------------------------------------------------------------------------
/// <summary>
/// Creates a Date object set to the current date/time plus a number of years, months, days, etc...
/// </summary>
/// <param name="iYears" type="int">The number of years to add to the current Date/Time.</param>
/// <param name="iMonths" type="int">The number of years to add to the current Date/Time.</param>
/// <param name="iDays" type="int">The number of years to add to the current Date/Time.</param>
/// <param name="iHours" type="int">The number of years to add to the current Date/Time.</param>
/// <param name="iMinutes" type="int">The number of years to add to the current Date/Time.</param>
/// <param name="iSeconds" type="int">The number of years to add to the current Date/Time.</param>
/// <param name="iMilliseconds" type="int">The number of years to add to the current Date/Time.</param>
/// <returns type="Date">The current date/time plus the number of years, months... specified
/// in the parameters.</returns>
gslb.Cookie.getDateTimeFromNow = function( iYears, iMonths, iDays, iHours, iMinutes, iSeconds, iMilliseconds )
{
  var dtNow = new Date();
  dtNow.setFullYear( dtNow.getFullYear() + iYears );
  dtNow.setMonth( dtNow.getMonth() + iMonths );
  dtNow.setDate( dtNow.getDate() + iDays );
  dtNow.setHours( dtNow.getHours() + iHours );
  dtNow.setMinutes( dtNow.getMinutes() + iMinutes );
  dtNow.setSeconds( dtNow.getSeconds() + iSeconds );
  dtNow.setMilliseconds( dtNow.getMilliseconds() + iMilliseconds );
  return dtNow;
}

// ----------------------------------------------------------------------------
/// <summary>
/// Internal method only
/// </summary>
gslb.Cookie.getValue = function( iOffset )
{
  var iEndstr = document.cookie.indexOf ( ";", iOffset );

  if ( iEndstr == -1 )
  {
    iEndstr = document.cookie.length;
  }
  return unescape( document.cookie.substring( iOffset, iEndstr ) );
}

// ----------------------------------------------------------------------------
/// <summary>
/// Gets a cookie identified by its name.
/// </summary>
/// <param name="strName" type="String">Cookie's name.</param>
/// <returns type="String">The cookie's value, or null if no corresponding cookie
/// is found.</returns>
gslb.Cookie.getCookie = function( strName )
{
  var strArg = strName + "=";
  var iArgLength = strArg.length;
  var iCookieLength = document.cookie.length;
  var i = 0;

  while ( i < iCookieLength )
  {
    var j = i + iArgLength;

    if ( document.cookie.substring( i, j ) == strArg )
    {
      return gslb.Cookie.getValue( j );
    }

    i = document.cookie.indexOf( " ", i ) + 1;

    if ( i == 0 )
    {
      break;
    }
  }
  return null;
}

// ----------------------------------------------------------------------------
/// <summary>
/// Sets a cookie.
/// </summary>
/// <param name="strName" type="String">Cookie's name.</param>
/// <param name="strValue" type="String">Cookie's value.</param>
/// <param name="dtExpires" type="Date">A Date object, which specifies until when the
/// cookie is valid. If this parameter is left blank, or null, the cookie
/// is a Session cookie.</param>
/// <param name="strPath" type="String">Cookie's path. If this parameter is left blank,
/// the cookie is valid for the whole path (inclusive subfolders).</param>
/// <param name="strDomain" type="String">Cookie's domain. If this parameter is left blank,
/// nothing is specified.</param>
/// <param name="bSecure" type="bool">If true, the cookie is secure. If this parameter
/// is left blank, nothing is specified.</param>
gslb.Cookie.setCookie = function( strName,
  strValue,
  dtExpires,
  strPath,
  strDomain,
  bSecure )
{
  document.cookie = strName + "=" + escape ( strValue )
    + ( ( dtExpires == null ) ? "" : ( "; expires=" + dtExpires.toGMTString() ) )
    + ( ( strPath == null ) ? "" : ( "; path=" + gslb.Cookie.PATH ) )
    + ( ( strDomain == null ) ? "" : ( "; domain=" + strDomain ) )
    + ( ( bSecure == true ) ? "; secure" : "" );
}

// ----------------------------------------------------------------------------
/// <summary>
/// Deletes a cookie identified by its name.
/// </summary>
/// <param name="strName" type="*String">Cookie's name.</param>
gslb.Cookie.deleteCookie = function( strName )
{
  var dtExpiration = new Date();
  dtExpiration.setTime ( dtExpiration.getTime() - 1 );  // This cookie is history
  gslb.Cookie.setCookie( strName, "", dtExpiration, gslb.Cookie.PATH );
}

// ----------------------------------------------------------------------------
/// <summary>
/// Tests if persistent cookies are enabled.
/// </summary>
/// <returns type="bool">True if persistent cookies are enabled, false otherwise.</returns>
gslb.Cookie.testPersistentEnabled = function()
{
  var dtExpiration = new Date();

  // Valid one minute.
  dtExpiration.setTime( dtExpiration.getTime() + ( 60 * 1000 ) );
  gslb.Cookie.setCookie( "testCookie", "OK", dtExpiration );
  var strTest = gslb.Cookie.getCookie( "testCookie" );

  if ( strTest == "OK" )
  {
    return true;
  }
  else
  {
    return false;
  }
}

// ----------------------------------------------------------------------------
/// <summary>
/// Tests if session cookies are enabled.
/// </summary>
/// <returns type="bool">True if session cookies are enabled, false otherwise.</returns>
gslb.Cookie.testSessionEnabled = function()
{
  gslb.Cookie.setCookie( "testCookieSession", "OK" );
  var strTest = gslb.Cookie.getCookie( "testCookieSession" );

  if ( strTest == "OK" )
  {
    return true;
  }
  else
  {
    return false;
  }
}
