I've broken it up into 3 seperate functions, one that calculates the total cost, one that gets the base cost and one that gets the costs of all the addons.
How it works. When you select a base model, and you addons, and hit the calculate button, it triggers the calculate_total_cost()
function, which then runs the two other functions to get the data required to do the calculations.
It then loops through all the addons, add them together and then displays the base cost plus the total addon cost.
Check the demo and let me know if this is what you expected.
//Function that calculates the final cost ( I have not taken into consideration TAX )
function calculate_total_cost() {
var base_cost = get_model_value(),
addons = getSelectedChbox(),
addonTotal = 0;
for (var i = 0; i < addons.length; i++) {
addonTotal += addons[i] + addonTotal;
}
document.getElementById('total_cost').innerHTML = base_cost + addonTotal;
}
//This function gets the base cost of the vehicle
function get_model_value() {
var cost = document.getElementsByName('model')[0].value;
return parseInt(cost);
}
//This here goes through all the checkboxes and pushes the values in to an array and returns it.
function getSelectedChbox() {
var selchbox = [],
inpfields = mini_cooper_calculation_form.getElementsByTagName('input'),
nr_inpfields = inpfields.length;
for (var i = 0; i < nr_inpfields; i++) {
if (inpfields[i].type == 'checkbox' && inpfields[i].checked == true) selchbox.push(parseInt(inpfields[i].value));
}
return selchbox;
}