/**********************************************************
 *
 *	clsCookie.js
 *
 *	By: SvenL on 2003-11-13
 *
 *
 *	[SL:20040123]	Checks voor de shift en unshift functies ingebouwd.
 *					Dit bleek een JS functie welke alleen in 5.5 en hoger bestaat.
 *
 ***/



function clsCookie(szCookieName)
{
	this.szCookieName		= szCookieName;
	this.szCookiePath		= null;
	this.szCookieDomain		= null;
	this.szCookieExpires	= null;
	this.szCookieSecure		= null;
	this.szCookieString		= '';
	
	if(typeof(this.aKeys) == 'undefined')
		this.aKeys			= new Array();

	this.storeCookie		= function()
								{
									var bUseless = false;
									if(!this.szCookieName)
										return false;

									if(this.szCookieName == '')
										return false;

									var node;
									var cookieString = '';
									var cookieKeyString = '';

									for(node in this.aKeys)
									{
										if(cookieKeyString != '')
											cookieKeyString += '&';

										cookieKeyString += escape(this.aKeys[node].name) + '=' + escape(this.aKeys[node].value);
									}

									if(cookieKeyString == '')
										bUseless = true;

									if(this.szCookiePath)
									{
										cookieString += 'path=' + this.szCookiePath + '; ';
										cookieKeyString += '&' + escape('$Path') + '='  + escape(this.szCookiePath);
									}

									if(this.szCookieDomain)
									{
										cookieString += 'domain=' + this.szCookieDomain + '; ';
										cookieKeyString += '&' + escape('$Domain') + '='  + escape(this.szCookieDomain);
									}

									if(bUseless)
									{
										// Realy old date for deleting cookies.
										cookieString += 'expires=Fri, 31 Dec 1999 23:59:59 GMT; ';
									}
									else
									{
										if(this.szCookieExpires)
										{
											cookieString += 'expires=' + this.szCookieExpires.toGMTString() + '; ';
											cookieKeyString += '&' + escape('$Expires') + '='  + escape(this.szCookieExpires);
										}
									}

									if(this.szCookieSecure)
									{
										cookieString += 'secure; ';
										cookieKeyString += '&' + escape('$Secure') + '=' + escape((this.szCookieSecure)?('1'):('0'));
									}

									cookieString = this.szCookieName + '=' + cookieKeyString + '; ' + cookieString;

									document.cookie = cookieString;
									return true;
								};

	this.readCookie			= function()
								{
									if(!this.szCookieName)
										return false;

									if(this.szCookieName == '')
										return false;

									var aCookie = document.cookie.split(';');

									for(var i = 0; i < aCookie.length; i++)
									{
										var pEqual = aCookie[i].indexOf('=');
										if(pEqual != -1)
										{
											var szName = unescape(aCookie[i].substring(0, pEqual));
											if(szName == this.szCookieName)
											{
												this.szCookieString = aCookie[i].substr(pEqual + 1); // +1 want het '=' teken hoeft niet mee.
												return true;
											}
										}
									}

									return false;
								};

	this.readKeys			= function(szString)
								{
									var splitted = szString.split('&');
									var pEqual, name, value, key;

									for(var i = 0; i < splitted.length; i++)
									{
										pEqual = splitted[i].indexOf('=');

										if(pEqual == -1)
										{
											this._setValue(unescape(splitted[i]), null);
										}
										else
										{
											name = splitted[i].substring(0, pEqual);
											value = splitted[i].substr(pEqual + 1);

											name = unescape(name);
											value = unescape(value);

											if(name.substr(0, 1) == '$')
											{
												// Special cases for internal use only
												switch(name.toLowerCase())
												{
													case '$path':
														{
															this.szCookiePath = value;
														} break;
													case '$domain':
														{
															this.szCookieDomain = value;
														} break;
													case '$expires':
														{
															this.szCookieExpires = new Date(value);
														} break;
													case '$secure':
														{
															// Force a boolean by dubble NOT-operator
															this.szCookieSecure = (value == '1')?(true):(false);
														} break;
												}
												
											}
											else
											{
												this._setValue(name, value)
											}
										}

										if(typeof(key) != 'undefined')
											if(key.containedIn != this.aKeys)
												this.aKeys = key.containedIn;
									}
								};

	this.getValue			= function(szKeyName)
								{
									if(this.aKeys.length > 0)
									{
										return this.aKeys[0].Get(szKeyName);
									}
									else
									{
										return null;
									}
								};

	this.setValue			= function(szKeyName, szKeyValue)
								{
									this._setValue(szKeyName, szKeyValue);
									this.storeCookie();
								};

	this._setValue			= function(szKeyName, szKeyValue)
								{
									var key;

									if(this.aKeys.length > 0)
									{
										key = this.aKeys[0];
									}
									else
									{
										key = new clsCookieKey(this.aKeys);
									}

									key.Set(szKeyName, szKeyValue);
								};

	this.removeKey			= function(szKeyName)
								{
									if(this._removeKey(szKeyName))
										this.storeCookie();
								};

	this._removeKey			= function(szKeyName)
								{
									var key;

									if(this.aKeys.length > 0)
									{
										key = this.aKeys[0];
										return key.Delete(szKeyName);
									}

									return false;
								};

	this.init				= function()
								{
									if(this.readCookie())
									{
										this.readKeys(this.szCookieString);
									}

									return this;
								};

	return this.init();
}


