...

View Full Version : what's wrong with this code?



Indigo1
12-14-2009, 08:09 PM
function CalculatePayment(form) {

// Check Amount is not a number
if (isNaN(form.CheckAmount.value) || form.CheckAmount.value=="") {
alert("Please enter a valid check amount.");
form.CheckAmount.select();
form.CheckAmount.focus();
return;
}
else {
form.CheckAmount.value = moneyConvert(form.CheckAmount.value);
}

var totalMoney = parseFloat(form.CheckAmount.value);
var copayDue = parseFloat(form.CopayDue.value);
var deductibleDue = parseFloat(form.DeductibleDue.value);
var totalDue = parseFloat(form.TotalDue.value);

// COPAY
if (totalMoney > copayDue) {
form.Copay.value = moneyConvert(copayDue);
totalMoney = (totalMoney - copayDue);
}
else {
form.Copay.value = moneyConvert(totalMoney);
totalMoney = 0;
}

// DEDUCTIBLE
if (totalMoney > deductibleDue) {
form.Deductible.value = moneyConvert(deductibleDue);
totalMoney = (totalMoney - deductibleDue);
}
else {
form.Deductible.value = moneyConvert(totalMoney);
totalMoney = 0;
}

var copay = parseFloat(form.Copay.value);
var deductible = parseFloat(form.Deductible.value);

// COPAY ADJUSTMENT
if (form.FullPay.checked)
form.CopayAdjustment.value = moneyConvert(copayDue - copay);
else
form.CopayAdjustment.value = "0.00";

// DEDUCTIBLE ADJUSTMENT
if (form.FullPay.checked)
form.DeductibleAdjustment.value = moneyConvert(deductibleDue - deductible);
else
form.DeductibleAdjustment.value = "0.00";

// OVERPAY
form.Overpay.value = moneyConvert(totalMoney);
}

function convertMoneyCents(txt) {
if (!isNaN(txt.value)) {
txt.value = moneyConvert(txt.value);
}
}

function DoCal(szTarget, szFieldLabel) {
var szDate = window.showModalDialog("../Calendar.html",szFieldLabel,"dialogHeight:230px;dialogWidth:220px;;center=1;status=0");
if((szDate != "") && (szDate != "undefined")) {
szTarget.value = szDate; }
}

function moneyConvert(strval) {
strval = Math.abs(strval) + "";
if (strval.indexOf(".") == -1) {
strval = strval + '.00';
}
else {
strval = strval + "00";
strval = strval.substr(0, strval.indexOf(".") + 3);
}
return strval;
}

HTML


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>patientCalc</title>
<link href="patientCalc.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="patientCalc.js"></script>
</head>
<body>
<form id="frmPayment" action="">
<p>
Total Balance: <input type="text" id="TotalDue" value="125.25" />
</p>

<p>
Total CoPay: <input type="text" id="CopayDue" value="114.54" />
</p>

<p>
Total Deductible: <input type="text" id="DeductibleDue" value="10.71" />
</p>

<p>
Original Total: <input type="text" id="OriginalTotal" value="125.25" disabled="disabled" />
</p>


<p>
Check Amount: <input type="text" id="CheckAmount" />
</p>






<p>
Copay: <input type="text" id="Copay" />
</p>

<p>
Deductible: <input type="text" id="Deductible" />
</p>

<p>
Pay in Full: <input type="checkbox" id="FullPay" checked="checked" />
</p>


<p>
Copay Adjustment: <input type="text" id="CopayAdjustment" />
</p>

<p>
Deductible Adjustment: <input type="text" id="DeductibleAdjustment" />
</p>

<p>
Overpay: <input type="text" id="Overpay" />
</p>

<p>
<input type="button" value="Calculate Payment" onclick="CalculatePayment(document.forms[0]);" />
</p>
</form>

</body>
</html>
the Deductible keeps coming up 0.00

Old Pedant
12-14-2009, 08:50 PM
Is it a trick question??

First answer: The JavaScript looks ugly because it's not indented.

Or do you mean it doesn't work in some way?

Indigo1
12-14-2009, 08:51 PM
it's not working in some way.

Old Pedant
12-14-2009, 09:40 PM
LOL!!! Okay, you win the battle of nerves!

In *WHAT* way is it not working????

Old Pedant
12-14-2009, 09:41 PM
Here's my cleanup of the ugly code.

