• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1047
  • Last Modified:

Javascript function autosum

Hi,

can anyone help me with a problem i am currently having. i recently posted a question about learning objects and if it was possible to auto update sums. i found this was best with Javascript, and it works, however i have run into another problem.

I have an example at http://eu.knowsleycollege.ac.uk/test/balancesheet.htm which is probably best you look at to help me with this question.

at the moment, i have 2 sets of sums happening, total assets and total liabilities, with this piece of code working the sums.

function autosum_totalassets()
     {
          var stock= parseFloat(document.autosumform.stock.value);
          if(isNaN(stock)) stock= 0;
         
          var debtors= parseFloat(document.autosumform.debtors.value);
          if(isNaN(debtors)) debtors= 0;

          var cash= parseFloat(document.autosumform.cash.value);
          if(isNaN(cash)) cash= 0;
         
          document.autosumform.totalassets.value = stock+ debtors+ cash;
     }

Now what i need, is to be able to add the 2 totals together with another field to make another total. for instance:

Current Assets - Current liabilities = Net Current Assets.

This is one major equation for me to be able to move on, as after this i will need the following,

Fixed Assets + Net assets - Long term Liabilities = total

But in this equation, i need Fixed assets to be a number that people can enter, same with long term liabilities but net current assets will have already been worked out. i have tried this in a few different ways based on the code example above, but i have had no joy.

IS THIS POSSIBLE? i would appreciate any help you can give me!

Cheers.

Rieto

ps i currently only have 155 points left, but will be purchasing more. so i will add points as soon as i have got them
0
Rieto
Asked:
Rieto
2 Solutions
 
sajuksCommented:
// 'vnt put validn in this, but is it something like this taht u want
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<script>
<!--
     function autosum_liabilities()
     {
          var creditors  = parseFloat(document.autosumform.creditors.value);
          if(isNaN(creditors)) creditors = 0;
         
          var dividends = parseFloat(document.autosumform.dividends.value);
          if(isNaN(dividends)) dividends = 0;

          var tax  = parseFloat(document.autosumform.tax.value);
          if(isNaN(tax)) tax = 0;
         
          document.autosumform.totalliabilities.value = creditors + dividends + tax;
                  document.autosumform.netcurrentassets.value =
                  document.autosumform.totalassets.value -
                  document.autosumform.totalliabilities.value
             
     }

     function autosum_totalassets()
     {
          var stock= parseFloat(document.autosumform.stock.value);
          if(isNaN(stock)) stock= 0;
         
          var debtors= parseFloat(document.autosumform.debtors.value);
          if(isNaN(debtors)) debtors= 0;

          var cash= parseFloat(document.autosumform.cash.value);
          if(isNaN(cash)) cash= 0;
         
          document.autosumform.totalassets.value = stock+ debtors+ cash;
              document.autosumform.netcurrentassets.value =
                  document.autosumform.totalassets.value -
                  document.autosumform.totalliabilities.value
     }

     function all_total()
     {
          var fixedassets  = parseFloat(document.autosumform.fixedassets.value)
          if(isNaN(fixedassets)) fixedassets = 0;
         
          var ltermliabilities = parseFloat(document.autosumform.ltermliabilities.value)
          if(isNaN(ltermliabilities)) ltermliabilities = 0;
                  
                  var netcurrentassets = parseFloat(document.autosumform.netcurrentassets.value)
                  if(isNaN(netcurrentassets)) netcurrentassets = 0;
                  document.autosumform.alltotal.value =
                   (fixedassets+netcurrentassets )-ltermliabilities

             
     }
-->
</script>

