/* This script is 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 CalculateTotal(frm) {
    var order_subtotal = 0;
    var order_total = 0;
		var do_discount = 0;
		var do_gift = 0;
		
		var secret_code = 'EAE';
		var gift_code = 'FUN';
		
		var per_cent_discount = 15;
		
		var discountable_amount = 0;
		
		dcode = document.getElementById('discount_code').value;
		
		if (dcode == secret_code) {
			do_discount = 1;
		}
		
		if (dcode == gift_code) {
			do_gift = 1;
		}
		
		var product_line_count = 0;
		
    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {

        // Get the current field
        form_field = frm.elements[i]

        // Get the field's name
        form_name = form_field.name

        // Is it a "product" field?
        if (form_name.substring(0,7) == "product") {
						
            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_subtotal += item_quantity * item_price;
								if (product_line_count > 0) {
									discountable_amount += item_quantity * item_price
								}
            }
						product_line_count++;
        }
    }



//save subtotal to display before discount
display_subtotal = order_subtotal

//do discount?
if (do_discount) {
	document.getElementById('discount_info').innerHTML = "";
	disc = discountable_amount * (per_cent_discount / 100);
	document.getElementById('discount_amount').value = '('+round_decimals(disc, 2)+')';
	order_subtotal -= disc;
	new Effect.Highlight('discount_amount', {startcolor:'#E1732E', endcolor:'#586B75'})
} else {
	document.getElementById('discount_amount').value = "(0.00)";
}

if (do_gift && display_subtotal >= 30) {
	document.getElementById('discount_info').innerHTML = "<br />FREE Dermaclear Organic&nbsp;Sun&nbsp;Cream<br />$27 Value</b>";
} else {
	document.getElementById('discount_info').innerHTML = "";
	
}

new Effect.Highlight('SUBTOTAL', {startcolor:'#E1732E', endcolor:'#586B75'})
new Effect.Highlight('SHIPPING', {startcolor:'#E1732E', endcolor:'#586B75'})
new Effect.Highlight('TOTAL', {startcolor:'#E1732E', endcolor:'#586B75'})

	// Add shipping
	if (frm.contact_approves_international_rates.checked) {	// If international order
		frm.SHIPPING.value = 'TBA'
		order_total = order_subtotal;
	} else if (order_subtotal < 100.00) {		// Orders less than $100
		frm.SHIPPING.value = '9.95'
		order_total = order_subtotal + 9.95
	} else {		// everything else
		frm.SHIPPING.value = 'Free'
		order_total = order_subtotal
	}

    // Display the total rounded to two decimal places
    frm.SUBTOTAL.value = round_decimals(display_subtotal, 2)
    frm.TOTAL.value = round_decimals(order_total, 2)
		document.getElementById('total_message').innerHTML = round_decimals(order_total, 2);
}

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
}
