///////////////////////////////////////////////////////////////////////////////////
//                                                                               //
//  cookieTest.js  |  version 1.1                                                //
//  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  //
//                                                                               //
//  COPYRIGHT (C) 2004 MIKE TOE.  ALL RIGHTS RESERVED.                           //
//                                                                               //
//  revisions:                                                                   //
//  ---------                                                                    //
//  Mike Toe, 03/13/04 | made URLs absolute instead of relative.                 //
//  Mike Toe, 03/14/04 | implemented the "escape from another page's frameset"   //
//                       functionality.                                          //
//  Mike Toe, 03/17/04 | fnExtractUrlFromGoogleSearchString( )                   //
//  Mike Toe, 03/18/04 | fixed fnExtractUrlFromGoogleSearchString( ) so that it  //
//                       works even if there is no search string.                //
//                                                                               //
///////////////////////////////////////////////////////////////////////////////////


// ensure this page is the top-level page, so that cookies may be read/written.

if ( top.location != location )
{
	top.location.replace( fnExtractUrlFromGoogleSearchString( ) );
}

// check the cookie.

fnCheckCookie( );


////////////////////////////////
//  function fnCheckCookie( ) //
////////////////////////////////
//  (c) Mike Toe 2004. All rights reserved.
//
function fnCheckCookie( )
{
	// if a cookie exists...
	if ( document.cookie )
	{
		// create a cookie object whose properties are the cookie name-value pairs;
		var oCookie = fnParseNameValuePairs( document.cookie );

		// if that cookie object has a property named "conditionsAreAccepted" and if that property's value is "true"...
		if ( ( oCookie.conditionsAreAccepted ) && ( oCookie.conditionsAreAccepted == "true" ) )
		{
			// delete the cookie object, as it is no longer needed.
			oCookie = null;
		}
		else // the conditions were not accepted...
		{
			// so go to the terms and conditions page, sending this location as the query string.
			location.replace( "http://www.themodernist.com/conditions.html?" + location );
		}
	}
	else // a cookie does not exist...
	{
		// so go to the terms and conditions page, sending this location as the query string.
		location.replace( "http://www.themodernist.com/conditions.html?" + location );
	}
}


////////////////////////////////////////
//  function fnParseNameValuePairs( ) //
////////////////////////////////////////
//  (c) Mike Toe 2004. All rights reserved.
//
function fnParseNameValuePairs( sStringToParse )
{
	var oNamedValueContainer = new Object( );

	var aasCookies = sStringToParse.split( ";" );

	for ( var n = 0; n < aasCookies.length; ++n )
	{
		aasCookies[ n ] = aasCookies[ n ].replace( /^\s+/, "" ); 
		aasCookies[ n ] = aasCookies[ n ].replace( /\s+$/, "" ); 
		aasCookies[ n ] = aasCookies[ n ].split( "=" );
		oNamedValueContainer[ aasCookies[ n ][ 0 ] ] = decodeURIComponent( aasCookies[ n ][ 1 ] );
	}

	asNameValuePairs = null;

	return oNamedValueContainer;
}


/////////////////////////////////////////////////////
//  function fnExtractUrlFromGoogleSearchString( ) //
/////////////////////////////////////////////////////
//  (c) Mike Toe 2004. All rights reserved.
//
function fnExtractUrlFromGoogleSearchString( )
{
	var sSearch = location.search.substring( 1 );

	if ( sSearch.search( "cache" ) > -1 )
	{
		var asSearchChunks = sSearch.split( ":", 4 );

		if ( asSearchChunks[ 3 ] )
		{
			var asUrlAndAttributes = asSearchChunks[ 3 ].split( "+" );
			var sAbsolutePath = "http://" + asUrlAndAttributes[ 0 ];

			sSearch = null;
			asSearchChunks = null;
			asUrlAndAttributes = null;

			return sAbsolutePath;
		}
	}
	else
	{
		if ( sSearch != "" )
		{
			return sSearch;
		}
		else
		{
			return location;
		}
	}
}

