<!--
// checks that a quantity is a whole number greater than or equal to the minimum
function checkItemQty(element, minimum) {
	
	//alert(element.value + " " + minimum);

	if (!isNumber(element.value) || element.value < minimum) {
		element.value = minimum;
		isAlertOpen = "true"; // set global var to show that alert box is open
		alert("The item quantity must be a valid number equal to or higher than the minimum quantity of " + minimum + ".");
		isAlertOpen = "false"; // set global var to show that alert box is closed
		element.focus();
		return false;
	}
}
// checks that an additional cost quantity is a whole number less than four
function checkAddCostsQty(element, quantity) {
	if (!isNumber(element.value)) {
		element.value = 0;
		isAlertOpen = "true"; // set global var to show that alert box is open
		alert("An additional cost quantity must be a valid whole number.");
		isAlertOpen = "false"; // set global var to show that alert box is closed
		element.focus();
		return false;
	}
	if (element.value > quantity) {
		element.value = 1;
		isAlertOpen = "true"; // set global var to show that alert box is open
		alert("You cannot enter more than " + quantity + " additional costs.\n\nAddtional costs are calculated per item, so for example, if the additional cost for an item is 'Extra color/location', 1 addtional cost means that you are requesting 1 extra color / location for EACH item you are ordering.\n\nExample: If you are ordering 24 of a specific item and you wish to have an additional cost on each item, please input 1 instead of 24.");
		isAlertOpen = "false"; // set global var to show that alert box is closed
		element.focus();
		return false;
	}
}
function checkColorSizeQty(element) {
	if (!isNumber(element.value)) {
		element.value = 0;
		isAlertOpen = "true"; // set global var to show that alert box is open
		alert("A quantity must be a valid whole number.");
		isAlertOpen = "false"; // set global var to show that alert box is closed
		element.focus();
		return false;
	}
}

// formats a price to USD format (1,000.00)
function formatPrice(price) {
	// round value to 2 decimal points and convert to string
	price = (Math.ceil((price * 100))) / 100;
	var strPrice = price.toString();

	// add trailing zeros if necessary
	if (strPrice.indexOf('.') == -1) { strPrice += '.00'; }
	else if (strPrice.charAt(strPrice.indexOf('.') + 2) == '') { strPrice += '0'; }
	
	// adds comma in thousands place
  		if (strPrice >= 1000) {
		// get dollars and cents
		var dollars = strPrice.substr(0, strPrice.indexOf('.'));
		var cents   = strPrice.substr(strPrice.indexOf('.'));
		strPrice = dollars.substring(0, dollars.length - 3) + "," + dollars.substring(dollars.length - 3, dollars.length) + cents;
	}
	return strPrice;
}

function totalItemPrice(product_id) {
	// item price / quantity / setup charge
	var itemPrice = parseFloat(eval("document.getElementById('item_price_" + product_id + "').value"));
	var itemQty   = parseFloat(eval("document.getElementById('item_qty_" + product_id + "').value"));
	var setup     = parseFloat(eval("document.getElementById('setup_charge_" + product_id + "').value"));

	// get inital price
	var total = (itemPrice * itemQty) + setup;
	var totalAddCosts = 0;
	
	// add costs
	var addCostsQty   = eval("document.getElementsByName('add_qty_" + product_id + "[]')");
	var addCostsPrice = eval("document.getElementsByName('add_price_" + product_id + "[]')");
	for (var x = 0; x < addCostsQty.length; x++) {
		if (addCostsQty[x].value != 0) {	
			totalAddCosts += parseInt(addCostsQty[x].value);
			total += (addCostsQty[x].value * parseFloat(addCostsPrice[x].value) * itemQty) + (addCostsQty[x].value * setup);
		}
	}
	
	// set item total
	if (total == 0 || isNaN(total)) {
		eval("document.getElementById('itemTotal_" + product_id + "').innerHTML = 'CALL'");
		eval("document.getElementById('item_total_" + product_id + "').value = 0");	
	} else {
		eval("document.getElementById('itemTotal_" + product_id + "').innerHTML = '$' + formatPrice(total)");
		eval("document.getElementById('item_total_" + product_id + "').value = Math.ceil(total * 100) / 100");	
	}
	
	
	// set setup charge if not 0 and there are additional costs
	if (setup != 0) {
		eval("document.getElementById('setupCharge" + product_id + "').innerHTML = '$' + formatPrice(setup * (totalAddCosts + 1));");
	}
	
	// set grand total
	grandTotal();
	return true;
}

