I am working on a script that calculates the rental rate for a property. I currently have this code and form, which works as I wish it to.
<script type="text/javascript">
function roundCents(amount) {
cents = amount * 100;
cents = Math.round(cents);
str = "" + cents;
len = str.length;
return str.substring(0, len - 2) + "." + str.substring(len - 2, len);
}
function update(form) {
var cleaningfee = 50
form.cleaningfee.value = '$' + roundCents(cleaningfee);
var subtotal1 = (form.nights.value - 0) * (form.unitcost.value - 0);
var subtotal2 = subtotal1 + (cleaningfee - 0);
form.subtotal.value = '$' + roundCents(subtotal2);
var tax = subtotal2 / 100 * (12 - 0);
form.tax.value = '$' + roundCents(tax);
var total = subtotal2 + tax;
form.total.value = '$' + roundCents(total);
var deposit = 200
form.deposit.value = '$' + roundCents(deposit);
balance = total + (deposit - 0);
form.balance.value = '$' + roundCents(balance);
}
function clearForm(oForm) {
var elements = oForm.elements;
oForm.reset();
for(i=0; i<elements.length; i++) {
field_type = elements[i].type.toLowerCase();
switch(field_type) {
case "text":
case "hidden":
elements[i].value = "";
break;
case "select-one":
elements[i].selectedIndex = -1;
break;
default:
break;
}
}
}
</script>
<form>
<table id="calc">
<tr><td>Nights: </td><td><input type="text" name="nights" size="8"></td></tr>
<tr><td>Length of Stay: </td><td><select name="unitcost">
<option value="58">3-5 nights</option>
<option value="52">6-28 nights</option>
<option value="50">29-plus nights</option></select></td></tr>
<tr><td>Cleaning Fee: </td><td><input type="text" name="cleaningfee" value="50.00" size="8"></td></tr>
<tr><td>Subtotal: </td><td><input type="text" name="subtotal" size="8"></td></tr>
<tr><td>Tax (12%): </td><td><input type="text" name="tax" size="8"></td></tr>
<tr><td>Total: </td><td><input type="text" name="total" size="8"></td></tr>
<tr><td>Refundable Damage Deposit: </td><td><input type="text" name="deposit" value="200" size="8"></td></tr>
<tr><td>Balance: </td><td><input type="text" name="balance" size="8"></td></tr>
<tr><td> </td><td><input type="button" onClick="update(this.form)" value="Calculate"><br /><input type="button" value="Reset" name="reset_form" onClick="this.form.reset()"></div></td></tr>
</table>
</form>
Originally, the rates in this calculator differed by season, so the drop down menu listed seasons/dates. However, now that the rates are being adjusted by length of stay, it would be preferable to get rid of the “Length of Stay” part of the form and have a function that evaluated the value in “Nights” as follows:
- Less than 3 nights, an alert message that says “A minimum of 3 nights stay is required”
- 3-5 nights, multiply the nights by 58
- 6-28 nights, multiply by 52
- 329 or more nights, multiply by 50
The minimum stay message would be ideal, but if it’s too complicated, I’d rather lose that and gain this other functionality. However, I just haven’t been able to wrap my head around how to write this if/else statement to do the job.
Any pointers?
Cheers!