Hi there,

I have coded up some script to make some simple calculations for a form that I require. In outputting the script to get the form to fill out certain data I am getting the message 'inifinaty', that is untill I select the submit button again where it performs the next calculation and again to perform the following and so on.

I need to have the code loop itself I guess? Therefore when I click the submit button all of the statements would run, therefore the entire outputs being filled in.

Could anyone help with how I should loop this statement?

JavaScript
Code:
<script language="JavaScript">
<!--
function showpay() {
 if ((document.calc.inpsurgperwk.value == null || document.calc.inpsurgperwk.value.length == 0) ||
     (document.calc.inpavgminpersurg.value == null || document.calc.inpavgminpersurg.value.length == 0) ||
     (document.calc.inpavgcostpersurg.value == null || document.calc.inpavgcostpersurg.value.length == 0) ||
     (document.calc.inpminsavpersurg.value == null || document.calc.inpminsavpersurg.value.length == 0) ||
     (document.calc.inpcostequip.value == null || document.calc.inpcostequip.value.length == 0))
	  
 { alert("Please enter all values");
 }
 else
 {
 var input1 = document.calc.inpsurgperwk.value;
 var input2 = document.calc.inpavgminpersurg.value;
 var input3 = document.calc.inpavgcostpersurg.value;
 var input4 = document.calc.inpminsavpersurg.value;
 var input5 = document.calc.inpcostequip.value;
 // results
 var output1 = document.calc.costsurg.value;
 var output2 = document.calc.savedpersurg.value;
 var output3 = document.calc.surguntinvestrecov.value;
  
 var output4 = document.calc.timewks.value;
 var output5 = document.calc.timemnths.value;
 var output6 = document.calc.timeyrs.value;
 // savings
 var output_a = document.calc.numsurgwkly.value;
 var output_b = document.calc.numsurgmnthly.value;
 var output_c = document.calc.numsurgannu.value;
 
 var output_d = document.calc.numcostwkly.value;
 var output_e = document.calc.numcostmnthly.value;
 var output_f = document.calc.numcostannu.value;
 
 var output_g = document.calc.minsavwkly.value;
 var output_h = document.calc.minsavmnthly.value;
 var output_i = document.calc.minsavannu.value;
 
 var output_j = document.calc.totalwkly.value;
 var output_k = document.calc.totalmnthly.value;
 var output_l = document.calc.totalannu.value;
 

// results
 document.calc.costsurg.value = input2 * input3;
 document.calc.savedpersurg.value = input3 * input4;
 document.calc.surguntinvestrecov.value = (input5/output2);
 
 document.calc.timewks.value = output3 / input1;
 document.calc.timemnths.value = output4 / 4.3;
 document.calc.timeyrs.value = output4 / 52;
 
 // savings
 document.calc.numsurgwkly.value = inputa;
 document.calc.numsurgmnthly.value = input1 * 4.3;
 document.calc.numsurgannu.value = 12 / output_b;
 
 document.calc.numcostwkly.value = output_a / output1;
 document.calc.numcostmnthly.value = output_b / output1;
 document.calc.numcostannu.value = output_c / output1;
 
 document.calc.minsavwkly.value = input4 * output_a;
 document.calc.minsavmnthly.value = output_g * 4.3;
 document.calc.minsavannu.value = 12 * output_h;
 
 document.calc.totalwkly.value = output_a * output2;
 document.calc.totalmnthly.value = output_b * output2;
 document.calc.totalannu.value = output_c * output2;
 return showpay
 }

}

// -->
</script>
HTML
Code:
<form name=calc method=POST>
<table class="caltable" width=85% border=0>
<tr>
  <th height="81" colspan="2">&nbsp;</th>
  <td colspan="3"><div align="left">Enter your data in the white fields to work out cost savings <br /> of a Bioptics Specimen Radiography system, or test different scenarios by varying the inputs.</div></td>
  </tr>
<tr><td height="42" colspan="2" align=left><h3>Costs</h3>
<th width=18%>&nbsp;</th>
  <th width=18%></th>
<th width=18%>&nbsp;</th>
</tr>

<tr height="20%"><td height="50" colspan="3" valign="top"><strong>Surgeries Per Week </strong><br>
     How many breast surgeries do you perform?</td>
  <td align=right valign="top"><input type=text name=inpsurgperwk size=13></td>
  <td align=right valign="top">&nbsp;</td>
</tr>
<tr>
  <td height="30" colspan="3" valign="top"><strong>Average Minutes Per Surgery </strong></td>
  <td
align=right valign="top"><input type=text name=inpavgminpersurg size=13></td>
  <td
align=right valign="top">&nbsp;</td>
</tr>
<tr>
  <td height="30" colspan="3" valign="top"><strong>Average Cost Per Surgery </strong></td>
  <td  align=right valign="top"><input name=inpavgcostpersurg type=text size=13></td>
  <td  align=right valign="top">&nbsp;</td>
</tr>
<tr>
  <td height="66" colspan="3" valign="top"><strong>Minutes Saved Per Surgery </strong> <br/ > 
    How many minutes can you save per surgery if you 
