var activeTimer = '';

function findItems() {
	navNode = document.getElementById('nav').childNodes;
	for (i=0;i<navNode.length;i++) {
		if (navNode[i].tagName && navNode[i].tagName == 'UL') {
			lis = navNode[i].childNodes;
			for (j=0;j<lis.length;j++) {
				if (lis[j].nodeType == 1 && lis[j].tagName == 'LI') {
					if (lis[j].firstChild.className == 'hasdropdown') {
						lis[j].firstChild.onmouseover = function() {
							showMenu(this);
						}
						lis[j].firstChild.onmouseout = function() {
							hideMenu(this);
						}
						lisDrop = lis[j].getElementsByTagName('ul')[0];
						if (lisDrop.offsetWidth >= (lis[j].offsetWidth -5)) {
							if (document.all) {
								lisDrop.style.width = (lisDrop.offsetWidth -24) +'px';
							}
							else {
								lisDrop.style.width = lisDrop.offsetWidth +'px';
							}
						}
						else {
							lisDrop.style.width = (lis[j].offsetWidth -5) +'px';
						}
						lisLinks = lisDrop.getElementsByTagName('a');
						for (k=0;k<lisLinks.length;k++) {
							lisLinks[k].style.display = 'block';
							lisLinks[k].style.width = '100%';
							lisLinks[k].onmouseover = function() {
								showMenu(this.parentNode.parentNode.parentNode.firstChild);
							}
							lisLinks[k].onmouseout = function() {
								hideMenu(this.parentNode.parentNode.parentNode.firstChild);
							}
						}
					}
				}
			}
		}
	}
}


function showMenu(obj) {
	window.clearTimeout(activeTimer);
	if (document.getElementById('active') && obj.id != 'active') {
		document.getElementById('active').style.visibility = 'hidden';
		document.getElementById("active").parentNode.firstChild.setAttribute('class', 'hasdropdown');
		document.getElementById('active').removeAttribute('id');
	}
	obj.setAttribute('class', 'hasdropdown active');
	theMenu = obj.parentNode.getElementsByTagName('ul')[0];
	theMenu.setAttribute('id','active');
	theMenu.style.visibility = 'visible';
}

function hideMenu(obj) {
	activeTimer = window.setTimeout('document.getElementById("active").parentNode.firstChild.setAttribute("class", "hasdropdown");document.getElementById("active").style.visibility="hidden";', 550);
}

function getUrl(obj) {
	if (obj.value != 'null') {
		self.location.href = obj.value;
		return true;
	}
	else {
		return false;
	}
}

function expandNode(obj) {
	nodeArr = obj.href.split('&aid=');
	nodeId = 'node'+ nodeArr[(nodeArr.length -1)];
	nodeObj = document.getElementById(nodeId);
	if (nodeObj.style.display == 'none') {
		nodeObj.style.display = 'block';
		obj.parentNode.style.border = 'none';
		obj.className = 'active';
	}
	else {
		nodeObj.style.display = 'none';
		obj.className = '';
		obj.parentNode.style.borderBottom = '1px solid #e3e4e3';
	}
	obj.blur();
	return false;
}

function validateForm(formObj) { // Hello, I'll be your form validating script today.
var formElements = formObj.elements;

// Now I'll set a bunch of boolean variables that will help us validate your form.
var canSubmit = true; // This will ultimately decide if the form gets submitted or not, and is dependant on the other vars to do so.
var emailValid = true; // This checks if the user's email address is valid (string@string.string).
var fieldsComplete = true; // This checks that all the required text fields are not blank.
var anyChecked = true; // This makes sure all required checkbox/radio box arrays are valid.

// These arrays are used to validate the check/radio boxes.
var rcBoxes = new Array();
rcBoxes[0] = new Array();
rcBoxes[1] = new Array();
var rcArrays = new Array();
rcArrays[0] = new Array;
rcArrays[1] = new Array;

// Getting down to business now, we'll loop through all the elements of your form
for (i=0;i<formElements.length;i++) {
// We'll start with the text fields - making sure all the required ones are not blank.
	if (formElements[i].type == 'text' ||  formElements[i].type == 'textarea') {
		if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req' && (formElements[i].value == '' || formElements[i].value == 'undefined')) {
		formElements[i].className += ' highlight'; // Highlights a required field left blank.
		fieldsComplete = false;
			}
// Now we'll check if the user has provided you with a valid email address...
		if (formElements[i].name == 'email_req') {
			if (formElements[i].value.search('.+@.+\\.[a-z]+') < 0) {
				formElements[i].className += ' highlight'; // Highlights the email field if blank or invalid.
				emailValid = false;
				}
			else { // Removes the highlight if email is valid.
				formElements[i].className = formElements[i].className.replace(' highlight','');
				}
			}
		else if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req' && formElements[i].value != '') {
			// Removes the highlight if fields are properly filled.
			formElements[i].className = formElements[i].className.replace(' highlight','');
			}
		}

// Finally, we go through the checkboxes and radio boxes, making sure each array of required check/radio boxes has at least one element checked.
	else if (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') {
		if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req') {
			// Adds all checkboxes to an array which we'll be looping through later.
			rcBoxes[0].push(formElements[i].name);
			rcBoxes[1].push(formElements[i].checked);
			}
		}
	}

// After loading all the check/radio boxes into a separate array, we go through them again.
if (rcBoxes[0].length > 0) {
	for (i=0;i<rcBoxes[0].length;i++) {
		// The first thing we'll do is separate them into little arrays where they have a common name attribute.
		targetCheck = formObj[rcBoxes[0][i]];
		i += targetCheck.length;
		// Then we'll loop through that array.
		for (j=0;j<targetCheck.length;j++) {
			if (targetCheck[j].checked) {
				// If we find a checked box within a required array, add the element's name to rcArrays[0] and add true to rcArrays[1].
				rcArrays[1].push(true);
				rcArrays[0].push(targetCheck[j].name);
				// That's all we need, so we can break out of the loop.
				break;
				}
			else if (j == (targetCheck.length -1)) {
				// If nothing is checked, we only add the name of the element to rcArrays[0].
				rcArrays[0].push(targetCheck[j].name);
				}
			}
		}
	// So, with all the info we gathered up there, we'll check the length of rcArrays[0] against rcArrays[1].
	// If all the required check/radio arrays have at least one box checked, they're valid and the lengths should match.
	if (rcArrays[0].length != rcArrays[1].length) {
		// If the lengths of rcArrays[0] and rcArrays[1] don't match, it means something that should've been checked was left unchecked.
		anyChecked = false;
		}
	}

// Remember our boolean vars? This is where we check them and give the user an appropriate response.
if (!fieldsComplete || !anyChecked) {
	// This message will display if a required textfield was left blank or a checkbox was left un-checked.
	alert('Please fill out all required fields before submitting this form!');
	canSubmit = false; // Don't let the form submit.
	}
else if (!emailValid) {
	// This message will display if the user didn't enter a valid email address.
	alert('Please enter a valid email address in the highlighted field!');
	canSubmit = false; // Don't let the form submit.
	}
if (fieldsComplete && emailValid && anyChecked) {
	// If everything has been properly filled and a valid email address has been entered, the form can submit!
	canSubmit = true;
	}
return canSubmit;
}

function blank(uri) {
	window.open(uri);
	return false;
}

window.onload = function() { 
	findItems();
}