// JavaScript code for working with the colorsets

// Using this file:
// This file must be called from the <head> section of an XHTML document.
// It also requires a second stylesheet, which should be blank, e.g.,

/*
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="default.css" type="text/css"?>
<?xml-stylesheet href="color.css" type="text/css"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<link rel="stylesheet" type="text/css" href="default.css" title="Default"></link>
<script type="text/javascript" src="color.js"></script>
<link rel="stylesheet" type="text/css" href="color.css"></link>
<script type="text/javascript">document.colorset = setcolorset();</script>
*/

// In the above example code, the main stylesheet is "default.css", and "color.css" is a
// blank sheet used for the colors,which will be added to the blank sheet with the
// setcolorset() function.  setcolorset() must be called from the <head> section, and its
// return value (the name of the implemented colorset) must be stored to the colorset property.

// The color selector script uses a cookie to store the user's choice of colorset.  This requires
// a select element with id "colorsetselect" allowing the user to change the color.  After the
// select element is created, it should be populated with options by the coloroptions() function.
// An example block of code for this implementation follows:

/*
<fieldset>
<legend>Choose your color scheme:</legend>
<select size="1" id="colorsetselect" onchange="cookiecolor(colorsetlist[colorsetselect.selectedIndex][0]); document.location.reload(false);">
<option></option>
</select>
</fieldset>
<script type="text/javascript">coloroptions();</script>
*/

// Unlike previous versions of this script, no external stylesheets for colors are required.
// The different colorsets appear in an array in this file and are implemented by script.

// -----------------------------------------------------------------------------------------

// Initialize a new document property, colorset
document.colorset = null;

// Create the list of colorset names
var colorsetlist = new Array();

// Fill in the list of names; if new sets are added, they should be added in this block of code
// Each colorset takes the form [name,bgcolor,fgcolor,linkcolor,vlinkcolor], where each of the
// four colors is assumed to be entered in RGB HEX code format
colorsetlist[ 0] = ['blues'     ,'006','33f','FF0','F00'];
colorsetlist[ 1] = ['classic'   ,'CCC','000','00C','C00'];
colorsetlist[ 2] = ['contrast'  ,'000','FFF','99F','F99'];
colorsetlist[ 3] = ['dartwith'  ,'0F0','FFF','00C','C00'];
colorsetlist[ 4] = ['forest'    ,'060','630','00C','C00'];
colorsetlist[ 5] = ['lavender'  ,'FCF','606','00C','C00'];
colorsetlist[ 6] = ['maize'     ,'FF3','009','00C','C00'];
colorsetlist[ 7] = ['orange'    ,'F90','006','00C','C00'];
colorsetlist[ 8] = ['parkland'  ,'090','FF0','99F','F99'];
colorsetlist[ 9] = ['pink'      ,'F9C','C00','F33','F69'];
colorsetlist[10] = ['purple'    ,'606','FCF','99F','F99'];
colorsetlist[11] = ['scarlet'   ,'C00','FFF','00C','FF3'];
colorsetlist[12] = ['sunshine'  ,'FF6','F90','00C','C00'];
colorsetlist[13] = ['teal'      ,'9CC','306','00C','C00'];


// JavaScript function for setting a cookie to a specific colorset (passed as argument)
// The cookie expires at the end of this year
function cookiecolor(colorset)

{
   var datenow = new Date();
   var endofyear = new Date(datenow.getFullYear(), 11, 31, 23, 59, 59, 999);

   document.cookie = 'colorset=' + colorset + '; expires=' + endofyear.toGMTString();
   return true;
}


// JavaScript function for setting the colorset for a web page
// (This function must be called in the <head> section of an HTML document.)

// Function checks cookie for stored colorset value; if found, chooses that colorset
//                                                   if not found, chooses default (1) colorset
// Function then adds rules to second stylesheet to implement colorset
// Function returns name of colorset implemented
function setcolorset()

{

   // Check the cookies
   var cookielist = document.cookie;

   // Look for the colorset value
   var whereiscolorset = cookielist.indexOf('colorset=');

   // If there is a colorset value, read it
   if (whereiscolorset != -1)
   {
      var valuestart = whereiscolorset + 8 + 1;  // "colorset" is 8 characters, plus 1 for "="
      var valueend = cookielist.indexOf(';', valuestart);  // ";" is the end of the value
      if (valueend == -1) valueend = cookielist.length;    // safety just in case is last value; then EOF is end
      var colorset = cookielist.substring(valuestart, valueend);
   }

   // If there is no colorset value, use the default colorset (list entry 1)
   else
      var colorset = colorsetlist[1][0];


   // Decide if the colorset is valid and return its index; if not, use the default index (1)
   var colorsetindex = 1;

   // Set up a for loop to step through the possible colorsets
   for( count = 0; count < colorsetlist.length; count++)

   {

      // If the colorset name matches this index, set the colorsetindex to this position
      if (colorset == colorsetlist[count][0]) colorsetindex = count;

   }

   // At this point, the index is assumed valid.
   // Set the six properties
   document.styleSheets[1].insertRule('body {background-color: #' + colorsetlist[colorsetindex][1] +'; color: #' + colorsetlist[colorsetindex][2] +';}',0);
   document.styleSheets[1].insertRule('h1,div.banner {color: #' + colorsetlist[colorsetindex][1] +'; background-color: #' + colorsetlist[colorsetindex][2] +';}',1);
   document.styleSheets[1].insertRule('a:link {color: #' + colorsetlist[colorsetindex][3] +';}',2);
   document.styleSheets[1].insertRule('a:visited {color: #' + colorsetlist[colorsetindex][4] +';}',3);

   // Returns the colorset value
   return colorset;
}


// JavaScript function for user declaration of the colorset
// (This function must be called in the <body> section of an HTML document.)
// This function also assumes a <select id="colorsetselect"> element to populate.

function coloroptions()

{

   // Set up a for loop to step through the possible colorsets
   for( count = 0; count < colorsetlist.length; count++)

   {

      document.getElementById('colorsetselect').options[count] = new Option (colorsetlist[count][0].substring(0,1).toUpperCase() + colorsetlist[count][0].substring(1),colorsetlist[count][0]);
      document.getElementById('colorsetselect').options[count].style.backgroundColor = '#' + colorsetlist[count][1];
      document.getElementById('colorsetselect').options[count].style.color = '#' + colorsetlist[count][2];
      // Write the <option> tags within the <select> tag

      // If this is the currently selected colorset, it should be the currently selected menu option as well
      // (The only way this will happen is if setcolorset() has been invoked.  If it hasn't, then document.colorset
      // will be null, and not equal to any of the valid colorset names, so no value will become selected, which is perfectly fine.)
      if (document.colorset == colorsetlist[count][0]) document.getElementById('colorsetselect').options[count].selected = true;

   // Close the for loop for writing the <option> tags
   }

   // End the function
   return true;

}
