Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Just learning - I need help with this code

Status
Not open for further replies.

ckdoublenecks

Programmer
Dec 28, 2010
9
0
0
US
nothing works beyond the //*###################

Code:
<script>
function $_(IDS) { return document.getElementById(IDS); }
function calculate_paid() {
   var amtpaid = document.getElementById("amtpaid");
   var rentdue = document.getElementById("rentdue");
 var prevbal = document.getElementById("prevbal");
var hudpay = document.getElementById("hudpay");
var tentpay = document.getElementById("tentpay");
 var datepaid = document.getElementById("datepaid");
 var late = document.getElementById("late");
 var damage = document.getElementById("damage");
 var courtcost = document.getElementById("courtcost");
 var nsf = document.getElementById("nsf");
var latechg = document.getElementById("latechg");
 var secdep = document.getElementById("secdep");
  var paidsum = document.getElementById("paidsum");
  var dateNow = new Date();
  var dayNow = dateNow.getDate();
  var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
datepaid.value = datePaid;
// ***************************************************************************************
// * if paid later than the 5th of the month, insert an "L" in the late field and add 10 the the rentdue 
// *************************************************************************************** 
              if(dayNow > 5) { late.value = "L"; rentdue = rentdue + 10; }
// ***************************************************************************************
// * add the amtpaid (current) to any previous payments (paidsum)
// *************************************************************************************** 
              paidsum.value = parseInt(paidsum.value) + parseInt(amtpaid.value);
// ***************************************************************************************
// * tentpay = amtpaid - hudpay
// ***************************************************************************************
             tentpay.value = parseInt(tentpay.value) + parseInt(amtpaid.value) - parseInt(hudpay.value);
// ##############################################################
// * totOwed = everything = everything owed
// ***************************************************************************************
          var totOwed = parseInt(rentdue.value) + parseInt(prevbal.value) + 
parseInt(secdep.value) + parseInt(damage.value) + parseInt(latechg.value) + 
parseInt(courtcost.value) + parseInt(nsf.value) - parseInt(hudpay.value);
// ***************************************************************************************
// * stillOwed = what is still owed after the payment(amtpaid)
// ***************************************************************************************
          var stillOwed = totOwed - parseInt(amtpaid.value);
// ***************************************************************************************
// * if an excess has been paid, prevbal will be a minus amt & all previous chgs will be zero
// ***************************************************************************************
       if (amtpaid.value >= totOwed) { prevbal.value = totOwed - amtpaid.value; secdep.value = 0; 
       damage.value = 0; latechg.value = 0; courtcost.value = 0; nsf.value = 0;}
// ***************************************************************************************
// * if money is still owed, pay in order-prevbal,secdep,damage,latechg,courtcost,nsf
// * until payment is exhausted
// ***************************************************************************************
if (stillOwed >= prevbal.value) {stillOwed = stillOwed - prevbal.value; prevbal.value = 0;}
if (stillOwed > 0) {prevbal.value = prevbal.value - stillOwed; stillOwed = 0;}
if (stillOwed >= secdep.value) {stillOwed = stillOwed - secdep.value; secdep.value = 0;}
if (stillOwed > 0) {secdep.value = secdep.value - stillOwed; stillOwed = 0;}
if (stillOwed >= damage.value) {stillOwed = stillOwed - damage.value; damage.value = 0;}
if (stillOwed > 0) {damage.value = damage.value - stillOwed; stillOwed = 0;}
if (stillOwed >= latechg.value) {stillOwed = stillOwed - latechg.value; latechg.value = 0;}
if (stillOwed > 0) {latechg.value = latechg.value - stillOwed; stillOwed = 0;}
if (stillOwed >= courtcost.value) {stillOwed = stillOwed - courtcost.value; courtcost.value = 0;}
if (stillOwed > 0) {courtcost.value = courtcost.value - stillOwed; stillOwed = 0;}
if (stillOwed >= nsf.value) {stillOwed = stillOwed - nsf.value; nsf.value = 0;}
if (stillOwed > 0) {nsf.value = nsf.value - stillOwed; stillOwed = 0;}
}
</script>
 
Hi

Please help us to help :
[ul]
[li]Post the related HTML too ( sorry, I have no time to reproduce and populate your 13 [tt]input[/tt] to be able to test your code )[/li]
[li]Do not post unused code ( you declared $_() as a shortcut to [tt]getElementById()[/tt], then never used it )[/li]
[li]Do not post decoration ( I mean the ASCII art boxes around your comments )[/li]
[li]Use consistent indenting ( choose an indent style, or even invent your own, but use it consistently )[/li]
[li]Explain the error or erroneous behavior ( "nothing works" is not an explanation )[/li]
[li]Use meaningful subject for the thread ( "I need help" should appear in the subject )[/li]
[/ul]
Anyway, the obvious problem is in this line :
Code:
[b]if[/b][teal]([/teal]dayNow [teal]>[/teal] [purple]5[/purple][teal])[/teal] [teal]{[/teal] late[teal].[/teal]value [teal]=[/teal] [green][i]"L"[/i][/green][teal];[/teal] rentdue [teal]=[/teal] rentdue [teal]+[/teal] [purple]10[/purple][teal];[/teal] [teal]}[/teal]
There you add an [tt]HTMLInputElement[/tt] object and a number. As the addition is not implemented for such operands, JavaScript stringifies them then concatenates the values. So rentdue's new value will be a string, which has no value property. And because [tt]parseInt()[/tt] returns [tt]NaN[/tt] for [tt]undefined[/tt], the whole formula for totOwed's calculation will return [tt]NaN[/tt].


Feherke.
 
As I indicated in my post, I'm just learning this and thanks for the input but I also indicated in the post that: when I enter the amtpaid the datepaid(current date), the "L"(late), tentpay & paidsum all are inserted.
 
I use Firefox with the Firebug add on. Things like NaN show up very quickly when you break and single step.

The IE debug environment can do this just as well and even though it is familiar if you use VB6 & VBA it can steer you to VBScript and generate browser compatibility problems.

I have up to to 1600 lines of JavaScript on some of my page and I couldn't begin to get to that level without the de-bugger.

using eg:
Code:
alert("variable value is "+variable);
was used first and it can only take yu so far.

Best of luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top