...

View Full Version : Jquery - my calculations are off by $100



Popsicletoes
01-19-2012, 06:20 PM
Hello,
This is my first try with Jquery and for some reason one of my form totals is always off by $100. It is the gr_total_dollars that is off, all others are calculating properly. Here is the code


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".TextBox").hover(function(){
$(this).toggleClass('TextBoxSelected');
},function(){
$(this).toggleClass('TextBoxSelected');
}).change(function(){
calculate();
});
});
function getFldValue(fldValue) {
return isNaN(fldValue) ? 0 : parseFloat(fldValue);
}
function calculate() {
var property_SPrice = getFldValue($('#property_SPrice').val());
var price = getFldValue($('#price').val());

var REO_sale_percentage = getFldValue($('#REO_sale_percentage').val());
var REO_sale_dollars = getFldValue($('#REO_sale_dollars').val());
var REO_sale_bonus_dollars = getFldValue($('#REO_sale_bonus_dollars').val());
var REO_sale_fixed_dollars = getFldValue($('#REO_sale_fixed_dollars').val());
var REO_sale_total_dollars = getFldValue($('#REO_sale_total_dollars').val());


var REO_list_percentage = getFldValue($('#REO_list_percentage').val());
var REO_list_dollars = getFldValue($('#REO_list_dollars').val());
var REO_list_bonus_dollars = getFldValue($('#REO_list_bonus_dollars').val());
var REO_list_fixed_dollars = getFldValue($('#REO_list_fixed_dollars').val());
var REO_list_total_dollars = getFldValue($('#REO_list_total_dollars').val());

var gr_comm_percentage = getFldValue($('#gr_comm_percentage').val());
var gr_comm_dollars = getFldValue($('#gr_comm_dollars').val());
var gr_bonus_dollars = getFldValue($('#gr_bonus_dollars').val());
var gr_fixed_dollars = getFldValue($('#gr_fixed_dollars').val());
var gr_total_dollars = getFldValue($('#gr_total_dollars').val());

$('#price').val(property_SPrice);
$('#gr_comm_percentage').val(REO_list_percentage + REO_sale_percentage);

$('#gr_comm_dollars').val(getFldValue(gr_comm_percentage/100*price));
$('#REO_list_dollars').val(getFldValue(REO_list_percentage/100*price));
$('#REO_sale_dollars').val(getFldValue(REO_sale_percentage/100*price));
$('#gr_fixed_dollars').val(getFldValue(REO_sale_fixed_dollars + REO_list_fixed_dollars));
$('#gr_bonus_dollars').val(getFldValue(REO_sale_bonus_dollars + REO_list_bonus_dollars));
$('#gr_total_dollars').val(getFldValue(gr_comm_dollars + gr_fixed_dollars + gr_bonus_dollars));
$('#REO_sale_total_dollars').val(getFldValue(REO_sale_dollars + REO_sale_fixed_dollars + REO_sale_bonus_dollars));
$('#REO_list_total_dollars').val(getFldValue(REO_list_dollars + REO_list_fixed_dollars + REO_list_bonus_dollars));



}
</script>


td><cfoutput><cfif #form.list_type# IS 1 OR #form.list_type# IS 2><b>*</b></cfif></cfoutput><b>Sold Price</b> </td>
<td><input type="text" id="price" size="8" class="TextBox" onFocus="this.className='TextBoxSelected';select()" onBlur="this.className='TextBox'" onChange="calculate();" /></td>
</tr>
<tr><td colspan="2"><b>Commission Information</b>
</td>
</tr>
<!---Form headings--->
<tr><td>&nbsp;</td>
<td colspan="2" align="center"><b>Main Commission</b></td>
<td align="center"><b>Fixed</b></td>
<td align="center"><b>Bonus/<br />Other</b></td>
<td align="center"><b>Total</b></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><b>%</b></td>
<td><b>$</b></td>
<td><b>$</b></td>
<td><b>$</b></td>
<td><b>$</b></td>
</tr>
<!---End form headings--->

<tr><td><b>*</b>Gross Comm</td>
<td><input type="text" id="gr_comm_percentage" size="1" class="TextBox" value="0"> </td>
<td><input type="text" id="gr_comm_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="gr_fixed_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="gr_bonus_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="gr_total_dollars" size="8" class="TextBox"> </td>
</tr>
<tr>
<cfoutput>
<cfif #form.list_type# IS 2>
<td><b>*</b>REO List </td>
<td><input type="text" id="REO_list_percentage" size="1" class="TextBox" value="0"> </td>
<td><input type="text" id="REO_list_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="REO_list_fixed_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="REO_list_bonus_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="REO_list_total_dollars" size="8" class="TextBox"> </td>


<!---List type 1 begins here--->


<cfelseif #form.list_type# IS 1 ><td><b>*</b>Amt Pd to O/B</td>
<td><input type="Text" name="OB_percentage" size="1" class="TextBox" onFocus="this.className='TextBoxSelected';select()" onBlur="this.className='TextBox'">% </td>
<td>$<input type="text" name="OB_dollars" size="8" class="TextBox" onFocus="this.className='TextBoxSelected';select()" onBlur="this.className='TextBox'" > </td>
<cfelse><td>Amt Pd to O/B</td>
<td><input type="Text" name="OB_percentage" size="1" class="TextBox" onFocus="this.className='TextBoxSelected';select()" onBlur="this.className='TextBox'">% </td>
<td>$<input type="text" name="OB_dollars" size="8" class="TextBox" onFocus="this.className='TextBoxSelected';select()" onBlur="this.className='TextBox'"> </td>
</cfif>
</cfoutput>
</tr>
<tr>
<cfoutput>
<cfif #form.list_type# IS 2>
<td><b>*</b>REO Sale </td><!---Comp. Dollar--->
<td><input type="text" id="REO_sale_percentage" size="1" class="TextBox" value="0"> </td>
<td><input type="text" id="REO_sale_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="REO_sale_fixed_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="REO_sale_bonus_dollars" size="8" class="TextBox" value="0"> </td>
<td><input type="text" id="REO_sale_total_dollars" size="8" class="TextBox"> </td>


<cfelse><td><b>*</b>REO Gross</td>
<td><input type="Text" name="comp_percentage" size="1" class="TextBox" onFocus="this.className='TextBoxSelected';select()" onBlur="this.className='TextBox'">% </td>
<td>$<input type="text" name="comp_dollars" size="8" class="TextBox" onFocus="this.className='TextBoxSelected';select()" onBlur="this.className='TextBox'"> </td>



Can anyone tell me what I need to change? Thanks in advance for your assistance.

devnull69
01-20-2012, 06:59 AM
Hmm without digging too deep into your code, the calculation logic seems to be a bit off

FIRST you are reading field values into variables like gr_comm_dollars, gr_fixed_dollars, gr_bonus_dollars ... THEN you are changing the respective field values (without updating the variables) and FINALLY you are calculating gr_total_dollars from the variables (and not from the changed field values). I don't see the logic in here ...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum