function setActiveStyleSheet(title) {
   var i, a, main;
   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;
     }
  
  //write the cookie everytime you hit the button - it will be a session cookie
  //because we dont set a date 
  	 createCookie("style", title);
   
   }
 }

function getActiveStyleSheet() {
var i, a;
 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;
}

function getPreferredStyleSheet() {
  var i, a;
  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;
}

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 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 (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function rnd() {

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
}

function rand(number) {
return Math.ceil(rnd()*number);
}

function switchstyle() {

  var cookie = readCookie("style");
    
  //set up an array of the titles of the alternate stylesheets  
  var sheets = new Array();
  sheets[1] = "default";
  sheets[2] = "h2";
  sheets[3] = "h3";
  sheets[4] = "h4";   
  arraylen = sheets.length;
  
  //generate a random number to pick a random alternate stylesheet
  
  var randomsheet = rand(arraylen-1);
   
  //if there is no cookie, or it is null, set the 
  //title to the random alternate stylesheet	
  
  if ((cookie == "null") || (!cookie) )	{  
   		var title = sheets[randomsheet];
  }
 //if you have been there, set the title to the already-created cookie 
  else  {
   		var title = cookie;
  }
  
  // set the alternate stylesheet
   setActiveStyleSheet(title);	
}

switchstyle();