
// FILE: utilities.js 
// COPYRIGHT: First Reformed Church of Cary (caryreformedchurch.org) 2008

// Note: For these scripts to work at all:
// 1. All html files for the site must have suffix .htm (not .html)
// 2. All html files must be in the same "top level" directory  
// 3. In the local test environment (your own computer, not the server)
//	 the .htm files must be in a directory named "docs"
// 4. There can be only one directory named "docs" in the path
// 5. There should be no blanks in any of the directory or file names
// 6. To see useful debug information, append "?dbg" to the URL 
// Note: Due to many problems during initial move to ATT test server
//	    we now simply set dirPath to "" (null) (formerly it 
//	    was set to "./"). 

//INITIALIZE THIS .JS FILE:

//SET SOME GLOBAL CONSTANTS 
var cr	 = "Copyright 1998 - 2010, First Reformed Church of Cary"
var wm	 = "webmaster@caryreformedchurch.org"
var ip	 = "65.83.225.172" // ip addr for testing new (ATT) server
var domain = "caryreformedchurch.org"
var currentNewsletter = "newsletters/2010sep"

//INITIALIZE GLOBAL VARIABLES 
var dbg	  = false // assume not in debug mode
var sfx 	  = ""	// reset to "?dbg" if in dbg mode, & append it to links
var domainInURL = false // Assume LOCAL (NOT SERVER -- no domain name in URL)

var dirPrefix = ""  // prefix to use with file names.  At one time it appeared 
				// necessary to prefix all links with a different directory
				// path depending on the environment (local, server, or 
			     // test-server.  It now works with no prefix at all, but 
			     // setting it null was easier/safer than removing all refs
			     // to this variable.  Actually there is still one case where 
				// dirPrefix must be non-null.  See pageName function for 
				// details. 
var thisPage  = ""  // simple file name of currently linked page, w/o .htm
				// (used to hilite link in navigator column)
				// Will be set in the pageName function 

var url	    = location.href	    // the complete URL
var searchArg = location.search   // look for debug indicator, ?dbg

// Set debug mode if requested 
if (searchArg == "?dbg") {
	dbg = true
	sfx = "?dbg" // this will be appended to all generated links
   			   // to maintain debug mode in subsequent pages.
	document.write("<p>&nbsp;&nbsp;DEBUG MODE IS ON (?dbg found in URL)</p>")
}

// SEE IF THE IP ADDRESS FOR ATT SITE IS IN THE URL
// IF SO, WE ARE ON THE NEW ATT SITE (IN TEST MODE)
if (dbg && location.hostname == ip) {
	document.write("<br>&nbsp;&nbsp;" + 
			" We're on new ATT TEST site (ip addr found in URL) ")
}

// SEE IF DOMAIN NAME IS IN THE URL
if (url.indexOf(domain) != -1) {
	domainInURL = true
	if (dbg) {
 	  document.write("<br>&nbsp;&nbsp;" + " Domain name found in URL ")
	}
} 

if (dbg) {
  document.write("<p>&nbsp;&nbsp; location.href is: " + location.href + "</p>")
  document.write("<p>&nbsp;&nbsp; location.hostname is: " + location.hostname + 
	"</p>")
  document.write("<p>&nbsp;&nbsp; location.pathname is: " +
	location.pathname + "</p>")
  document.write("<p>&nbsp;&nbsp; location.search is: " +
	searchArg + "</p>")
}


// INVOKE pageName FUNCTION, which formerly determined the dirPrefix variable.
// It now mostly just determines the thisPage (as the simple name of the file 
// -- which determines the hiliting of links.  
// It does still set dirPrefix for one case. 

pageName() 


if (dbg) { 
	document.write("<p>&nbsp;&nbsp; dirPrefix: &quot;" + dirPrefix + "&quot;");
	document.write("<br>&nbsp;&nbsp; thisPage: " + "&nbsp;&nbsp;&quot;" + 	thisPage + "&quot;</p>");
}

// INITIALIZATION COMPLETE


// SET STRING CONSTANTS TO BE USED IN CONSTRUCTING THE HTML PAGE 
var startOuterTable = "<center><table width='850' cellpadding='0' cellspacing='0' border='0' bordercolor=#000099><tr><td>"

