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")) {
	if (a.getAttribute("title").indexOf("Font") == -1)
	  {
		  a.disabled = true;
	  }      
	  if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function setActiveFontStyleSheet(font) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
     
	  if (a.getAttribute("title").indexOf("Font") != -1)
	  {
		  a.disabled = true;
	  }
      if(a.getAttribute("title") == font) 
		  {
			a.setAttribute("title",getActiveStyleSheet());
			a.disabled = false;
			a.setAttribute("title",font);
		  }
    }
  }
}


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 getActiveFontStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled && a.getAttribute("title").indexOf("Font") != -1) 
	  {
		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,value1,value2,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+"="+value1+"&"+value2+expires+"; path=/";
}

function readSCookie(name) {
  var v1=1;
  var v2=1;
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i <= ca.length;i++) {
	var c = ca[i];
	var ind= c.indexOf(nameEQ);
	if (ind != -1)
	{
	var co= ind+6;
	var s=c.substring(co,c.length);
	var v1=s.substring(0,s.indexOf("&"));
	var nameFQ=v1+"&";
	if (s.indexOf(nameFQ) == 0) 
		{
			v2=s.substring(nameFQ.length,s.length);
		}
	return v1;
	}
}
  return null;
}
function readFCookie(name) {
  var v1=1;
  var v2=1;
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i <= ca.length;i++) {
	var c = ca[i];
	var ind= c.indexOf(nameEQ);
	if (ind != -1)
	{
	var co= ind+6;
	var s=c.substring(co,c.length);
	var v1=s.substring(0,s.indexOf("&"));
	var nameFQ=v1+"&";
	if (s.indexOf(nameFQ) == 0) 
		{
			v2=s.substring(nameFQ.length,s.length);
		}
	return v2;
	}
}
  return null;
}

window.onload = function(e) {
  var cookie = readSCookie("style");
  var fontcookie = readFCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  var font = fontcookie ? fontcookie : "Medium Font";
  setActiveStyleSheet(title);
  setActiveFontStyleSheet(font);
}

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

var cookie = readSCookie("style");
var fontcookie = readFCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
var font = fontcookie ? fontcookie : "Medium Font";
setActiveStyleSheet(title);
setActiveFontStyleSheet(font);
