// JavaScript Document

//
// embed contact email address in script code.
// updated design uses input fields and javascript
// to pass this address as the recipient value.
//
var primaryEmailAddress = "paula@transparentcy.org"

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// navigation link object
function NavigationLink (linkText, linkHREF, linkDesc, linkGroup)
// define an object to hold any page links used on the site.
// define object elements enough to be able to differentiate main, footer and sub-page links
//   and be able to easily insert the correct links with the corrent formatting.
{
	this.linkText  = linkText       /* the text displayed to the user */
	this.linkHREF  = linkHREF       /* the actual link */
	this.linkDesc  = linkDesc       /* text to go into the <a> TITLE attribute */
	this.linkGroup = linkGroup      /* used to identify a grouping of pages related to each other */
}
// end of NavigationLink object definition


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// insert navigation links
function InsertNavLinks(strPageSection, aryLinks, strForPageGroup)
// some special processing is necessary if the links to insert are children,
// because we want the parent tab to be correctly highlighted as well as the
// current page link of the child. If we are viewing a sub-page, we need the
// main link (parent) to be an active link AND to show as the current main
// page group.
{
	var navListBeg
	var navListEnd
	var navListCode = ""
	var linkCounter
	var rowCounter = 1
	var tabCounter = 1
	var linkInserted = 0
	var maxTabsPerRow = 6
	var forPage = location.href.substring(GetLastCharacterPosition(location.href, "/"), location.href.length)
	var atPage
	var aryTabLayout = new Array(1, maxTabsPerRow, 0) /* No. of rows, tabs per row, 1st or last row offset */
	
	//if we are processing tabs and there are more than maxTabsPerRow, calculate the
	// tab layout values:
	if (strPageSection == "tabs" && aryLinks.length > maxTabsPerRow)
	{
		aryTabLayout = getTabRowsAndTabsPerRow(aryLinks.length, maxTabsPerRow)
	}

	// define the appropriate list wrapping code:
	switch (strPageSection)
	{
			case "main" :
			case "tabs" :
			case "footer" :
				navListBeg = '<ul>'
				navListEnd = '</ul>'
				break;
			default :
				navListBeg = ""
				navListEnd = ""
				break;
	} // end wrapping code switch

	// loop the length our our array:
	for (linkcounter = 0; linkcounter < aryLinks.length; linkcounter++)
	{
		// parse the nav link array linkHREF value to get only the file name, no "/" type characters:
		atPage = aryLinks[linkcounter].linkHREF.substring(GetLastCharacterPosition(aryLinks[linkcounter].linkHREF, "/"), aryLinks[linkcounter].linkHREF.length)

		// wrap our link item using the appropriate html tags
		switch (strPageSection)
		{
			case "main" :
					if ((forPage == atPage) || (strForPageGroup == aryLinks[linkcounter].linkGroup))
					{
						// we are viewing a sub-page, so we need to keep the main current page image
						// so viewers know where they are, plus have the ability to get back to the main page group page.
						navListCode += '<li id="currentpage"><a '
					} else {
						navListCode += '<li><a '
						}
				navListCode += 'href="' + aryLinks[linkcounter].linkHREF + '" '
				navListCode += 'title="' + aryLinks[linkcounter].linkDesc + '"><span>'
				navListCode += aryLinks[linkcounter].linkText + '</span></a></li>'
				break;
			case "tabs" :
				if (aryLinks[linkcounter].linkGroup == strForPageGroup)
				{
					linkInserted++
					if (forPage == atPage)
					{
						// we are viewing a sub-page, so we need to keep the main current page image
						// so viewers know where they are, plus have the ability to get back to the main page group page.
						navListCode += '<li id="currentpage"><a '
					} else {
						navListCode += '<li><a '
						}
					navListCode += 'href="' + aryLinks[linkcounter].linkHREF + '" '
					navListCode += 'title="' + aryLinks[linkcounter].linkDesc + '"><span>'
					navListCode += aryLinks[linkcounter].linkText + '</span></a></li>'
					// control the number of links per row for the page top nav links:
					if (linkInserted == maxTabsPerRow)
					{
						navListCode += '<br />'
						maxTabsPerRow = linkcounter + 6
					}
				}
				break;

			case "footer" :
				navListCode += '[<a '
				navListCode += 'href="' + aryLinks[linkcounter].linkHREF + '" '
				navListCode += 'title="' + aryLinks[linkcounter].linkDesc + '">'
				navListCode +=  aryLinks[linkcounter].linkText + '</a>]&nbsp;'
				break;
		} // end switch of strPageSection
	
	
	} // end for number of items in our links array
	
	return (navListBeg + navListCode + navListEnd)
	
} // end function InsertNavLinks


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// some scripts need just the leafname of our current location:
// calling function can use the returned value as one argument to the
// <string>.substring(start, end) built-in function.
function GetLastCharacterPosition(strLocationHREF, strCharacter)
{
	var intLastSeparator
	var strSeparator = strCharacter
	var intCounter
	var intCurrentIndex
	
	// for the length of the argument locate the last instance of the separator
	for (intCounter = 0; intCounter < strLocationHREF.length; intCounter++)
	{
		intCurrentIndex = strLocationHREF.indexOf(strSeparator)
		if (strLocationHREF.substring(intCounter-1,intCounter) == strSeparator)
		{
			intLastSeparator = intCounter
		}
	}
	
	return(intLastSeparator)
} //end GetLastCharacterPosition()


