// JavaScript Document

function Cookie( name, daysToLive )
{
	this.name = name;
	var msPerDay = 24 * 60 * 60 * 1000;
	if ( daysToLive === undefined )
	{
		this.expiration = null;
	}
	else
	{
		this.expiration = new Date( new Date().valueOf() + 
			( daysToLive * msPerDay ) ).toGMTString();
	}
}

Cookie.prototype.set = function( value )
{
	this.dp( "Cookie set (" + this.name + ") escaped value = " + escape( value ) );
	if ( value.length > CookieSizeLimit )
	{
		alert( "Could not set cookie: " + this.name + "; cookie size limit (" +
			CookieSizeLimit + ") exceeded" )
		return
	}
	
	var nameValue = this.name + "=" + escape( value );
	var newCookie;
	if ( this.expiration == null )
	{
		newCookie = nameValue + 
			"; path=" + CookiePath;
	}
	else
	{
		newCookie = nameValue + 
			"; path=" + CookiePath +
			"; expires=" + this.expiration;
	}
	
	var oldCookie = document.cookie;
	var oldValue = this.get();
	document.cookie = newCookie;
	if ( !this.verify( oldCookie, oldValue, value ) )
	{
		alert( "Cookie value missing or corrupted: \n\n" + 
			"\n\nThis error may be due to security restrictions or limitations of your browser." );
		return;
	}

	//this.dp( "Cookie set raw size: " + document.cookie.length )
	
	if ( value != this.get() )
	{
		alert( "Could not set cookie: " + this.name + 
			".  This error may be due to security restrictions or limitations of your browser." );
		return
	}
}

Cookie.prototype.verify = function( oldCookie, oldValue, newValue )
{
	if ( oldCookie.length == 0 ) { return true; }

	var re = new RegExp( this.name + "=" );
	var i = oldCookie.search( re );
	if ( i < 0 ) { return true; }
	
	var allCookies = this._replace( oldCookie, this.name, escape( newValue ) );
	var oldCookieList = allCookies.split( /;\s+/ ).sort();
	var newCookieList = document.cookie.split( /;\s+/ ).sort();
	
	var result = ( oldCookieList.toString() == newCookieList.toString() );
	if ( !result )
	{
		this.dp( "Cookie verify (" + this.name + ") failed: \n\n" + 
			"Old: " + oldCookieList + "\n\n" + 
			"New: " + newCookieList );
	}
	return result;
}

Cookie.prototype.remove = function()
{
	this.expiration = new Date( 0 ).toGMTString();
	document.cookie = this.name + "=" + 
		"; path=" + CookiePath +
		"; expires=" + this.expiration;
}

Cookie.prototype.get = function()
{
	//this.dp( "Cookie get (" + this.name + ") raw cookies: " + document.cookie )
	//this.dp( "Cookie get (" + this.name + ") raw size: " + document.cookie.length )
	
	var allcookies = unescape( document.cookie );
	this.dp( "Cookie get (" + this.name + ") unescaped cookies: " + allcookies )

	var cookieValue = this._get( allcookies, this.name );
	
	this.dp( "Cookie get (" + this.name + ") returns: " + cookieValue )
	return cookieValue;
}

Cookie.prototype._get = function( allCookies, cookieName )
{
	var cookieList = allCookies.split( /;\s+/ )
	var cookieValue = ""
	for ( var i = 0; i < cookieList.length; i++ )
	{
		var c = cookieList[i]
		if ( c.indexOf( cookieName ) == 0 )
		{
			var start = c.indexOf( "=" ) + 1
			cookieValue = c.substring( start )
			break;
		}
	}

	return cookieValue;
}

Cookie.prototype._replace = function( allCookies, cookieName, newValue )
{
	var nameValueIndex = allCookies.indexOf( cookieName + "=" );
	var valueIndex = allCookies.indexOf( "=", nameValueIndex );
	var nextIndex = allCookies.indexOf( ";", valueIndex );
	var nameValue;
	if ( nextIndex < 0 )
	{
		nameValue = allCookies.substring( nameValueIndex );
	}
	else
	{
		nameValue = allCookies.substring( nameValueIndex, nextIndex );
	}
	var value = nameValue.substring( nameValue.indexOf( "=" ) + 1 );
	var rePair = new RegExp( nameValue );
	var reValue = new RegExp( value );
	var newNameValue = nameValue.replace( reValue, newValue );
	var result = allCookies.replace( rePair, newNameValue );
	
	return result;
}

Cookie.prototype.dp = function( s )
{
	if ( ( typeof DPCookie != "undefined" ) && ( DPCookie ) ) { alert( s ) }
}
