function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function isPageInCookie ( cookie_name, cookie_value )
{
	var results = get_cookie ( cookie_name );
	if ( results ) {
		var ca = results.split(',');
		for(var i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1,c.length);
			if (c == cookie_value) return true;
		}
		return false;
	}
	return false;
}

function set_cookie ( name, value, expires_year, expires_month, expires_day, path, domain, secure )
{
	cookie_string = name + "=" + escape ( value );
	if ( expires_year )
	{
		var expires = new Date ( expires_year, expires_month, expires_day );
		cookie_string += "; expires=" + expires.toGMTString();
	}

	if ( path )
	cookie_string += "; path=" + escape ( path );

	if ( domain )
	cookie_string += "; domain=" + escape ( domain );

	if ( secure )
	cookie_string += "; secure";

	document.cookie = cookie_string;
}


function append_cookie ( name, value, expires_year, expires_month, expires_day, path, domain, secure )
{
	var cookie_string;
	if (get_cookie( name )) {
		if (!isPageInCookie( name, value)) {
			cookie_string = name + "=" + escape ( value ) + "," + get_cookie( name );
		}
		else cookie_string = name + "=" + get_cookie( name );
	}
	else cookie_string = name + "=" + escape ( value );
	
	if ( expires_year )
	{
		var expires = new Date ( expires_year, expires_month, expires_day );
		cookie_string += "; expires=" + expires.toGMTString();
	}

	if ( path )
	cookie_string += "; path=" + escape ( path );

	if ( domain )
	cookie_string += "; domain=" + escape ( domain );

	if ( secure )
	cookie_string += "; secure";

	document.cookie = cookie_string;
}

function countPercentType(type) {
	var pages = 25;
	var pagesWithType = 0;
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (aCrumb[0] == type) 
		{
			aMolecule = aCrumb[1].split(",");
			pagesWithType = aMolecule.length;
		}
		else break;
	}
	return ((pagesWithType/pages)*100);
}

function countPageOfType(type) {
	var pagesWithType = 0;
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (aCrumb[0] == type) 
		{
			aMolecule = aCrumb[1].split(",");
			pagesWithType = aMolecule.length;
			return pagesWithType;
		}
	}
	return pagesWithType;
}

function checkIfVisited(name, value) {
	if (isPageInCookie(name, value))
	{
		document.write( "<b style=\"font-size:14px; color:blue\"> (Seen Before)</b>" );
	}
}

function checkIfSeenAll(type) {
	if ((type == "Cryo") && (countPageOfType(type) == 25))
	{
		document.write( "<b style=\"font-size:14px; color:green\"> (Seen All)</b>" );
	}
	if ((type == "Inuit") && (countPageOfType(type) == 20))
	{
		document.write( "<b style=\"font-size:14px; color:green\"> (Seen All)</b>" );
	}
	if ((type == "Wild") && (countPageOfType(type) == 16))
	{
		document.write( "<b style=\"font-size:14px; color:green\"> (Seen All)</b>" );
	}
}

function mainWritePagesRemaining(type) {
	
	var pagesSeen = countPageOfType(type);
	var totalPages;
	if ((type == "Cryo")) totalPages = 24;
	else if ((type == "Inuit")) totalPages = 19;
	else if ((type == "Wild")) totalPages = 15;
	else totalPages = -1; //In this case, there's a problem.
	
	var pagesLeft = totalPages - pagesSeen;
	if (pagesLeft == 0) document.write( "<td class=\"complete\" align=\"left\" width=\"268px\">Adventure Complete!</td>" );
	else if (pagesLeft == totalPages) document.write ( "<td align=\"left\" width=\"268px\">&nbsp;</td>" );
	else 
	{
		if (pagesLeft <= 3) document.write( "<td align=\"left\" class=\"close\" width=\"268px\">" );
		else if (pagesLeft <= 10) document.write( "<td align=\"left\" class=\"almost\" width=\"268px\">" );
		else document.write( "<td align=\"left\" class=\"incomplete\" width=\"268px\">" );
		document.write( "Pages seen: " + pagesSeen + "/" + totalPages + "</td>" );
	}
}

function checkIfCookiesEnabled() {
set_cookie( "foo", "bar" );
// if get_cookie succeeds, cookies are enabled, since the cookie was successfully created.
	if ( get_cookie( "foo" ) )
	{
		//document.write( "<p>Cookies are currently enabled.</p>" );
		// delete the test cookie
		delete_cookie("foo");
	}
	// if the get_cookie test fails, cookies are not enabled for this session.
	else
	{
		document.write( "<p>Cookies are not currently enabled. You must enable them if you want us to track your progress.</p>" );
	}
}

function change_parent_url(url) {
	document.location=url;
}	