/* provide pretty mouse over effect on the (LHS) menu for IE6 and earlier - Others are CSS driven*/
function menuHighlightOn(item) { item.className += ' menuSideHover'; }
function menuHighlightOff(item) {
var pattern = /\b\s?menuSideHover\b/;
item.className = item.className.replace(pattern, ''); }


function elementShowHide(elementToShowHide)
{ 
//support legacy browsers
if (document.all)
el = document.all[elementToShowHide];
else
el = document.getElementById(elementToShowHide);


//show-hide search area
if (el.style.display == ' none')
el.style.display = 'block';

if (el.style.display == '')
el.style.display = ' none';
}

//Show or hide an element
function elementHide(elementId)
{ 
//support legacy browsers
if (document.all)
el = document.all[elementId];
else
el = document.getElementById(elementId);

el.style.display = 'none';
}
function elementShow(elementId)
{ 
//support legacy browsers
if (document.all)
el = document.all[elementId];
else
el = document.getElementById(elementId);

el.style.display = '';
}


function addLoadListener(functionName){if (typeof window.addEventListener != 'undefined') {window.addEventListener('load', functionName, false);}else if (typeof document.addEventListener != 'undefined') {document.addEventListener('load', functionName, false);}else if (typeof window.attachEvent != 'undefined') {window.attachEvent('onload', functionName);} else{var oldfn = window.onload;if (typeof window.onload != 'function'){window.onload = functionName;} else{window.onload = function(){oldfn();functionName();};}}}


// Courtesy of http://www.quirksmode.org/dom/getElementsByTagNames.html
function getElementsByTagNames(list,obj){if (!obj) var obj = document;var tagNames = list.split(',');var resultArray = new Array();for (var i=0;i<tagNames.length;i++) {var tags = obj.getElementsByTagName(tagNames[i]);for (var j=0;j<tags.length;j++) {resultArray.push(tags[j]);}}var testNode = resultArray[0];if (!testNode) return [];if (testNode.sourceIndex) {resultArray.sort(function (a,b) {return a.sourceIndex - b.sourceIndex;});}else if (testNode.compareDocumentPosition) {resultArray.sort(function (a,b) {return 3 - (a.compareDocumentPosition(b) & 6);});}return resultArray;}


// Generate a ToC from page headings
// Field: headingLevels = Tags to convert into menu items
function generateTOC()
{
var menu = document.getElementById('TOC');

if (null == menu)
return;


var headings = getElementsByTagNames('h2,h3,h4', document.getElementById('colContent'));
var anchorContainer, anchor, linkId;

if (headings.length < 2) return; //only make a TOC if more than one heading


// Don't allow more than 10 headings unless they're all top level ones
if (headings.length > 10)
{
headings = getElementsByTagNames('h2', document.getElementById('colContent'));

if (headings.length > 10)
{
headings = getElementsByTagNames('h2', document.getElementById('colContent'));
}
}


menu.className = 'TOC';

var tocAnchor = document.createElement('a');
tocAnchor.setAttribute('name', 'pageTop');
tocAnchor.id = 'pageTop';
tocAnchor.className = 'TOCTitle';

var anchorText = document.createTextNode('Page content');
tocAnchor.appendChild(anchorText);
menu.appendChild(tocAnchor);

//Create the TOC
tocList = document.createElement("ul");


for (var entryPosition=0; entryPosition<headings.length; entryPosition++) 
{
var thisEntry = headings[entryPosition];

// Determine the link
linkId = thisEntry.id || uniqueId('tocTarget' + entryPosition);

// Define the anchor container
anchorContainer = document.createElement("li");
anchorContainer.className = 'tocEntry' + thisEntry.nodeName.toLowerCase();

// Define the anchor
anchor = document.createElement("a");
anchor.innerHTML = thisEntry.innerHTML; // TODO: Remove images, etc...
anchor.href = '#' + linkId;

// Add the item
anchorContainer.appendChild(anchor);
tocList.appendChild(anchorContainer);

// Modify the link target
thisEntry.id = linkId; // Change the element to ensure it has an id

//Make the wrapper around heading so we can add 'top of page' etc with impunity
var goTopWrapper = document.createElement('a');
goTopWrapper.href='#pageTop';
goTopWrapper.className = 'top';

var goTopContent = document.createTextNode('Top'); //Language concerns?
goTopWrapper.appendChild(goTopContent);

var hWrapper = document.createElement('div');
hWrapper.className = 'tocHeader';
hWrapper.appendChild(goTopWrapper);


//then copy heading inside wrapper
var hClone = thisEntry.cloneNode(true);
hWrapper.appendChild(hClone); 

//add wrapper next to target...
thisEntry.parentNode.replaceChild(hWrapper, thisEntry);
}

menu.appendChild(tocList);
}
// Generate a unique identity name
// Field: Identity to form the root
function uniqueId(root)
{
var unique = root;

while (document.getElementById(unique)) 
{
unique += "_";
}

return unique;
}


// Written: Jan 2007 (SEL)
//Create alternate row styles on correct table types
function highlightAlternateRows ()
{
var tables = document.getElementsByTagName('table');
var contentTables = getElementsByClassName(tables, 'contentTable');

for(var i = 0; i < contentTables.length; i++)
{
//get child nodes of type TBODY - but only the first so we don't affect tables within the one we want to update
var TBodyNode = contentTables[i].getElementsByTagName('TBODY')[0];

//get child nodes of TBODY that are TRs
var TBodyChildren = TBodyNode.childNodes;
var TRs = new Array();

//get an array of all TRs
for (var m = 0; m < TBodyChildren.length; m++)
{
var isHeader = false;

if (TBodyChildren[m].nodeName == 'TR')
{ 
var cells = TBodyChildren[m].childNodes;

for (var cell = 1; cell < cells.length; cell++) //start at one so we keep row highlighting for tables with vertical headers
{
//var thClassName = /\b\s?th\b/; //regex pattern for classname of th
if (cells[cell].nodeName == 'TH') /*|| (thClassName.test(cells[cell].className )) )*/
isHeader = true;
}

if (isHeader == false)
TRs[TRs.length] = TBodyChildren[m];
}
}

for(var j = 0; j < TRs.length; j++)
{
if ( j % 2 != 0) //even row
TRs[j].className = " altRow"; //leave a space so we don't interfere with existing classes
} 
}
}

function getElementsByClassName(elementArray, NameofClass)
{
var matchedArray = new Array();

for (var i = 0; i < elementArray.length; i++)
{
var pattern = new RegExp("(^| )" + NameofClass + "( |$)");

if (pattern.test(elementArray[i].className))
{
matchedArray[matchedArray.length] = elementArray[i];
}
}
return matchedArray;
}
 
function copyrightDate()
{
	// rewrite the copyright date
	// get todays year
	var d = new Date();
	var thisYear = d.getYear();
	
	var elementsInFooter = document.getElementById('footer').getElementsByTagName('P');
	for (var i=0;i<elementsInFooter[0].childNodes.length;i++) {
		var el = elementsInFooter[0].childNodes[i];
		if (el.nodeType==3 && el.data.match(new RegExp('2008'))) {
			el.data = el.data.replace(new RegExp('2008'), thisYear);
			break;
		}
	}
}
addLoadListener(copyrightDate);