Solved

calculate the percentage for each row

Posted on 2007-11-21
16
504 Views
Last Modified: 2012-06-27
Hello Everyone, I need a javaScript function that will calculate the percentage for each percentage column which is at the right of the amount column... so that when the percentages are added up they equal 100%

For example

____________________________        ______________
|_________200________________|       |_______20____| %


____________________________        ______________
|_________800________________|       |_______80____| %

TOTAL:  
____________________________           ______________
|_________1000_______________|       |_______100____| %

The formula is the TOTAL divided by the column next to the percentage box and multiplied by 100

So 200/1000 * 100

And that will give us the percentage for that box

If the entry is 0 the percentage will be 0

Can you help me code it at least for one row, than I can use that code to take it from there, or at least let me know if my logic makes sense, and it will give me the percentage automatically becasue the percentage textboxes are readonly the user cannot enter anything in them...




<table><tr  colspan="2">
            <td align="center"><strong>Cost Category</strong></td>
            <td align="center"><strong>US$</strong></td>
            <td align="center"><strong>%</strong></td>
          </tr>
          <tr>
            <td><input type = "text" name = "Expenses" id = "Expenses"  size = "50"  value = "<%=sExpenses%>"  /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "POEMoney" id = "POEMoney" maxlength = "9"  size = "11"  class="money" onblur="this.value=formatCurrency(this.value); CalculateMoney();"  value = "<%=sPOEMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text"  name = "POEPercentage" id = "POEPercentage" readonly  maxlength = "3" size = "3"  onblur="CalculatePercentage();" value = "<%=sPOEPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "Land"  id = "Land" value = "<%=sLand%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "LandMoney" id = "LandMoney" maxlength = "9"  size = "11"  class="money" onblur="this.value=formatCurrency(this.value); CalculateMoney();"  value = "<%=sLandMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "LandPercentage" id = "LandPercentage" readonly maxlength = "3" size = "3"  onblur="CalculatePercentage();" value = "<%=sLandPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><strong>Total </strong></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" readonly style = "background: #CCCCCC" name = "TotalMoney" id = "TotalMoney" maxlength = "11" size = "14" class="money" onblur="this.value=formatCurrency(this.value);"  value = "<%=sTotalMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" readonly style = "background: #CCCCCC" name = "TotalPercentage" id = "TotalPercentage"  maxlength = "4" size = "5" value = "<%=sTotalPercentage%>" />
              </span></td>
          </tr>      </table>
0
Comment
Question by:Gemini532
  • 11
  • 5
16 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 20346371
Does this help?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title> Percentage </title>

<script type='text/javascript'>

  function CurToNum( val ) {

    val = val.replace(/\$|\,/g,'');

    return parseFloat( val );

  }

  

  function formatCurrency( num ) {

    num = num.toString().replace(/\$|\,/g,'');

    if( isNaN( num ) )

      num = "0";

    sign = ( num == ( num = Math.abs( num ) ) );

    num = Math.floor( num * 100 + 0.50000000001 );

    cents = num % 100;

    num = Math.floor( num / 100 ).toString();

    if ( cents < 10 )

      cents = "0" + cents;

    for ( var i = 0; i < Math.floor( ( num.length - ( 1 + i ) ) / 3 ); i++ )

      num = num.substring( 0, num.length -( 4 * i + 3 ) ) + ',' +

        num.substring( num.length - ( 4 * i + 3 ) );

    return ( ( ( sign ) ? '' : '-' ) + '$' + num + '.' + cents );

  }
 

  function field( id ) {

    var obj = document.getElementById( id );

    if ( obj == null ) {

      alert( 'Specified field with id "' + id + '" not found.' );

    }

    return obj;

  }

  

  function Compute( dataName, percentName, totalId, percentId ) {

    var data    = document.getElementsByName( dataName );

    var percent = document.getElementsByName( percentName );

    if ( data.length != percent.length ) {

      alert( 'Data/percentage fields have different number of instances.' );

    } else {

      var totField = field( totalId );

      var total = 0.0;

      var count = 0;

      for ( var i = 0; i < data.length; i++ ) {

        var val = data[ i ].value;

        if ( val != '' ) {

          total += CurToNum( val );

          count++;

        }

      }

      totField.value = formatCurrency( total );

      for ( var i = 0; i < percent.length; i++ ) {

        var val = data[ i ].value;

        if ( val != '' ) {

          var per = ( CurToNum( val ) * 100.0 / total );

          percent[ i ].value = per.toFixed( 2 );

        }

      }

      field( percentId ).value = '100.00';

    }

  }

