Need coding help

I have this code(part of a working program) which automatically inserts the “tentpay” & date paid. I’m trying to add the the code seen below the comment line(//*). It does nothing ? Can someone look at it?

function calculate_paid(v)
{
 var rentdue = document.getElementById("rentdue");
 var prevbal = document.getElementById("prevbal");
 var misc = document.getElementById("misc");
 var late = document.getElementById("late");
 var amtpaid = document.getElementById("amtpaid");
 var tentpay = document.getElementById("tentpay");
 var hudpay = document.getElementById("hudpay");
 var datepaid = document.getElementById("datepaid");
 var late = document.getElementById("late");
 var dateNow = new Date();
 var dayNow = dateNow.getDate();
 var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
 switch(v)
 {
  case amtpaid:
   var tpay = amtpaid.value - hudpay.value;
   if(tpay >= 0){tentpay.value = tpay;}
   if(amtpaid.value > 0){datepaid.value = datePaid;}
   break;
  case tentpay:
   if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;}
   if(!tentpay.value || tentpay.value == " "){tentpay.value = 0;tentpay.select();}
   amtpaid.value = parseInt(tentpay.value) + parseInt(hudpay.value);
   if(tentpay.value > 0){datepaid.value = datePaid;}
   break;
  case hudpay:
   if(!tentpay.value || tentpay.value == " "){tentpay.value = 0;}
   if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;hudpay.select();}
   amtpaid.value = parseInt(tentpay.value) + parseInt(hudpay.value);
   if(hudpay.value > 0){datepaid.value = datePaid;}
   break;
//****************************************
  case prevbal:
  var balance = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value);
  if(amtpaid.value < balance.value){
  prevbal.value = parseInt(balance.value) - parseInt(amtpaid.value);}
  if(dayNow.value > 5){late.value = "L";
   prevbal.value = prevbal.value + 10;}
  break;
  } 
     } 

How are you expecting dayNow.value > 5 to work - it is testing if the date/time is more than 5 milliseconds after midnight on the 1st January 1970 which would surely always be the case.

thanks for the input. obviously I’m just learning. the code inserts the current date but I must work on the day comparison. More importantly I need to figure out why the “L” isn’t inserted when the date > 5 & why the prevbal isn’t being inserted?

<html><head>
<script>
function calculate_paid(v)
{
 var rentdue = document.getElementById("rentdue");
 var prevbal = document.getElementById("prevbal");
 var misc = document.getElementById("misc");
 var late = document.getElementById("late");
 var amtpaid = document.getElementById("amtpaid");
 var tentpay = document.getElementById("tentpay");
 var hudpay = document.getElementById("hudpay");
 var datepaid = document.getElementById("datepaid");
 var late = document.getElementById("late");
 var dateNow = new Date();
 var dayNow = dateNow.getDate();
 var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
 switch(v)
 {
  case amtpaid:
   var tpay = amtpaid.value - hudpay.value;
   if(tpay >= 0){tentpay.value = tpay;}
   if(amtpaid.value > 0){datepaid.value = datePaid;}
   break;
  case tentpay:
   if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;}
   if(!tentpay.value || tentpay.value == " "){tentpay.value = 0;tentpay.select();}
   amtpaid.value = parseInt(tentpay.value) + parseInt(hudpay.value);
   if(tentpay.value > 0){datepaid.value = datePaid;}
   break;
  case hudpay:
   if(!tentpay.value || tentpay.value == " "){tentpay.value = 0;}
   if(!hudpay.value || hudpay.value == " "){hudpay.value = 0;hudpay.select();}
   amtpaid.value = parseInt(tentpay.value) + parseInt(hudpay.value);
   if(hudpay.value > 0){datepaid.value = datePaid;}
   break;
//*
  case prevbal:
  var balance = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value);
  if(amtpaid.value < balance.value){
  prevbal.value = parseInt(balance.value) - parseInt(amtpaid.value);}
  if(dayNow.value > 5){late.value = "L";
   prevbal.value = prevbal.value + 10;}
  break;
  } 
     } 
</script>
<script type="text/javascript">
window.google_analytics_uacct = "UA-256751-2";
</script>

<script type="text/javascript">
window.google_analytics_uacct = "UA-256751-2";
</script>