function totalAlmaPrice(product_id) {
	// item price / quantity / setup charge
	var itemPrice = parseFloat(eval("document.getElementById('item_price_" + product_id + "').value"));
	var itemQty   = parseFloat(eval("document.getElementById('item_qty_" + product_id + "').value"));
	var setup     = parseFloat(eval("document.getElementById('setup_charge_" + product_id + "').value"));

	// get inital price
	var total = (itemPrice * itemQty) + setup;
	var totalAddCosts = 0;
	
	// add costs
	var addCostsQty   = eval("document.getElementsByName('add_qty_" + product_id + "[]')");
	var addCostsPrice = eval("document.getElementsByName('add_price_" + product_id + "[]')");
	for (var x = 0; x < addCostsQty.length; x++) {
		if (addCostsQty[x].value != 0) {	
			totalAddCosts += parseInt(addCostsQty[x].value);
			total += (addCostsQty[x].value * parseFloat(addCostsPrice[x].value) * itemQty) + (addCostsQty[x].value * setup);
		}
	}

	eval("var selShaftIndex = document.getElementById('shaftType_" + product_id + "').selectedIndex");
	eval("var selClubIndex = document.getElementById('clubSelect_" + product_id + "').selectedIndex");
	eval("var selSwingIndex = document.getElementById('swingPref_" + product_id + "').selectedIndex");
	eval("if (document.getElementById('shaftType_" + product_id + "')[" + selShaftIndex + "].value == 'Graphite (+$10)') { total += (10 * itemQty); }");
	eval("if (document.getElementById('shaftType_" + product_id + "')[" + selShaftIndex + "].value == 'Graphite (+$100)') { total += (100 * itemQty); }");
	
	// set item total
	if (total == 0 || isNaN(total)) {
		eval("document.getElementById('itemTotal_" + product_id + "').innerHTML = 'CALL'");
		eval("document.getElementById('item_total_" + product_id + "').value = 0");	
	} else {
		eval("document.getElementById('itemTotal_" + product_id + "').innerHTML = '$' + formatPrice(total)");
		eval("document.getElementById('item_total_" + product_id + "').value = Math.ceil(total * 100) / 100");	
	}

	// set setup charge if not 0 and there are additional costs
	if (setup != 0) {
		eval("document.getElementById('setupCharge" + product_id + "').innerHTML = '$' + formatPrice(setup * (totalAddCosts + 1));");
	}
	
	// set grand total
	grandTotal();
	return true;
}

// calculates grand total of cart
function grandTotal() {
	var grandTotal = 0;
	for (var x = 0; x < product_ids.length; x++) {
		var itemTotal = parseFloat(eval("document.getElementById('item_total_" + product_ids[x] + "').value"));
		if (!isNaN(itemTotal)) { grandTotal += itemTotal; }
	}
	if (grandTotal == 0) {
		document.getElementById('grandTotalValue').innerHTML = "CALL";
		document.getElementById('grand_total').value = 0;
	} else {
		eval("document.getElementById('grandTotalValue').innerHTML = '$" + formatPrice(grandTotal) + "'");
		eval("document.getElementById('grand_total').value = " + grandTotal);
	}
	return true;
}

// add a new size or color menu
function addMenu(obj, product_id, type) {
	
	// get currentMenu and hide current add / remove buttons
	var currentMenu = obj.id.substr(obj.id.lastIndexOf("_") + 1);
	eval("document.getElementById('" + type + "_buttons_" + product_id + "_" + currentMenu + "').style.display = 'none';");
	
	// if first added item, change color of box, add total quantity box
	if (currentMenu == 0) {
		eval("document.getElementById('" + type + "_qty_" + product_id + "[0]').className = 'addquantity';");
		eval("document.getElementById('" + type + "_total_" + product_id + "').style.display = 'block';");
	}
	
	// turn on next menu
	currentMenu++;
	eval("document.getElementById('" + type + "_id_" + product_id + "[" + currentMenu + "]').disabled = false;");
	eval("document.getElementById('" + type + "_id_" + product_id + "[" +  currentMenu + "]').value = 'NULL';"); // reset select
	eval("document.getElementById('" + type + "_qty_" + product_id + "[" +  currentMenu + "]').disabled = false;");
	eval("document.getElementById('" + type + "_qty_" + product_id + "[" +  currentMenu + "]').value = 0;"); // reset qty
	eval("document.getElementById('" + type + "_" + product_id + "_" + currentMenu + "').style.display = 'block';");
	
	totalQuantities(product_id, document.forms["order"], type);
	return true;
}