</script>

<style type='text/css'>

  .right {

    text-align : right;

  }

</style>

</head>

<body>

  <table>

    <tr  colspan='2'>

      <td align='center'><strong>Cost Category</strong></td>

      <td align='center'><strong>US$</strong></td>

      <td align='center'><strong>%</strong></td>

    </tr>

    <tr>

      <td>

        <input type='text'   id='Expenses'  size='50'  value=''  />

      </td>

      <td align='right'>

        <span id='rInputs'>

          <input type='text' name='data' id='POEMoney' size='9'  class='money right' onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value='' />

        </span>

      </td>

      <td align='right'>

        <span id='rInputs'>

          <input type='text' class='right' name='percent' id='POEPercentage' readonly  size='6' value='' />

        </span>

      </td>

    </tr>

    <tr>

      <td>

        <input type='text'   id='Land' value='' size='50' />

      </td>

      <td align='right'>

        <span id='rInputs'>

          <input type='text' name='data' id='LandMoney' size='9'  class='money right' onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value='' />

        </span>

      </td>

      <td align='right'>

        <span id='rInputs'>

          <input type='text' class='right' name='percent' id='LandPercentage' readonly size='6' value='' />

        </span>

      </td>

    </tr>

    <tr>

      <td><strong>Total </strong></td>

      <td align='right'>

        <span id='rInputs'>

          <input type='text' readonly style='background: #CCCCCC' id='TotalMoney' maxlength='11' size='14' class='money right' value='' />

        </span>

      </td>

      <td align='right'>

        <span id='rInputs'>

          <input type='text' class='right' readonly style='background: #CCCCCC' id='TotalPercentage'  size='6' value='' />

        </span>

      </td>

    </tr>

  </table>

</body>

</html>

Open in new window

0
 

Author Comment

by:Gemini532
ID: 20351087
the code is perfect that for the amoutn I cannot have any decimal points, can you help me with that?
angie
0
 

Author Comment

by:Gemini532
ID: 20351097
also why did you remove my value from what it was to value = ''

I need it to be the value that I have saved from the database so that when the uses leave the page but come back they can see the numbers that they entered...

Can I put the value back in or will that destroy the code?
0
 

Author Comment

by:Gemini532
ID: 20351268
I cannot understand how to fix the code, I need the value to remain and I really cannot have decimal points...
Can you help me... It seems the code you gave me does not work with my own code but I don' t know i replace my code with yours... I'll give it a try but I REALLY NEED YOU to help me eliminate the decimal points, because I'm not sure what puts them there...

Also the values, I don't understand why they have to be blank...
0
 

Author Comment

by:Gemini532
ID: 20351330
You changed the name of every field to data, I cannot have that, the name CANNOT Be changed!!!!
I'm sorry I cannot use your code

The way my application is programed the name on the page has to match the name in the database and then the update is done in one line because of that... each page has its own table in the database... THE NAME cannot be changed... Can this be done in another way, without chianging the name... I was wandering why it was just wasn't workikng, but now I know why

What if we change the id, can that work?
0
 

Author Comment

by:Gemini532
ID: 20351562
I changed the code to use the ID rather than the name, but it does not add up the money as total, it still has decimal points and it does not do the percentage... can you help me?
I know you understand this code a lot better than me...


