Web Analytics Made Easy -
StatCounter adding up parts etc - CodingForum


No announcement yet.

adding up parts etc

  • Filter
  • Time
  • Show
Clear All
new posts

  • adding up parts etc

    ok this is what i have

    <script type="text/javascript">

    function calc(){
    // Gathers field information
    var f1 = validNum(document.productForm.elements['tp1'].value);
    var f2 = validNum(document.productForm.elements['tp2'].value);
    var f3 = validNum(document.productForm.elements['tp3'].value);
    var f4 = validNum(document.productForm.elements['tp4'].value);
    var f5 = validNum(document.productForm.elements['tp5'].value);
    var f6 = validNum(document.productForm.elements['labour'].value);
    var deposit = validNum(document.productForm.elements['deposit'].value);
    var prevp = validNum(document.productForm.elements['prevp'].value);

    // Calculates Information
    var subtotal = f1 + f2 + f3 + f4 + f5 + f6;
    var vat = (0.175 * subtotal);
    var total = vat + subtotal;
    var balo = total - deposit
    var balp = total - prevp
    var totalparts = f1 + f2 + f3 + f4 + f5;

    // Post totals
    document.productForm.elements['totalparts'].value = totalparts;
    document.productForm.elements['subtotal'].value = subtotal;
    document.productForm.elements['vat'].value = vat;
    document.productForm.elements['total'].value = total;
    document.productForm.elements['balo'].value = balo;
    document.productForm.elements['balp'].value = balp;


    // Makes sure variable is a number
    function validNum(nmbr){
    // forces variable into integer type
    nmbr = (nmbr * 1);

    // Checks if variable is Not a Number
    // If a number, return value
    return nmbr;
    // If not a number, return zero
    return 0;

    heres an example http://www.ipaul.co.uk/example.html

    these are the problems i got ( well the main problems)

    as you add numbers into total price colums it adds but untill you get to the deposit colum then it refuses to add up unless you go back to the total price colum and re-enter the figure.
    Also i would like the figure to round up to two decimals ie £3.25 etc

    i was wondering if an calculate button would be easier that just lets someone enter the figures and then once they hit the calculate buttons it does the sums.
    If anyone has some example code for this i would be most thankful.

    one other thing i would like to add aswell is if the amount is over a sum say £150 and no repair code is entered into the "REPAIR CODE" box then the form errors notifying that no authority code has been entered into "AUTHORITY NUMBER" box, but if the amount is under 150 no error accurs.

    I know i'm asking alot any pointers are helpfull.

  • #2
    1) Please fix your HTML errors.

    2) You seem to have divitis and span-mania. I suggest you learn how semantics apply to X/HTML.
    The Meaning of Semantics Take II: Naming Conventions for Class and ID in CSS
    Use class with semantics in mind

    3) Don't forget to validate those totals on the server.

    4) You forgot to put an onkeyup handler on #deposit.

    5) Use toFixed() to keep two decimal places.
      // for IE versions older than 5.5 and Netscape 4.x. Early versions of NS4.x 
      // don't support ===, so I used == and typeof() to simulate it.
          var n=this;
          var f=Math.pow(10,d);
          return n.substring(0,(d==0)?n.indexOf('.'):n.indexOf('.')+d+1);
    6) I suggest you read these:
    The Behavior Layer
    Separating behavior and structure
    Behavioral Separation
    Unobtrusive JavaScript
    DOM Scripting - Sample chapter: Best Practices
    Accessible DHTML

    P.S. Please read Guidelines and Suggestions for Posting on Web Development Forums.
    Last edited by Kravvitz; Oct 15, 2006, 04:19 PM.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.