// JavaScript Document
<!--

var oddColor = '#FFFFFF';
var evenColor = '#F7F7F0';
var selectedColor = '#CCCC99';
var topBorderColor = '#FFFFFF';
var bottomBorderColor = '#DDDDDD';
var itemSubtotalValues = new Array();
var discountRate = 0;

document.write("<style type='text/css'>#quickorder {visibility:hidden;}</style>");
document.write("<style type='text/css'>#addtocart {visibility:hidden;}</style>");
document.write("<style type='text/css'>#subtotal {visibility:hidden;}</style>");
if (document.getElementById) {
	var itemsTable = document.getElementById('quickorder');
	if (itemsTable) {itemsTable.style.visibility = "hidden";}
	var subtotalTable = document.getElementById('subtotal');
	if (subtotalTable) {subtotalTable.style.visibility = "hidden";}
}

function paintAndCreate(id) {
	var table = document.getElementById(id);
	var rows = table.getElementsByTagName("tr");
	var rowCount = rows.length;
	for (i = 0; i < rowCount; i++) {
		var checkboxItem = document.getElementById("chk" + i);
		if (checkboxItem) {
			var rowSelected = (checkboxItem.checked)? true : false;
			var subtotalItem = document.getElementById("sub" + i);
			itemSubtotalValues[i] = subtotalItem.value;
		} else {
			var rowSelected = false;
		}
		rows[i].style.backgroundColor = colorRow(i,rowSelected);			
		if (rowSelected) {
			rows[i].style.fontWeight = "bold";			
		} else {
			rows[i].style.fontWeight = "normal";						
		}
	}
	updateSubtotals();
}

function colorRow(rowId, rowSelected) {
	if (rowId % 2 == 0) {
		var rowColor = evenColor;
	} else {
		var rowColor = oddColor;
	}
	if (rowSelected) {rowColor = selectedColor;}
	return rowColor;
}

function updateQuickRowItem(id) {
	if (document.getElementById) {
		var rowItem = document.getElementById("quick" + id);
		var chkItem = document.getElementById("chk" + id);
		var subItem = document.getElementById("sub" + id);
		var qtyItem = document.getElementById("qty" + id);
		var priceItem = document.getElementById("price" + id);
		if (qtyItem.value > 0) {
			chkItem.checked = true;
			rowItem.style.backgroundColor = selectedColor;
			rowItem.style.fontWeight = "bold";
		} else {
			chkItem.checked = false;
			rowItem.style.border = 0;			
			if (id % 2 == 0) {
				rowItem.style.backgroundColor = evenColor;
				rowItem.style.fontWeight = "normal";
			} else {
				rowItem.style.backgroundColor = oddColor;
				rowItem.style.fontWeight = "normal";				
			}
		}
		var priceValue = priceItem.innerHTML;
		priceValue = priceValue.substring(3, priceValue.length);
		var subValue = (priceValue * qtyItem.value);
		subItem.value = subValue.toFixed(2);
		itemSubtotalValues[id] = subValue.toFixed(2);
		updateSubtotals();
	}
}

function hideAndShow() {
	if (document.getElementById) {
		var itemsTable = document.getElementById('quickorder');
		var loadingTable = document.getElementById('loading');
		var subtotalTable = document.getElementById('subtotal');
		var addtocartButton = document.getElementById('addtocart');
		if ( itemsTable && loadingTable && subtotalTable && addtocartButton ) {
			itemsTable.style.visibility = "visible";
			loadingTable.style.display = "none";
			subtotalTable.style.visibility = "visible";
			addtocartButton.style.visibility = "visible";
		}
	}	
}

function calculateGoodsTotal() {
	var itemRows = itemSubtotalValues.length;
	var goodsTotal = 0;
	for (i = 1; i < itemRows; i++) {
		goodsTotal += parseFloat(itemSubtotalValues[i]);
	}
	return goodsTotal;
}

function calculateDiscount(goodsTotal) {
	if (goodsTotal>=750) {
		discountRate = 0.2;
	} else if (goodsTotal>=300) {
		discountRate = 0.15;
	} else if (goodsTotal>=125) {
		discountRate = 0.1;
	} else if (goodsTotal>=50) {
		discountRate = 0.05;
	} else {
		discountRate = 0;
	}
	return goodsTotal * discountRate;
}

function calculateSubtotal(goodsTotal, discountValue) {
	return goodsTotal - discountValue;
}

function updateSubtotals() {
	var goodsTotal = calculateGoodsTotal();
	var discountValue = calculateDiscount(goodsTotal);
	var subTotal = calculateSubtotal(goodsTotal, discountValue);
	
	if (document.getElementById) {
		var goodsTotalItem = document.getElementById("goodstotal");
		if (goodsTotalItem) {
			goodsTotalItem.innerHTML = goodsTotal.toFixed(2);
		}
		var discountValueItem = document.getElementById("valuediscount");
		if (discountValueItem) {
			discountValueItem.innerHTML = discountValue.toFixed(2);
		}
		var discountLabelItem = document.getElementById("discountlabel");
		if (discountLabelItem) {
			discountLabelItem.innerHTML = "Value Discount (" + discountRate * 100 + "%)";
		}
		var subTotalItem = document.getElementById("goodssubtotal");
		if (subTotalItem) {
			subTotalItem.innerHTML = subTotal.toFixed(2);
		}		
	}
}

function initializePage() {
	paintAndCreate("quickorder");
	hideAndShow();
}

-->