<script>
  function CurToNum( val ) {
    val = val.replace(/\$|\,/g,'');
    return parseFloat( val );
  }
 
 function formatCurrency( num ) {
    num = num.toString().replace(/\$|\,/g,'');
    if( isNaN( num ) )
      num = "0";
    sign = ( num == ( num = Math.abs( num ) ) );
    num = Math.floor( num * 100 + 0.50000000001 );
    cents = num % 100;
    num = Math.floor( num / 100 ).toString();
    if ( cents < 10 )
      cents = "0" + cents;
    for ( var i = 0; i < Math.floor( ( num.length - ( 1 + i ) ) / 3 ); i++ )
      num = num.substring( 0, num.length -( 4 * i + 3 ) ) + ',' +
        num.substring( num.length - ( 4 * i + 3 ) );
    return ( ( ( sign ) ? '' : '-' ) + num  );
  }

  function field( id ) {
    var obj = document.getElementById( id );

      
    if ( obj == null ) {
      alert( 'Specified field with id "' + id + '" not found.' );
    }
      
    return obj;
  }
 
  function Compute( dataName, percentName, totalId, percentId ) {
    var amount    = document.getElementById(dataName);
    var percentage = document.getElementById(percentName);

      var data = document.getElementById(dataName).value;
      var percent = document.getElementById(percentName).value;
      
    if (amount.length != percentage.length ) {
      alert( 'Data/percentage fields have different number of instances.' );
    } else {
      var totField = field( totalId ).value;
        //alert(totField);  //gives me the total which is zero right now, why is that?
     
        var total = 0;
      var count = 0;
      alert(amount.length);  //<---- THe ERROR IS HERE!
        
        for ( var i = 0; i < amount.length; i++ ) {
        var val = amount[ i ].value;
            alert(val);
        if ( val != '' ) {
          total += CurToNum( val );
          count++;
        }
      }
        
      totField.value = formatCurrency( total );
        
      for ( var i = 0; i < percentage.length; i++ ) {
        var val = amount[ i ].value;
        if ( val != '' ) {
          var per = ( CurToNum( val ) * 100 / total );
          percentage[ i ].value = per.toFixed( 2 );
        }
      }
      field( percentId ).value = '100';
    }
  }
</script>  


        <tr>
            <td><input type = "text" name = "Expenses" id = "Expenses"  size = "50"  value = "<%=sExpenses%>"  /></td>
            <td align = "right">
                  <span id="rInputs">
              <input type='text' name='POEMoney' id='data' maxlength = "9"  size = "11"  class='money' onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sPOEMoney%>"  />
            </span></td>
                  <td align = "right"><span id="rInputs">
              <input type = "text"  name = "POEPercentage" id = "percent" readonly  maxlength = "3" size = "3"  value = "<%=sPOEPercentage%>" />
            </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "Land"  id = "Land" value = "<%=sLand%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "LandMoney" id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sLandMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "LandPercentage" id = "percent" readonly maxlength = "3" size = "3"  value=''  />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "BI" id = "BI" value = "<%=sBI%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "BIMoney"  id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sBIMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "BIPercentage" id = "percent"  readonly maxlength = "3" size = "3"  value=''  />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "Equipment" id = "Equipment"  value = "<%=sEquipment%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "EquipmentMoney" id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sEquipmentMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "EquipmentPercentage" id = "percent" readonly maxlength = "3" size = "3"  value = "<%=sEquipmentPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "WC" id = "WC"  value = "<%=sWC%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "WCMoney" id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sWCMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "WCPercentage" id = "percent" readonly maxlength = "3" size = "3"  value = "<%=sWCPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "Contingency" id = "Contingency" value = "<%=sContingency%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "ContingencyMoney" id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sContingencyMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "ContingencyPercentage" id = "percent" readonly maxlength = "3" size = "3"  value = "<%=sContingencyPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "GDF"  id = "GDF" value = "<%=sGDF%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "GDFMoney" id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");' value = "<%=sGDFMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "GDFPercentage" id = "percent" readonly maxlength = "3" size = "3"  value = "<%=sGDFPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "DSR" id = "DSR" value = "<%=sDSR%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "DSRMoney" id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sDSRMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "DSRPercentage" id = "percent" readonly maxlength = "3" size = "3"  value = "<%=sDSRPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "FLF" id = "FLF" value = "<%=sFLF%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "FLFMoney" id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sFLFMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "FLFPercentage" id = "percent" readonly maxlength = "3" size = "3"  value = "<%=sFLFPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><input type = "text" name = "IDS"  value = "<%=sIDS%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "IDSMoney" id = "data"  maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sIDSMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "IDSPercentage" id = "percent"  readonly maxlength = "3" size = "3"  value = "<%=sIDSPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td height="31"> <input type = "text" name = "Other"  value = "<%=sOther%>" size = "50" /></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "OtherMoney" id = "data" maxlength = "9"  size = "11"  class="money" onblur='this.value=formatCurrency(this.value); Compute("data","percent","TotalMoney","TotalPercentage");'  value = "<%=sOtherMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" name = "OtherPercentage" id = "percent"  readonly maxlength = "3" size = "3"  value = "<%=sOtherPercentage%>" />
              </span></td>
          </tr>
          <tr>
            <td><strong>Total Project Costs:<br>
              </strong>Total amount must be
              greater than $0 <br>
              Total Percentage
              cannot be greater than 100%.</td>
            <td align = "right"><span id="rInputs">
              <input type = "text" readonly style = "background: #CCCCCC" name = "TotalMoney" id = "TotalMoney" maxlength = "11" size = "14" class="money" onblur="this.value=formatCurrency(this.value);"  value = "<%=sTotalMoney%>" />
              </span></td>
            <td align = "right"><span id="rInputs">
              <input type = "text" readonly style = "background: #CCCCCC" name = "TotalPercentage" id = "TotalPercentage"  maxlength = "4" size = "5" value = "<%=sTotalPercentage%>" />
              </span></td>
          </tr>