var endOuterTable = "</td></tr></table>" 

var headerTable = "<table class='HeaderBG' width='100%'cellspacing=0 cellpadding=0><tr valign=top><td width=15% align=center valign=middle><a href=" + dirPrefix + "index.htm" + sfx + "><img src=images/rcalogo.jpg border=0 alt='' height=113 width=71></a></td> <td class='ColNavBG' width=60% align=center valign=middle ><table border=0 cellspacing=0 cellpadding=0> <tr><td colspan=2 bgcolor=white>&nbsp;</td></tr> <tr><td colspan=2><p align=center><strong><font face='lucida calligraphy, arial, courier' size='6' color=white>First Reformed Church of Cary</font></strong></p></td></tr> <tr><td><p align=left><font face='arial' color=orange size='2'>&nbsp;&nbsp;(919) 469-0678</font></p></td> <td><p align=right><font face='arial' color=orange size='2'>Cary, North Carolina&nbsp;&nbsp;</font></p></td></tr> <tr><td colspan=2 bgcolor=white>&nbsp;</td></tr></table></td> <td width=25% valign=middle><a href=" + dirPrefix + "index.htm" + sfx + "><br><p align=center>&quot;A Growing Church <br>for a<br> Growing Community&quot; </p></a></td></tr><tr><td class='Divider' colspan='3'><img src=" + dirPrefix + "images/ecblank.gif alt='' height=1 width=1></td></tr></table>"

/* Not sure what ecblank.gif is.  It's not a file in the images directory as the  	code in headerTable above suggests.  But if remove that part of the code, 
	the dividing line between the header and the navigator/text area 	disappears!  I suspect the code is just using this dummy file name to get 
	an area set up to just display the background color defined for the
"Divider" class in the default.css file - tricky if so. 
   Also, in the code from orc, there seems to be an extra, 	unbalanced <tr> tag
   before the <td class='Divider' above. So WHY DOES this work at all??!! 
*/

var startBodyTable = "<table class='TextArea' width='100%' border='0' cellpadding='0' cellspacing='0' ><tr valign='top'>"

var endBodyTable = "</tr></table>" 

/* The Body Table will contain a single row and 3 columns, each defined
   by a TD. The content and announcement areas, defined in the next 2 variables,
   are the 2nd (center) and 3rd (right) TDs in the body tbl. The buildNavigator
   function (further down) will construct the 1st (left) TD within the body
   table. 
*/

var startEdgeSpacerTbl = "<table size=100% hspace=10 vspace=5><tr><td>" 
// <tr><td>&nbsp;</td></tr><tr><td>

var endEdgeSpacerTbl = "</td></tr></table>"

var startContentAreaTD = "<td class='TextArea' width='70%' align=center valign=top><table border='0' width='100%' cellspacing=0 cellpadding=0><tr><td>" + startEdgeSpacerTbl

var endContentAreaTD = endEdgeSpacerTbl + "</td></tr></table></td>" 

var startAnnounceAreaTD = "<td class='TextArea' width='20%' align=right valign=top><table border='0' width='100%' cellspacing=0 cellpadding=4> <tr><td>&nbsp;</td></tr> <tr><td><p align=center>&nbsp;&nbsp;&nbsp;<font size=3 color=orange><strong><u>Announcements</u>:&nbsp;&nbsp;&nbsp;</strong></font> </p></td></tr><tr><td>" 
// + startEdgeSpacerTbl

// <tr><td>&nbsp;</td></tr>  <tr><td>" + startEdgeSpacerTbl

var endAnnounceAreaTD = endEdgeSpacerTbl + "</td></tr></table></td>"

var footerTable = "<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td class='Divider' colspan=2><img src=images/ecblank.gif alt='' height=1 width=1></td></tr><tr valign=top><td class='footerbg'><font class=FooterText>" + cr + "</td><td class='footerbg><font class=FooterText>&nbsp;</td><td class='footerbg' align=right><font class=FooterText>Email: <a class=footerlinks href='mailto:" + wm + "'>" + wm + "</a></font></td></tr></table>"

