// These are the functions for the style sheet selections and individual element selections
// for Ray's Links web site.
// This function changes to the selected style sheet temporarily when the mouse is over the 
// selection, the var StyleFile passes in the url to the
// style sheet selected for Mozilla. Skin is the id of the default style sheet.
// this function sets the style sheet for Internet Explorer browsers
function setActiveStyleSheet(title) {
   var i, a, main;
   // load all of the style sheet info into an array and loop through them
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     // if the array isn't empty and this position in the array has a title attribute
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       // set all style sheets to disabled
       a.disabled = true;
       // except for the one that matches the passed in value for title
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}
// This function returns the title of the active style sheet in Internet Explorerm browsers
function getActiveStyleSheet() {
var i, a;
// load all link elements into an array and loop through them
 for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
  // if this position in the array is a style
  if(a.getAttribute("rel").indexOf("style") != -1
  // and has a title attribute
  && a.getAttribute("title")
  // and isn't currently disaabled it must be the active stylesheet, return it's title
  && !a.disabled) return a.getAttribute("title");
  }
  return null;
}
// this function is called by onmouseover to allow previewing a style without selecting it
function previewStyle(StyleFile, title)
{    // test browser type. IIE sets styles by style title attribute
     var Browser = navigator;
     if (Browser.appName == "Microsoft Internet Explorer") {
         setActiveStyleSheet(title);
     }
     // Mozilla sets styles by file name of the style sheet
     else {
         document.getElementById("Skin").href= StyleFile;
     }
}
// This function runs when javascript is enabled and replaces the default text in the 
// javascript is disabled message div with one of two messages, whichever is appropriate at the time.
function CookieWarn() {
      var Browser = navigator;
      var cookiesEnabled = Browser.cookieEnabled;
      if (cookiesEnabled==1) {
      document.getElementById("JavascriptWarn").innerHTML="Javascript is enabled. Cookies are enabled. You can change styles and they will stick." 
      }
      else {
      document.getElementById("JavascriptWarn").innerHTML="Javascript is enabled. Cookies are disabled. You can change styles but they will not stick until you <a href=\"http://www.google.com/cookies.html\">enable cookies</a>." 
      }
}
// This functions code was lifted directly from the W3C schools cookie tutorial page.
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  var c_start, c_end;
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return ""
}
// triggered when the user clicks on a text size selection, sets the font size
// and writes a cookie for retrieving the selected size on page loads.
function setFontSize(Size) {
 document.body.style.fontSize= Size;
 document.getElementById("navigate").style.fontSize= Size;
 document.getElementById("main").style.fontSize= Size;
 document.getElementById("mylinks").style.fontSize= Size;
 setCookie("TextSize",Size,365);
}
// if they are in IE we set a different cookie for the style sheet by style title
function setIECookie()
{
     var Browser = navigator;
     if (Browser.appName == "Microsoft Internet Explorer") {
         // get the title of the current style sheet
         var Stylesheet=getActiveStyleSheet();
         // and set it's value as a cookie named IEStyle
         setCookie("IEStyle",Stylesheet,365);
     }     
}
// this function sets the font size. Different pages have to be set differently so
// I'm passing in a name for the page setFonts was called from.
function setFonts(Page) {
// find out how big to make the text
var Size=getCookie('TextSize');
// only set the font size if there was a cookie set for it.
if (Size!=null && Size!="") {

      // for some reason I can't get the index page to address the font sizes in individual
      // elements of the page in Mozilla, so I have to set it globally to the body element
      if ( Page=="index" ) {
         document.body.style.fontSize= Size;
         // If they're using IE we need to set the indivdual elements in the page.
         var Browser = navigator;
         if (Browser.appName == "Microsoft Internet Explorer") {
         document.getElementById("intro").style.fontSize= Size;
         document.getElementById("welcome").style.fontSize= Size;
         document.getElementById("main").style.fontSize= Size;
         document.getElementById("mylinks").style.fontSize= Size;
         }
      }
      // The other pages all accept setting font sizes in individual elements, and won't 
      // change globally with just a specification to the body element, go figure???
      if ( Page!="index" ) {
         // this page doesn't have an html-code element so skip setting read only for it
         if ( Page!="submission" && Page!="notfound" ) {
         document.getElementById("html-code").readOnly=true; }// make the text boxes read only
         if ( Page!="notfound" ) {
         document.body.style.fontSize= Size;
         document.getElementById("navigate").style.fontSize= Size;
         document.getElementById("main").style.fontSize= Size;
         document.getElementById("mylinks").style.fontSize= Size;
         }
         else {
         document.body.style.fontSize= Size;
         document.getElementById("Welcome").style.fontSize= Size;
         document.getElementById("intro").style.fontSize= Size;
         document.getElementById("main").style.fontSize= Size;
         document.getElementById("mylinks").style.fontSize= Size;
         }
       }
  }
}

// This functions code was lifted right from the W3C tutorial page on cookies.
function setCookie(c_name,value,expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}
// This function runs on page load and retrieves the cookie settings for font size and style sheet
// and loads those settings for each page as it is loaded. A page name is passed in to handle 
// different pages differently.
function setStyle(Page) {
     // first check is this IE?
     var Browser = navigator;
     if (Browser.appName == "Microsoft Internet Explorer") {
        // returns the title of the style sheet set in the cookie
        var StyleSet=getCookie("IEStyle");
        if (StyleSet!=null && StyleSet!="") {
            // This function sets the style sheet in Internet Explorer
            setActiveStyleSheet(StyleSet);
        } 
     }
     else {
        // get and set the style sheet selection for Mozilla browsers
        // first get the value of the cookie named StyleSet
        var StyleSelection=getCookie('StyleSet');
        // only set a style if there was actually a cookie set
        if (StyleSelection!=null && StyleSelection!="") {
           // set the style by setting the style sheet url for the link with id = "Skin"
           document.getElementById("Skin").href= StyleSelection;
        }
        // This takes care of pages in sub folders off the main folder
        if ( Page=="script" ) {
           document.getElementById("Skin").href= "../" + StyleSelection;
        }
        // This takes care of 404 error page
        if ( Page=="notfound" ) {
           document.getElementById("Skin").href= "http://www.rayslinks.com/" + StyleSelection;
        }
     }
// Finally, we set the font size
var thepage=Page;
setFonts(thepage);
}

