function DeleteCookie(name,path,domain) {
  if(GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function GetCookie(name, def) {
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++) {
    var aCrumb = aCookie[i].split("=");
    if (name == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  return def;
}

function cssSetCurToX(cssToLoad) {
  if(document.styleSheets){
    if(cssToLoad>=document.styleSheets.length) cssToLoad=0;
    for(var i=0;i<document.styleSheets.length;i++) {
      var ss=document.styleSheets[i];
      if(ss.id>'' && ss.id.indexOf("_")!=0);
      else
        ss.disabled = (i!=cssToLoad);
    }
  }
  var expdate = new Date ();
  expdate.setTime (expdate.getTime() + (30*24*60*60*1000)); // 30 days * 24 hrs * 60 min * 60 sec * 1000ms from now 
  SetCookie("cssCurrent", cssToLoad, expdate, "/");
}

function cssSetCurToNext() {
  var sheet=Number(GetCookie("cssCurrent", 0))+1;
  wrap=0;
  while(wrap<2) {
    while(sheet<document.styleSheets.length && document.styleSheets[sheet].id>'')
      sheet++;
    if(sheet>=document.styleSheets.length) sheet=0;
    wrap++;
  }
  cssSetCurToX(sheet);
}

function cssSetCurrent() { cssSetCurToX(GetCookie("cssCurrent", 1)); }

function cssFindID(x) { 
  for(var i = 0; i < document.styleSheets.length; i++) {
    if(document.styleSheets[i].id == x)
      return i;
  }
  return -1;
}

function cssToggleSheetX(x)    { document.styleSheets[x].disabled=(!document.styleSheets[x].disabled); }
function cssToggleSheet(cssID) { var x=cssFindID(cssID); if(x>0) cssToggleSheetX(x); }
function cssGetName(id, i)     { return "css_"+(id>""?id:"sheet"+String(i)); }
function cssGetState(id, i)    { return GetCookie(cssGetName(id,i), "disabled")!="disabled"; }

function cssSaveState() {
  var expdate = new Date ();
  expdate.setTime (expdate.getTime() + (365*24*60*60*1000)); // 365 days * 24hrs * 60min * 60sec * 1000ms from now 
  for(var i = 0; i < document.styleSheets.length; i++) {
    var sht=document.styleSheets[i];
    SetCookie(cssGetName(sht.id, i), sht.disabled ? "disabled" : "enabled", expdate, "/");
  }
}

function cssLoadState() {
  var found=0;
  for(var i=0; i<document.styleSheets.length; i++) {
    var sht=document.styleSheets[i];
    var x=sht.disabled = !cssGetState(sht.id, i);
    if(!(x)) found=1; // guard against disabling all of them if not set
  }
  if(!found)
    document.styleSheets[0].disabled = false;
}