function addBothMenus(obj, product_id) {

	// get currentMenu and hide current add / remove buttons
	var currentMenu = obj.id.substr(obj.id.lastIndexOf("_") + 1);
	eval("document.getElementById('menu_buttons_" + product_id + "_" + currentMenu + "').style.display = 'none';");
	
	// if first added item, change color of box, add total quantity box
	if (currentMenu == 0) {
		eval("document.getElementById('color_qty_" + product_id + "[0]').className = 'addquantity';");
		eval("document.getElementById('color_total_" + product_id + "').style.display = 'block';");
	}
	
	// turn on next menu
	currentMenu++;
	eval("document.getElementById('color_id_" + product_id + "[" + currentMenu + "]').disabled = false;");
	eval("document.getElementById('color_id_" + product_id + "[" +  currentMenu + "]').value = 'NULL';"); // reset select
	eval("document.getElementById('color_qty_" + product_id + "[" +  currentMenu + "]').disabled = false;");
	eval("document.getElementById('color_qty_" + product_id + "[" +  currentMenu + "]').value = 0;"); // reset qty
	eval("document.getElementById('color_" + product_id + "_" + currentMenu + "').style.display = 'block';");
	
	eval("document.getElementById('size_id_" + product_id + "[" + currentMenu + "]').disabled = false;");
	eval("document.getElementById('size_id_" + product_id + "[" +  currentMenu + "]').value = 'NULL';"); // reset select
	eval("document.getElementById('size_qty_" + product_id + "[" +  currentMenu + "]').disabled = false;");
	eval("document.getElementById('size_qty_" + product_id + "[" +  currentMenu + "]').value = 0;"); // reset qty
	
	totalQuantities(product_id, document.forms["order"], 'color');
	return true;
}

// removes a size or color menu
function removeMenu(obj, product_id, type) {
	
	// get currentMenu
	var currentMenu = obj.id.substr(obj.id.lastIndexOf("_") + 1);
	
	// turn off current menu
	eval("document.getElementById('" + type + "_id_" + product_id + "[" + currentMenu + "]').disabled = true;");
	eval("document.getElementById('" + type + "_id_" + product_id + "[" +  currentMenu + "]').value = 'NULL';"); // reset select
	eval("document.getElementById('" + type + "_qty_" + product_id + "[" +  currentMenu + "]').disabled = true;");
	eval("document.getElementById('" + type + "_qty_" + product_id + "[" +  currentMenu + "]').value = 0;"); // reset qty
	eval("document.getElementById('" + type + "_" + product_id + "_" + currentMenu + "').style.display = 'none';");
	
	// if last menu to be removed, change color of box, hide total box
	if (currentMenu == 1) {
		eval("document.getElementById('" + type + "_qty_" + product_id + "[0]').className = 'totalquantity';");
		eval("document.getElementById('" + type + "_total_" + product_id + "').style.display = 'none';");
	}
	
	// show previous add / remove buttons
	currentMenu--;
	eval("document.getElementById('" + type + "_buttons_" + product_id + "_" + currentMenu + "').style.display = 'block';");
	
	totalQuantities(product_id, document.forms["order"], type);
	return true;
}

function removeBothMenus(obj, product_id) {

	// get currentMenu
	var currentMenu = obj.id.substr(obj.id.lastIndexOf("_") + 1);
	
	// turn off current menu
	eval("document.getElementById('color_id_" + product_id + "[" + currentMenu + "]').disabled = true;");
	eval("document.getElementById('color_id_" + product_id + "[" +  currentMenu + "]').value = 'NULL';"); // reset select
	eval("document.getElementById('color_qty_" + product_id + "[" +  currentMenu + "]').disabled = true;");
	eval("document.getElementById('color_qty_" + product_id + "[" +  currentMenu + "]').value = 0;"); // reset qty
	eval("document.getElementById('color_" + product_id + "_" + currentMenu + "').style.display = 'none';");
	
	eval("document.getElementById('size_id_" + product_id + "[" + currentMenu + "]').disabled = true;");
	eval("document.getElementById('size_id_" + product_id + "[" +  currentMenu + "]').value = 'NULL';"); // reset select
	eval("document.getElementById('size_qty_" + product_id + "[" +  currentMenu + "]').disabled = true;");
	eval("document.getElementById('size_qty_" + product_id + "[" +  currentMenu + "]').value = 0;"); // reset qty
	
	// if last menu to be removed, change color of box, hide total box
	if (currentMenu == 1) {
		eval("document.getElementById('color_qty_" + product_id + "[0]').className = 'totalquantity';");
		eval("document.getElementById('color_total_" + product_id + "').style.display = 'none';");
	}
	
	// show previous add / remove buttons
	currentMenu--;
	eval("document.getElementById('menu_buttons_" + product_id + "_" + currentMenu + "').style.display = 'block';");
	
	totalQuantities(product_id, document.forms["order"], 'color');
	return true;
}

