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 (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

function cookieNotice() {
	createCookie('isenabled',"true",1);
	var check = readCookie('isenabled');
	if (check!="true") {document.write("<p id='smallnotice' align='center'>Please enable cookies</p>")};
}

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;
    }
  }
}

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 setStyle(style) {
document.getElementById("jsstyle").innerHTML="Style: " + style + "<div><!--This is the downarrow--></div>";
setActiveStyleSheet(style);
createCookie("ma_stylename", title, 365);
}


function styleSelectInit() {
document.getElementById("jsshow").innerHTML=document.getElementById("jshide").innerHTML;
document.getElementById("jsstyle").innerHTML="Style: " + readCookie("ma_stylename") + "<div><!--This is the downarrow--></div>";
	if (readCookie("ma_stylename")==null) {
		document.getElementById("jsstyle").innerHTML="Style: Dark" + "<div><!--This is the downarrow--></div>";
	}
 
}

window.onunload = function(e) {
	var title = getActiveStyleSheet();
	createCookie("ma_stylename", title, 365);
}

//read stored style from cookie and apply to page before the xhtml loads rather than after
	cookie = readCookie("ma_stylename");
	title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
