var DBNameHTML_C	= '/passage/nl/cms.nsf';
// Create object for easy access to CSS Cookie methods
var CSSCookies					= new Passage_objCSSCookies();
// Globals
var sCSSCookieName_Prefix	= 'css-';
var sCSSCookieName_Other	= 'other';
var sCSSCookieName_Size	= 'size';
var sCSSCookieName_Color	= 'color';
var sCSSURLParamName		= 'css';
var iCSSCookieDays				= 1;
// Debugging: set to true to enable debug alerts
var bDEBUG = false;
function Passage_setCSSCookieValue( name, value )
/*
	Sets cookie values for the CSS-cookie.
	@param	name		String with the name of the cookie. Will be prepended with 'css-'
	@param	value			String containing the comma-separated keys of CSS documents to be loaded
	@param	days			(optional) Integer that defines the number of days the cookie should exist
*/
{
	// An empty value doesn't overwrite an existing value, so use "*" instead
	value	= ( value == "" ) ? "*" : value;
	Passage_setCookie( sCSSCookieName_Prefix + name, value, iCSSCookieDays, '/' );
}
function Passage_getCSSCookieValue( name )
/*
	Gets values of  the CSS-cookie.
	@param	name		String with the name of the cookie. Will be prepended with 'css-'
	@return		String		String containing the comma-separated keys of the CSS documents to be loaded.
									If the cookie wasn't present, and empty string is returned.
*/
{
	var sCookieValue = Passage_getCookie( sCSSCookieName_Prefix + name );
	if( bDEBUG ) {alert( 'processing cookie: ' + sCSSCookieName_Prefix + name );}
	// Rewrite the cookie so its expiry date is refreshed
	if( sCookieValue != null && sCookieValue != '*' )
	{
		if( bDEBUG ) {alert( sCSSCookieName_Prefix + name + ' != null => ' + sCookieValue + ', rewriting cookie to refresh' );}
		Passage_setCSSCookieValue( name, sCookieValue );
	}
	
	// Return empty string if the cookie values doesn't exist or if it has value "*"
	var sReturnValue = (sCookieValue == null || sCookieValue == "*" ) ? "" : sCookieValue;
	return sReturnValue;
}
function Passage_addCSSToDocument( aStyleSheets )
/*
	Adds additional CSS Style Sheets to the document, based on URL parameters and cookie values
	If a key is empty, no stylesheet will be added for that entry.
	Only works on DOM-compatible browsers.
	@param	aStyleSheets		Array containing the keys of the CSS documents from Passage CMS to be loaded
*/
{
	if( document.createElement && document.getElementsByTagName )
	{
		for( var iS = 0; iS < aStyleSheets.length; iS++ )
		{
			if( aStyleSheets[iS] != "" )
			{
				var newSS		= document.createElement('link');
				newSS.rel		= 'stylesheet';
				newSS.href	= DBNameHTML_C + '/v9914BBS1/' + aStyleSheets[iS];
				newSS.type	= 'text/css';
				document.getElementsByTagName('head')[0].appendChild(newSS);
			}
		}
	}
}
function Passage_getURLParameter( sParamName )
/*
	Gets the value from a parameter specified in the URL.
	@param	sParamName		String specifying the name of the parameter to return
	@return		String					String with the value specified in the parameter; empty string if 
												the parameter wasn't present in the URL
*/
{
	var sReturnValue = "";
	// Get the QueryString from the URL
	var sURL	= document.location.href;
	var iAmp	= sURL.indexOf( '&' );
	var sQ		= sURL.substring( iAmp, sURL.length );
	
	// Get an array of argument/value pairs
	var aArgPairs = sQ.split('&');
	
	// Parse arg pairs
	for (var iArg=0; iArg < aArgPairs.length; iArg++)
	{
		// Put the parameter in aArg[0] and the value in aArg[1]
		var aArg = aArgPairs[iArg].split("=");
	
		if( sParamName == aArg[0] )
		{
			sReturnValue = unescape( aArg[1] );
			break;
		}
	}
	return sReturnValue ;
}
function Passage_setCookie(name, value, expires, path, domain, secure)
/*
	Sets a Cookie with the given name and value.
	Source: http://www.netspade.com/articles/javascript/cookies.xml
	@param	name		String, name of the cookie
	@param	value			String, value of the cookie
	@param	[expires]	Date or number, expiration date of the cookie or number of days (default: end of current session)
	@param	[domain]	String, domain where the cookie is valid
	@param	[secure]		Boolean, value indicating if the cookie transmission requires a secure transmission
*/
{
	if( typeof( expires ) == 'number' )
	{
		// expires is passed as a number of days; convert to Date
		var days	= expires;
		expires	= new Date();
		expires.setTime( expires.getTime() + days*24*60*60*1000);
	}
	document.cookie= name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}
function Passage_getCookie(name)
/*
	Gets the value of the specified cookie.
	@param	name	String, name of the desired cookie
	@return		String	Value of the specified cookie
*/
{
	var dc		= document.cookie;
	var prefix	= name + "=";
	var begin	= dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}
