// This JavaScript was originally written by Paul Sowden, and was first 
// published at http://www.alistapart.com/articles/alternate/
// Paul Sowden has generously allowed me to use this Javascript on the
// MaraDNS web page and place this Javascript in the MaraDNS tarball
function setActiveStyleSheet(title) {
  var i, a, main;
  if(isOKbrowser()) {
      for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && 
               a.getAttribute("title")) {
         a.disabled = true;
          if(a.getAttribute("title") == title) a.disabled = false;
       }
    }
  }
}

function getActiveStyleSheet() {
var i, a;
  if(isOKbrowser()) {
     for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
      if(a.getAttribute("rel").indexOf("style") != -1
      && a.getAttribute("title")
      && !a.disabled) return a.getAttribute("title");
      }
  }
  return null;
}

// These cookie functions were written by Peter-Paul Koch
// and are available at http://www.quirksmode.org/js/cookies.html
// These functions are copyright-free, see
// http://www.quirksmode.org/about/copyright.html
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if(isOKbrowser()) {
		    if (c.indexOf(nameEQ) == 0)  {
			return c.substring(nameEQ.length,c.length);
                        }
                    }
	}
	return null;
}

// This function I wrote myself.  I am releasing this one
// JS function in to the Public Domain.  This function returns true on
// browsers with JS support but without good CSS support
// Using a blacklist
function isOKbrowser() {

  if(navigator.appVersion.indexOf) {
      if(navigator.appName == "Netscape" && 
         navigator.appVersion.indexOf("4") == 0 &&
         navigator.userAgent.indexOf("MSIE") == -1) {
            return false;
         }
      if(navigator.appName == "Microsoft Internet Explorer" && 
         navigator.appVersion.indexOf("MSIE 4") != -1) {
            return false;
         }
  // Ghostzilla's last release uses an ancient broken Gecko
  if(navigator.userAgent.indexOf("Gecko/2002") != -1) {
      return false;
      }

      return true;
  }	
  
  return true;

}

// These remaining functions were written, again, by Paul Swoden
// See above for source and copyright
function getPreferredStyleSheet() {
  var i, a;
  if(isOKbrowser()) {
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
      if(a.getAttribute("rel").indexOf("style") != -1
         && a.getAttribute("rel").indexOf("alt") == -1
         && a.getAttribute("title")
         ) return a.getAttribute("title");
    }
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  // If you're going to use this script, remove the following check which
  // just make sure th cookie value will make this a valid stylesheet (IE
  // bug workaround)
  if(title != "Woodson (Default)" && title != "Large Print") {
      title = "Woodson (Default)";
      }
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}


