/**********************************************************************
 *                                                                    *
 *      © 2010 - 2011 First Baptist Church of Morrisville,            *
 *      all rights reserved.                                          *
 *                                                                    *
 *      Common javascript functions for www.fbcmorrisville.net        *
 *                                                                    *
 *     Created by Rollin Hippler - last updated on October 2, 2011    *
 *                                                                    *
 **********************************************************************/

// This function is used to verify that I was here.
function test()
{
  alert("This is a test!");
}

// Return month and two digit year
function monthYear()
{
 var today = new Date();
 var year = today.getFullYear().toString().substr(2,2);
 var month = today.getMonth() +1;
 month = month.toString();
 month = (month.length == 1)? "0" + month : month;
 return month + "_" + year;
}

// Generate the navigation menu common to every page
// Each menu item definition consists of three parts:
//   1. The title text
//   2. The href string
//   3. A unique id number, 0 means open the target in  a new window
function genMenu(action)
{
 var mTitle = new Array;
 var mRef   = new Array;
 var mId    = new Array;
 var i = 0;
 var host = (window.location.host == "server")? "server/public" : "www.fbcmorrisville.net";

 mId[i] = 1;
 mTitle[i] = "Home";
 mRef[i] = "/";

 i++;
 mId[i] = 1;
 mTitle[i] = "About Us";
 mRef[i] = "/home/About_Us.html";

 i++;
 mId[i] = 0;
 mTitle[i] = "Directions";
 mRef[i] = "http://www.mapquest.com/maps/map.adp?searchtype=address&amp;country=US&amp;addtohistory=&amp;searchtab=home&amp;formtype=address&amp;popflag=0&amp;latitude=&amp;longitude=&amp;name=&amp;phone=&amp;level=&amp;cat=&amp;address=209+Church+St.&amp;city=Morrisville&amp;state=NC&amp;zipcode=";

 i++;
 mId[i] = 1;
 mTitle[i] = "Service Times";
 mRef[i] = "/home/service_times.html";

 i++;
 mId[i] = 1;
 mTitle[i] = "Contact Us";
 mRef[i] = "/home/contact.html";

 i++;
 mId[i] = 1;
 mTitle[i] = "Bulletins and Sermons";
 mRef[i] = "/bulletin/index.html";

 i++;
 mId[i] = 0;
 mTitle[i] = "Calendar";
 mRef[i] = "http://" + host + "/calendar/" + monthYear() + ".html";

 i++;
 mId[i] = 1;
 mTitle[i] = "Children's Ministry";
 mRef[i] = "/children/index.html";

 i++;
 mId[i] = 1;
 mTitle[i] = "Youth Ministry";
 mRef[i] = "/youth/index.html";

 i++;
 mId[i] = -1;
 mTitle[i] = "Group Discussions";
 mRef[i] = "bbs/logon.asp";

 i++;
 mId[i] = 2;
 mTitle[i] = "Member Pages";
 if (host == "server/public")
   mRef[i] = "http://server/members/";
 else
   mRef[i] = "https://members.fbcmorrisville.net/";

 i++;
 mId[i] = 0;
 mTitle[i] = "Member E-Mail";
 mRef[i] = "https://webmailcluster.perfora.net/";

 i++;
 mId[i] = -1;
 mTitle[i] = "DMin project";
 mRef[i] = "http://dmin.fbcmorrisville.net/";

 i++;
 mId[i] = 1;
 mTitle[i] = "Recommended Reading";
 mRef[i] = "/home/Recommended_Reading.html";

 i++;
 mId[i] = 1;
 mTitle[i] = "Related Links";
 mRef[i] = "/home/links.html";

 i++;
 mId[i] = 1;
 mTitle[i] = "See Us on YouTube";
 mRef[i] = "/home/YouTube_Videos.html";

 i++;
 mId[i] = 0;
 mTitle[i] = "How to become a Christian";
 mRef[i] = "http://www.sbc.net/knowjesus/default.asp";

 if (action != "append")
   document.write('<div class="menu"><ul>');
 for (i=0; i<mTitle.length; i++)
   switch(mId[i])
    {
     case 0:
       document.write('<li><a href="' + mRef[i] +'" target="_blank">' + mTitle[i] + '</a></li>');
       break;
     case 1:
       document.write('<li><a href="http://' + host + mRef[i] +'">' + mTitle[i] + '</a></li>');
       break;
     case 2:
       document.write('<li><a href="' + mRef[i] + '">' + mTitle[i] + '</a></li>');
       break;
     default: ;
    }
 if (action != "append")
   document.write('</ul></div>');
}