</head><body>
<?php
mysql_connect(localhost,root,"");
mysql_select_db(prerentdb) or die( "Unable to select database");
if(!empty($_POST["submit"]))
{
$apt = $_POST['apt'];
$query="SELECT * FROM payments Where apt='$apt'";
$result=mysql_query($query);
if(mysql_num_rows($result))
{
echo "<form action='#' method='post'><b>Rent Payment :<br /><br />
<table cellspacing=0 cellpadding=0 border=1>
<tr>
<th>dep</th>
<th>tenant</th>
<th>apt</th>
<th>paid</th>
<th>due</th>
<th>prev</th>
<th>misc</th>
<th>tent</th>
<th>hud</th>
<th>date</th>
<th>late</th>
<th>comments</th>
</tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>
<td><input type='text' size=5 name='dep' value='" . $row['dep'] . "'></td>
<td><input type='text' size=25 name='name' value='" . $row['name'] . "'></td>
<td><input type='text' size=2 name='apt' value='" . $row['apt'] . "' ></td>
<td><input type='text' size=4 id='amtpaid' name='amtpaid' value='" . $row['amtpaid'] . "'
onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=4 name='rentdue' value='" . $row['rentdue'] . "'></td>
<td><input type='text' size=4 name='prevbal' value='" . $row['prevbal'] ."'
onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=4 name='misc' value='" . $row['misc'] . "'></td>
<td><input type='text' size=4 id='tentpay' name='tentpay' value='" . $row['tentpay'] . "' onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=4 id='hudpay' name='hudpay' value='" . $row['hudpay'] . "'
onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=10 id='datepaid' name='datepaid' value='" . $row['datepaid'] . "'></td>
<td><input type='text' size=1 name='late' value='" . $row['late'] . "'
onkeyup='calculate_paid(this)'></td>
<td><input type='text' size=25 name='comments' value='" . $row['comments'] . "'></td>
</tr>";
}
echo "</table>
<input type='submit' name='update' value='Make Payment' />
</form>";
}
else{echo "No listing for apartment $apt.<br />Please select another.<br />";}
}
if(!empty($_POST["update"]))
{
$sql = "UPDATE payments SET
dep = '" . mysql_real_escape_string($_POST['dep']) . "',
name = '" . mysql_real_escape_string($_POST['name']) . "',
amtpaid = '" . mysql_real_escape_string($_POST['amtpaid']) . "',
rentdue = '" . mysql_real_escape_string($_POST['rentdue']) . "',
prevbal = '" . mysql_real_escape_string($_POST['prevbal']) . "',
misc = '" . mysql_real_escape_string($_POST['misc']) . "',
tentpay = '" . mysql_real_escape_string($_POST['tentpay']) . "',
hudpay = '" . mysql_real_escape_string($_POST['hudpay']) . "',
datepaid = '" . mysql_real_escape_string($_POST['datepaid']) . "',
late = '" . mysql_real_escape_string($_POST['late']) . "',
comments = '" . mysql_real_escape_string($_POST['comments']) . "'
WHERE apt='".$_POST["apt"]."'";
mysql_query($sql) or die("Update query failed.");
echo "Record for apartment ".$_POST["apt"]." has been updated";
}
?>
<form method="post" action="#">
<br />
<input type="text" name="apt"/> <p>
<input type="submit" name="submit" value="select apartment"/>
</form>

<script type="text/javascript"><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//-->
</script>

<script type="text/javascript"><!--
try {
var pageTracker = _gat._getTracker("UA-256751-2");
pageTracker._trackPageview();
} catch(err) {}
//-->
</script>

<script type="text/javascript"><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//-->
</script>

<script type="text/javascript"><!--
try {
var pageTracker = _gat._getTracker("UA-256751-2");
pageTracker._trackPageview();
} catch(err) {}
//-->
</script>
</body></html>

with the below code, when I enter the first digit of the amtpaid, the date & the “L” are inserted as well as the correct (at that time) prevbal & misc. When I enter the second digit it goes to hell.


function calculate_paid()
{
var rentdue = document.getElementById("rentdue");
var prevbal = document.getElementById("prevbal");
var misc = document.getElementById("misc");
var amtpaid = document.getElementById("amtpaid");
var tentpay = document.getElementById("tentpay");
var hudpay = document.getElementById("hudpay");
 var datepaid = document.getElementById("datepaid");
 var late = document.getElementById("late");
 var dateNow = new Date();
 var dayNow = dateNow.getDate();
 var datePaid = (dateNow.getMonth()+1)+"/"+dateNow.getDate()+"/"+dateNow.getFullYear();
 var total = parseInt(rentdue.value) + parseInt(prevbal.value) + parseInt(misc.value) - parseInt(hudpay.value); 
 datepaid.value = datePaid; 
 tentpay.value = amtpaid.value - hudpay.value;
 prevbal.value = total - amtpaid.value; 
 if(amtpaid.value >= misc.value)
{
misc.value = 0;
}
  if(dayNow > 5)
{
   late.value = "L";