/*
 * Alternate style sheet selector for Internet Explorer
 * Keith Beidelman 7-Jan-2004 <keith@kgbsw.com>
 *
 * Usage:  In your HTML, include the following items:
 * 
 *         In the <HEAD> section:
 *           <LINK rel=stylesheet href=default.css type=text/css title=default>
 *           <LINK rel=stylesheet href=print.css type=text/css title=print>
 *           <SCRIPT type=text/javascript src=styles.js></SCRIPT>
 *
 *         In the <BODY> section:
 *           Add links to change the default style sheet of the form:
 *             <BUTTON onclick="return setStyle('default')">
 *               Default Style Sheet (with default title in the LINK)
 *             </BUTTON>
 *             <BUTTON onclick="return setStyle('print')">
 *               Style Sheet with print title in the LINK
 *             </BUTTON>
 *
 * Where:  'print' and 'default' come from titles of style sheets
 *          included in link tags. The stylesheet URL is in the href of
 *          the link tag.
 *    
 * Notes:  The first listed link will be the default link.  
 *         Alternate links can be listed under that.
 *
 * 	   The style sheets take effect for all pages on the site 
 *         which use this package.  The links to change the style 
 *	   only need to appear on a single page.  They take effect
 *	   for all pages.  The SCRIPT and the LINKs must be included
 *	   in all the pages.
 *
 *	   Once a browser selects a style, it is remembered for that
 *	   browser for 1 year using cookies.  If the user disables
 *	   cookies, the style change will only stick for the page 
 *	   where the change link was pressed.  No error telling the
 *	   user to enable cookies is ever displayed.
 */

var curstyle;

function getStyle () 
{  
var i, e, a;  

    e = document.getElementsByTagName ("link");
    for (i=0; a = e[i]; i++) 
      if (a.getAttribute ("rel").indexOf ("style") != -1 && 
          a.getAttribute ("title") && !a.disabled) 
        return a.getAttribute ("title");  
    return null;
}

function defaultStyle () 
{  
var i, e, a;  

    e = document.getElementsByTagName ("link");
    for (i=0; a = e[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 makeCookie (name, value, days) 
{  
var expires, date;

    if (days) 
      {    
      date = new Date ();    
      date.setTime (date.getTime () + days * 24 * 60 * 60 * 1000);    
      expires = "; expires=" + date.toGMTString ();  
      }  
    else 
      expires = "";  
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie (name) 
{  
var i, c, namevar, va;

    namevar = name + "=";  
    vc = document.cookie.split (';');  
    for (i = 0; i < vc.length; i++) 
      {    
      for (c = vc[i]; c.charAt (0) == ' '; c = c.substring (1, c.length));
      if (c.indexOf (namevar) == 0) 
        return c.substring (namevar.length, c.length);
      }  
    return null;
}

function setStyle (title) 
{  
var i, e, a, main;  

    e = document.getElementsByTagName ("link");
    for (i=0; a = e[i]; i++) 
      if (a.getAttribute ("rel").indexOf ("style") != -1 && 
          a.getAttribute ("title")) 
        a.disabled = (a.getAttribute ("title") != title);
    makeCookie ("style", title, 365);
    curstyle = title;
    return false;
}

function useCookieStyle ()
{
var cookie, style;

    cookie = readCookie ("style");  
    title = cookie ? cookie : defaultStyle ();  
    setStyle (title);
    return title;
}

window.onload = function (e) 
{  
    useCookieStyle ();
}

useCookieStyle ();

function rotStyle (a, b)
{
  if (curstyle == a)
    return setStyle (b);
  else
    return setStyle (a);
}

