/*
 * <copyright>
 *  Copyright (c) by Hyperwave AG
 * </copyright>
 *
 * <file>
 *  Name:        CSCookie.js
 *  Created:     2001-08-09
 *  $Id: CSCookie.js,v 1.1.2.1 2003/07/16 10:11:49 wputz Exp $
 * </file>
 */

/**
 * A static class for setting, getting and removing cookies in a Browser environment.
 *
 * @author Fessl Christian
 */

//------------------------------------------------------------
//<JSClass Name="CSCookie">

  //----------------------------------------------------------
  /**
   * Constructor for the static class.
   * @return CSCookie
   */
  function CSCookie(aParam)
  {
    if ( aParam == "__proto__" ) return;
  }
  class$ = doInherit(CSCookie);

  //----------------------------------------------------------
  /**
   * Sets a specific Cookie in the Browser.
   * @param aParam: Object: contains various parts of a cookie
   * @param aParam.key: String: the name of the Cookie
   * @param aParam.value: String: the value of the Cookie
   * @param aParam.expiration: Date: the expiration-date of the Cookie
   * @param aParam.path: String: the path where the Cookie is valid
   * @param aParam.domain: String: the domain where the Cookie is valid
   * @param aParam.secure: Boolean: secure mode
   * @return void
   */
  class$.static_.set = function(aParam)
  {
    var my_cookie = aParam.key + "=";

    if (aParam.value)
      my_cookie += escape(aParam.value);

    if (aParam.expiration)
      my_cookie += "; expires=" + aParam.expiration.toGMTString();

    if (aParam.path)
      my_cookie += "; path=" + aParam.path;

    if (aParam.domain)
      my_cookie += "; domain=" + aParam.domain;

    if (aParam.secure)
      my_cookie += "; secure";

    document.cookie = my_cookie;
  }

  //----------------------------------------------------------
  /**
   * Returns a specific Cookie.
   * @param aKey: String: the name of the Cookie
   * @return String: the escaped value of the requested Cookie
   */
  class$.static_.get = function(aKey)
  {
    var cookies = document.cookie;
    var prefix = aKey + "=";

    var begin = cookies.indexOf("; " + prefix);
    if (begin == -1)
    {
      begin = cookies.indexOf(prefix);
      if (begin != 0)
      {
        return "";
      }
    }
    else
    {
      begin += 2;
    }

    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
      end = cookies.length;
    }

    return unescape(cookies.substring(begin + prefix.length, end));
  }

  //----------------------------------------------------------
  /**
   * Removes a specific Cookie.
   * @param aParam: Object: contains various parts of a cookie
   * @param aParam.key: String: the name of the Cookie
   * @param aParam.path: String: the path where the Cookie is valid
   * @param aParam.domain: String: the domain where the Cookie is valid
   * @return void
   */
  class$.static_.remove = function(aParam)
  {
    if (CSCookie.static_.get(aParam.key))
    {
      var param = {
                    key: aParam.key,
                    expiration: new Date(1970,1,1,0,0,0)
                  };

      if (aParam.path)
        param.path = aParam.path;

      if (aParam.domain)
        param.domain = aParam.domain;

      CSCookie.static_.set(param);
    }
  }

// </JSClass>
//------------------------------------------------------------

