// Simon Willison's unobtrusive onLoad function [http://simonwillison.net/2004/May/26/addLoadEvent/]
function addLoadEvent( func ) {
	var oldOnLoad = window.onload;
	if( typeof window.onload != "function" ) {
		window.onload = func;
	} else {
		window.onload = function( ) {
			if( oldOnLoad ) { oldOnLoad( ); }
			func( );
		}
	}
}
// Roshambo's cross-browser implementation of addEventListener [http://snipplr.com/view/561/add-event-listener/]
function addListener( htmlElement, eventType, func ) {
	var returnValue = false;
	if( window.addEventListener ) {
		htmlElement.addEventListener( eventType, func, false );
		returnValue = true;
	} else if( window.attachEvent ) {
		htmlElement.attachEvent( "on" + eventType, func );
		returnValue = true;
	}
	return returnValue;
}
// Peter-Paul Koch's cross-browser implementation of event targets [http://www.quirksmode.org/js/events_properties.html]
function findTarget( e ) {
	var targetNode = null;
	if( e.target ) targetNode = e.target;
	else if( e.srcElement ) targetNode = e.srcElement;
	if( targetNode.nodeType == 3 ) targetNode = targetNode.parentNode;
	return targetNode;
}
// Brian Arthmire's unobtrusive targetBlank to open links in a new window [blarthmi@franklincountyohio.gov]
// usage: <a class="externalLink" ...
function targetBlank( ) {
	var i, anchors = document.getElementsByTagName( "a" ), returnValue = false;
	if( anchors ) {
		// iterate through every anchor element
		for( i = 0 ; i < anchors.length ; i++ ) {
			// if anchor element is marked as an externalLink...
			if( anchors[ i ].className == "externalLink" && anchors[ i ].getAttribute( "href" ) ) {
				// ...then add the targetBlank
				returnValue = true;
				anchors[ i ].target = "_blank";
			}
		}
	}
	return returnValue;
}
// Brian Arthmire's unobtrusive autoFormSubmit to submit forms onChange [blarthmi@franklincountyohio.gov]
// usage: <select class="autoSubmit" name= ...
function autoFormSubmit( ) {
	var i, nodes = document.getElementsByTagName( "select" ), returnValue = false;
	if( nodes ) {
		// iterate through every select element
		for( i = 0 ; i < nodes.length ; i++ ) {
			// if select element is marked as an autoSubmit...
			if( nodes[ i ].className == "autoSubmit" ) {
				returnValue = true;
				// ...then add the submit form onChange event
				addListener( nodes[ i ], "change", function( e ) {
					var obj = findTarget( e );
					// move up the dom tree until we find the form to submit
					while( obj != null && obj.nodeName != "FORM" ) { obj = obj.parentNode; }
					if( obj ) { obj.submit( ); }
				} );
			}
		}
	}
	return returnValue;
}
// Brian Arthmire's very basic show/hide function [blarthmi@franklincountyohio.gov]
function swap( id, displayValue ) {
	var obj = document.getElementById( id );
	if( displayValue == null ) displayValue = "";
	if( obj ) {
		obj.style.display = ( obj.style.display == "none" ) ? displayValue : "none";
	}
	else {
		if( document.layers ) {
			document.id.display = ( document.id.display == "none" ) ? displayValue : "none";
		} else {
			document.all.id.style.display = ( document.all.id.style.display == "none" ) ? displayValue : "none";
		}
	}
}
// force show element
function show( id, displayValue ) {
	var obj = document.getElementById( id );
	if( obj ) {
		if( displayValue == null ) displayValue = "";
		obj.style.display = displayValue;
	}
}
// force hide element
function hide( id ) {
	var obj = document.getElementById( id );
	if( obj ) obj.style.display = "none";
}
// Brian Arthmire's unobtrusive toggle elements function [blarthmi@franklincountyohio.gov]
// requires the /_lib/scripts/main.js file
// unfortunately suffers from the flicker effect
// clickable text to trigger the toggle: <label for="elementID">Hello World</label>
// element to be toggled: <anyTagName id="elementID">Text is initially hidden if JS is enabled</anyTagName>
function toggle( ) {
	var i, nodes = document.getElementsByTagName( "label" ), returnValue = false;
	if( nodes ) {
		// iterate through every label element
		for( i = 0 ; i < nodes.length ; i++ ) {
			// if label element is marked with a for attribute...
			if( nodes[ i ].htmlFor ) {
				returnValue = true;
				// ...then hide the id element and add the toggle or swap onClick event
				document.getElementById( nodes[ i ].htmlFor ).style.display = "none";
				addListener( nodes[ i ], "click", function( e ) {
					var obj = findTarget( e );
					if( obj.htmlFor ) { swap( obj.htmlFor ); }
				} );
			}
		}
	}
	return returnValue;
}