// JavaScript Document
function calculate() {
/* The round_decimals and pad_with_zeros functions in this script are Copyright (c) Paul McFedries 
and Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return result3
}

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
}
	// Display the total rounded to two decimal places
   // frm.TOTAL.value = round_decimals(order_total, 2)

// ----------------------------------------------------------------------------------
// values to be displayed on the page should use pad_with_zeros()
// values to be used for calculation should use round_decimals()

SubTotal = 0;
GrandTotal = 0;
Item5Cost = document.orderform.ItemPrice_OneDegree.value; /*One Degree Deer Scent*/
Item6Cost = document.orderform.ItemPrice_OneDegreeWithScentTube.value; /*One Degree Deer Scent with Scent Bue*/
Item1Cost = document.orderform.ItemPrice_ThreePack.value; 
Item2Cost = document.orderform.ItemPrice_SixPack.value; 
Item3Cost = document.orderform.ItemPrice_TwelvePack.value; 
Item4Cost = document.orderform.ItemPrice_Wicks.value; 

Item5Qty = document.orderform.Qty_OneDegree.value;
Item6Qty = document.orderform.Qty_OneDegreeWithScentTube.value;
Item1Qty = document.orderform.Qty_ThreePack.value;
Item2Qty = document.orderform.Qty_SixPack.value;
Item3Qty = document.orderform.Qty_TwelvePack.value;
Item4Qty = document.orderform.Qty_Wicks.value;

TotalPrice_OneDegree = (Item5Cost * Item5Qty);
TotalPrice_OneDegreeWithScentTube = (Item6Cost * Item6Qty);
TotalPrice_ThreePack = (Item1Cost * Item1Qty);
TotalPrice_SixPack = (Item2Cost * Item2Qty);
TotalPrice_TwelvePack = (Item3Cost * Item3Qty);
TotalPrice_Wicks = (Item4Cost * Item4Qty);

//ttl += aItem1Cost * aItem1Qty;
SubTotal += TotalPrice_OneDegree;
SubTotal += TotalPrice_OneDegreeWithScentTube;
SubTotal += TotalPrice_ThreePack;
SubTotal += TotalPrice_SixPack;
SubTotal += TotalPrice_TwelvePack;
SubTotal += TotalPrice_Wicks;
SubTotal = round_decimals(SubTotal,2);
document.orderform.SubTotal.value = pad_with_zeros(SubTotal, 2);


if (TotalPrice_OneDegree > 0) { 
	document.orderform.TotalPrice_OneDegree.value = pad_with_zeros(TotalPrice_OneDegree, 2);
	} else { 
document.orderform.TotalPrice_OneDegree.value = "";
}
if (TotalPrice_OneDegreeWithScentTube > 0) { 
	document.orderform.TotalPrice_OneDegreeWithScentTube.value = pad_with_zeros(TotalPrice_OneDegreeWithScentTube, 2);
	} else { 
document.orderform.TotalPrice_OneDegreeWithScentTube.value = "";
}
if (TotalPrice_ThreePack > 0) { 
	document.orderform.TotalPrice_ThreePack.value = pad_with_zeros(TotalPrice_ThreePack, 2);
	} else { 
document.orderform.TotalPrice_ThreePack.value = "";
}
if (TotalPrice_SixPack > 0) { 
document.orderform.TotalPrice_SixPack.value = pad_with_zeros(TotalPrice_SixPack, 2);
	} else { 
document.orderform.TotalPrice_SixPack.value = "";
}
// Oddly enough, the next two items display the incorrect number of decimals places without the round_decimals function, but the two above are OK
if (TotalPrice_TwelvePack > 0) { 
document.orderform.TotalPrice_TwelvePack.value = pad_with_zeros(round_decimals(TotalPrice_TwelvePack, 2),2);
	} else { 
document.orderform.TotalPrice_TwelvePack.value = "";
}
if (TotalPrice_Wicks > 0) { 
document.orderform.TotalPrice_Wicks.value = pad_with_zeros(round_decimals(TotalPrice_Wicks, 2),2);
	} else { 
document.orderform.TotalPrice_Wicks.value = "";
}


// Calculate Shipping
Shipping = 0;

if (SubTotal > 0 && SubTotal <= 8.00) {
Shipping = 1.25;
}

if (SubTotal > 8.00 && SubTotal <= 30.00) {
Shipping = 3.00;
}

//if (SubTotal > 20.00 && SubTotal <= 60.00) {
//Shipping = 5.00;
//}

if (SubTotal > 30.00) {
Shipping = 5.00;
}

// ****DISCOUNT*****
// If discountShipAmt exists, set the shipping to that amount (for Free Shipping)
if (discountShipAmt) {
	Shipping = discountShipAmt;
}
// *******************

ShippingAmount = round_decimals(Shipping,2);
document.orderform.ShippingAmount.value = pad_with_zeros(Shipping, 2);

// Calcualte taxes
TaxAmount = 0;

if (document.orderform.IsTaxable.checked == true) {
	TaxAmount = round_decimals(SubTotal * 0.055,2);
} 
if (document.orderform.IsTaxable.checked == false) {
	TaxAmount = round_decimals(0,2);
}
	document.orderform.TaxAmount.value = pad_with_zeros(TaxAmount,2);


if (SubTotal > 0) {
GrandTotal = round_decimals((SubTotal + ShippingAmount + TaxAmount),2);
}
document.orderform.GrandTotal.value = pad_with_zeros(GrandTotal,2);

}
