function update_qty(id,price2,commands){ qty = $('div_agent_qty'+id).innerHTML; qty2 = $('div_total_qty').innerHTML; price = $('div_price').innerHTML; qty = parseInt(qty); qty2 = parseInt(qty2); price = parseFloat(price); price2 = parseFloat(price2); if (commands == 'add') { qty = qty + 1; qty2 = qty2 + 1; price = price + price2; } else { if (qty != 0) { qty = qty - 1; qty2 = qty2 - 1; price = price - price2; } } if (qty2 == 0) { Element.hide('checkout_button'); } else { Element.show('checkout_button'); } updatediscount(qty2, price); $('div_agent_qty'+id).innerHTML = qty; $('div_price').innerHTML = roundNumber(price, 2); $('div_total_qty').innerHTML = qty2; document.getElementById('form_agent_qty'+id).value = qty; } function roundNumber(number,decimals) { var newString;// The new rounded number decimals = Number(decimals); if (decimals < 1) { newString = (Math.round(number)).toString(); } else { var numString = number.toString(); if (numString.lastIndexOf(".") == -1) {// If there is no decimal point numString += ".";// give it one at the end } var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point while (cutoff > 0 && (d1 == 9 || isNaN(d1))) { if (d1 != ".") { cutoff -= 1; d1 = Number(numString.substring(cutoff,cutoff+1)); } else { cutoff -= 1; } } } d1 += 1; } newString = numString.substring(0,cutoff) + d1.toString(); } if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string newString += "."; } var decs = (newString.substring(newString.lastIndexOf(".")+1)).length; for(var i=0;i < decimals-decs;i++) newString += "0"; return newString; // Output the result to the form field (change for your purposes) } function updatediscount(qty, price) { var qty = parseInt(qty); if (1 <= qty && 3 >= qty) { var final = price - (price * 0)/ 100; $('div_discount').innerHTML = '0%'; $('div_final').innerHTML = roundNumber(final,2); } if (4 <= qty && 6 >= qty) { var final = price - (price * 5)/ 100; $('div_discount').innerHTML = '-' + roundNumber((price * 5)/ 100, 2) + ' @ 5%'; $('div_final').innerHTML = roundNumber(final,2); } if (7 <= qty && 8 >= qty) { var final = price - (price * 10)/ 100; $('div_discount').innerHTML = '-' + roundNumber((price * 10)/ 100, 2) + ' @ 10%'; $('div_final').innerHTML = roundNumber(final,2); } if (8 <= qty && 10 >= qty) { var final = price - (price * 15)/ 100; $('div_discount').innerHTML = '-' + roundNumber((price * 15)/ 100, 2) + ' @ 15%'; $('div_final').innerHTML = roundNumber(final,2); } if (10 <= qty && 15 >= qty) { var final = price - (price * 20)/ 100; $('div_discount').innerHTML = '-' + roundNumber((price * 20)/ 100, 2) + ' @ 20%'; $('div_final').innerHTML = roundNumber(final,2); } if (16 <= qty && 1000000000 >= qty) { var final = price - (price * 25)/ 100; $('div_discount').innerHTML = '-' + roundNumber((price * 25)/ 100, 2) + ' @ 25%'; $('div_final').innerHTML = roundNumber(final,2); } }