0
 

Author Comment

by:Gemini532
ID: 20351568
I'm sorry I should have told you that the NAME can never be changed, the whole foundation of this application depends on the name being the same in the application as in the database...
So now i need to work with the ID, I have tried to make changes, but I cannot seem to get it to work...

Can you help me?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 20355492
Sorry for not seeing your updates until this moment.

1.  The amount can't have any decimal places
     You might consider using value.toFixed( 0 ), instead of the valuetoFixed( 2 ) shown.

2. Why did I remove "my values"?
    I don't have PHP, and didn't want to install/learn it... :-)

3. Can you put the value back in?
    Sure.

4. You changed the name attributes ...
    Well, that was so the function could easily locate all of the associated "input" fields
    as well as the associated percentage output fields.

5. "The way my application is programed the name on the page has to match the name in the database and then the update is done in one line because of that... each page has its own table in the database... THE NAME cannot be changed... Can this be done in another way, without chianging the name... I was wandering why it was just wasn't workikng, but now I know why

What if we change the id, can that work?

    With a (little) effort, yes.

    I wasn't expecting you to be using the name attributes in the way you describe.

Let me take a look....
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 20355753
First, I had to make the id values unique.  This is a requirement.

I also validated the HTML, and made the changes necessary to allow the code to pass this validation without any errors, or warnings.

Please take a look at this code, and see if it satisfies your needs/requirements.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title> Percentage </title>
 

<style type='text/css'>

  .right {

    text-align : right;

  }

</style>
 

