View Full Version : adding up parts etc

10-15-2006, 03:40 PM
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.

10-15-2006, 08:16 PM
1) Please fix your HTML errors. (http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.ipaul.co.uk%2Fexample.html)

2) You seem to have divitis and span-mania. I suggest you learn how semantics apply to X/HTML. (http://www.dynamicsitesolutions.com/html/semantics/)
The Meaning of Semantics Take II: Naming Conventions for Class and ID in CSS (http://www.informit.com/articles/article.asp?p=170514&rl=1)
Use class with semantics in mind (http://www.w3.org/QA/Tips/goodclassnames)

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 (http://www.digital-web.com/articles/the_behavior_layer/)
Separating behavior and structure (http://www.digital-web.com/articles/separating_behavior_and_structure_2/)
Behavioral Separation (http://www.alistapart.com/articles/behavioralseparation/)
Unobtrusive JavaScript (http://www.onlinetools.org/articles/unobtrusivejavascript/)
DOM Scripting - Sample chapter: Best Practices (http://www.domscripting.com/book/sample/)
Accessible DHTML (http://www.severnsolutions.co.uk/articles/001/)

P.S. Please read Guidelines and Suggestions for Posting on Web Development Forums (http://www.dynamicsitesolutions.com/other/forum_posting_guidelines/).