Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function not right?

Status
Not open for further replies.

shamrox7

Programmer
Aug 1, 2007
14
US
I have written a function that does a few calculations based on numbers entered into several fields. I have most of it working, but a few things just don't ever fire and perhaps someone can see what's errored in my code.

Background: User inputs the vendor list price, quantity, a discount (if one is offered), a discount the company receives from vendor (STB discount) and lastly, payment method. If a credit card type is selected for payment method, a fee amount would come into the calculations.

What I can't seem to get to work right is the calculation and addition of the credit card fee to the rest of the calculations. See code below, and thanks.

Oh, the function fires with the onChange of one of the discount fields....

Code:
function calc_discount(){
  	price = document.mainform.vendorlistprice.value;
	price = price.replace(/[^\d\.]/g,"");
	quantity = document.mainform.quantity.value;
	discnt = document.mainform.discount.value;
	discnt = discnt.replace(/[^\d\.]/g,"");
	newprice = price*quantity
	reducedprice = newprice*(discnt/100);
	saleprice = (newprice-reducedprice).toFixed(2);
	saleprice = saleprice.replace(/[^\d\.]/g,"");
	document.mainform.sale_price.value = (newprice-reducedprice).toFixed(2);
	// Start calc of customer savings
	document.mainform.customersavings.value = (price-saleprice).toFixed(2);
	// Start Secondary reduction calculation
	discnt = document.mainform.stb_discount.value;
	discnt = discnt.replace(/[^\d\.]/g,"");
	reducedprice = price*(discnt/100);
	cogs = (price-reducedprice).toFixed(2);
  	document.mainform.cogs.value = cogs;
	// Start calc of Profit
	document.mainform.profit.value = (saleprice-cogs).toFixed(2);
	// Start calc for cc fee
	var paymentmethod = document.getElementById("paymentmethod").selectedIndex;
  	if (paymentmethod.length>0){
	var type  = paymentmethod;
	switch (type)
	{
	case 6:
	var perct = .0325;
	break
	case 7:
	var perct = .003;
	break
	case 8:
	var perct = .03;
	break
	default:
	var perct = 0;
	  }
	creditfee = (cogs*perct).toFixed(2);  
	document.mainform.creditfee.value = creditfee;
	document.mainform.profit.value = (saleprice-cogs-creditfee).toFixed(2);
  }
  }
  function docalcs() {
 var discount = document.mainform.discount.value
 if (discount.length>0) {
 calc_discount()
 } 
 }
 
Hi

Code:
function calc_discount() {
    price = document.mainform.vendorlistprice.value;
    price = [red]pareseInt([/red]price.replace(/[^\d\.]/g,"")[red],10)[/red];
    quantity = [red]pareseInt([/red]document.mainform.quantity.value[red],10)[/red];
    discnt = document.mainform.discount.value;
    discnt = [red]pareseInt([/red]discnt.replace(/[^\d\.]/g,"")[red],10)[/red];
    newprice = price*quantity
    reducedprice = newprice*(discnt/100);
    saleprice = (newprice-reducedprice).toFixed(2);
    saleprice = [red]pareseInt([/red]saleprice.replace(/[^\d\.]/g,"")[red],10)[/red];
    document.mainform.sale_price.value = (newprice-reducedprice).toFixed(2);
    // Start calc of customer savings
    document.mainform.customersavings.value = (price-saleprice).toFixed(2);
    // Start Secondary reduction calculation
    discnt = document.mainform.stb_discount.value;
    discnt = [red]pareseInt([/red]discnt.replace(/[^\d\.]/g,"")[red],10)[/red];
    reducedprice = price*(discnt/100);
    cogs = (price-reducedprice).toFixed(2);
      document.mainform.cogs.value = cogs;
    // Start calc of Profit
    document.mainform.profit.value = (saleprice-cogs).toFixed(2);
    // Start calc for cc fee
    var paymentmethod = document.getElementById("paymentmethod").selectedIndex;
    if (paymentmethod.length>0) {
        var type  = paymentmethod;
        switch (type) {
            case 6:
                perct = .0325;
                break
            case 7:
                perct = .003;
                break
            case 8:
                perct = .03;
                break
            default:
                perct = 0;
        }
        creditfee = (cogs*perct).toFixed(2);  
        document.mainform.creditfee.value = creditfee;
        document.mainform.profit.value = (saleprice-cogs-creditfee).toFixed(2);
    }
}

Feherke.
 
My profit and Creditfee fields are still showing up with no data on the page. Any thoughts on that?
 
I think pareseInt should be parseInt.

I'd suggest putting some alerts to see the values of the variables to check where are you losing the track.

Cheers,
Dian
 
NP, I've been there :)

Btw, discnt is calculated and then overwritten without being read. I'd seriously consider the alert thingie.

Cheers,
Dian
 
Ahhh perhaps that's it. I'll check. Thanks All.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top