don't<br> have to send the specimen out for imaging?</td>
  <td 
align=right valign="top"> <input type=text name=inpminsavpersurg size=13></td>
  <td 
align=right valign="top">&nbsp;</td>
</tr>
<tr>
  <td height="48" colspan="3" valign="top"><strong>Cost of Digital Equipment</strong> <br/ >
  List price quoted by Bioptics sales representative.</td>
  <td 
  align=right valign="top"> <input name=inpcostequip type=text size=13></td>
  <td 
  align=right valign="top">&nbsp;</td>
</tr>
<tr>
  <td width="25%" height="35" align=center><input type="button" onClick='showpay()' value=Calculate></td>
  <td width="21%" height="35" align=center>&nbsp;</td>
  <td height="35" align=center><input name="reset" type=reset value=Reset></td>
  <td height="35" align=center valign="top"><img src="images/calc_btn_up.png" alt="" name="Pictures" width="100" height="33" align="top">
    </td>
  <td height="35" align=center valign="top"><img src="images/reset_btn_up.png" alt="" name="Pictures" width="100" height="33" align="top">
 </td>
</tr>
<tr>
  <td height="42" colspan="2" align=left><h3>Results</h3></td>
  <td align=right valign="top">&nbsp;</td>
  <td align=right valign="top">&nbsp;</td>
  <td align=right valign="top">&nbsp;</td>
</tr>
<tr>
  <td height="30" colspan="3" valign="top"><strong>Cost of Surgeries</strong> </td>
  <td align=right valign="top"> <input type=text name=costsurg size=13></td>
  <td align=right>&nbsp;</td>
</tr>
<tr>
  <td height="30" colspan="3" valign="top"><strong>Amount saved per surgery</strong> </td>
  <td align=right valign="top"><input type=text name=savedpersurg size=13></td>
  <td align=right>&nbsp;</td>
</tr>
<tr>
  <td height="30" colspan="3" valign="top"><strong>Surgeries Until Investment is recovered</strong> </td>
  <td align=right valign="top">
    <input type=text name=surguntinvestrecov size=13></td>
  <td align=right>&nbsp;</td>
</tr>
<tr>
  <td height="45" colspan="2" rowspan="2" valign="top"><strong>Time Until Investment is recovered</strong></td>
  <td align=right valign="top"><input type=text name=timewks size=13></td>
  <td align=right valign="top"><input type=text name=timemnths size=13></td>
  <td align=right valign="top"><input type=text name=timeyrs size=13></td>
</tr>
<tr>
  <td align=center valign="top">Weeks</td>
  <td align=center valign="top">Months</td>
  <td align=center valign="top">Years</td>
</tr>
<tr>
  <td height="42" colspan="2" align=left><h3>Savings</h3></td>
  <td align=right valign="top">&nbsp;</td>
  <td align=right valign="top">&nbsp;</td>
  <td align=right valign="top">&nbsp;</td>
</tr>
<tr>
  <td height="35" align=center>&nbsp;</td>
  <td height="35" align=center><strong>Total Number<br />of Surgeries</strong></td>
  <td height="35" align=center><strong>Total Cost<br />of Surgeries</strong></td>
  <td height="35" align=center><strong>Total Minutes<br />Saved</strong></td>
  <td height="35" align=center><strong>Total Amount<br />Saved</strong></td>
</tr>
<tr>
  <td width="25%" height="30" valign="top"><strong>Weekly</strong></td>
  <td align=right valign="top"><input type=text name=numsurgwkly size=13></td>
  <td align=right valign="top"><input type=text name=numcostwkly size=13></td>
  <td align=right valign="top"><input type=text name=minsavwkly size=13></td>
  <td align=right valign="top"><input type=text name=totalwkly size=13></td>
</tr>
<tr>
  <td width="25%" height="30" valign="top"><strong>Monthly</strong></td>
  <td align=right valign="top"><input type=text name=numsurgmnthly size=13></td>
  <td align=right valign="top"><input type=text name=numcostmnthly size=13></td>
  <td align=right valign="top"><input type=text name=minsavmnthly size=13></td>
  <td align=right valign="top"><input type=text name=totalmnthly size=13></td>
</tr>
<tr>
  <td width="25%" height="33" valign="top"><strong>Annually</strong></td>
  <td align=right valign="top"><input type=text name=numsurgannu size=13></td>
  <td align=right valign="top"><input type=text name=numcostannu size=13></td>
  <td align=right valign="top"><input type=text name=minsavannu size=13></td>
  <td align=right valign="top"><input type=text name=totalannu size=13></td>
</tr>
<tr>
  <td height="30" align=center>&nbsp;</td>
  <td align=right valign="top">&nbsp;</td>
  <td align=right valign="top">&nbsp;</td>
  <td align=right valign="top">&nbsp;</td>
  <td height="45" align=center valign="top"><img src="images/print_btn_up.png" alt="" name="Pictures" width="100" height="33" align="top"></td>
</tr>
</table>
</form>
As I said the first section is working fine, but only when I keep pressing on the calculate button to answer the next queries.

Many thanks for your help.

AR