var returnLink = "<div align=right><a href=index.htm><b class=rcmgblue>&nbsp;Back&nbsp;</b></a></div>"

var endPage = "</body><!-- WORKAROUND FOR MSIE CACHE BUG --><head><meta http-equiv='Pragma' content='no-cache'></head></html>" 

// FINISHED SETTING CONSTANTS


// DEFINE FUNCTIONS TO BE USED IN BUILDING THE PAGE

/* The following source code "format" is a pain to work with,
   but don't mess with it unless you really understand it. 
   JavaScript commands are generally considered done at a line break, 
   so if you break any of the lines to make 
   it easier to find things here, JavaScript can get very confused 
   and that's the last thing you want! 
*/  

// ---------------------------
// THE buildNavigator FUNCTION
// BUILDS THE NAVIGATOR COLUMN (the list of links in the left column)
// AS A TD ELEMENT OF THE BODY TABLE (IT BOTH STARTS AND ENDS THE TD)
// NOTE: Shouldn't there be a </a> following each of the link names?
//		Somehow it seems to work without it. ?? 
// ---------------------------
function buildNavigator() {

	return 	"<td class='ColNavBG' width= '10%'>" + 	
	"<table align=left border=0 cellspacing=0 cellpadding=0>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "index") + "href=" + dirPrefix + "index.htm" + sfx + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HOME</a></td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "welcome") + "href=" + dirPrefix + "welcome.htm" + sfx + ">&nbsp;&nbsp;<strong>WE&nbsp;&nbsp;WELCOME <br>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;YOU! </strong>&nbsp;</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
"<tr>" + fetchClass(thisPage, "contact") + " href=" + dirPrefix + "contact.htm" + sfx + ">&nbsp;&nbsp;&nbsp;CONTACT US&nbsp;</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
"<tr><td><font size=-1 color=yellow><b>&nbsp;<u>How&nbsp;to&nbsp;Find&nbsp;Us</u>&nbsp;&nbsp; </b></font></td></tr>" +
"<tr>" + fetchClass(thisPage, "map") + " href=" + dirPrefix + "map.htm" + sfx + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Map<br></td></tr>" +
"<tr>" + fetchClass(thisPage, "directions") + " href=" + dirPrefix + "directions.htm" + sfx + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Directions<br></td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "reformed") + "href=" + dirPrefix + "reformed.htm" + sfx + ">Reformed?<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... From What?!</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "ourbelie") + "href=" + dirPrefix +
"ourbelie.htm" + sfx + ">Our Basic Beliefs<br></td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
"<tr>" + fetchClass(thisPage, "brochure") + " href=" + dirPrefix + "brochure.htm" + sfx + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Brochure&nbsp;</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
"<tr><td><font size=-1 color=yellow><b>&nbsp; &nbsp;&nbsp;<u>Newsletter</u>&nbsp;</b></font></td></tr>" +
"<tr>" + fetchClass(thisPage, currentNewsletter) + 
" href=JavaScript:popup(\'" + dirPrefix + currentNewsletter +
".pdf\',1100,1000)>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Current<br></td></tr>" +"<tr>" + fetchClass(thisPage, "newsletterarchive") + " href=" + dirPrefix + "newsletterarchive.htm" + sfx + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Archive<br></td></tr>" +
"<tr>" + fetchClass(thisPage, "newsletter") + " href=" + dirPrefix + "newsletter.htm" + sfx + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Schedule<br></td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "pastorchuck") + " href=" + dirPrefix + "pastorchuck.htm" + sfx + ">Our Pastor </td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "worship") + " href=" + dirPrefix + "worship.htm" + sfx + ">How We Worship </td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "education") + " href=" + dirPrefix + 
"education.htm" + sfx + ">Christian<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Education </td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "music") + " href=" + dirPrefix + "music.htm" + sfx + ">Music Ministries </td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "comfort") + " href=" + dirPrefix + "comfort.htm" + sfx + ">Hope &amp; Comfort</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "fellowship") + " href=" + dirPrefix + 
"fellowship.htm" + sfx + ">Fellowship </td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "missionsupport") + " href=" + dirPrefix + 
"missionsupport.htm" + sfx + ">Missions<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Near & Far </td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "youth") + " href=" + dirPrefix + "youth.htm" + sfx + ">Our Youth </td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "adults") + " href=" + dirPrefix + "adults.htm" + sfx + ">Our Not-Quite-<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;So-Youthful!</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "building") + " href=" + dirPrefix + "building.htm" + sfx + ">Our Building</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "memories") + " href=" + dirPrefix + "memories.htm" + sfx + ">Memories</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "consistory") + " href=" + dirPrefix + "consistory.htm" + sfx + ">Consistory</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "emergency") + " href=" + dirPrefix + "emergency.htm" + sfx + ">Emergency<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Preparedness</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr><td nowrap>&nbsp;<a href=" + dirPrefix + "testpage.htm" + sfx + "><font color=#336699>Test Page</font></a></td></tr>" +
	"<tr><td> <p>&nbsp;</p> <p>&nbsp;</p></td></tr>" + 
	"<tr><td>&nbsp;</td></tr></table>"

} // end buildNavigator function


