//	cookie.js - JavaScript for handling cookies
//
//	From JavaScript the Definitive Guide (5th Edition), by David Flanagan, O'Reilly and Associates (converted to standard format, modified to fix bug)
//
aModule.has ( 'cCookie' );


cCookie.prototype.$name = 'cCookie';

function cCookie( name )
{
this.$name = name;
var sAllCookies = document.cookie;
if ( sAllCookies == "" )
	return;
var sCookies = sAllCookies.split( ';' );
var sCookie = null;
var nLeadingSpaces;
for ( var i = 0; i < sCookies.length; i++ )
  {
	for ( var j = 0, nLeadingSpaces = 0; j < sCookies[i].length; ++j, ++nLeadingSpaces )
	  {
		if ( sCookies[i].charAt(j) != ' ' )
			break;
	  }
	sCookies[i] = sCookies[i].substr(nLeadingSpaces);
	if ( sCookies[i].substring(0, name.length+1) == (name + "=") )
	  {
		sCookie = sCookies[i];
		break;
	  }
  }
if ( sCookie == null )
	return;
var sCookieValue = sCookie.substring( name.length+1 );
var aCookie = sCookieValue.split( '&' );
for ( var i = 0; i < aCookie.length; i++ )
	aCookie[i] = aCookie[i].split( ':' );
for ( var i = 0; i < aCookie.length; i++ )
	this[aCookie[i][0]] = decodeURIComponent( aCookie[i][1] );
}

cCookie.prototype.storeCookie = function( daysToLive, path, domain, secure )
{
var $name = 'storeCookie';
var sCookieValue = "";
for (var prop in this )
  {
	if ( (prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') ) 
		continue;
	if ( sCookieValue != "" )
		sCookieValue += '&';
	sCookieValue += prop + ':' + encodeURIComponent(this[prop]);
  }
var sCookie = this.$name + '=' + sCookieValue;
if ( daysToLive || daysToLive == 0 )
	sCookie += "; max-age=" + (daysToLive*24*60*60);
if ( path )
	sCookie += "; path=" + path;
if ( domain )
	sCookie += "; domain=" + domain;
if ( secure )
	sCookie += "; secure";
document.cookie = sCookie;
}

cCookie.prototype.removeCookie = function( path, domain, secure )
{
var $name = 'removeCookie';
for ( var prop in this )
  {
	if ( (prop.charAt(0) != '$') && (typeof this[prop] != 'function') ) 
		delete this[prop];
  }
this.store( 0, path, domain, secure );
}

cCookie.prototype.isEnabled = function ( )
{
var $name = 'isEnabled';
if ( navigator.cookieEnabled != undefined )
	return( navigator.cookieEnabled );
if ( cCookie.enabled.cache != undefined )
	return( cCookie.enabled.cache );
document.cookie = "testcookie=test; max-age=10000";
var sAllCookies = document.cookie;
if (sAllCookies.indexOf("testcookie=test") == -1)
  {
	return( cCookie.enabled.cache = false );
  }
else
  {
	document.cookie = "testcookie=test; max-age=0";
	return( cCookie.enabled.cache = true );
  }
}

