// JavaScript Document
// dragons_cookies.js
//		by Michael Lofgren, created 18/1/2004

//setPageConfig() stores data to the page_config cookie
//Data consists of top.navframes = "on" or "off", and
//top.navmenuid = 0 for main menu, and 1-? for submenus
//NB: frames and navmenuid have been defined in multiple places to make
//this work OK. Not sure this is the most elegant way to do this...
function setPageConfig() 
{
	var the_cookie;
	
	//validate frameson
	if (top.navframes != "on" && top.navframes != "off") top.navframes = "on";
	if (top.navmenuid < 0 || top.navmenuid >= 20) top.navmenuid = 0;
	
	the_cookie = "page_config=" + escape("navframes:" + top.navframes + "/" + 
			"navmenuid:" + top.navmenuid);
	the_cookie += ";path=/;";
	document.cookie = the_cookie;
} //end setPageConfig()

//readPageConfig() reads the page_config cookie and stores the results
//in top.navframes and top.navmenuid.
function readPageConfig() 
{
	//alert(window.location);	
	var cookievalue = readCookie("page_config"); //get the cookie value
	//alert("cookievalue = "+cookievalue);
	if (cookievalue != null) {
		top.navframes = readSubCookie(cookievalue, "navframes");
		top.navmenuid = parseInt(readSubCookie(cookievalue, "navmenuid"));	
	}

//alert(top.navframes);
//alert(top.navmenuid);
	
	if (top.navframes == null) {
		alert("subcookie navframes not found");
		top.navframes = "on";
//	} else {
//		alert("hello");
	}
	if (top.navmenuid == null) {
		alert("subcookie navmenuid not found");
		top.navmenuid = 0;
	}
	
	//alert("navframes = "+top.navframes);
	//alert("navmenuid = "+top.navmenuid);
	return;
} //end readPageConfig()

//Taking the string cookievalue from readCookie(), search for the subcookie of
//interest and return the subcookievalue.
function readSubCookie(cookievalue, subcookie)
{
	var values, the_property, the_value, subcookiedata;
	
	if (cookievalue != null) { //ie if a valid cookie is returned
		//Split cookie into its smaller elements... split using '/'
		var separated_values = cookievalue.split("/");

		for (var i = 0; i < separated_values.length; i++) {
			subcookiedata = separated_values[i];
			values = subcookiedata.split(":");

			the_property 	= values[0];
			the_value 		= values[1];

			if (the_property == subcookie) return the_value;
		}
	}
	return null; //only occurs if subcookie not found
} //end readSubCookie()

//readCookie() finds the cookie named name (if it exists), and returns it's value.
//Inputs: name is the name of the cookie
//Returns: null if the cookie does not exist, or the value of the cookie if it
// does exist.
function readCookie(name) 
{
	var firstchar, lastchar;
	
	if (document.cookie == '') { //no cookie
		return null;
	} else { //cookie exists
		the_supercookie = document.cookie;
		firstchar = the_supercookie.indexOf(name);
		if (firstchar != -1) { //found cookie
			firstchar += name.length + 1; //get stuff following name=

			//find the end of the value string
			lastchar = the_supercookie.indexOf(';', firstchar);
			if (lastchar == -1) lastchar = the_supercookie.length;
			return unescape(the_supercookie.substring(firstchar, lastchar));
		} else { //no cookie of that name found
			return null;
		}
	}
} //end readCookie()