function clsCookieKey(oContainer)
{
	this.containedIn		= oContainer;
	this.name				= '';
	this.value				= '';

	this.Set				= function(szKey, szValue)
								{
									var bFound = false;

									if((szKey.toLowerCase() == this.name.toLowerCase()) || (this.name == ''))
									{
										if(this.name == '')
										{
											this.name = szKey;

											// Helaas is de unshift-method alleen voor JS5.5 en hoger (IE5.5 en hoger)
											// unshift ipv concat omdat dan de referentie naar het object niet verdwijnt.
											if(this.containedIn.unshift)
											{
												this.containedIn.unshift(this);
											}
										}

										this.value = szValue;
										bFound = true;
									}
									else
									{
										for(node in this.containedIn)
										{
											if(this.containedIn[node].name.toLowerCase() == szKey.toLowerCase())
											{
												this.containedIn[node].value = szValue;
												bFound = true;
											}
										}
									}
									
									if(!bFound)
									{
										var newKey = new clsCookieKey(oContainer);
										newKey.name = szKey;
										newKey.value = szValue;
										// Helaas is de unshift-method alleen voor JS5.5 en hoger (IE5.5 en hoger)
										// unshift ipv concat omdat dan de referentie naar het object niet verdwijnt.
										if(this.containedIn.unshift)
										{
											this.containedIn.unshift(newKey);
										}
										
										if(oContainer != this.containedIn)
											oContainer = this.containedIn;
									}
								};

	this.Get				= function(szKey)
								{
									if(szKey.toLowerCase() == this.name.toLowerCase())
									{
										return this.value;
									}
									else
									{
										for(node in this.containedIn)
										{
											if(this.containedIn[node].name.toLowerCase() == szKey.toLowerCase())
											{
												return this.containedIn[node].value;
											}
										}
									}

									return null;
								};

	this.Delete				= function(szKey)
								{
									if(szKey.toLowerCase() == this.containedIn[0].name.toLowerCase())
									{
										// Helaas is de shift-method alleen voor JS5.5 en hoger (IE5.5 en hoger)
										// Het eerst element is diegene die verwijderd moet worden.
										if(this.containedIn.shift)
										{
											this.containedIn.shift();
										}
										return true;
									}
									else
									{
										var iTarget		= -1;

										for(var i  = 0; i < this.containedIn.length; i++)
										{
											if(this.containedIn[i].name.toLowerCase() == szKey.toLowerCase())
											{
												iTarget = i;
											}
										}
										
										if(iTarget != -1)
										{
											this.containedIn[iTarget].name	= this.containedIn[0].name;
											this.containedIn[iTarget].value	= this.containedIn[0].value;
											// Helaas is de shift-method alleen voor JS5.5 en hoger (IE5.5 en hoger)
											if(this.containedIn.shift)
											{
												this.containedIn.shift();
											}
											return true;
										}
									}

									return false;
								};

	this.Remove				= function(szKey)
								{
									return this.Delete(szKey);
								};
}