<form name="autosumform" action=""><b>
  <p>Current <b>Assets<b></b></b></p>

  <table width="75%" border="0">
    <tr>
      <td>Stock:</td>
      <td><input type="text" name="stock" onchange="autosum_totalassets()"></td>
    </tr>
    <tr>
      <td>Debtors: </td>
      <td><input type="text" name="debtors" onchange="autosum_totalassets()"></td>

    </tr>
    <tr>
      <td>Cash:</td>
      <td><input type="text" name="cash" onchange="autosum_totalassets()"></td>
    </tr>
    <tr>
      <td>Total Assets: </td>
      <td><input type="text" name="totalassets"></td>

    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
  <p>Current Liabilities<b></p>
  <table width="75%" border="0">

    <tr>
      <td>Creditors:</td>
      <td><input type="text" name="creditors" onchange="autosum_liabilities()"></td>
    </tr>
    <tr>
      <td>Dividends: </td>
      <td><input type="text" name="dividends" onchange="autosum_liabilities()"></td>
    </tr>

    <tr>
      <td>Tax: </td>
      <td><input type="text" name="tax" onchange="autosum_liabilities();"></td>
    </tr>
    <tr>
      <td>Total Liabilities: </td>
      <td><input type="text" name="totalliabilities"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Net Current Assets </td>
      <td><input type="text" name="netcurrentassets" readonly></td>
    </tr>
<tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Enter Fixed Assets </td>
      <td><input type="text" name="fixedassets" onchange="all_total()"></td>
    </tr>
    <tr>
      <td>Enter Long Term Liabilities </td>
      <td><input type="text" name="ltermliabilities" onchange="all_total()"></td>
    </tr>
          <tr>
      <td>Total</td>
      <td><input type="text" name="alltotal"></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p> <br>
    <br>
  <p>&nbsp;
  <p>
<p> <br>
    <br>
  <p>&nbsp;
  <p>

</form>

</body>
</html>
0
 
surajgupthaCommented:
Part I

Current Assets - Current liabilities = Net Current Assets.

If u want to display the "Net Current Assets" in a new textbox.

function AutoSum_NetCurrentAssets()
     {
          var assets= parseFloat(document.autosumform.totalassets.value );
          if(isNaN(assets)) assets= 0;
         
          var liaibilities= parseFloat(document.autosumform.debtors.value);
          if(isNaN(liaibilities)) liaibilities= 0;

         document.autosumform.netcurrentassests.value = assets + liabilities;
     }


Where "netcurrentassests" is the new textbox that u will have to add to display the "Net Current Assets"
0
 
eyeh8uCommented:
OK so you have document.autosumform.totalassets and document.autosumform.totalliabilities containing your assets and liabilites.

So you can add another input box for fixed assets and do the same as you do for your other forms to produce:


function autosum_total()
{
  var totalassets = parseFloat(document.autosumform.totalassets.value);
  if(isNaN(totalassets)) totalassets = 0;
  var totalliabilities = parseFloat(document.autosumform.totalliabilities.value);
  if(isNaN(totalliabilities)) totalliabilities = 0;
   var totalfixed = parseFloat(document.autosumform.totalfixed.value);
  if(isNaN(totalfixed)) totalfixed = 0;
   var totallt = parseFloat(document.autosumform.totallt.value);
  if(isNaN(totallt)) totallt = 0;

document.autosumform.total.value = totalfixed + (totalassets - totalliabilities) - totallt
}

This assumes you've added an input called total for your final total, and an input called totallt for your long term liabilites and a field called totalfixed for your fixed assets.

You could even add an input to display the net assets and add the READONLY attribute to it if you wanted to display that value.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
surajgupthaCommented:
Part II

Fixed Assets + Net assets - Long term Liabilities = total

Since u want people to enter a value for Fixed Assests and  Long term Liabilities u will need to make a textboxes
Lets call it "txtFixedAssets" and "txtLongTermLiabilites"

Since we need to display the total we have to create another textbox called "txtTotal"

function AutoSum_Total()
     {
          var fixedAssets= parseFloat(document.autosumform.txtFixedAssets.value );
          if(isNaN(fixedAssets)) fixedAssets= 0;
         
          var longTermLiaibilities= parseFloat(document.autosumform.txtLongTermLiabilites.value);
          if(isNaN(longTermLiaibilities)) longTermLiaibilities= 0;

         var netAssets = parseFloat(document.all.netcurrentassests.value);
          if(isNaN(netAssets )) netAssets = 0;

         document.autosumform.txtTotal.value = fixedAssets + netAssets - longTermLiaibilities;
     }

Please let me know if i have addressed your requirement
Would you need the HTML for the same?
0
 
