//////////////////////////////////////////////////////////////////////
// the new cookie engine follows:

function setCookie( name, value ) // name, value, expires, path, domain;
{
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;

	var cookie_string = name + '=' + escape(value) + ';';
	if(argc > 2) cookie_string += 'expires=' + argv[2] + ';';
	if(argc > 3) cookie_string += 'path=' + argv[3] + ';';
	if(argc > 4) cookie_string += 'domain=' + argv[4] + ';';
	document.cookie = cookie_string;
}

function getCookie( cookie_name ) // returns the value of the specified cookie
{
	var our_cookie = null;
	var cookie_jar = document.cookie;	
	var cookies = cookie_jar.split(';');

	// for each crumb, split it in half and check the name;
	// if it agrees, return the cookie's value
	for ( var i = 0; i < cookies.length; i++ )
	{
		var this_cookie = cookies[i].split('=');
		while( this_cookie[0].charAt(0) == ' ' ) this_cookie[0] = this_cookie[0].substring(1);
		if( this_cookie[0] == cookie_name )
			return this_cookie[1];
	}
	return "";
}

function getCrumbs( cookie_name, cookie_array ) // returns an associative array of a cookie's crumbs
{
	var our_cookie = unescape(getCookie(cookie_name));
	var cookie_crumbs = our_cookie.split(';');
	for ( var i = 0; i < cookie_crumbs.length-1; i++ )
	{
		var this_crumb = cookie_crumbs[i].split('=');
		cookie_array[this_crumb[0]] = unescape(this_crumb[1]);
	}
}

function daysFromNow( how_many )
{
	var new_time = new Date();
	new_time.setTime(new_time.getTime() + how_many * 24 * 60 * 60 * 1000);
	return new_time.toGMTString();
}

//////////////////////////////////////////////////////////////////////
// Style Things go here

undefined = 'undefined';
var default_style = 'white';
var new_style = default_style;
new_style = getCookie('style');
if( new_style == undefined ) {
	/* IE... */
	new_style = default_style;
}

if( new_style != '' ) {
	document.write('<link rel="stylesheet" type="text/css" href="/' + new_style + '.css">\n');
} else {
	document.write('<link rel="stylesheet" type="text/css" href="/' + default_style + '.css">\n');
}

function setStyle( newStyle )
{
		var expires = new Date();
		expires.setTime(expires.getTime + (1000*3600*24*7)); 
		// one week, by default
		setCookie('style',newStyle,daysFromNow(7),'/');
		alert("This style choice will persist for a week, or until changed.\n You may need to reload the page or clear your browser's cache.\n\ncookie:" + document.cookie);
		self.location = self.location;
}

/*
var index = document.cookie.indexOf('style=');
if(index >= 0)
{
	var foo = new Array();
	readCookie(style,foo);
	
	var cstring = document.cookie.substring(index);
	var closeindex = cstring.indexOf(';')>-1 ? cstring.indexOf(';') : 255;
	var cstyle = cstring.substring(6,closeindex);
	if (cstyle.length != 0)
		document.write('<link rel="stylesheet" type="text/css" href="/' + cstyle + '.css">\n');
}
*/

/*
   The following cookie functions are adapted from code by
   Mattias Sjoberg, and are available at The Javascript Source,
   at http://javascript.internet.com/
*/
 /*

var exp = new Date(); 
exp.setTime(exp.getTime() + (7*24*60*60*1000));

function SetCookie (name, value) {  
  var argv = SetCookie.arguments;  
  var argc = SetCookie.arguments.length;  
  var expires = (argc > 2) ? argv[2] : null;  
  var path = (argc > 3) ? argv[3] : null;  
  var domain = (argc > 4) ? argv[4] : null;  
  var secure = (argc > 5) ? argv[5] : false;  
  document.cookie = name + "=" + escape (value) + 
  ((expires == null) ? "" : (";expires=" + expires.toGMTString())) + 
  ((path == null) ? "" : ("; path=" + path)) +  
  ((domain == null) ? "" : (";domain=" + domain)) +    
  ((secure == true) ? "; secure" : "");
}

function getCookieVal (offset) {  
  var endstr = document.cookie.indexOf (";", offset);  
  if (endstr == -1)    
    endstr = document.cookie.length;  
  return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {  
  var arg = name + "=";  
  var alen = arg.length;  
  var clen = document.cookie.length;  
  var i = 0;  
  while (i < clen) {    
    var j = i + alen;    
    if (document.cookie.substring(i, j) == arg)      
      return getCookieVal (j);    
    i  = document.cookie.indexOf(" ", i) + 1;    
    if (i == 0) break;   
  } 
  return null;
}


*/

//////////////////////////////////////////////////////////////////////
// And here's a random tangent function.

function showHide( divname ) {
   var node = document.getElementById( divname );
   if( node != null ) {
      if( node.style[ 'display' ] ) {
         if( node.style[ 'display' ] == 'none' ) {
            node.style[ 'display' ] = 'block';
         }
         else {
            node.style[ 'display' ] = 'none';
         }
      }
   }
}
