// Set a Cookie with the given name and value.
// name       Name of the cookie
// value      Value of the cookie
// [expires]  Expiration date of the cookie (default: end of current session)
// [path]     Path where the cookie is valid (default: path of calling document)
// [domain]   Domain where the cookie is valid
//              (default: domain of calling document)
// [secure]   Boolean value indicating if the cookie transmission requires a
//              secure transmission
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

// Get the value of the specified cookie.
// name  Name of the desired cookie.
// Returns a string containing value of specified cookie,
//   or null if cookie does not exist.
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

// Delete a specified cookie.
// name      name of the cookie
// [path]    path of the cookie (must be same as path used to create cookie)
// [domain]  domain of the cookie (must be same as domain used to create cookie)
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// Generate a single menu entry in the old-style header.
function menuEntry(select, number, ref, name)
{
  var txt = '';
  txt += '<td align="center" width="12%">';
  txt +=   '<font face="arial">';
  txt +=     '<a href="' + ref + '" style="text-decoration:none">';
  if (select == number) {
    txt +=   '<font color="#E3BF5A"><b>' + name + '<\/b><\/font>';
  }
  else {
    txt +=   name;
  }
  txt +=     '<\/a>';
  txt +=   '<\/font>';
  txt += '<\/td>';

  return txt;
}

// Generate the text for the old-style header.
function generateOldHeader(select, prefix)
{
  var txt = '';
  txt += '<body topmargin=0 leftmargin=0 bgcolor="#FFFFFF" text="#000000" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">';
  txt += '<center>';
  txt += '<table border=0 cellpadding=0 cellspace=0 width="100%" cellspacing=0>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=   '<\/tr>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td align="center" valign="center">';
  txt +=       '<a href="' + prefix + 'index.htm"><img src="' + prefix + 'images\/LeftTitleBar.gif" height=88 border=0><\/a>';
  txt +=     '<\/td>';
  txt +=     '<td align="center" valign="center" height=88>';
  txt +=       '<a href="' + prefix + 'director.htm"><img src="' + prefix + 'images\/RightTitleBar.gif" border=0><\/a>';
  txt +=     '<\/td>';
  txt +=   '<\/tr>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=   '<\/tr>';
  txt += '<\/table>';
  txt += '';
  txt += '<table border=0 cellpadding=1 cellspace=0 width="100%" cellspacing=1>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     menuEntry(select, 1, prefix + "index.htm", "Home");
  txt +=     menuEntry(select, 2, prefix + "madcal.htm", "Concert Calendar");
  txt +=     menuEntry(select, 3, prefix + "information.htm", "About Us");
  txt +=     menuEntry(select, 4, prefix + "recordings.htm", "Recordings");
  txt +=     menuEntry(select, 5, prefix + "gallery/gallery.htm", "Photos");
  txt +=     menuEntry(select, 6, prefix + "links/links.htm", "Links");
  txt +=     menuEntry(select, 7, prefix + "members/membersOnly.htm", "For Members");
  txt +=   '<\/tr align="center">';
  txt +=   '<tr>';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=   '<\/tr>';
  txt += '<\/table>';
  return txt;
}

// Set the color of a menu entry by specifying the header style.
function headerStyle(select, current)
{
  var txt;
  if (select == current)
    txt = "SelHeaderlinks";
  else
    txt = "headerlinks";
  return txt;
}