sajuksCommented:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<script>
<!--
     function autosum_liabilities()
     {
          var creditors  = parseFloat(document.autosumform.creditors.value);
          if(isNaN(creditors)) creditors = 0;
         
          var dividends = parseFloat(document.autosumform.dividends.value);
          if(isNaN(dividends)) dividends = 0;

          var tax  = parseFloat(document.autosumform.tax.value);
          if(isNaN(tax)) tax = 0;
         
          document.autosumform.totalliabilities.value = creditors + dividends + tax;
                  document.autosumform.netcurrentassets.value =
                  document.autosumform.totalassets.value -
                  document.autosumform.totalliabilities.value
             
     }

     function autosum_totalassets()
     {
          var stock= parseFloat(document.autosumform.stock.value);
          if(isNaN(stock)) stock= 0;
         
          var debtors= parseFloat(document.autosumform.debtors.value);
          if(isNaN(debtors)) debtors= 0;

          var cash= parseFloat(document.autosumform.cash.value);
          if(isNaN(cash)) cash= 0;
         
          document.autosumform.totalassets.value = stock+ debtors+ cash;
              document.autosumform.netcurrentassets.value =
                  document.autosumform.totalassets.value -
                  document.autosumform.totalliabilities.value
     }

     function all_total()
     {
          var fixedassets  = parseFloat(document.autosumform.fixedassets.value)
          if(isNaN(fixedassets)) fixedassets = 0;
         
          var ltermliabilities = parseFloat(document.autosumform.ltermliabilities.value)
          if(isNaN(ltermliabilities)) ltermliabilities = 0;
                  
                  var netcurrentassets = parseFloat(document.autosumform.netcurrentassets.value)
                  if(isNaN(netcurrentassets)) netcurrentassets = 0;
                  document.autosumform.alltotal.value =
                   (fixedassets+netcurrentassets )-ltermliabilities

             
     }
-->
</script>

<form name="autosumform" action=""><b>
  <p>Current Assets</b></p>
  <table width="75%" border="0">
    <tr>
      <td width="245">Stock:</td>
      <td><input type="text" name="stock" onchange="autosum_totalassets()"></td>
    </tr>
    <tr>
      <td width="245">Debtors: </td>
      <td><input type="text" name="debtors" onchange="autosum_totalassets()"></td>
    </tr>
    <tr>
      <td width="245">Cash:</td>
      <td><input type="text" name="cash" onchange="autosum_totalassets()"></td>
    </tr>
    <tr>
      <td width="245"><b>Total Assets:</b> </td>
      <td><input type="text" name="totalassets"></td>
    </tr>
  </table>
 
  <p><b>Current Liabilities</b></p>
  <table width="75%" border="0">
    <tr>
      <td width="245">Creditors:</td>
      <td><input type="text" name="creditors" onchange="autosum_liabilities()"></td>
    </tr>
    <tr>
      <td width="245">Dividends: </td>
      <td><input type="text" name="dividends" onchange="autosum_liabilities()"></td>
    </tr>

    <tr>
      <td width="245">Tax: </td>
      <td><input type="text" name="tax" onchange="autosum_liabilities();"></td>
    </tr>
    <tr>
      <td width="245"><b>Total Liabilities: </b> </td>
      <td><input type="text" name="totalliabilities"></td>
    </tr>
    <tr>
      <td width="245">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="245"><b>Net Current Assets:</b></td>
      <td><input type="text" name="netcurrentassets" readonly></td>
    </tr>
<tr>
      <td width="245">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td width="245">Enter Fixed Assets </td>
      <td><input type="text" name="fixedassets" onchange="all_total()"></td>
    </tr>
    <tr>
      <td width="245">Enter Long Term Liabilities </td>
      <td><input type="text" name="ltermliabilities" onchange="all_total()"></td>
    </tr>
          <tr>
      <td width="245"><b>Total:</b></td>
      <td><input type="text" name="alltotal"></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <p> <br>
    <br>
  <p>&nbsp;
  <p>
<p> <br>
    <br>
  <p>&nbsp;
  <p>

</form>

</body>
</html>
0
 
RietoAuthor Commented:
surajguptha + sajuks

Thanks for all your help, in showing and giving me the theory behind. Its much appreciated!

Rieto
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now