<script type='text/javascript'>

  //------------------------------------------------------------------

  // Name: CurToNum()

  // Role: Convert specified currency value to the equivalent numeric

  //       value.

  //------------------------------------------------------------------

  function CurToNum( val ) {

    val = val.replace(/\$|\,/g,'');

    return parseFloat( val );

  }

 

  //------------------------------------------------------------------

  // Name: formatCurrency()

  // Role: Convert specified numeric value to the equivalent currency

  //       value.

  //------------------------------------------------------------------

  function formatCurrency( num ) {

    num = num.toString().replace(/\$|\,/g,'');

    if( isNaN( num ) )

      num = "0";

    sign = ( num == ( num = Math.abs( num ) ) );

    num = Math.floor( num * 100 + 0.50000000001 );

    cents = num % 100;

    num = Math.floor( num / 100 ).toString();

    if ( cents < 10 )

      cents = "0" + cents;

    for ( var i = 0; i < Math.floor( ( num.length - ( 1 + i ) ) / 3 ); i++ )

      num = num.substring( 0, num.length -( 4 * i + 3 ) ) + ',' +

        num.substring( num.length - ( 4 * i + 3 ) );

    return ( ( ( sign ) ? '' : '-' ) + num  );

  }
 

  //------------------------------------------------------------------

  // Name: field()

  // Role: Locate the DOM element having the specified id attribute.

  //------------------------------------------------------------------

  function field( id ) {

    var obj = document.getElementById( id );
 

    if ( obj == null ) {

      alert( 'Specified field with id "' + id + '" not found.' );

    }

    return obj;

  }

 

  //------------------------------------------------------------------

  // Global arrays of ids of the data, and associated percentage

  // fields, and the ids of the overall total output field, as well as

  // the totalPercentage output field.

  //------------------------------------------------------------------

  var dataIds = 'LandMoney,BIMoney,EquipmentMoney,WCMoney,POEMoney,GDFMoney,DSRMoney,FLFMoney,IDSMoney,OtherMoney'.split( ',' );

  var perIds  = 'LandPercentage,BIPercentage,EquipmentPercentage,WCPercentage,ContingencyPercentage,GDFPercentage,DSRPercentage,FLFPercentage,IDSPercentage,OtherPercentage'.split( ',' );

  var totalId = 'TotalMoney';

  var totalPercentId = 'TotalPercentage';

  

  //------------------------------------------------------------------

  // Name: Compute()

  // Role: Calcuate the total of the currency fields, then display the

  //       associated percentage values

  // Note: document.getElementById() is used to locate the single DOM

  //       (Document Object Model) element that has the specified

  //       (unique) id attribute specified.

  //------------------------------------------------------------------

  function Compute() {
 

    if ( dataIds.length != perIds.length ) {

      alert( 'Data/percentage fields have different number of elements.' );

    } else {

      var totField = field( totalId );

      

      // gives me the total which is zero right now, why is that?

      // Because we haven't yet assigned the value.

      //alert(totField.value);

     

      var total = 0;

      var count = 0;
 

      //--------------------------------------------------------------

      // Since you are using unique name attributes for the "data"

      // fields, we are using a list of ids to identify each and every

      // data field to be processed.

      // Each element of dataIds is the id attribute of a single data

      // element.

      //--------------------------------------------------------------

      for ( var i = 0; i < dataIds.length; i++ ) {

        var val = field( dataIds[ i ] ).value;

        alert( 'i: ' + i + ' = "' + dataIds[ i ] + '" => ' + val );

        //------------------------------------------------------------

        // If this field has data, convert the content to its

        // equivalent numeric value, and add this value to the total.

        //------------------------------------------------------------

        if ( val != '' ) {

          total += CurToNum( val );

          count++;

        }

      }

      //--------------------------------------------------------------

      // Assign the "overall total" using the computed sum.

      //--------------------------------------------------------------

      totField.value = formatCurrency( total );

        

      //--------------------------------------------------------------

      // Now, compute each of the associated percentage values

      //--------------------------------------------------------------

      for ( i = 0; i < perIds.length; i++ ) {

        var val = field( dataIds[ i ] ).value;

        //------------------------------------------------------------

        // If this field has data, compute the percentage of the total

        // it represents, and display this in the associated percent

        // field.

        //------------------------------------------------------------

        // Note: If you show too few decimal places for these % output

        //       fields, the sum of the percentage output fields may

        //       not add up to 100%

        //------------------------------------------------------------

        if ( val != '' ) {

          var per = ( CurToNum( val ) * 100 / total );

          field( perIds[ i ] ).value = per.toFixed( 2 );

        }

      }

      field( totalPercentId ).value = '100';

    }

  }

</script>  

</head>

