function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase();
   this.isOpera       = (ua.indexOf('opera') != -1);
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) );
   }
var browser = new BrowserDetect();

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 getAlternateStyleSheetList(){
   var i,j;
   var list = new Array();
   j=0;
   for(i=0; (a=document.getElementsByTagName("link")[i]); i++){
      if(a.getAttribute("rel").indexOf("style") != -1
      && a.getAttribute("title")
      && a.getAttribute("rel").indexOf("alt") != -1){
         list[j]=a.getAttribute("title");
         j++;
      }
   }
   return list
}

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 removeCookie (name) {
   document.cookie = name+"="+"gone"+"; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/"
}

function setActiveStyleSheetIE (style) {
  setActiveStyleSheet(style);
}

function createSwitcher() {
   var opt = new Array;
   var list = new Array;
   var f=document.createElement('form');
   f.setAttribute('name','styleswitcher');
   selectf=document.createElement('select');
   selectf.setAttribute('size','1');
   selectf.setAttribute('name','style');
   if (browser.isIE) {
      selectf["onchange"] = new Function("setActiveStyleSheetIE(this.value)");
   }
   else {
      selectf.setAttribute('onChange','setActiveStyleSheet(this.form.style.value);');
   }
   opt[0]=document.createElement('option');
   opt[0].setAttribute('value',getPreferredStyleSheet());
   opt[0].appendChild(document.createTextNode(getPreferredStyleSheet()));
   list=getAlternateStyleSheetList();
   for(i=0;i<list.length;i++){
      j=i+1;
      opt[j]=document.createElement('option');
      opt[j].setAttribute('value',list[i]);
      if (list[i]==getActiveStyleSheet()){
         opt[j].setAttribute('selected','selected');
      }
      opt[j].appendChild(document.createTextNode(list[i]));
   }
   for(i=0;i<list.length+1;i++){
      selectf.appendChild(opt[i]);
   }
   var checker=document.createElement('input');
   checker.setAttribute('type','checkbox');
   checker.setAttribute('name','usecooky');
   checker.setAttribute('id','usecooky');
   f.appendChild(selectf);
   f.appendChild(document.createElement('br'));
   f.appendChild(checker);
   f.appendChild(document.createTextNode("cooky to remember"));
   document.getElementById("styleswitcher").appendChild(f);
   return null;
   }

window.onload = function(e) {

  //only if we produce a PDF-Newsletter
  if (this.showNewsletterLinks){
    showNewsletterLinks();
  };
  if (this.showNewsletterList){
    showNewsletterList()
  };

  //set the stylesheet and create the form to change it
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  if (document.getElementById("styleswitcher")) {
     createSwitcher();
  }
  if (document.getElementById('usecooky')){
     if (cookie) {
         document.getElementById('usecooky').checked=true;
      }
   }
}

// save selected style into cookie if user wishes that
window.onunload = function(e) {
  var title = getActiveStyleSheet();
   if (document.getElementById('usecooky')){
      if (document.getElementById('usecooky').checked) {
         if (readCookie("style")!=title) {
            createCookie("style", title, 30);
         }
      }
      else {
         if (cookie) {
         removeCookie("style");
         }
      }
   }
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);