function expand(section,focusTo) {
	document.getElementById(section + "_e").style.display = "none";
	document.getElementById(section + "_c").style.display = "";

        // Now that it's expanded, were we told where to foc?
        if (!focusTo) return;
        el = document.getElementById(focusTo);
        if (!el) return;
        el.focus();
}

function collapse(section) {
	document.getElementById(section + "_c").style.display = "none";
	document.getElementById(section + "_e").style.display = "";
}

function cleanTextInput(element) {
	// remove extraneous blanks from an input field.
	// typical call: <input type="text" ... onchange="cleanTextInput(this);"/>
	var lastNonBlank = element.value.length-1;
	if (lastNonBlank < 0) return; // empty field, nothing to do
	for (; lastNonBlank>=0; lastNonBlank--) {
		if (' ' != element.value.charAt(lastNonBlank)) break;
	}
	var recent = ' '; // pretend we copied a blank already (so we skip leading blanks)
	var tmp = '';
	for (var i = 0; i <= lastNonBlank; i++)  {
		var c = element.value.charAt(i);
		if ((c != ' ') || (' ' != recent)) {
			tmp += c;
			recent = c;
		}
	}
	element.value = tmp;
}

function emailAddressAppearsValid(addr) {
	var regexpMatchEmail = RegExp(/^[^@\s]+@([-a-z0-9]+\.)+([a-z]{2}|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum)$/i);
	
	return (addr.match(regexpMatchEmail) != null);
}

function isValidEmail(element) {
	if (element.value == '') {
		alert('Please enter a valid e-mail address');
		element.focus();
		return false;
	}
	if (!emailAddressAppearsValid(element.value)) {
		if (!confirm('The E-Mail address appears invalid. Continue anyway?')) {
			element.focus();
			return false;
		}
	}
	return true;
}

// Do not call this function directly... it's called by other functions
function __openNewWindow() {
	window.open(this.href).focus();
	return false;
}

// This function changes the onclick behavior of anchor elements of class "newwindow"
// so the page is opened in a new window...
// It uses the function above to do it.
// Normally, a small "new window" icon will be added to the and of the anchor, but the class
// When combined with the "noind" class, that icon is omitted.
// Example: <a class="newwindow" href=...> ... </a>
// Typically this will be called by the body onload function, or equivalent.
function setNewWindowAnchors() {
	// Change onclick= property of <a class="newwindow" ...> anchors to force a new page
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var a = anchors[i];
		var classes = " " + a.className + " ";
		if (classes.indexOf(" newwindow ") != -1) {
			a.onclick=__openNewWindow;
			if (classes.indexOf(" noind ") == -1) {
				a.innerHTML += "<img class=\"newwindow\" title=\"[Link opens in a new window or tab]\" alt=\"[Link opens in a new window or tab]\" src=\"/images/newwindow\"/>";
			}
		}
	}
}

function showSubMenu(sm) {
	var item = document.getElementById(sm);
	for (var i=1; i<=10; i++) {
		var smenu = "smenu"+ i;
		if (document.getElementById(smenu)) {
			document.getElementById(smenu).style.display='none';
		}
	}
	if (item) {
		item.style.display='block';
	}
}