// Storage area for old junk: 
/* ----- Old Lines Extracted From the Above buildNavigator Function -----

	"<tr><td><hr size=1 color=navy></td></tr>" +
	"<tr>" + fetchClass(thisPage, "calendar") + " href=" + dirPrefix + "calendar.htm" + sfx + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Calendar</td></tr>" +

	"<tr>" + fetchClass(thisPage, "emergency") + " href=" + dirPrefix + "emergency.htm" + sfx + ">Emergency<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Preparedness</td></tr>" +
	"<tr><td><hr size=1 color=navy></td></tr>" +

	"<tr>" + fetchClass(thisPage, "capital/capital%20home") + " href=" + dirPrefix + "capital/capital%20home.htm>Capital Campaign</td></tr>" +
	"<tr><td> <hr size=1 color=navy> </td></tr>" + 

Note that  this one has  special font
	Tweek the .css file to get it? 
		  <p align="left"><strong>
          <font face="Monotype Corsiva" size="2"><a href="capital/capital%20home.htm">
Capital Campaign News </a></font> </strong></p>

<!--		  <p align="left"><font size="2"><strong><a href="http://www.carynews.com/105/story/2943.html" target="_blank">
Look For Us <br> In The Cary News</a></strong></font></p>
-->  
<p>&nbsp;</p>
   ------------------------------------------
*/
// end stored old junk! 


// ---------------------------
// THE fetchClass FUNCTION CAUSES HIGHLIGHTING 
// OF THE CURRENTLY SELECTED NAVIGATION ITEM
// ---------------------------
function fetchClass(thisPage, arg) {

	if (thisPage==arg) {	
		returnVal = "<td nowrap class='ColNavActive'>&nbsp;" + 
		  "<a class='ColNavActive'"
	} else {
		returnVal = "<td nowrap>&nbsp;<a class='ColNav'"
	}

	return returnVal

} // end fetchClass function


// ---------------------------
//GENERIC POPUP WINDOW FUNCTION(S): launch, closeWin, popup
//   Example of usage (formerly used in the music.htm file): 
//	   <a href="JavaScript:popup('anthem.htm')"> click here for details.</a> 
//	Unclear if we really need these, though: it seems that simply adding 
//	target="_blank" (note the underscore character) to the href 
//	for a link produces the same result as invoking these funcions!  
//	(See the newsletterarchive.htm file for many examples - thank you Becky!)
//	For example (replacement for above example from music.htm): 
//	   <a href="anthem.htm" target="_blank")"> click here for details.</a> 
//	However, I don't know if that trick works directly from the navigator
//	column or only within the .htm files.  Also, these functions control the 
//	size of the popup window, which use of target="_blank" doesn't do. 
//	It may also be positioning it differently on the screen so that it's more 
//	obviously a separate window?     
// ---------------------------
var remote
var oldURL
var nav=navigator.appName
var ver=navigator.appVersion.charAt(0)

