// <!--
// *------------------*
// * CALCULATE TOTALS *
// *------------------*
function calcTotals(orderform) {

	var nbr_of_items = 8;
	var subtotamt    = 0;
	var subtotwork   = 0;
	var chk          = new Array(nbr_of_items);
	var qty          = new Array(nbr_of_items);
	var prc          = new Array(nbr_of_items);
	var tot          = new Array(nbr_of_items);
	var linetot      = new Array(nbr_of_items);

// *-- LINE ITEMS --*

	for (var i=0; i < nbr_of_items; i++) {

		var j = i+1;
		var a = 'aa';
		
		(j<10) ? (a = '0' + j) : (a = j);

		chk[i] = 'chkDL' + a;
		qty[i] = 'qtyDL' + a;
		prc[i] = 'prcDL' + a;
		tot[i] = 'totDL' + a;


		if (document.orderform[chk[i]].checked) {
			document.orderform[qty[i]].disabled = false;
			if (isNaN(document.orderform[qty[i]].value) || document.orderform[qty[i]].value == "") {
				document.orderform[qty[i]].value = 1;
			}
			var itemwork = parseFloat(document.orderform[qty[i]].value) * parseFloat(document.orderform[prc[i]].value);
			var itemamt  = rndDec(itemwork);
			document.orderform[tot[i]].value = itemamt;
			subtotwork = subtotwork + parseFloat(document.orderform[tot[i]].value);
		} else {
			document.orderform[qty[i]].disabled = true;
			document.orderform[qty[i]].value = "";
			document.orderform[tot[i]].value = "";
		}
	}

// *-- SUBTOTAL --*

	(subtotwork == 0) ? (subtotamt = 0) : (subtotamt = rndDec(subtotwork));

	document.orderform.subtotal.value = subtotamt;

// *-- SHIPPING --*

	var shipamt  = 0;
	var shipwork = 0;
	var DL01tot  = 0;

	if (!isNaN(parseFloat(document.orderform[tot[0]].value))) {
		DL01tot = parseFloat(document.orderform[tot[0]].value);
	}

	shipwork = (parseFloat(subtotamt) - parseFloat(DL01tot)) * .05; 

	(shipwork == 0) ? (shipamt = 0) : (shipamt = rndDec(shipwork));

	document.orderform.shipping.value = shipamt;

// *-- CA TAX --*

	var taxamt  = 0;
	var taxwork = parseFloat(subtotamt) * .0775;

	(document.orderform.chkCA.checked) ? (taxamt = rndDec(taxwork)) : (taxamt = 0);

	document.orderform.tax.value = taxamt;

// *-- TOTAL --*

	var totamt  = 0;
	var totwork = parseFloat(subtotamt) + parseFloat(shipamt) + parseFloat(taxamt);

	(totwork == 0) ? (totamt = 0) : (totamt = rndDec(totwork));

	document.orderform.total.value = totamt;
}

// *----------------------------------------------------------------------------------*
// * ROUND TO DECIMAL(2)                                                              *
// * Copyright (c) Paul McFedries and Logophilia Limited (http://www.mcfedries.com/). *
// *----------------------------------------------------------------------------------*
function rndDec(inNbr) {
    var result1 = inNbr * Math.pow(10,2)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10,2)
    return pad_with_zeros(result3, 2)
}
function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
// -->

