function setActiveStyleSheet(title) {
  var i, a, main;
  var isPrinterPage = document.URL.indexOf("print=yes");
  // not sure if we actually still use this page but it shows up in search results
  if (document.URL.indexOf("PrinterTemplate.cfm") > 0) isPrinterPage = 1;
  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;
      // printer-friendly style sheet is on only if on PrinterTemplate.cfm page
      if (isPrinterPage != -1 && a.getAttribute("title") == "printer") a.disabled = false;
      // text-only style sheet is on only if user has set it to on
      if (cookie2 == "on" && a.getAttribute("title") == "textonly") 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 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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var cookie2 = readCookie("textonly");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
  createCookie("textonly", cookie2, 365);
}

function setTextOnlyStyleSheet(){
  if (cookie2 == "off") cookie2 = "on";
  else cookie2 = "off";
//  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")) {
      // text-only style sheet is on only if user has set it to on
      if (cookie2 == "on" && a.getAttribute("title") == "textonly") a.disabled = false;
      else if (cookie2 == "off" && a.getAttribute("title") == "textonly") a.disabled = true;
    }
  }
}

function getTextOnlyValue(){
  return cookie2;
}


/* function printThisPage(){
  var i, a, main;
  var tempArray = new Array();
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (!a.disabled)
      tempArray[i] = "false";
    else
      tempArray[i] = "true";

    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
      // printer-friendly style sheet is on only if on PrinterTemplate.cfm page
      if (a.getAttribute("title") == "printer") a.disabled = false;
      // text-only style sheet is on only if user has set it to on
      if (cookie2 == "on" && a.getAttribute("title") == "textonly") a.disabled = false;
    }
  }

  window.print();

  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (tempArray[i] == "false")
      a.disabled = false;
    else
      a.disabled = true;
  }
} */


var cookie = readCookie("style");
var cookie2 = readCookie("textonly");

if (cookie2 == null){
  cookie2 = "off";
}

var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

var isPrinterPage = document.URL.indexOf("print=yes");
if (isPrinterPage != -1)
	window.print();
