var IMAGEPATH = "images/";

// Define colors in logo
var LOGOCOLOR1 = "#13468C";
var LOGOCOLOR2 = "#5C3800";

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

//Add onload event to set up navigation buttons
addLoadEvent(navigation);

//Prototype for find method of Array object
Array.prototype.find = function(searchStr)
{
   var found = false;
   for (i=0; i<this.length; i++) {
       if (this[i] == searchStr) {
           found = true;
       }
   } // for
   return found;
}

	/*
	 * 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()
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@else
		xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	return xmlhttp;
} // ajaxObject

/*
 * 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

 /*
  * Validate e-mail address
  *
  * @param addr - e-mail address to validate
  */
function emailCheck(addr)
{
	var pattern = '^([\\d\\w\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$';
	if (addr.match(pattern)) {
		return true;
	}
	else {
		alert('Please enter a valid e-mail address.');
		return false;
	}
} // emailCheck

// 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()

// 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()


/*
 * Highlight proper navigation button and add mouse behaviors
 */
function navigation()
{
	var button;

	// Leave if navigation buttons not defined
	if (document.getElementById('buttons') == null) {
		return;
	}

	button = document.getElementById('navhome');
	if (document.body.id == 'home') {
		// Highlight "Home" navigation button
		swap('navhome', 'navhome_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('navhome', 'navhome.gif')};
		button.onmouseout = function() {swap('navhome', 'navhome_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('navhome', 'navhome_on.gif')};
		button.onmouseout = function() {swap('navhome', 'navhome.gif')};
	}	

	button = document.getElementById('navabout');
	if (document.body.id == 'about') {
		// Highlight "About" navigation button
		swap('navabout', 'navabout_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('navabout', 'navabout.gif')};
		button.onmouseout = function() {swap('navabout', 'navabout_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('navabout', 'navabout_on.gif')};
		button.onmouseout = function() {swap('navabout', 'navabout.gif')};
	}	

	button = document.getElementById('navissues');
	if (document.body.id == 'issues') {
		// Highlight "Issues" navigation button
		swap('navissues', 'navissues_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('navissues', 'navissues.gif')};
		button.onmouseout = function() {swap('navissues', 'navissues_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('navissues', 'navissues_on.gif')};
		button.onmouseout = function() {swap('navissues', 'navissues.gif')};
	}	

	button = document.getElementById('navgetinvolved');
	if (document.body.id == 'getinvolved') {
		// Highlight "Get Involved" navigation button
		swap('navgetinvolved', 'navgetinvolved_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('navgetinvolved', 'navgetinvolved.gif')};
		button.onmouseout = function() {swap('navgetinvolved', 'navgetinvolved_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('navgetinvolved', 'navgetinvolved_on.gif')};
		button.onmouseout = function() {swap('navgetinvolved', 'navgetinvolved.gif')};
	}	

	button = document.getElementById('navevents');
	if (document.body.id == 'events') {
		// Highlight "News & Events" navigation button
		swap('navevents', 'navevents_on.gif');
		// Set mouse behaviors
		button.onmouseover = function() {swap('navevents', 'navevents.gif')};
		button.onmouseout = function() {swap('navevents', 'navevents_on.gif')};
	}
	else {
		// Set mouse behaviors
		button.onmouseover = function() {swap('navevents', 'navevents_on.gif')};
		button.onmouseout = function() {swap('navevents', 'navevents.gif')};
	}	
} // navigation

//Add styling to all required input form elements
function setRequiredLabels()
{
	var css;
	// Modify text of all required labels
	elements = document.getElementsByTagName('div');
	for (var i=0; i<elements.length; i++) {
		if ((elements[i].className != null) && (elements[i].innerHTML != null)) {
			// Get all CSS classes for element
			css = elements[i].className.split(' ');
			// Update text only for elements with a 'required' class
			if ((css != '') && (css.find('required'))) {
				elements[i].innerHTML = elements[i].innerHTML + '*';
			}
		}
	} // for
} // setRequiredLabels

// 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

/*
 * 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