// calculate order form total

function CalculateTotal(frm) {
    var order_total = 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,4) == "item") {

            // 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_total += item_quantity * item_price
            }
        }
    }

    // Display the total rounded to two decimal places
    frm.total.value = 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"
        }
	
	// Add the dollar sign
	value_string = '$' + value_string;
	
    return value_string
}

function clear_input(inputID,frm){
	var input = document.getElementById(inputID);
	input.value = '0';
	input.value = '';
	CalculateTotal(frm);
}

/*
// CLEAR AN INPUT
function ClearInput(value, id){
	var input = document.getElementById(id); // Gets the input field based on its id.
	
	if(value == input.value){ // If the default value is equal to the current value.
		input.value = ''; // Empty It.
	} else{ // Else the value is not equal to the current input field value.
		input.value = input.value; // Leave it the same.
	}
}

// show or hide via a checkbox
function togglecontent(id1,id2,id3,id4) {
	
	var obj1 = document.getElementById(id1);
	var obj2 = document.getElementById(id2);
	var obj3 = document.getElementById(id3);
	var obj4 = document.getElementById(id4);
	
	if (obj1.style.visibility=="visible") {
		obj1.style.visibility = "hidden";
		obj1.style.display = "none";
	} else {
		obj1.style.visibility = "visible";
		obj1.style.display = "block";
	}
	
	if (obj2.style.visibility=="visible") {
		obj2.style.visibility = "hidden";
		obj2.style.display = "none";
	} else {
		obj2.style.visibility = "visible";
		obj2.style.display = "block";
	}
	
	if (obj3.style.visibility=="visible") {
		obj3.style.visibility = "hidden";
		obj3.style.display = "none";
	} else {
		obj3.style.visibility = "visible";
		obj3.style.display = "block";
	}
	
	if (obj3.style.visibility=="hidden") {
		document.order_list.obj4.value = "";
	} else {
		//obj4.value = obj4.value;
	}
	
}

// show or hide via a checkbox
function togglecontent2(id) {
	
	var obj = document.getElementById(id);
	
	if (obj.style.visibility=="visible") {
		obj.style.visibility = "hidden";
		//obj.style.display = "none";
	} else {
		obj.style.visibility = "visible";
		//obj.style.display = "inline-block";
	}
	
}*/
