// FormatSet.js | ver 3.2
// JavaScript Font Format & Style Setter
//
// Originally inspired by the persistant font settings in sites such as
// FanFiction.net and FanficAuthors.net
//
// Programming resources:
// The free tutorials available at W3Schools
// http://www.w3schools.com/
// The how-to at Alternative Style: Working With Alternate Style Sheets by Paul Sowden
// http://www.alistapart.com/stories/alternate/
// Browser compatablility issues from QuirksMode by Peter-Paul Koch
// http://www.quirksmode.org/
// And many, many thanks go to Google for performing so many, many searches!
// http://www.google.com/


// this part isn't a function; it runs as soon as the file loads
// downside: page display is held up until this module loads

if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }                        // this is to allow this script to work on IE4
}                            // this is also untested  

if(document.getElementById)  // a test to see if later functions will work
{
  var today=new Date();
  var EndDate=new Date(today.getTime()+730*24*60*60*1000); // now plus two years
  var FontCode=6;                        // default font-face for new visitors
  var FontSize=12;                       // default font-size for new visitors
  var PageStyle='Blue';                  // default style for new visitors
  var FontName=new Array(8);             // array that will hold available font names

// the next three lines check to see if the variables are already stored in local cookies
// if they are, then those values are used instead

  if(parseInt(GetCookie('HowRusFontSize'))>0){FontSize=GetCookie('HowRusFontSize');};
  if(parseInt(GetCookie('HowRusFontCode'))>0){FontCode=GetCookie('HowRusFontCode');};
  if(GetCookie('HowRusPageStyle').length>0){PageStyle=GetCookie('HowRusPageStyle');};

// the following determines which style and font options are available on a given page
// the selects are written in JavaScript because they require JavaScript to work
// if the browser can't use JavaScript, no useless controls are displayed
// they won't even see an empty DIV

  if(document.title=='Howard Russell' || document.title=='\"The Mailer Daemon\"')
  {         // these two pages get only style options, displayed in the top-right corner
    document.write("<div class=\"InHead\">");
    WriteStyleSelector();
    document.write("</div>");
  }
  else      // other pages get all options, displayed as a bar below the header
  {
    document.write("<div class=\"format\">");
    document.write("<div class=\"ftxt\">Print and display settings</div>");
    document.write("<div class=\"fsel\">");
    WriteStyleSelector();
    WriteFontChanger();
    document.write("</div></div>");
  };
  setCurrentStyle(PageStyle);
};


// end of the execute-on-load segment
// below are functions


function WriteStyleSelector() // the style options are written to the HTML
{ var sel;
document.write("<select name=stylechanger onChange=setCurrentStyle(value)>");
sel='';
if(PageStyle=='Blue'){sel=' selected="selected"'};
document.write("<option  value='Blue'"+sel+">Blue");
sel='';
if(PageStyle=='Grey'){sel=' selected="selected"'};
document.write("<option  value='Grey'"+sel+">Grey");
sel='';
if(PageStyle=='Purple'){sel=' selected="selected"'};
document.write("<option  value='Purple'"+sel+">Purple");
sel='';
if(PageStyle=='White'){sel=' selected="selected"'};
document.write("<option  value='White'"+sel+">White");

document.write("</select>");
};

function WriteFontChanger()   // this writes the font-size and font-face selectors
{ var i,sel;
document.write("&nbsp; <select name=sizechanger onChange=setCurrentSize(value)>");
  for(i=6;i<=30;i++)
   {
    sel='';
    if(FontSize==i){sel=' selected="selected"'};
    document.write("<option  value='"+i+"'"+sel+">"+i+" px");
   };
document.write("</select> &nbsp;");

document.write("<select name=fontchanger onChange=updateFontCode(value)>");

FontName[1]='\'Comic Sans MS\',cursive';sel='';
if(FontCode==1){sel=' selected="selected"'};
document.write("<option  value='1'"+sel+">Comic Sans");

FontName[3]='\'Courier New\',courier,monospace';sel='';
if(FontCode==3){sel=' selected="selected"'};
document.write("<option  value='3'"+sel+">Fixed Width");

FontName[0]='Arial,Helvetica,sans-serif';sel='';
if(FontCode==0){sel=' selected="selected"'};
document.write("<option  value='0'"+sel+">Sans-Serif");

FontName[5]='\'Times New Roman\',times,serif';sel='';
if(FontCode==5){sel=' selected="selected"'};
document.write("<option  value='5'"+sel+">Serif");

FontName[6]='Verdana,sans-serif';sel='';
if(FontCode==6){sel=' selected="selected"'};
document.write("<option  value='6'"+sel+">Verdana");


document.write("</select>");
};

function GetCookieValue(offset)
{
  var endstr=document.cookie.indexOf(";",offset);
  if(endstr==-1){endstr=document.cookie.length;};
return unescape(document.cookie.substring(offset,endstr));
};

function GetCookie(name)
{
  var arg=name+"=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  while(i<clen)
  {
    var j=i+alen;
    if(document.cookie.substring(i,j)==arg)
      {return GetCookieValue(j);};
    i=document.cookie.indexOf(" ",i)+1;
    if(i==0)
      {break;};
  };
  return-1;
};

function SetCookie(WhichCookie,CookieValue)
{
  document.cookie=WhichCookie+"="+escape(CookieValue)+";path=/;expires="+EndDate.toGMTString();
};

function setCurrentSize(value)
{
  {SetCookie('HowRusFontSize',value);
  document.getElementById('ChangeMe').style.fontSize=value+'px';};
};

function updateFontCode(FontCode)
{
  SetCookie('HowRusFontCode',FontCode);
  document.getElementById('ChangeMe').style.fontFamily=FontName[FontCode];
};

function setCurrentStyle(PageStyle)
{
  var i,a;
  SetCookie('HowRusPageStyle',PageStyle);
  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")==PageStyle){a.disabled=false;};
      }; // end if
    }; // end for
};
