$(document).ready(function() {
function calcTotal() {
var total = 0;
// Somma tutti gli input e select che NON hanno data-tipo="logo"
$("input:checked, select:selected").each(function() {
if ($(this).data("tipo") !== "logo") {
var price = parseFloat($(this).data("prezzo")) || 0;
total += price;
}
});
// Processa i radio button con data-tipo="logo"
var logoRadios = $("input:checked[data-tipo='logo']");
// In questo gruppo, contiamo solo quelli che hanno valore "2" (cioè "sì")
var logoYes = logoRadios.filter(function() {
return $(this).val() === "2";
});
var countLogoYes = logoYes.length;
// Se il numero di loghi "sì" è maggiore di 3, aggiungiamo il prezzo per ogni logo in eccesso
if (countLogoYes > 3) {
// Supponiamo che tutti i radio button "logo" abbiano lo stesso prezzo (preso dal primo)
var logoPrice = parseFloat(logoYes.first().data("prezzo")) || 0;
total += logoPrice * (countLogoYes - 3);
}
return Number(total).toFixed(2);
}
function updateTotal() {
var totalz = calcTotal();
// Aggiorna i div come nel codice originale
$("#riassunto1").html('€' + totalz + '
Prices are VAT and Shipping fees excluded.');
$("#riassunto2").html('€' + totalz + '
Prices are VAT and Shipping fees excluded.');
$("#riassunto3").html('€' + totalz + '
Prices are VAT and Shipping fees excluded.');
$("#riassunto4").html('€' + totalz + '
Prices are VAT and Shipping fees excluded.');
$("#riassunto5").html('€' + totalz + '
Prices are VAT and Shipping fees excluded.');
$("#riassunto6").html('€' + totalz + '
Prices are VAT and Shipping fees excluded.');
$("#riassunto7").html('€' + totalz + '
Prices are VAT and Shipping fees excluded.');
$("#riassuntot").html('€' + totalz + '
Prices are VAT and Shipping fees excluded.');
}
// Calcola e aggiorna il totale all'avvio
updateTotal();
// Ricalcola il totale ogni volta che viene cliccato o modificato un input
$("input:checkbox, input:radio, select").on("click change", function() {
updateTotal();
});
});