function isInt(s) {
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function validateQty(theID) {
	if(!document.getElementById("productQty"+theID)) return false;
	var productQty = document.getElementById("productQty"+theID);
	if(!isInt(productQty.value)) {
		alert("Quantity must be a number.");
		productQty.value = 1;
	}
	if(productQty.value<0) {
		alert("Negative quantities not allowed.");
		productQty.value = 1;
	}
}

function updateTotals(theID) {
	if(!document.getElementById("productQty"+theID) || !document.getElementById("productPrice"+theID) || !document.getElementById("subtotal"+theID)) return false;
	var productQty = document.getElementById("productQty"+theID);
	var productPrice = document.getElementById("productPrice"+theID);
	var subtotal = document.getElementById("subtotal"+theID);
	
	var num = parseFloat(productPrice.value) * parseInt(productQty.value);
	subtotal.value = num.toFixed(2);
}


function updateTotal(theIDs) {
	var runningSubtotal=0;
	var runningTotal=0;
	var runningShipping=0;
	for (var z in theIDs) {
		if(!document.getElementById("productQty"+theIDs[z]) || !document.getElementById("productPrice"+theIDs[z]) || !document.getElementById("productShipping"+theIDs[z])) return false;
		var productQty = document.getElementById("productQty"+theIDs[z]);
		var productPrice = document.getElementById("productPrice"+theIDs[z]);
		var productShipping = document.getElementById("productShipping"+theIDs[z]);

		runningSubtotal = runningSubtotal + parseFloat(productPrice.value) * parseInt(productQty.value);
		runningShipping = runningShipping + parseFloat(productShipping.value) * parseInt(productQty.value);
		runningTotal = runningTotal + (parseFloat(productPrice.value) + parseFloat(productShipping.value)) * parseInt(productQty.value);
	}
	if(!document.getElementById("productsSubtotal") || !document.getElementById("shippingTotal") || !document.getElementById("grandTotal")) return false;
	var productsSubtotal = document.getElementById("productsSubtotal");
	var shippingTotal = document.getElementById("shippingTotal");
	var grandTotal = document.getElementById("grandTotal");
	
	productsSubtotal.value = runningSubtotal.toFixed(2);
	shippingTotal.value = runningShipping.toFixed(2);
	grandTotal.value = runningTotal.toFixed(2);
}


function blurProductQty() {
	var productIDs = new Array();
	var nearlyIDs = document.body.innerHTML.match(/\sid=["\']productQty([0-9]+)["\']/gi);
	for(var x in nearlyIDs) {
		productIDs[x] = nearlyIDs[x].match(/[0-9]+/);
	}
	for (var y in productIDs) {
		if(!document.getElementById("productQty"+productIDs[y])) return false;
		var productQty = document.getElementById("productQty"+productIDs[y]);
		productQty.onblur = function() {
			validateQty(productIDs[y]);
			updateTotals(productIDs[y]);
			updateTotal(productIDs);
		}	
	}
}


function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if(oldonload) {
				oldonload();
			}
		func();
	    }
	}
}
addLoadEvent(blurProductQty);