// Generate the text for the page header with drop-down menus.
function generateNewHeader(select, prefix)
{
  var txt = '';
  txt += '<body bgcolor="#FFFFFF" text="#000000" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">';
  txt += '<table border=0 cellpadding=0 width="100%" cellspacing=0>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=   '<\/tr>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td align="center" valign="middle">';
  txt +=       '<a href="' + prefix + 'index.htm"><img src="' + prefix + 'images\/LeftTitleBar.gif" height=88 border=0><\/a>';
  txt +=     '<\/td>';
  txt +=     '<td align="center" valign="middle" height=88>';
  txt +=       '<a href="' + prefix + 'director.htm"><img src="' + prefix + 'images\/RightTitleBar.gif" border=0><\/a>';
  txt +=     '<\/td>';
  txt +=   '<\/tr>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=   '<\/tr>';
  txt += '<\/table>';
  txt += '<ul id="csstopmenu">';
  txt += '<li class="mainitems" style="border-left-width: 1px">';
  txt += '<div class="' + headerStyle(select, 1) + '"><a href="' + prefix + 'index.htm">Home<\/a><\/div>';
  txt += '<\/li>';
  txt += '<li class="mainitems" style="border-left-width: 1px">';
  txt += '<div class="' + headerStyle(select, 2) + '"><a href="' + prefix + 'madcal.htm">Concert Calendar<\/a><\/div>';
  txt += '<ul class="submenus">';
  txt +=   '<li><a href="' + prefix + 'madcal.htm">Coming Concerts<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'recentconcerts.htm">Recent Concerts<\/a><\/li>';
  txt += '<\/ul>';
  txt += '<\/li>';
  txt += '<li class="mainitems" style="border-left-width: 1px">';
  txt += '<div class="' + headerStyle(select, 3) + '"><a href="' + prefix + 'information.htm">About Us<\/a><\/div>';
  txt += '<ul class="submenus">';
  txt +=   '<li><a href="' + prefix + 'history.htm">History of Madrigalia<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'director.htm">Music Director<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'mission.htm">Mission and Vision<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'awards.htm">Awards<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'patron.htm">Become a Patron<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'wilhelm.htm">Roger Wilhelm<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'recentNews.htm">Recent News<\/a><\/li>';
  txt += '<\/ul>';
  txt += '<\/li>';
  txt += '<li class="mainitems" style="border-left-width: 1px">';
  txt += '<div class="' + headerStyle(select, 4) + '"><a href="' + prefix + 'recordings.htm">Recordings<\/a><\/div>';
  txt += '<ul class="submenus" style="width: 15em">';
  txt +=   '<li><a href="' + prefix + 'cd1.htm">Anthems Across America<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'cd2.htm">Christmas Across America<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'cd3.htm">Christmas With Harp and Voice<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'cd4.htm">Madrigalia Live at Christmas<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'cd5.htm">Music from the Emerald Isle<\/a><\/li>';
  txt += '<\/ul>';
  txt += '<\/li>';
  txt += '<li class="mainitems" style="border-left-width: 1px">';
  txt += '<div class="' + headerStyle(select, 5) + '"><a href="' + prefix + 'gallery\/gallery.htm">Photos<\/a><\/div>';
  txt += '<ul class="submenus">';
  txt +=   '<li><a href="' + prefix + 'gallery\/thumbnails\/board.htm">Board of Directors<\/a><\/li>';
  txt +=   '<li><a href="' + prefix + 'gallery\/thumbnails\/members.htm">Performing Members<\/a><\/li>';
  txt += '<\/ul>';
  txt += '<\/li>';
  txt += '<li class="mainitems" style="border-left-width: 1px">';
  txt += '<div class="' + headerStyle(select, 6) + '"><a href="' + prefix + 'links\/links.htm">Links<\/a><\/div>';
  txt += '<\/li>';
  txt += '<li class="mainitems" style="border-left-width: 1px">';
  txt += '<div class="' + headerStyle(select, 7) + '"><a href="' + prefix + 'members\/membersOnly.htm">For Members<\/a><\/div>';
  if ((select == 7) || (getCookie("numberOfVisits") != null))
  {
    txt += '<ul class="submenus">';
    txt +=   '<li><a href="' + prefix + 'members\/phpbb/">Member Forum<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/iemail.htm">E-mail an individual<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/schedule.htm">Upcoming meetings<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/minutes.htm">Minutes of meetings<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/treasurer.htm">Treasurer\'s reports<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/graphics.htm">Graphics and such<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/forms.htm">Fundraiser Forms<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/documents.htm">Corporate Documents<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/personnel.htm">Personnel list<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'members\/printcal.htm">Rehearsal calendar<\/a><\/li>';
    txt +=   '<li><a href="' + prefix + 'browsertest.htm">Browser test<\/a><\/li>';
    txt += '<\/ul>';
  }
  txt += '<\/li>';
  txt += '<\/ul>';
  txt += '<div id="clearmenu" style="clear: left"><\/div><br>';
  txt += '<center>';
  return txt;
}

