var IMAGEPATH = "images/";

// Define colors in logo
var LOGOCOLOR1 = "#002D62";
var LOGOCOLOR2 = "#6EA023";
// Define collapse and expand symbols
var COLLAPSE = '-';
var EXPAND = '+';

// Add onload event to set target of external links
addLoadEvent(externalLinks);

/*
 * Event handler to execute after page has loaded
 *
 * @param func - Function to execute once the page has loaded 
 */
function addLoadEvent(func)
{
	// Store existing onload event
	var oldonload = window.onload;
	// Assign function parameter to onload event if none exists
	if (typeof window.onload != 'function') {
		window.onload = func;
	// Create new function to call existing and new function
	} else {
		window.onload = function()
		{
			if (oldonload) {
				oldonload();
			}
			func();
		} // function()
	} // if/else
} // addLoadEvent

/*
 * Get the AJAX XMLHttpRequest object
 *
 * @return xmlHttp - XMLHttpRequest object
 */
function ajaxObject(func)
{
	var xmlHttp;

	// Firefox, Opera 8.0+, Safari
	try {
		xmlHttp=new XMLHttpRequest();
	}
	// Internet Explorer 6+
	catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			// Internet Explorer 5.5+
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
//				alert("Your browser does not support AJAX!");
				return null;
			}
		}
	}

	return xmlHttp;
} // ajaxObject

/*
 * Check search input form for errors
 *
 * @param form - Form element containing search input 
 */
function checkSearchForm(form)
{
	if (!form) {
		form = document.getElementById('searchForm');
	}

	if (form.terms.value == '') {
		alert('Please enter search text.');
		return false; 
	}
	else {
		form.submit();
	}
} // checkSearchForm

/*
 * Collapse/expand element ID and associated symbol ID
 *
 * @param id - Element ID of container
 * @param symbolId - Element ID of symbol to change
 */
 function collapseExpand(id, symbolId)
{
	elem = document.getElementById(id);
	if (elem.style.display == "none") {
		elem = document.getElementById(symbolId);
		elem.innerHTML = COLLAPSE;
		show(id);
	}
	else {
		elem = document.getElementById(symbolId);
		elem.innerHTML = EXPAND;
		hide(id);
	}
} // collapseExpand

/*
 * Bread crumbs navigation
 *
 * @param separator - Character to separate directories
 * @param index - Name of index page
 */
 function crumbs(separator, index)
{
	sURL = new String;
	bits = new Object;
	var x = 0;
	var stop = 0;
	var output = '<a href="/">Home</a>';
	var currPage = '';
	sURL = location.href;
	sURL = sURL.slice(8,sURL.length);
	chunkStart = sURL.indexOf("/");
	sURL = sURL.slice(chunkStart+1,sURL.length)
	while(!stop){
		chunkStart = sURL.indexOf("/");
		if (chunkStart != -1){
			bits[x] = sURL.slice(0,chunkStart)
			sURL = sURL.slice(chunkStart+1,sURL.length);
		}
		else {
			stop = 1;
		}
		x++;
	} // while
	for(var i in bits){
		output += '&nbsp;' + separator + '&nbsp;<a href="';
		for(y=1;y<x-i;y++) {
			output += "../";
		}
		output += bits[i] + '/">' + bits[i] + '</a> ';
	} // for

	// Use index.htm as directory index if none given
	if (!index) {
		index = 'index.htm';
	}
	// Retrieve current page name if not directory index
	if (getPageName().indexOf(index) == -1) {
		currPage = getPageName();
		currPage = currPage.substring(0, currPage.indexOf('.'));
		output += separator + ' ' + currPage;
	}

	document.write(output);
} // crumbs

// Change the 'rel="external"' attribute on all anchor tags to the _blank target
function externalLinks()
{
	if (!document.getElementsByTagName) {
		return;
	}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	} // for...
} // externalLinks()

// Get page name from URI
function getPageName(url)
{
	if (url == null) {
		url = location.href;
	}
	return url.substring(url.lastIndexOf('/') + 1);
} // getCurrentPage()

// Hide element
function hide(id)
{
	document.getElementById(id).style.display = "none";
} // hide()

// Check if current window is displayed in a popup box
function isPopup()
{
	// Determine current window width
	if (window.innerWidth)
	 	width=window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		 width=document.documentElement.clientWidth;
	else if (document.body)
	 	width=document.body.clientWidth;

	// Determine current window height
	if (window.innerHeight)
	 	height=window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		 height=document.documentElement.clientHeight;
	else if (document.body)
		 height=document.body.clientHeight;

	if ((width < 800) || (height < 550))
		return true;
	else
		return false;
} // isPopup

// Obfuscate mailto address
function mailto(address, domain, text)
{
	var at = String.fromCharCode(64); // @
	document.write('<a href="mailto:');
	document.write(address + at + domain);
	document.write('">');
	if (text != null)
		document.write(text);
	else
		document.write(address);
	document.write('</a>');
} // mailto()

// Display element
function show(id)
{
	document.getElementById(id).style.display = "block";
} // show()

// Toggle element display
function showHide(id)
{
	var elem = document.getElementById(id);
	if (elem.style.display == "block") {
		hide(id);
	}
	else {
		show(id);
	}
} // showHide()

/*
 * Alternate element colors on elements within parent element
 *
 * @param parentId - Id of parent element
 * @param childTag - Element within parent to change
 * @param background1 - Background color for odd-numbered elements
 * @param color1 - Text color for odd-numbered elements
 * @param background2 - Background color for even-numbered elements
 * @param color2 - Text color for even-numbered elements
 */
function stripe(parentId, childTag, background1, color1, background2, color2)
{
	// Get all child elements of parent Id
	el=document.getElementById(parentId).getElementsByTagName(childTag);
	numChildren=el.length;

	// Use logo colors as background colors if none given
	if (!background1) {
		background1 = LOGOCOLOR1;
	}
	if (!background2) {
		background2 = LOGOCOLOR2;
	}

	for(i=0; i<numChildren; i++){
		if((i+1)%2==0) {
			el[i].style.backgroundColor=background1;
			// Change text color only if provided
			if (color1) {
				el[i].style.color = color1;
			}
		} else {
			el[i].style.backgroundColor=background2;
			// Change text color only if provided
			if (color2) {
				el[i].style.color = color2;
			}
		}
	} // for
} // stripe

/*
 * Swap image source
 */
function swap()
{
	if (!document.getElementById) {
		return;
	}

	var swapPath = IMAGEPATH;
	var i = 0;
	var elem;

	// If # arguments is odd, then first parameter is image path
	if ((swap.arguments.length%2) != 0) {
		swapPath=swap.arguments[0];
		i = 1;
	}

	// Use arguments[] array to loop through images to swap
	for ( ; i<swap.arguments.length; i+=2) {
		elem = document.getElementById(swap.arguments[i]);
		// Swap image source
		elem.src = swapPath + swap.arguments[i+1];
	}
} // swap()