/* style switcher functions */
function StyleOnload() {
  var sheet = ReadCookie("style");
  if(sheet) SetActiveStyleSheet(sheet);
}
function StyleOnunload() {
  var Title = GetActiveStyleSheet();
  CreateCookie("style", Title, 365);
}

function SetActiveStyleSheet(Title) {
  var active = GetActiveStyleSheet();
  var links = document.getElementsByTagName("link");
  for(var i = 0; i < links.length; i++)
    if(links[i].getAttribute("rel").indexOf("style")!=-1 && links[i].title)
      links[i].disabled = !(links[i].title == Title);
  if(Title == active)
    SetActiveStyleSheet("main");
}

function GetActiveStyleSheet() {
  var links = document.getElementsByTagName("link");
  for(var i = 0; i < links.length; i++)
    if(links[i].getAttribute("rel").indexOf("style")!=-1 && links[i].title && !links[i].disabled)
      return links[i].title;
  return null;
}

function GetPreferredStyleSheet() {
  var a, links = document.getElementsByTagName("link");
  for(var i = 0; (a = links[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("rel").indexOf("alt") == -1
        && a.title) {
      return a.getAttribute("title");
    }
  }
  return null;
}

/* cookie functions (used by styleswitcher) */
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 regex = new RegExp("$\s*"+name+"=(.*)");
  var cookies = document.cookie.split(';');
  for(var i = 0; i < cookies.length; i++) {
    var match = cookies[i].match(regex);
    if(match) return match[1];
  }
  return null;
}
