sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
};
if (window.attachEvent) window.attachEvent("onload", sfHover);

function toggleElem(sElemID, bShow) {
	if(bShow) {
		document.getElementById(sElemID).style.visibility = "visible";
	}
	else {
		document.getElementById(sElemID).style.visibility = "hidden";
	}
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

function cleanVal(sDirty) {
	sClean = sDirty.replace(/(<([^>]+)>)/ig,"").trim();
	return sClean;
};

function currencyFormat(amount) {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}


function validateQuantity(oInput) {
	if(isNaN(oInput.value)) {
		oInput.value = "";
	}
	
	calculateOrder();
};

function calculateOrder() {
	
	var totalPrice = 0;
	
	for(var i = 0; i < numProducts; i++) {
		
		var thisQuantity = parseInt(document.getElementById("products["+i+"][quantity]").value);
		if(!isNaN(thisQuantity) && thisQuantity > 0) {
			
			document.getElementById("products["+i+"][name]").value = cleanVal(document.getElementById("product_"+i+"_name").innerHTML);
			document.getElementById("products["+i+"][size]").value = cleanVal(document.getElementById("product_"+i+"_size").innerHTML);
			document.getElementById("products["+i+"][price]").value = cleanVal(document.getElementById("product_"+i+"_price").innerHTML);
			
			linePrice = parseFloat(thisQuantity * cleanVal(document.getElementById("product_"+i+"_price").innerHTML));
			
			if (!isNaN(linePrice)) {
				totalPrice = totalPrice + linePrice;
			}
			
		}
		else {
			document.getElementById("products["+i+"][name]").value = "";
			document.getElementById("products["+i+"][size]").value = "";
			document.getElementById("products["+i+"][price]").value = "";
		}
		
	}
	
	var totalFormatted = currencyFormat(totalPrice);
	document.getElementById("total_price").innerHTML = "$" + totalFormatted;
	document.getElementById("order[total_price]").value = totalFormatted;
	
};