// Determine whether this browser can handle the new menus.
function isNewBrowser()
{
    if (navigator.userAgent.match(/MSIE [0-5].[0-9]/))
    {
        // Internet Explorer 5 or lower.
        return 0;
    }
    if (navigator.userAgent.match(/MSIE [6-9].[0-9]/))
    {
        // Internet Explorer 6 or higher.
        return 1;
    }
    if (navigator.userAgent.match(/Firefox\/0.[0-9]/))
    {
        // Firefox pre 1.0.
        return 0;
    }
    if (navigator.userAgent.match(/Firefox\/[1-9].[0-9]/))
    {
        // Firefox 1.0 or higher.
        return 1;
    }
    if (navigator.userAgent.match(/Netscape\/[7-9].[0-9]/))
    {
        // Netscape 7.0 or higher.
        return 1;
    }
    if (navigator.userAgent.match(/Safari\/[4-9][0-9][0-9]\.[0-9]/))
    {
        // Safari 400.0 or higher.
        return 1;
    }
    if (navigator.userAgent.match(/Konqueror\/[2-9].[0-9]/))
    {
        // Konqueror 2.0 or higher.
        return 0;
    }
    if (navigator.appName.match(/Netscape/))
    {
        // Netscape pre 7.0 or compatible.
        return 0;
    }
    // unknown browser type
    return 0;
}

// Generate the text for a header, selecting either the old or new style.
function generateHeader(select, prefix)
{
  if (isNewBrowser())
  {
    return generateNewHeader(select, prefix);
  }
  else
  {
    return generateOldHeader(select, prefix);
  }
}

// Generate the text for the common page footer.
function generateFooter(prefix)
{
  var txt = '<\/center>';
  txt +=   '<table border=0 cellpadding=0 width="100%" cellspacing=0>';
  txt +=   '<tr>';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=   '<\/tr>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td width=33%>&nbsp;<\/td>';
  txt +=     '<td width=33%>&nbsp;<\/td>';
  txt +=     '<td width=33%>&nbsp;<\/td>';
  txt +=   '<\/tr>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td align="center" valign="middle">';
  txt +=       '<font face="arial" color="#FFFFFF" size=2>Web services provided by<\/font><br>';
  txt +=       '<a href="http:\/\/www.ggw.org" target="_blank">';
  txt +=       '<font face="arial" color="#FFFFFF" size=2>The Genesee Gateway<\/font>';
  txt +=       '<\/a>';
  txt +=     '<\/td>';
  txt +=     '<td align="center" valign="middle">';
  txt +=       '<a href="http:\/\/www.icra.org\/labelv02.html" target="_blank">';
  txt +=         '<img src="' + prefix + 'images\/icra_sb.gif" border=0 width=88 height=31 alt="Labelled with ICRA">';
  txt +=       '<\/a>';
  txt +=     '<\/td>';
  txt +=     '<td align="center" valign="middle">';
  txt +=       '<font face="arial" color="#FFFFFF" size=2>Scripts adapted from<\/font><br>';
  txt +=       '<a href="http:\/\/www.javascriptkit.com" target="_blank">';
  txt +=       '<font face="arial" color="#FFFFFF" size=2>JavaScript Kit.<\/font>';
  txt +=       '<\/a>';
  txt +=     '<\/td>';
  txt +=   '<\/tr>';
  txt +=   '<tr bgcolor="#000060">';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=     '<td>&nbsp;<\/td>';
  txt +=   '<\/tr>';
  txt += '<\/table>';
  txt += '<\/body>';
  return txt;
}

// Print the page header to the current document.
function printHeader(select, prefix)
{
  var txt = generateHeader(select, prefix);
  window.document.open();
  window.document.write(txt);
  window.document.close();
}

// Print the page footer to the current document.
function printFooter(prefix)
{
  var txt = generateFooter(prefix);
  window.document.open();
  window.document.write(txt);
  window.document.close();
}

