/********************************************************************
* Copyright ©  Acsys, Inc.  All rights reserved.
* 
* This material contains the valuable properties and trade secrets of
* Acsys, Inc. embodying substantial creative efforts and confidential 
* information, ideas, and expressions, no part of which may be 
* reproduced or transmitted in any form or by any means, electronic, 
* mechanical, or otherwise, including photocopying and recording or 
* in connection with any information storage or retrieval system 
* without the permission in writing of Acsys, Inc.
*/

/********************************************************************
* This library defines a class for managing cookies.
* 
* Dependencies: 
*	None
*/

function Cookie(sName, sValue, iHours, sPath, sDomain, bSecure, bUnescape)
{
  this._name = escape(sName).replace(/\./, "%2E");
  this.Value = (sValue)?sValue:"";

	this._unescape=(bUnescape==true)?true:false;

  if (iHours)
    this._expiration = new Date((new Date()).getTime() + (iHours*3600000))
  else
    this._expiration = null;
  
  this._path = (sPath) ? sPath : null;
  this._domain = (sDomain) ? sDomain : null;
  this._secure = (bSecure) ? bSecure : null;

  this.Load = _Cookie_load;
  this.Store = _Cookie_store;
  this.Load = _Cookie_load;
  this.Remove = _Cookie_remove;
  this.HasKeys = _Cookie_HasKeys;
  this.Load();
}

function _Cookie_HasKeys()
{
  for (var prop in this)
  {
    if ((prop.charAt(0) == '_') || 
        (typeof(this[prop]) == 'function') ||
        (prop.search(/^value$/i) > -1)) continue;
    return true;
  }
  return false;
}

// this function is the "private" implementation of the "public" store method
function _Cookie_store()
{
  var sCookieVal = '';
  for (var prop in this)
  {
    if ((prop.charAt(0) == '_') || (typeof(this[prop]) == 'function')) continue;
    if (sCookieVal != '') sCookieVal += '&';
    sCookieVal += prop + '=' + ((this._unescape)?escape(this[prop]):this[prop]);
  }
  var sCookie = (sCookieVal) ? this._name + '=' + sCookieVal : this._name + '=' + escape(this.Value);
  if (this._expiration) sCookie += '; expires=' + this._expiration.toGMTString();
  if (this._path) sCookie += '; path=' + this._path;
  if (this._domain) sCookie += '; domain=' + this._domain;
  if (this._secure) sCookie += '; secure';

  document.cookie = sCookie;
}

function _Cookie_load()
{
  var sAllCookies = document.cookie;
  if (sAllCookies == '') return false;
  
  var iStart = sAllCookies.indexOf(this._name + '=');
  if (iStart == -1) return false;
  iStart += this._name.length + 1;
  var iEnd = sAllCookies.indexOf(';', iStart);
  if (iEnd == -1) iEnd = sAllCookies.length;
  
  var sCookieVal = sAllCookies.substring(iStart, iEnd);
  
  if (sCookieVal.indexOf('&') > -1)
  {
    var aNameValues = sCookieVal.split('&');
    for (var i = 0; i < aNameValues.length; ++i)
      aNameValues[i] = aNameValues[i].split('=');
  
    for (var i = 0; i < aNameValues.length; ++i)
      this[aNameValues[i][0]] = (this._unescape) ? unescape(aNameValues[i][1]) : aNameValues[i][1];
    this.Value = "";
  }
  else
  {
		if (sCookieVal && sCookieVal.indexOf("=")>=0)
		{
			if (this._unescape)
				this.Value = (sCookieVal) ? unescape(sCookieVal.split("=")[1]) : "";
			else
				this.Value = (sCookieVal)?sCookieVal.split("=")[1]:"";
		}
		else
		{
			this.Value = (sCookieVal) ? sCookieVal : "";
		}
  }
  
  return true;
}

function _Cookie_remove()
{
  var sCookie;
  sCookie = this._name + '=';
  if (this._path) sCookie += '; path=' + this._path;
  if (this._domain) sCookie += '; domain=' + this._domain;
  sCookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
  
  document.cookie = sCookie;
}

function ExpireCookie(sName)
{
  var cookie = new Cookie(sName, "", 1, "/");
  cookie.Load();
  cookie.Remove();
}