Calculating Percentage in a form
Hi there and thanks in advance for any assistance.
I am writing a form and doing a bunch of calculations one of which is a percentage based on the value selected. I am returning a correct result for two of the values but for the other two I am not.
Clearly I am missing something and maybe not testing for it but as I am clueless I can't think of what.
The 'discount' value is what I am looking for. It should return as 0 for values of 1 but it is doing the math as though it is multiplying by 1. And again for the value of 6 it should be 10% but is taking the value as 4.6 or something.
Code is below I've included all of it (sorry) for better ease of understanding the math that is happening:
HTML Code:
<form id="form1" action="" method="get">
<fieldset>
<p>
<label for="Type">Type of Storage:</label>
<select id="Type" name="type">
<option name="Heated Storage" value="60">Heated - $60/month</option>
<option name="Unheated Storage" value="50">Unheated - $50/month</option>
<option name="Offsite Storage" value="40">Offsite - $40/month</option>
</select>
</p>
<p>
<label for="Length">How long you need storage:</label>
<select id="Length" name="length">
<option name="Don't know" value="1">Unsure (month to month)</option>
<option name="One month" value="1">One month </option>
<option name="3 months" value="3">3 months (5% pre-payment discount)</option>
<option name="6 months" value="6">6 months (10% pre-payment discount)</option>
<option name="one year" value="12">One year (15% pre-payment discount)</option>
</select>
</p>
<p>
<label for="Gobox">Number of Goboxes:</label>
<select id="Gobox" name="gobox">
<option value="1">1 - $70 delivery fee</option>
<option value="2">2 - $100 delivery fee</option>
<option value="3">3 - $130 delivery fee</option>
<option value="4">4 - $160 delivery fee</option>
<option value="5">5 - $190 delivery fee</option>
<option value="6">6 - $220 delivery fee</option>
<option value="7">7 - $250 delivery fee</option>
<option value="8">8 - $280 delivery fee</option>
<option value="9">9 - $310 delivery fee</option>
<option value="10">10 - $340 delivery fee</option>
</select>
</p>
<p>
<label for="delivery">Delivery:</label>
<input type="text" id="delivery" size="10" maxlength="10" readonly="readonly" value="0" />
<br />
<label for="storageCosts">Storage Costs:</label>
<input type="text" id="storageFees" size="10" maxlength="10" readonly="readonly" value="0" />
<br />
<label for="discount">Discount:</label>
<input type="text" id="discount" size="10" maxlength="10" readonly="readonly" value="0" />
<br />
<label for="taxes">Taxes:</label>
<input type="text" id="roundedTaxes" size="10" maxlength="10" readonly="readonly" value="0" />
<br />
<label for="total">Final Total:</label>
<input type="text" id="finalTotal" size="10" maxlength="10" readonly="readonly" value="0" />
</p>
<p align="right" ><input type="button" id="submit" value="Get an Esitmate" onclick="processForm()" /> </p>
</fieldset>
</form>
<script type="text/javascript">
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(100,dec))/Math.pow(100,dec);
return result;
}
function processForm() {
//get form variables
var theType = form1.type.options[form1.type.selectedIndex].value;
var theLength = form1.length.options[form1.length.selectedIndex].value;
var theGoBox = form1.gobox.options[form1.gobox.selectedIndex].value;
//set tax rate
var theTaxes = 0.07;
//set shipping rate
var theShipping = 30.00;
//variable for order total
var orderTotal = 0.00;
//the display box
var deliveryBox = document.getElementById( "delivery" );
var storageBox = document.getElementById( "storageFees" );
var discountBox = document.getElementById( "discount" );
var taxesBox = document.getElementById( "roundedTaxes" );
var totalBox = document.getElementById( "finalTotal" );
//Calculate Delivery
delivery = (theGoBox * theShipping) + 40;
//Calculate Storage Costs
storageFees = ((theType * theGoBox)* theLength);
//Calculate prepayment Discount
if (theLength = 3) {
//multiply storage costs by 5%
discount = storageFees *(5/100);
}
else if (theLength = 6) {
//multiply storage costs by 10%
discount = storageFees *(10/100);
}
else if (theLength = 12) {
//multiply storage costs by 15%
discount = storageFees *(15/100);
}
else {
discount = 0;
}
//Calculate Taxes
taxes = (storageFees + delivery) * theTaxes;
var roundedTaxes = roundNumber(taxes,2);
//figure final total
finalTotal = delivery + storageFees + roundedTaxes;
//output to text boxes
deliveryBox.value = "$" + delivery;
storageBox.value = "$" + storageFees;
discountBox.value = "$" + discount;
taxesBox.value = "$" + roundedTaxes;
totalBox.value = "$" + finalTotal;
}
</script>