It *seems* to work, but maybe not doing what it should????


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>patientCalc</title>
<script type="text/javascript">
function getNum(fld)
{
var val = fld.value;
if ( val == "" ) return null;
val = parseFloat(val.replace(/[^\d\.]/g,""));
if ( isNaN(val) ) return null;
return val;
}
function getNumZ(fld)
{
var val = getNum(fld);
if ( val == null ) val = 0;
return val;
}
function CalculatePayment(form)
{

// Check Amount is not a number
var camt = getNum(form.CheckAmount);
if ( camt == null )
{
alert("Please enter a valid check amount.");
form.CheckAmount.select();
form.CheckAmount.focus();
return;
}
form.CheckAmount.value = "$" + camt.toFixed(2);

var totalMoney = camt;
var copayDue = getNumZ(form.CopayDue);
var deductibleDue = getNumZ(form.DeductibleDue);
var totalDue = getNumZ(form.TotalDue);

// COPAY
if (totalMoney > copayDue)
{
form.Copay.value = "$" + copayDue.toFixed(2);
totalMoney = (totalMoney - copayDue);
} else {
form.Copay.value = "$" + totalMoney.toFixed(2);
totalMoney = 0;
}

// DEDUCTIBLE
if (totalMoney > deductibleDue)
{
form.Deductible.value = "$" + deductibleDue.toFixed(2);
totalMoney = (totalMoney - deductibleDue);
} else {
form.Deductible.value = "$" + totalMoney.toFixed(2);
totalMoney = 0;
}

var copay = getNumZ(form.Copay);
var deductible = getNumZ(form.Deductible);
// COPAY ADJUSTMENT
if (form.FullPay.checked)
{
form.CopayAdjustment.value = "$" + (copayDue - copay).toFixed(2);
} else {
form.CopayAdjustment.value = "$0.00";
}

// DEDUCTIBLE ADJUSTMENT
if (form.FullPay.checked)
{
form.DeductibleAdjustment.value = "$" + (deductibleDue - deductible).toFixed(2);
} else {
form.DeductibleAdjustment.value = "$0.00";
}
// OVERPAY
form.Overpay.value = "$" + totalMoney.toFixed(2);

}
</script>
</head>
<body>
<form id="frmPayment" action="">
<p>
Total Balance: <input type="text" id="TotalDue" value="125.25" />
</p>
<p>
Total CoPay: <input type="text" id="CopayDue" value="114.54" />
</p>
<p>
Total Deductible: <input type="text" id="DeductibleDue" value="10.71" />
</p>
<p>
Original Total: <input type="text" id="OriginalTotal" value="125.25" disabled="disabled" />
</p>
<p>
Check Amount: <input type="text" id="CheckAmount" />
</p>
<p>
Copay: <input type="text" id="Copay" />
</p>
<p>
Deductible: <input type="text" id="Deductible" />
</p>
<p>
Pay in Full: <input type="checkbox" id="FullPay" checked="checked" />
</p>
<p>
Copay Adjustment: <input type="text" id="CopayAdjustment" />
</p>
<p>
Deductible Adjustment: <input type="text" id="DeductibleAdjustment" />
</p>
<p>
Overpay: <input type="text" id="Overpay" />
</p>
<p>
<input type="button" value="Calculate Payment" onclick="CalculatePayment(this.form);" />
</p>
</form>

</body>
</html>

Indigo1
12-14-2009, 09:49 PM
the Deductible keeps coming up 0.00


........

Old Pedant
12-14-2009, 10:23 PM
Not in my cleaned up version.

If I put in a check amount of $121.00 then I (correctly, I think) get a deductible payment of $6.46.

Try mine.

Indigo1
12-15-2009, 01:14 AM
i think the deductible isn't coming out correctly though.

Old Pedant
12-15-2009, 02:58 AM
With my code, and a Check Amount of 100.00, I see the following:

Total Balance: 125.25
Total CoPay: 114.54
Total Deductible: 10.71
Original Total: 125.25
Check Amount: 100.00

Copay: 100.00 [so the check went ENTIRELY to the copay due]
Deductible: 0.00 [and none is applied to the deductible]
Pay in Full: [checked]
Copay Adjustment: 14.54 [so person still owes this much on the copay]
Deductible Adjustment: 10.71 [and person still owes *ALL* of the deductible]
Overpay: 0.00

If I use a check amount of 120.00 I see this:


Total Balance: 125.25
Total CoPay: 114.54
Total Deductible: 10.71
Original Total: 125.25
Check Amount: 120.00

Copay: 114.54 [full amount of the copay]
Deductible: 5.46 [and this much is applied to the deductible]
Pay in Full: [checked]
Copay Adjustment: 0.00 [copay is paid in full]
Deductible Adjustment: 5.25 [and person still owes only this on the deductible]
Overpay: 0.00

Looks right to me.

NOW....

In *MOST* medical payment systems I've seen, the Deductible is paid first and *then* the Copay. It would be easy to change the code to that, if that's what you need.

[/code]



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum