function startList() {
	var navRoot = document.getElementById("primarynav");
	for (i=0; i < navRoot.childNodes.length; i++) {
		node = navRoot.childNodes[i];
		if (node.nodeName == "LI") {
			if(document.all) {
				node.onmouseover=function() {
					this.className += " over";
				}
				node.onmouseout=function() {
					this.className = this.className.replace(" over", "");
				}
			}
		}
	}
}

function setProductImages() {
	var x = document.getElementsByTagName("A");
	for(i=0;i<x.length;i++) {
		if(x[i].className.indexOf("product") > -1) {
			x[i].onclick = function() {
				alert(this.href);
				return false;
			}
		}
	}
}

function validateForm(who) {
	var f = who;
	var missing = new Array();
	var msg = "Please be sure to fill out all required fields, and use valid email addresses.";
	
	for(i=0;i<f.elements.length;i++) {
		if(f.elements[i].className == "label" || (f.elements[i].value.length < 1 && f.elements[i].className == "required") || (f.elements[i].name.indexOf("email") > -1 && !validateEmail(f.elements[i].value))) {
			missing.push(f.elements[i].name);
		}
	}
	if (missing.length > 0) {
		for(x=0;x<f.elements.length;x++) {
			f.elements[x].style.color = "";
		}
		for(y=0;y<f.getElementsByTagName("TH").length;y++) {
			f.getElementsByTagName("TH")[y].style.color = "";
		}
		for(z=0;z<missing.length;z++) {
			if(document.getElementById(missing[z])) {
				document.getElementById(missing[z]).style.color = "#ff0000";
			} else {
				f.elements[missing[z]].style.color = "#ff0000";
			}
		}
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

function validateEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function setPrice(who) {
	if(document.getElementById("price")) {
		var x = document.getElementById("price").getElementsByTagName("SPAN")[0];
		x.childNodes[0].nodeValue = "$ "+ prices[who][0];
		document.forms['product'].elements['price'].value = prices[who][0];
	}
	if(document.getElementById("productdetail") && prices[who][1] != '') {
		var y = document.getElementById("productdetail");
		y.href = "/images/600/"+prices[who][1];
		y.getElementsByTagName("IMG")[0].src = "/images/250/"+prices[who][1];
	}

}

function checkProduct() {
	var x = document.forms['product'];
	if(x.elements['quantity'].length < 1 || x.elements['quantity'].value < 1) {
		alert("Please enter a quantity.");
		return false;
	} else {
		return true;
	}
}

function validateSearch() {
	var x=document.getElementById("searchbox");
	if(x.value && x.value != "Search...") {
		return true;
	} else {
		alert("Please enter your search criteria.");
		return false;
	}
}

function checkInput(who) {
	if(who.value == "Search...") {
		who.className = who.className.replace(" label","");
		who.value = "";
	} else if(who.value == "") {
		who.className = who.className+" label";
		who.value = "Search...";
	}
}

function init() {
	startList();
	setProductImages();
}
	
window.onload = init;
