/**
 * calc.js
 * Author: Tharsan Bhuvanendran
 * Date: Dec 31 2008
 *
 * Perform cost calculations and update displays with calculated values.
 */

/** CONSTANTS **/
var GST_RATE = 0.05, PST_RATE = 0.08;
var SHIPPING_COST = 12.95;

/**
 * calcTotal will calculate all fields and update display fields with their
 * calculated values.
 */
function calcTotal() {
	var qty1, qty2, qtyCovers;
	var cost1, cost2, costCovers;
	var coversTotal, otsTotal, finalTotal;
	var gst1, pst1, gst2, pst2;

	// parse the quantity values from form fields
	qty1 = parseInt(document.getElementById("Text1").value);
	if(isNaN(qty1) || qty1 < 0) qty1 = 0;
	qty2 = parseInt(document.getElementById("Text5").value);
	if(isNaN(qty2) || qty2 < 0) qty2 = 0;
	qtyCovers = parseInt(document.getElementById("Text11").value);
	if(isNaN(qtyCovers) || qtyCovers < 0) qtyCovers = 0;

	// calculate the costs of items based on their respective quantities
	cost1 = (qty1 < 20) ? 4.50*qty1 : 3.50*qty1;
	cost2 = (qty2 < 20) ? 12*qty2 : 10*qty2;
	costCovers = (qtyCovers < 1500 || isNaN(qtyCovers)) ? 0 : 9*qtyCovers;

	// calculate taxes and the off-the-shelf total
	if(qty1 == 0 && qty2 == 0) {
		gst1 = pst1 = otsTotal = 0;
	} else {
		gst1 = Math.round((cost1 + cost2 + SHIPPING_COST) * GST_RATE * 100)/100;
		pst1 = Math.round((cost1 + cost2 + SHIPPING_COST) * PST_RATE * 100)/100;
		otsTotal = Math.round((cost1 + cost2 + SHIPPING_COST + gst1 + pst1) * 100)/100;
	}

	// calculate the cost of custom covers, if chosen
	if(qtyCovers < 1500 || isNaN(qtyCovers)) {
		gst2 = pst2 = coversTotal = 0;
	} else {
		gst2 = Math.round(costCovers * GST_RATE * 100)/100;
		pst2 = Math.round(costCovers * PST_RATE * 100)/100;
		coversTotal = Math.round((costCovers + gst2 + pst2) * 100)/100;
	}

	finalTotal = Math.round((coversTotal + otsTotal) * 100)/100;

	// update the displays with their calculated values
	document.getElementById("gst1").value = gst1.toFixed(2);
	document.getElementById("pst1").value = pst1.toFixed(2);
	document.getElementById("Text10").value = otsTotal.toFixed(2);

	document.getElementById("Text12").value = costCovers.toFixed(2);

	document.getElementById("Text13").value = gst2.toFixed(2);
	document.getElementById("Text14").value = pst2.toFixed(2);
	document.getElementById("Text15").value = coversTotal.toFixed(2);

	document.getElementById("Text16").value = finalTotal.toFixed(2);
	
	document.getElementById("Hidden1").value = (qty1 < 20) ? 4.50 : 3.50;
	document.getElementById("Hidden2").value = (qty2 < 20) ? 12 : 10;

	document.getElementById("Hidden4").value = (gst1 + gst2);
	document.getElementById("Hidden6").value = (pst1 + pst2);

	return false;
}

/**
 * calcSubtotal(fld) will calculate and display the subtotal for the item whose
 * quantity is specified in the form field fld.
 */
function calcSubtotal(fld) {
	var qty = parseInt(fld.value);

	if(isNaN(qty)) {
		alert("Please enter a numerical quantity.");
		return false;
	}

	if(fld.name == "cGmp11Qty") {
		document.getElementById("Text4").value = ((qty < 20) ? 4.50*qty : 3.50*qty).toFixed(2);

	} else if(fld.name == "CombinedQty") {
		document.getElementById("Text7").value = ((qty < 20) ? 12*qty : 10*qty).toFixed(2);

	} else if(fld.name == "CustomQty") {
		if(qty < 1500) {
			alert("You must order 1500 units or more of the custom booklets.");
			return false;
		}

		document.getElementById("Text12").value = (9*qty).toFixed(2);
	}
	calcTotal();
}