//
// function to insert each page's copyright stuff
//
function getCopyrightFooter(pageUpdateDate)
{
	var returnCopyrightFooter
	var currentDate = new Date()
	
	returnCopyrightFooter = '<div class="copyright" id="copyright">'
	returnCopyrightFooter += 'Copyright &copy; 2001-'
	returnCopyrightFooter += currentDate.getFullYear()
	returnCopyrightFooter += ' TransParentcy'
	returnCopyrightFooter += '<br>All Rights Reserved'
	returnCopyrightFooter += '<br>{Last Updated: ' + pageUpdateDate + '}</div>'

	return returnCopyrightFooter

} // end getCopyrightFooter

//
// function to dynamically determine the number of row and tabs per row for
// a horizontal tab list.
//
function getTabRowsAndTabsPerRow(intNumberOfLinks, intTargetTabsPerRow)
{
	var intMaxTabsPerRow = intTargetTabsPerRow
	var intTestNumber = intNumberOfLinks
	var intTabRows = Math.round(intTestNumber / intMaxTabsPerRow)
	var intTabsPerRow = Math.round(intTestNumber / intTabRows)
	var intTabsMultiplied = intTabRows * intTabsPerRow
	var intLeftOverTabs = intTestNumber - ((intTabRows-1) * intTabsPerRow)
	var intRowAdjustment = intTestNumber - intTabsMultiplied
	
	return new Array(intTabRows, intTabsPerRow, intRowAdjustment)

/*
	//debugging stuff to be inserted into a script container in a document along with the
	// rest of this function's code.
	document.write('<br /><p>')
	document.write(intTestNumber + ' Tabs / ' + intMaxTabsPerRow + ' Max Tabs Per Row = ' + intTabRows + ' Tab Rows')
	document.write('<br />Tabs / Tabs per row: ' + intTabsPerRow + ' Calculated Tabs per Row')
	document.write('<br />Calculated Tab Rows * Calculated Tabs Per Row: ' + intTabsMultiplied)
	document.write('<br/>Tabs - (Calculated Tabs * (Tab Rows - 1)): ' + intLeftOverTabs + ' Tabs')
	if (intTestNumber >= intTabsMultiplied)
	{
		document.write('<br />Tabs added to last row: ' + (intTestNumber - intTabsMultiplied))
	} else {
		document.write('<br />Tabs removed from first row: ' + (intTabsMultiplied - intTestNumber))
	}
	document.write('</p>')
*/
} // end getTabRowsAndTabsPerRow