// JavaScript code for working with the colorsets
// This script must be included in the <head> section of an HTML document,
//      as one of the functions must be called in the <head>.

// 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 sheets are added, they should be added in this block of code
colorsetlist[0] = 'blues';
colorsetlist[1] = 'classic';
colorsetlist[2] = 'contrast';
colorsetlist[3] = 'dartwith';
colorsetlist[4] = 'forest';
colorsetlist[5] = 'lavender';
colorsetlist[6] = 'maize';
colorsetlist[7] = 'orange';
colorsetlist[8] = 'pink';
colorsetlist[9] = 'purple';
colorsetlist[10] = 'scarlet';
colorsetlist[11] = 'sunshine';
colorsetlist[12] = 'teal';


// 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 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];

   // Write the stylesheet <link> tag to the document, and write the colorset to document.colorset
   document.write('<link rel="stylesheet" type="text/css" href="colorstyles/' + colorset + '.css"><\/link>');
   document.colorset = colorset;

   // 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.)

function colorform()

{

   // Write a form to the HTML document
   document.write('<form method="get" action="dummy" name="colorselector">');

   // Write some helpful text so users know what this form does
   document.write('Please select your color scheme:  ');

   // Write the start of the <select> tag which will contain the color menu
   document.write('<select size="1" name="colorset" ');

   // When a colorset is selected by the user, invoke cookiecolor to record that selection
   // Then reload the document with the cookie set, which will (assuming the document calls
   // setcolorset() ) set the document's colorset to the newly selected one
   document.write('onChange="cookiecolor(colorsetlist[colorset.selectedIndex]); location.reload(false);"'); 

   // Finish the <select> tag
   document.write('>');

   // Set up a for loop to step through the possible colorsets
   for( count = 0; count < colorsetlist.length; count++)

   {

      // Write the <option> tags within the <select> tag
      // First, start the <option> tag
      document.write('<option ');

      // 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]) document.write('selected="selected" ');

      // Next, write the value of the <option> tag, which will be a colorset name from the list
      document.write('value="' + colorsetlist[count] + '"');

      // Finally, finish the <option> tag
      document.write('>');

      // Within the <option></option> tags (i.e., here), a label for the option is needed
      // To make it look pretty, capitalize the first letter of the colorset name
      var capitalcolorset = colorsetlist[count].substring(0,1).toUpperCase() + colorsetlist[count].substring(1);
      document.write(capitalcolorset);

      // Close the <option> tag
      document.write('<\/option>');

   // Close the for loop for writing the <option> tags
   }

   // Close the <select> tag
   document.write('<\/select>');

   // Close the form
   document.write('<\/form>');

   // End the function
   return true;

}
