Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

Javascript calculation

I have the provider_payment field set to auto-calculation, however I want to override this and be able to manually enter the provider_payment amount IF the broker_name is set to 0, or null OR it can be done by checking if the broker_discount is 0.0000 or null.


<script type="text/javascript">
var prevSelect=0;
function showDivSelect(divID) {
        var rates_1=[0.0000, 0.0100, 0.0250, 0.0761, 0.000]; 
        if( parseInt(divID) != 0 )
        document.getElementById("promo_rate_span").style.display="";
        else
        document.getElementById("promo_rate_span").style.display="none"; 
    document.getElementById("promo_rate").value=rates_1[divID].toFixed(4);
calculate_worksheet()
} 
 
function showDivSelect2(divID) {
        var rates_2=[0.0000, 0.0600, 0.0600, 0.0600, 0.0600]; 
        if( parseInt(divID) != 0 )
        document.getElementById("broker_discount_rate_span").style.display="";
        else
        document.getElementById("broker_discount_rate_span").style.display="none"; 
    document.getElementById("broker_discount_rate").value=rates_2[divID].toFixed(4);
calculate_worksheet()
} 
 
 
 
 
function two_decimals(number) {
 
      var my_number = String(number);
      
      if(my_number.indexOf('.') < 1) {
            my_number += ".00";
            return my_number;
      } 
      if((my_number.length == my_number.indexOf('.') + 2) && (my_number.indexOf('.') != 0)) {
            my_number += "0";
            return my_number;
      }
      if((my_number.length > my_number.indexOf('.') + 2) && (my_number.indexOf('.') != 0)) {
            my_number = parseFloat(my_number);
            return Math.round(my_number*100)/100;
      }
      return my_number;
}
 
function calculate_worksheet() {
      
      //Promo Amount
      var promo_amount = parseFloat(document.getElementById('charge_auth_amount').value) * parseFloat(document.getElementById('promo_rate').value); 
      document.getElementById('promo_amount').value = two_decimals(promo_amount);      
      if( isNaN(document.getElementById('promo_amount').value) ) { document.getElementById('promo_amount').value = '0.00'; }
 
      //Sub Total Amount
      var sub_total_amount = parseFloat(document.getElementById('charge_auth_amount').value) - parseFloat(document.getElementById('charge_auth_amount').value) * parseFloat(document.getElementById('provider_discount').value); 
      document.getElementById('sub_total_amount').value = two_decimals(sub_total_amount);   
      if( isNaN(document.getElementById('sub_total_amount').value) ) { document.getElementById('sub_total_amount').value = '0.00'; }
 
      //Provider Payment
      var provider_payment = parseFloat(document.getElementById('sub_total_amount').value) - parseFloat(document.getElementById('promo_amount').value); 
      document.getElementById('provider_payment').value = two_decimals(provider_payment);      
      if( isNaN(document.getElementById('provider_payment').value) ) { document.getElementById('provider_payment').value = '0.00'; }     
       
      //mcf Amount
      var mcf_amount = parseFloat(document.getElementById('charge_auth_amount').value) * parseFloat(document.getElementById('broker_discount_rate').value) - parseFloat(document.getElementById('promo_amount').value); 
      document.getElementById('mcf_amount').value = two_decimals(mcf_amount);      
      if( isNaN(document.getElementById('mcf_amount').value) ) { document.getElementById('mcf_amount').value = '0.00'; }
       
      //Broker Amount
      var broker_amount = parseFloat(document.getElementById('sub_total_amount').value) - parseFloat(document.getElementById('provider_payment').value) - parseFloat(document.getElementById('promo_amount').value); 
      document.getElementById('broker_amount').value = two_decimals(broker_amount);       
      if( isNaN(document.getElementById('broker_amount').value) ) { document.getElementById('broker_amount').value = '0.00'; }  
 
}
 
</script>
 
<body onLoad="javascript:calculate_worksheet();">
 
<Table>
 
<tr>
<td id="label">Broker Deal</td>
<td>
<span style="float: left">
<SELECT id="rates_2" NAME="broker_name" onchange="showDivSelect2(this.value);">
<OPTION VALUE="0" <? if ($broker_name == "" || $broker_name == 0) { echo "selected"; } ?>>None</option>
<OPTION VALUE="1" <? if ($broker_name == 1) { echo "selected"; } ?>>MRP</option>
<OPTION VALUE="2" <? if ($broker_name == 2) { echo "selected"; } ?>>Surgery Loans</option>
<OPTION VALUE="3" <? if ($broker_name == 3) { echo "selected"; } ?>>Acquired</option>
<OPTION VALUE="4" <? if ($broker_name == 4) { echo "selected"; } ?>>Surgery Finance Center (TSFC)</option>
<OPTION VALUE="5" <? if ($broker_name == 5) { echo "selected"; } ?>>Other</option>
</SELECT>
</span>			
</td>
</tr>
 
<tr>
<td id="label">Broker Discount</td>
<td>
<span id="broker_discount_rate_span" style="<? if ($broker_name == 0 || $broker_name == "" || $broker_name == 1 || $broker_name == 2 || $broker_name == 3 || $broker_name == 4 || $broker_name == 5) { echo "display: visible"; } else { echo "display:none"; } ?>; float: left">
<input type="text" name="broker_discount_rate" value="<? if ($broker_name == 0 || $broker_name == "") { echo "0.0000"; } elseif ($broker_name == 1) { echo "0.0600"; } elseif ($broker_name == 2) { echo "0.0600"; } elseif ($broker_name == 3) { echo "0.0600"; } elseif ($broker_name == 4) { echo "0.0600"; } elseif ($broker_name == 5) { echo "0.0600"; }?>" size="15" id="broker_discount_rate" onBlur="javascript:calculate_worksheet();">
</span> 
 </td>
</tr>
 
<tr>
<td id="label">Provider Payment</td>
<td><input type="text" name="provider_payment" value="<?=$provider_payment?>" size="10" id="provider_payment" onBlur="javascript:calculate_worksheet();"></td>
</tr>	 
 
</body>

Open in new window

0
pdheady
Asked:
pdheady
  • 2
1 Solution
 
HonorGodCommented:
Where you have:

<SELECT id="rates_2" NAME="broker_name" onchange="showDivSelect2(this.value);">

Calls showDivSelect2() and passes it the specified value (e.g., 0, 1, 2,...5)

So the code that you have in that function, "kind of"  does what you want.

Is this the kind of thing you want?


function showDivSelect2(divID) {
  var rates_2=[0.0000, 0.0600, 0.0600, 0.0600, 0.0600]; 
  var span = document.getElementById("broker_discount_rate_span");
  if ( span ) {
    if ( parseInt( divID ) != 0 ) {
      span.style.display="";
      document.getElementById("broker_discount_rate").value=rates_2[divID].toFixed(4);
      calculate_worksheet();
    } else {
      span.style.display="none"; 
    }
  }
 
} 

Open in new window

0
 
pdheadyAuthor Commented:
Thanks forgot to close this out.
0
 
HonorGodCommented:
You are very welcome.  Thanks for the grade and points.

Good luck & have a great day
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now