Solved

Javascript calculation

Posted on 2008-06-24
3
280 Views
Last Modified: 2012-06-27
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
Comment
Question by:pdheady
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 21860276
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
 
LVL 1

Author Closing Comment

by:pdheady
ID: 31470317
Thanks forgot to close this out.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 22195015
You are very welcome.  Thanks for the grade and points.

Good luck & have a great day
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question