// JavaScript Document
function calculate() {
SubTotal = 0;
GrandTotal = 0;
Item1Cost = document.orderform.ItemPrice_Doe.value; // Doe Urine
Item2Cost = document.orderform.ItemPrice_Buck.value; // Buck Urine
Item3Cost = document.orderform.ItemPrice_ScentTube.value; //  Blaze Orange Cap
Item4Cost = document.orderform.ItemPrice_Wicks.value; //  Blaze Orange Cap
Item5Cost = document.orderform.ItemPrice_OrangeCap.value; //  Blaze Orange Cap
Item6Cost = document.orderform.ItemPrice_CamoCap.value; //  Camouflage Cap
Item7Cost = document.orderform.ItemPrice_Windsock.value; // Camouflage Windsock
Item8Cost = document.orderform.ItemPrice_DoeInEstrus.value; // Doe In Estrus Urine

Item1Qty = document.orderform.Qty_Doe.value;
Item2Qty = document.orderform.Qty_Buck.value;
Item3Qty = document.orderform.Qty_ScentTube.value;
Item4Qty = document.orderform.Qty_Wicks.value;
Item5Qty = document.orderform.Qty_OrangeCap.value;
Item6Qty = document.orderform.Qty_CamoCap.value;
Item7Qty = document.orderform.Qty_Windsock.value;
Item8Qty = document.orderform.Qty_DoeInEstrus.value;

TotalPrice_Doe = Item1Cost * Item1Qty;
TotalPrice_Buck = Item2Cost * Item2Qty;
TotalPrice_ScentTube = Item3Cost * Item3Qty;
TotalPrice_Wicks = Item4Cost * Item4Qty;
TotalPrice_OrangeCap = Item5Cost * Item5Qty;
TotalPrice_CamoCap = Item6Cost * Item6Qty;
TotalPrice_Windsock = Item7Cost * Item7Qty;
TotalPrice_DoeInEstrus = Item8Cost * Item8Qty;

//ttl += aItem1Cost * aItem1Qty;
SubTotal += TotalPrice_Doe;
SubTotal += TotalPrice_Buck;
SubTotal += TotalPrice_ScentTube;
SubTotal += TotalPrice_Wicks;
SubTotal += TotalPrice_OrangeCap;
SubTotal += TotalPrice_CamoCap;
SubTotal += TotalPrice_Windsock;
SubTotal += TotalPrice_DoeInEstrus;

/* 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 pad_with_zeros(result3, decimals)
}

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)
if (TotalPrice_Doe > 0) { 
	document.orderform.TotalPrice_Doe.value = round_decimals(TotalPrice_Doe, 2);
	} else { 
document.orderform.TotalPrice_Doe.value = "";
}
if (TotalPrice_Buck > 0) { 
document.orderform.TotalPrice_Buck.value = round_decimals(TotalPrice_Buck, 2);
	} else { 
document.orderform.TotalPrice_Buck.value = "";
}
if (TotalPrice_ScentTube > 0) { 
document.orderform.TotalPrice_ScentTube.value = round_decimals(TotalPrice_ScentTube, 2);
	} else { 
document.orderform.TotalPrice_ScentTube.value = "";
}
if (TotalPrice_Wicks > 0) { 
document.orderform.TotalPrice_Wicks.value = round_decimals(TotalPrice_Wicks, 2);
	} else { 
document.orderform.TotalPrice_Wicks.value = "";
}
if (TotalPrice_OrangeCap > 0) { 
document.orderform.TotalPrice_OrangeCap.value = round_decimals(TotalPrice_OrangeCap, 2);
	} else { 
document.orderform.TotalPrice_OrangeCap.value = "";
}
if (TotalPrice_CamoCap > 0) { 
document.orderform.TotalPrice_CamoCap.value = round_decimals(TotalPrice_CamoCap, 2);
	} else { 
document.orderform.TotalPrice_CamoCap.value = "";
}
if (TotalPrice_Windsock > 0) { 
document.orderform.TotalPrice_Windsock.value = round_decimals(TotalPrice_Windsock, 2);
	} else { 
document.orderform.TotalPrice_Windsock.value = "";
}
if (TotalPrice_DoeInEstrus > 0) { 
	document.orderform.TotalPrice_DoeInEstrus.value = round_decimals(TotalPrice_DoeInEstrus, 2);
	} else { 
document.orderform.TotalPrice_DoeInEstrus.value = "";
}

document.orderform.SubTotal.value = round_decimals(SubTotal, 2);

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