function launch(newURL, newName, newFeatures, orgName) {
	if (nav=="Microsoft Internet Explorer" && remote!=null && oldURL!=newURL) {
		closeWin();
	}
	if (!remote || remote.closed || oldURL!=newURL) {
		remote = open(newURL, newName, newFeatures)
	if (oldURL!=null && nav == "Netscape" && ver!="2") {
		remote.focus()
		}
		oldURL = newURL
	} else if (nav == "Netscape"&& ver!="2") {
		remote.focus()
	} else {
		closeWin();
		remote = open(newURL, newName, newFeatures)
	}
	if (remote!=null && remote.opener == null) {
		remote.opener = window;
		remote.opener.name = orgName
	}
	return remote
} // end launch function

function closeWin() {
	remote.close();
	remote=null
} // end closeWin function

function popup(url,w,h) {
	if (url.indexOf("/")==-1) {
		url="/"+DB+"/Popup/" + url
	}
	if (h==null) {
		h=600
	}
	if (w==null) {
		w=800
	}
	var features="height=" + h + ",width=" + w + ",top=20,left=20,alwaysLowered=0,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=1,location=0,menubar=0,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar=0,z-lock=0";
	myRemote = launch(url,"PopUp1",features,"myWindow");

} // end popup function

// end generic popup window functions


// ---------------------------
// THE pageName FUNCTION sets the thisPage variable  
// ---------------------------
function pageName() {

	// Find start of the simple file name of the file

	// If domain name is in the url, we're in SERVER (vs. LOCAL) environment 
	if (domainInURL) {
	  fileNameStart = url.indexOf('.org') + 4
	  if (url.indexOf('.org/')!=-1) { 
  	    fileNameStart = fileNameStart + 1 // skip over slash
	  } 
	  if (dbg) {
	    document.write("<p>&nbsp;&nbsp;We're on a Server (not local test computer)</p>")
	  }
	}  
	// Otherwise, we're on a LOCAL test machine (NOT SERVER).
	else {
	  // Note: the "last" directory before filename has to be "docs" 
	  fileNameStart = url.indexOf('docs/') + 5;
	  if (dbg) {
	    document.write("<p>&nbsp;&nbsp;We're on a local computer, not server</p>")
	  }
	} 

	// Now set the thisPage variable
     // (used to hilite currently linked page, etc.) 
	if (url.indexOf('.htm')==-1) {
     // No file name in the path! 
	  thisPage = "index" // Set it to the default page, index.htm
	  if (location.hostname == ip) {
	  // We're on ATT Test server and no file name in the url
	   
	   dirPrefix = domain + "/" // When there's no file name (normal for 
						   // the initial url) the ATT Test server does
   						   // not insert the domain name in linked-to 
						   // urls - a major problem! (When a file name
						   // is present, however, it DOES!)  To 
   						   // compensate, we need to insert the domain
   						   // name ahead of the file name for this one 
   						   // case.  This means we will not be testing 
   						   // exactly the same code as on PortBridge, 
   						   // but what else can I do??  
	   if (dbg) {document.write("Setting dirPrefix to domain name: " + domain)}
	  }
	}
	else { 
     // File name is present	
	  endAdj = 4 // prepare to remove .htm from end
	  if (dbg) { 
		endAdj = endAdj + 4 // remove ?dbg also
	  }
	  thisPage = url.substring(fileNameStart, url.length-endAdj);
	}

} // end pageName function

// FINISHED DEFINING FUNCTIONS


// HERE'S WHERE THE REAL ACTION BEGINS!: 

// Using the functions and variables above,
// build the top (header) and left side (navigator) of the page
// and start the content area
document.write(startOuterTable + headerTable + startBodyTable)
document.write(buildNavigator() + startContentAreaTD)


// NOW LET THE CONTENT FILE (index.htm, welcome.htm, or whatever) TAKE OVER 
// to build the center (content area) of the page, again making use of 
// functions and variables defined above to keep things looking consistent. 

// Each content file should fill the content area, then do a document.write 
// of endContentAreaTD.  It can then (optionally) document.write
// startAnnounceAreaTD, fill the announcement area and write endAnnounceAreaTD.  
// It must then write endBodyTable, footerTable, endOuterTable, and endPage. 

// USE THE FILE "TEMPLATE PAGE.htm" AS A GUIDE!  

