var gMnemonic = "vrt";   //prefix for cookie chips
var _privateCC = {
	/**
	 * Cookie storage is partitiioned into two levels:
	 *		the common level,
	 *		the mnemonic prefix level (default)
	 * So applications won't overwrite each other chip.
	 */
	_impl : YAHOO.util.Cookie,
	_mne : null,
	// Default to jvlretail cookie and let the child class to override the cookiename
	_sessionCookieName : "jvlretail_session",
	_sessionCookieConfig : { path : "/", domain : ".troweprice.com" },
	_persistentCookieName : "jvlretail_persistent",
	_persistentCookieConfig : { expires : new Date(new Date().getTime() + 1000 * 3600 * 24 * 365), path : "/", domain : ".troweprice.com" },

	
	/*
	 * Return the chipname prefixed with the mnemonic.  It is used in the precedence order search.
	 */
	_getPrefixes : function(chipName, options /* optional */) {
				var a = new Array();

				if (options!=null && options.setToCommonLevel==true)
				{
					// only common level.  no prefix.
					a.push(chipName);
				}
				else
				{
					// Search order prefix list.
					if (this._mne==null)
					{
						if (typeof gMnemonic == "undefined")
							this._mne = "";
						else
							this._mne = gMnemonic;  //troweprice.js
					}
					a.push(this._mne + "." + chipName);
					a.push(chipName);
				}
				return a;
			},

	getChip : function(chipName) {
				// Search session cookie.
				var hash = this._impl.getSubs(this._sessionCookieName);
				var prefixes = this._getPrefixes(chipName);
				var r = null;

				if (hash!=null)
				{
					for (var i=0; i < prefixes.length; i++)
					{
						r = hash[prefixes[i]];
						if (r != null)
							break;
					}
				}
				if (r==null)
				{
					// Search persistent cookie
					hash = this._impl.getSubs(this._persistentCookieName);
					if (hash!=null)
					{
						for (var i=0; i < prefixes.length; i++)
						{
							r = hash[prefixes[i]];
							if (r != null)
								break;
						}
					}
				}
				return r;
			},

	setChip : function(chipName, chipValue, options /* optional */) {
				var prefixes = this._getPrefixes(chipName, options);
				if (options!=null && options.persistent==true)
				{
					return this._impl.setSub(this._persistentCookieName, prefixes[0], chipValue, this._persistentCookieConfig);
				}
				else
				{
					return this._impl.setSub(this._sessionCookieName, prefixes[0], chipValue, this._sessionCookieConfig);
				}
			},

	removeChip : function(chipName, options /* optional */) {
				if (options!=null && options.persistent==true)
				{
					var hash = this._impl.getSubs(this._persistentCookieName);
					var prefixes = this._getPrefixes(chipName, options);
					if (hash!=null)
					{
						if (hash[prefixes[0]]!=null)
						{
							delete hash[prefixes[0]];
							this._impl.setSubs(this._persistentCookieName, hash, this._persistentCookieConfig);
						}
					}					
				}
				else
				{
					var hash = this._impl.getSubs(this._sessionCookieName);
					var prefixes = this._getPrefixes(chipName, options);
					if (hash!=null)
					{
						if (hash[prefixes[0]]!=null)
						{
							delete hash[prefixes[0]];
							this._impl.setSubs(this._sessionCookieName, hash, this._sessionCookieConfig);
						}
					}
				}
			}

};


var retailcookie = {
	
	getChip : function(chipName) {
				return _privateCC.getChip(chipName);
			},

	/*
	 * @param chipName
	 * @param chipValue
	 * @param options		optional field to set the persistent and set to the common level.
	 * @return {String} The created cookie string.
	 */
	setChip : function(chipName, chipValue, options /* optional */) {
				return _privateCC.setChip(chipName, chipValue, options);
			},

	/*
	 * @param chipName
	 * @param options	optional field to set the persistent and set to the common level.
	 */
	removeChip : function(chipName, options /* optional */) {
				_privateCC.removeChip(chipName, options);
			}

};