function changeSizeQty(product_id, x) {
	eval("document.getElementById('size_qty_" + product_id + "[" +  x + "]').value = document.getElementById('color_qty_" + product_id + "[" +  x + "]').value;"); // reset qty
}

// totals all color or size elements sets item qty to total
function totalQuantities(product_id, form, type) {
	var fieldName;
	var total = 0;
	
	for (x = 0; x < form.elements.length; x++) {
		var pattern = eval("/" + type + "_qty_" + product_id + "/");
		fieldName = form.elements[x].name;
		
		if (pattern.test(fieldName) && form.elements[x].disabled != true) { 
			total += parseInt(form.elements[x].value);
		} 
	}
	eval("document.getElementById('" + type + "_tqty_" + product_id + "').value = total;");
	eval("document.getElementById('item_qty_" + product_id + "').value = total;");
	totalItemPrice(product_id);
	return true;
}

// form validate function
function isCartReady(form) {

	// check for open alert boxes 
	if (isAlertOpen == "true") { return false; }

	var fieldName;
	for (x = 0; x < form.elements.length; x++) {
		
		// patterns for select boxes
		var colorPattern = /color_id/;
		var sizePattern = /size_id/;

		// patterns for golf club options
		var clubPattern = /clubSelect/;
		var shaftPattern = /shaftType/;
		var swingPatten = /swingPref/;
		
		fieldName = form.elements[x].name;
		
		
		// bob coe's shipping
		if (isObject(form.shipping_location) && form.shipping_location.selectedIndex <= 0) { 
			alert("Please select a shipping location.");
			form.shipping_location.focus();
			return false;
		}
		if (isObject(form.shipping) && form.shipping.selectedIndex <= 0) { 
			alert("Please select a shipping option.");
			form.shipping.focus();
			return false;
		}

		// check for non-selected golf club options
		if (clubPattern.test(fieldName) && form.elements[x].disabled != true) { 
			if (form.elements[x].selectedIndex == 0) {
				alert("Please select a specific club type.");
				form.elements[x].focus();
				return false;
			}
		
		} if (shaftPattern.test(fieldName) && form.elements[x].disabled != true) { 
			if (form.elements[x].selectedIndex == 0) {
				alert("Please select a shaft type.");
				form.elements[x].focus();
				return false;
			}
		} if (swingPatten.test(fieldName) && form.elements[x].disabled != true) { 
			if (form.elements[x].selectedIndex == 0) {
				alert("Please select your swing preference.");
				form.elements[x].focus();
				return false;
			}
		}
		
		// check for non-selected colors and sizes
		if (colorPattern.test(fieldName) && form.elements[x].disabled != true) { 
			if (form.elements[x].value == "NULL") {
				alert("Please select a color.");
				form.elements[x].focus();
				return false;
			}
		
		} else if (sizePattern.test(fieldName) && form.elements[x].disabled != true) { 
			if (form.elements[x].value == "NULL") {
				alert("Please select a size.");
				form.elements[x].focus();
				return false;
			}
		}
		
		// patterns for qty boxes
		var colorQtyPattern = /color_qty/;
		var sizeQtyPattern = /size_qty/;
		
		// check for 0 value qty boxes
		if (colorQtyPattern.test(fieldName) && form.elements[x].disabled != true) { 
			if (form.elements[x].value == "0") {
				alert("Please enter a quantity higher than 0 for this color.");
				form.elements[x].focus();
				return false;
			}
		
		} else if (sizeQtyPattern.test(fieldName) && form.elements[x].disabled != true) { 
			if (form.elements[x].value == "0") {
				alert("Please enter a quantity higher than 0 for this size.");
				form.elements[x].focus();
				return false;
			}
		}
	}
	
	// does total item qty match total color qty and total size qty?
	for (i = 0; i < product_ids.length; i++) {
	
		var color_qty = eval("document.getElementById('color_tqty_" + product_ids[i] + "');");
		var size_qty = eval("document.getElementById('size_tqty_" + product_ids[i] + "');");
		var item_qty  = eval("document.getElementById('item_qty_" + product_ids[i] + "');");
		
		if (color_qty != null && color_qty.value != "NULL") {
			if (color_qty.value != item_qty.value) {
				alert("The total quantity of items purchased, does NOT match the total number of colors.\nPlease adjust the number of colors so that the quantity matches the Total in the grey box.");
				item_qty.focus();
				return false;
			}
		}
		if (size_qty != null && size_qty.value != "NULL") {
			if (size_qty.value != item_qty.value) {
				alert("The total quantity of items purchased, does not match the total number of sizes.\nPlease adjust the number of colors so that the quantity matches the Total in the grey box.");
				item_qty.focus();
				return false;
			}
		}
	}

	return true;
}

function removeDollarSign(obj) {
	obj.value = "";
}
//-->