function Passage_deleteCookie(name, path, domain)
/*
	Deletes the specified cookie.
	@param	name		String, name of the cookie
	@param	[path]		String, path of the cookie
	@param	[domain]	String, domain of the cookie
*/
{
	if( Passage_getCookie( name ) )
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
function Passage_processCSS( )
/*
	Adds the various CSS documents:
	- based on the values stored in the cookies for Size, Color and Other
	- based on the parameters passed in the documents' URL
	
	URL param 'css-order' (optional) defines the sequence in which the CSS documents will be added:
		'cookies-url'	-	first CSS from cookies, then CSS from URL (default)
		'url-cookies'	-	first CSS from URL, then CSS from cookies
*/
{
	if( Passage_getURLParameter( 'css-order' ) == 'url-cookies' )
	{
		Passage_addCSSToDocument( Passage_getURLParameter( sCSSURLParamName ).split(',') );
		Passage_addCSSToDocument( Passage_getCSSCookieValue( sCSSCookieName_Other ).split(',') );
		Passage_addCSSToDocument( Passage_getCSSCookieValue( sCSSCookieName_Size ).split(',') );
		Passage_addCSSToDocument( Passage_getCSSCookieValue( sCSSCookieName_Color ).split(',') );
	}
	else
	{
		Passage_addCSSToDocument( Passage_getCSSCookieValue( sCSSCookieName_Other ).split(',') );
		Passage_addCSSToDocument( Passage_getCSSCookieValue( sCSSCookieName_Size ).split(',') );
		Passage_addCSSToDocument( Passage_getCSSCookieValue( sCSSCookieName_Color ).split(',') );
		Passage_addCSSToDocument( Passage_getURLParameter( sCSSURLParamName ).split(',') );
	}
}
function Passage_objCSSCookies( )
/*
	Constructor for easy access to css cookie handling functions:
		function toggleOther toggles the cookie associated with the 'other' css documents
		function toggleSize toggles the cookie associated with the 'size' css documents
		function toggleColor toggles the cookie associated with the 'color' css documents
*/
{
	// Initialize instance variables
	this.CSS_Larger		= "";
	this.CSS_Largest	= "";
	this.CSS_BW			= "";
	// Public object Methods
	this.toggleOther	= function( value, btoggle ) { return toggle( sCSSCookieName_Other, value, btoggle ) }
	this.toggleSize	= function( value, btoggle ) { return toggle( sCSSCookieName_Size, value, btoggle ) }
	this.toggleColor	= function( value, btoggle ) { return toggle( sCSSCookieName_Color, value, btoggle ) }
	
	// Overlay methods for simpler implementation:
	this.setOther	= function(value,btoggle) { if(this.toggleOther(value,btoggle)) doReload() };
	this.setLarger	= function() { if(this.toggleSize(this.CSS_Larger)) doReload() };
	this.setLargest	= function() { if(this.toggleSize(this.CSS_Largest)) doReload() };
	this.setBW	= function() { if(this.toggleColor(this.CSS_BW, true)) doReload() };
	this.reset	= function() { if(this.toggleSize('*') + this.toggleColor('*')) doReload() };
	this.fullreset	= function() { if(this.toggleSize('*') + this.toggleColor('*') + this.toggleOther('*')) doReload() };
	
	// Private methods
	function doReload()
	/*
		Checks if the calling page is in a frameset and if that frameset is from Passage CMS
		Then it reloads every single frame, or just itself if no Passage frameset is available
	*/
	{
		if( window.parent && parent.frames.length > 0 )
		{
			for( iC = 0; iC < parent.frames.length; iC++ )
			{
				// Reload current document
				window.location.reload();
				// Check if other frames should be reloaded
				sFrameName = parent.frames[iC].name;
				if
				(
					sFrameName != window.name &&
					(
						sFrameName == 'links' ||
						sFrameName == 'rechts' ||
						sFrameName == 'boven' ||
						sFrameName == 'londer'
					)
				)
				{
					parent.frames[iC].location.reload();
				}
			}
		}
		else
		{
			window.location.reload();
		}
	}
	function toggle( name, value, btoggle )
	/*
		Writes the given value to the cookie if it's different from its current value.
		
		@param	name		String, name of the cookie
		@param	value			String containing the comma-separated keys of CSS documents to be loaded
	*/
	{
		var bReturnValue = false;
		var sCurrValue	= Passage_getCookie( sCSSCookieName_Prefix + name );
		var bCurrEmpty	= ( sCurrValue == null || (sCurrValue == "" || sCurrValue == "*" ) ) ? true : false;
		var bNewEmpty	= ( value == "" || value == "*" ) ? true : false;
		var bDoToggle	= ( btoggle != null && btoggle == true ) ? true : false;
		var iDoWhat		= 0;
		
		if( bDEBUG ) {alert( 'Passage_objCSSCookies( ).toggle() - name=' + name + ', new value=' + value )}
		
		if
		(
			( bCurrEmpty && ! bNewEmpty ) ||
			( ! bCurrEmpty && sCurrValue != value )
		)
		{
			// Current value empty & new not empty, or current not empty and new is different
			// Set cookie to new value
			iDoWhat = 1;
		}
		
		if
		(
			! bCurrEmpty && sCurrValue == value && bDoToggle
		)
		{
			// Current value not empty & same as new & toggle = true
			// Empty the cookie
			iDoWhat = 2;
		}
		
		switch( iDoWhat )
		{
			case 1 :
				if( bDEBUG ) {alert( name + ' - Current value empty & new not empty, or current not empty and new is different, setting new' );}
				Passage_setCSSCookieValue( name, value );
				bReturnValue = true;
				break;
			case 2 :
				if( bDEBUG ) {alert( name + ' - Current value not empty & same as new & toggle = true, set to empty' );}
				Passage_setCSSCookieValue( name, "" );
				bReturnValue = true;
				break;
		}
		
		return bReturnValue;
		
	} // End function toggle
}
// Clear any cookies for this document
/*
// Passage_deleteCookie( sCSSCookieName );
// Passage_deleteCookie( sCSSCookieName, '/' );
Passage_deleteCookie( sCSSCookieName_Prefix + sCSSCookieName_Other, '/' );
Passage_deleteCookie( sCSSCookieName_Prefix + sCSSCookieName_Size, '/' );
Passage_deleteCookie( sCSSCookieName_Prefix + sCSSCookieName_Color, '/' );
*/