<body>

  <table>

    <tr>

      <td align='center'><strong>Cost Category</strong></td>

      <td align='center'><strong>US$</strong></td>

      <td align='center'><strong>%</strong></td>

    </tr>
 

    <tr>

      <td>

        <input type='text' name='Expenses' id='Expenses'  size='50'  value='<%=sExpenses%>'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='POEMoney' id='POEMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sPOEMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text'  name='POEPercentage' id='POEPercentage' readonly  maxlength='3' size='3'  value='<%=sPOEPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='Land'  id='Land' value='<%=sLand%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='LandMoney' id='LandMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sLandMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='LandPercentage' id='LandPercentage' readonly maxlength='3' size='3'  value=''>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='BI' id='BI' value='<%=sBI%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='BIMoney'  id='BIMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sBIMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='BIPercentage' id='BIPercentage'  readonly maxlength='3' size='3'  value=''>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='Equipment' id='Equipment'  value='<%=sEquipment%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='EquipmentMoney' id='EquipmentMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sEquipmentMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='EquipmentPercentage' id='EquipmentPercentage' readonly maxlength='3' size='3'  value='<%=sEquipmentPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='WC' id='WC'  value='<%=sWC%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='WCMoney' id='WCMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sWCMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='WCPercentage' id='WCPercentage' readonly maxlength='3' size='3'  value='<%=sWCPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='Contingency' id='Contingency' value='<%=sContingency%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='ContingencyMoney' id='ContingencyMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sContingencyMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='ContingencyPercentage' id='ContingencyPercentage' readonly maxlength='3' size='3'  value='<%=sContingencyPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='GDF'  id='GDF' value='<%=sGDF%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='GDFMoney' id='GDFMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();' value='<%=sGDFMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='GDFPercentage' id='GDFPercentage' readonly maxlength='3' size='3'  value='<%=sGDFPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='DSR' id='DSR' value='<%=sDSR%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='DSRMoney' id='DSRMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sDSRMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='DSRPercentage' id='DSRPercentage' readonly maxlength='3' size='3'  value='<%=sDSRPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='FLF' id='FLF' value='<%=sFLF%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='FLFMoney' id='FLFMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sFLFMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='FLFPercentage' id='FLFPercentage' readonly maxlength='3' size='3'  value='<%=sFLFPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='IDS'  value='<%=sIDS%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='IDSMoney' id='IDSMoney'  maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sIDSMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='IDSPercentage' id='IDSPercentage'  readonly maxlength='3' size='3'  value='<%=sIDSPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td>

        <input type='text' name='Other'  value='<%=sOther%>' size='50'>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='OtherMoney' id='OtherMoney' maxlength='9'  size='11'  class='money' onblur='this.value=formatCurrency(this.value); Compute();'  value='<%=sOtherMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' name='OtherPercentage' id='OtherPercentage'  readonly maxlength='3' size='3'  value='<%=sOtherPercentage%>'>

        </span>

      </td>

    </tr>

    

    <tr>

      <td><strong>Total Project Costs:<br>

        </strong>Total amount must be

        greater than $0 <br>

        Total Percentage

        cannot be greater than 100%.</td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' readonly style='background: #CCCCCC' name='TotalMoney' id='TotalMoney' maxlength='11' size='14' class='money' onblur='this.value=formatCurrency(this.value);'  value='<%=sTotalMoney%>'>

        </span>

      </td>

      <td align='right'>

        <span class='rInputs'>

          <input type='text' readonly style='background: #CCCCCC' name='TotalPercentage' id='TotalPercentage'  maxlength='4' size='5' value='<%=sTotalPercentage%>'>

        </span>

      </td>

    </tr>

  </table>

</body>

</html>

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 20355756
I re-read your comments.

Did you not want any decimal places at all?
or are you asking for no decimal places only in the money, or percent output fields?
0
 

Author Comment

by:Gemini532
ID: 20358709
no decimal places ANYWHERE!  Is that OK? can you help me with that?  Sorry , my boss is VERY ANTI decimal points if he sees any he'll get mad at me
0
 

Author Comment

by:Gemini532
ID: 20358802
can you tell me what sets the decimal points so I can remove that code?
0
 

Author Comment

by:Gemini532
ID: 20358906
SORRY MY BOSS WANTS THE PERCENTAGES NOW!!!  I CONVINCED HIM!! yeyyy!  :)
oH AND YOUR CODE WORKS  PERFECTLY!
THANK YOU HonorGod:
0
 

Author Comment

by:Gemini532
ID: 20359055
My boss changed his mind again, this code is perfect after you help me get rid of the decimal points..
Then I will give you all the points!!!

THANKS THOUGH!  IT REALLY IS BRILLIANT!  YOUR CODE!
0
 

Author Comment

by:Gemini532
ID: 20359078
field( perIds[ i ] ).value = per.toFixed( 0 );
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 20361019
Thanks for the points, and good luck with your boss.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now