// Make email address harvesting more difficult
//   To add an entry:
//     * Add another to<xxxx> constant
//     * Add the corresponding case to the switch block
//
var toPastor     = 0;
var toOffice     = 1;
var toSecretary  = 2;
var toWebMaster  = 3;
var toChildDir   = 4;
var toYouthDir   = 5;
var toSSchoolDir = 6;
var toChoirDir   = 7;
function sendMail(toId, title)
{
 var atSign = "@";
 var user = "";
 var domain = "";

 switch(toId)
  {
   case toPastor:
     user = "\"Rev. Myron Yandle\"<myrony";
     domain = atSign + "fbcmorrisville.com>";
     break;
   case toOffice:
     user = "\"FBCM Office\"<office";
     domain = atSign + "fbcmorrisville.com>";
     break;
   case toSecretary:
     user = "\"Debra Hippler\"<office";
     domain = atSign + "fbcmorrisville.com>";
     break;
   case toWebMaster:
     user = "\"Rollin Hippler\"<webmaster";
     domain = atSign + "fbcmorrisville.com>";
     break;
   case toChildDir:
     user = "\"Karen Parker\"<karenp";
     domain = atSign + "fbcmorrisville.com>";
     break;
   case toYouthDir:
     user = "\"Maury Turner\"<mauryt";
     domain = atSign + "fbcmorrisville.com>";
     break;
   case toSSchoolDir:
     user = "\"Rollin Hippler\"<rollinh";
     domain = atSign + "fbcmorrisville.com>";
     break;
   case toChoirDir:
     user = "\"Melissa King\"<melissak";
     domain = atSign + "fbcmorrisville.com>";
     break;
   default:
     alert("ERROR: email id " + toId + " is not defined!");
     return;
  }
 document.write("<a href='mailto:" + user + domain + "'>" + title + "</a>");
}

function pageFooter()
{
 var d = new Date(document.lastModified);
 document.write('<div class="footer">');
 document.write('<p>Copyright &copy; 2011 First Baptist Church of Morrisville, all rights reserved.<br />');
 document.write("Experiencing problems with this page?" +
   "<br />Please let&nbsp;" +
   '<script language="javascript">sendMail(toWebMaster, "webmaster@fbcmorrisville.com");</script>&nbsp;' +
   "know about it via email.<br />");
 document.write("This page last updated: " + 
   d.toLocaleDateString() + " at " +
   d.toLocaleTimeString());
 document.write('</p></div>');
}

// Add Previous, Next and Close buttons to calendar pages
// The arguments to this function are the names of the previous and next
// calendar pages.  If either is the null string, a disabled button
// is added.
function addButtons(Prev, Next)
{
 if (Prev == "")
   document.write('<button disabled="disabled">&lt; Previous</button>&nbsp;&nbsp;');
 else
   document.write('<button onclick="parent.location=\'' + Prev + '.html\';">&lt; Previous</button>&nbsp;&nbsp;');
 if (Next == "")
   document.write('<button disabled="disabled">Next &gt;</button>&nbsp;&nbsp;');
 else
   document.write('<button onclick="parent.location=\'' + Next + '.html\';">Next &gt;</button>&nbsp;&nbsp;');
 document.write('<button onclick="window.close();">Close</button>');
}

function validateInput()
 {
  var month = document.getElementById('bMonth');
  var day = document.getElementById('bDay');
  if (!checkDate(month, day))
   {
    alert("Birth date requires valid selections for Month and Day");
    return false;
   }

  month = document.getElementById('aMonth');
  day = document.getElementById('aDay');
  if (!checkDate(month, day))
   {
    alert("Anniversary date requires valid selections for Month and Day");
    return false;
   }
  document.getElementById('action').value = 'Save';
  document.getElementById('profile').submit();
  return true;
 }

function checkDate(month, day)
 {
  if (month.value == '' && day.value == '')
    return true;
  if (day.value == '')
   {
    day.focus();
    return false;
   }
  if (month.value == '')
   {
    month.focus();
    return false;
   }
  validDates = [0,31,29,31,30,31,30,31,31,30,31,30,31];
  if (parseInt(day.value) > validDates[parseInt(month.value)])
   {
    month.focus();
    return false;
   }
  return true;
 }

