
function Cookies( sPath, sDomain, dExpires )
{
	this.sPath   = ((typeof(sPath)     != 'undefined') && (sPath    != null))  ? sPath    : '';
	this.sDomain = ((typeof(sDomain)   != 'undefined') && (sDomain  != null))  ? sDomain  : '';
	this.dExpires = ((typeof(dExpires) != 'undefined') && (dExpires != null))  ? dExpires : '';
	this.bSecure = false;
	this.Get = Cookies_Get;
	this.Set = Cookies_Set;
	this.Remove = Cookies_Remove;
	this.FindValue = Cookies_FindValue;
	this.Cookies_Get = Cookies_Get;
	this.Cookies_Set = Cookies_Set;
}

function Cookies_Get( sName, sKey )
{
	var sCookie = this.FindValue( document.cookie, sName );
	if (sCookie == null)
	return null;
	if ((typeof(sKey) != 'undefined') && (sKey != null) && (sKey.length))
	sCookie = this.FindValue( sCookie, sKey, true );
	return sCookie;
}

function Cookies_Set( sName, sKey, sValue, dExpires, bSecure )
{
	var sCookie = sName + '=';
	if ((typeof(sKey) != 'undefined') && (sKey != null) && (sKey.length))
	{
		var sOldCookie = this.Cookies_Get( sName );
		if (sOldCookie == null)
		{
			sCookie += escape(sKey) + '=' + escape(sValue) + ';';
		} 
		else
		{
			var oRegExp = new RegExp( sKey + '=[^&;]*' + '[&;]*' + '|$', 'i' );
			var aValues = sOldCookie.match( oRegExp );
			if ((aValues == null) || (aValues.length == 0) || (aValues[0].length == 0))
			{
				sCookie += sOldCookie + '&' + escape(sKey) + '=' + escape(sValue) + ';';
			}
			else
			{  
				var sOldDelimiter = aValues[0].charAt(aValues[0].length-1);
				if (!((sOldDelimiter == '&') || (sOldDelimiter == ';')))
					sOldDelimiter = '';
				var oRegExp = new RegExp( sKey + '=[^&;]*' + '[&;]*' + '|$', 'i' );
				var sReplaceString = escape(sKey) + '=' + escape(sValue) + sOldDelimiter;
				sCookie += (sOldCookie.replace( oRegExp, sReplaceString ) + ';');
			}
			oRegExp = null;
		}
	}
	else
	{
		sCookie += escape( sValue ) + ';';
	}
	if ((typeof(dExpires) != 'undefined') && (dExpires != null))
	{
		var dDateZero = new Date(0);
		var dDate = new Date;
		dDate.setTime( dExpires.getTime() );
		if (dDateZero.getTime() > 0) 
			dDate.setTime( dDate.getTime() - dDateZero.getTime() );
		sCookie += 'expires=' + dDate.toGMTString() + ';';
	}
	if (this.sPath.length)
		sCookie += 'path=' + this.sPath + ';';
	if (this.sDomain.length)
		sCookie += 'domain=' + this.sDomain + ';';
	if ((typeof(bSecure) != 'undefined') && (bSecure != null) && (bSecure))
		sCookie += 'secure;';
	
	document.cookie = sCookie;
}

function Cookies_Remove( sName, sKey )
{
	var sCookies = document.cookie;
	var sCookie = this.FindValue( sCookies + ';', sName, ';' );
	if (sCookie == null) 
	return;
	var oCookieRE = new RegExp( sName + '=[^;]*[;]', 'i');
	if ((typeof(sKey) != 'undefined') && (sKey != null) && (sKey.length))
	{
		var oKeyRE    = new RegExp( sKey  + '=[^&;]*[&;]', 'i');
		sCookie += ';';
		sCookie = sCookie.replace( oKeyRE, '' );
		if (sCookie.charAt(sCookie.length-1) == '&')
			sCookie = sCookie.substr(0, sCookie.length-1);
		this.Set( sName, sCookie );
	}
	else
	{  
		sCookies = sName + '=';      
		sCookies += (this.sPath.length)   ? '; path=' + this.sPath : '';
		sCookies += (this.sDomain.length) ? '; domain=' + this.sDomain : '';
		sCookies += '; expires=Thu, 01-Jan-70 00:00:01 GMT;';  
	}  
	document.cookie = sCookies;
}

function Cookies_FindValue( sString, sKey, bIsSubKey )
{
	var sDelimiters = ((arguments.length == 3) && (bIsSubKey)) ? ';&' : ';';
	var oRegExp = new RegExp( sKey + '([^' + sDelimiters + ']*)' + '[' + sDelimiters + ']*' + '|$', 'i' );
	var aValues = sString.match( oRegExp );
	if ((aValues == null) || (aValues.length == 0) || (aValues[1].length == 0))
	return null;
	return aValues[1].substr( 1 );
}