// Pick a picture to be used in the home page.
// The parameters are names of pictures in the images directory.
function pickPicture()
{
  // Determine the number of parameters passed.
  var number = arguments.length;

  // Pick a number in [0 .. number). If a cookie is available
  // describing the last selection, use that as a starting point.
  var select = Math.floor(Math.random() * number);
  if (getCookie("lastPicture") != null)
    select = Number(getCookie("lastPicture")) + 1;
  if (select >= number)
    select = 0;

  // Save the picture number for next time.
  setCookie("lastPicture", select);

  // Select the picture based on the selection.
  var picture = arguments[select];

  // Generate the HTML code for the image.
  var txt = '<img src="images\/' + picture + '">';

  window.document.open();
  window.document.write(txt);
  window.document.close();
}

// Print a single table entry for a song on a CD.
// title    the title of the song
// composer the composer's name
// [site]   the composer's web site
// [audio]  URL of audio file sample of the song
function song(title, composer, site, audio)
{
  var txt = '<tr>';
  txt += '<td width="50%">';
  if (audio != undefined)
  {
    txt += '<a href="samples\/' + audio + '" class="songtext">';
    txt += title;
    txt += '<\/a>&nbsp;<img src="images\/audio.gif">';
  }
  else
  {
    txt += title;
  }
  txt += '<\/td><td width="50%">';
  if (site != undefined && site != '')
  {
    var target = '';
    if (site.substring(0, 7) == 'http:\/\/')
        target = ' target="_blank"';
    txt += '<a href="' + site + '"' + target + ' class="composer">' + composer + '<\/a>';
  }
  else
  {
    txt += composer;
  }
  txt += '<\/td><\/tr>';

  window.document.open();
  window.document.write(txt);
  window.document.close();
}

// Print a link to a subpage, fixing the font and color for a white background.
function subPage(title, file)
{
  var txt = '';
  if (file != '')
  {
    txt += '<a href="' + file + '" class="submenu">';
  }
  txt +=     title;
  if (file != '')
  {
    txt += '<\/a>';
  }
  txt += '<br>';

  window.document.open();
  window.document.write(txt);
  window.document.close();
}

// Include some padding in-line.
function padding()
{
  window.document.open();
  window.document.write("&nbsp;&nbsp;&nbsp;&nbsp;");
  window.document.close();
}

// Some useful date data.
var dayNames, monthNames, thisYear;

// Compute a date code to be used for retiring table entries.
function currentDateCode()
{
    var d = new Date();
    year = d.getFullYear();
    month = d.getMonth() + 1;
    day = d.getDate();
    code = ((year * 100) + month) * 100 + day;

    dayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    monthNames = new Array("January", "February", "March", "April", "May", "June",
                               "July", "August", "September", "October", "November", "December");
    thisYear = year;
    return code;
}

// Compute a date string corresponding to a coded date.
function dateString(retire)
{
    var d = new Date();
    var year = Math.floor(retire / 10000);
    var month = Math.floor(retire / 100) % 100 - 1;
    var day = Math.floor(retire % 100);
    d.setFullYear(year, month, day);
    var dayOfWeek = d.getDay();
    var txt = dayNames[dayOfWeek] + ', ' + monthNames[month] + ' ' + day;
    if (year != thisYear)
    {
        txt += ', ' + year;
    }
    return txt;
}

// CSS Top Menu- By JavaScriptKit.com (http://www.javascriptkit.com)
// Adopted from SuckerFish menu
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// Please keep this credit intact

startMenu = function() {
  if (document.all&&document.getElementById) {
    cssmenu = document.getElementById("csstopmenu");
    for (i = 0; i < cssmenu.childNodes.length; i++) {
      node = cssmenu.childNodes[i];
      if (node.nodeName == "LI") {
        node.onmouseover = function() {
          this.className += " over";
        }
        node.onmouseout = function() {
          this.className = this.className.replace(" over", "")
        }
      }
    }
  }
}

// Add mouse-over events on all menu entries.
if (window.attachEvent)
  window.attachEvent("onload", startMenu)
else
  window.onload = startMenu;

// Store the date code in a global variable.
var dateCode = currentDateCode();

window.document.open();
window.document.write('<link rel="shortcut icon" href="http:\/\/www.madrigalia.org\/images\/madIcon.ico" \/>');
window.document.close();
