views:

248

answers:

3

I have this code in the HEAD:

<script LANGUAGE="JavaScript">
<!--
function calculate(form)

        {

                height = eval(form.height.value)
                width = eval(form.width.value)
                photos = eval(form.photos.value)
                lgtext = eval(form.lgtext.value)
                mountlam = eval(form.mount.value)
                mountlam = eval(form.lam.value)
                GetPriceOne (form, height, width, photos, lgtext, mount, lam) 

        }

        function GetPriceOne(form, height, width, photos, lgtext, mount, lam)

        {

                PriceOne = height * width
                GetPriceTwo(form, height, width, photos, lgtext, mount, lam, PriceOne)
        }

        function GetPriceTwo(form, height, width, photos, lgtext, mount, lam, PriceOne)

        {

                PriceTwo = PriceOne / 144
                GetPriceThree(form, height, width, photos, lgtext, mount, lam, PriceTwo)

        }

        function GetPriceThree(form, height, width, photos, lgtext, mount, lam, PriceTwo)

        {

                PriceThree = PriceTwo * 15
                GetPriceFour(form, height, width, photos, lgtext, mount, lam, PriceThree)

        }

        function GetPriceFour(form, height, width, photos, lgtext, mount, lam, PriceThree)

        {

                if(form.lgtext.checked)
                {
                        PriceFour = PriceThree + 20
                        GetPriceFive(form, height, width, photos, lgtext, mount, lam, PriceFour)
                }
                else
                {
                        PriceFour = PriceThree
                        GetPriceFive(form, height, width, photos, lgtext, mount, lam, PriceFour)
                }

        }

        function GetPriceFive(form, height, width, photos, lgtext, mount, lam, PriceFour)

        {

                if(form.mount.checked)
                {
                        PriceFive = PriceFour + PriceTwo * 5
                        GetPriceSix(form, height, width, photos, lgtext, mount, lam, PriceFive)
                }
                else
                {
                        PriceFive = PriceFour
                        GetPriceSix(form, height, width, photos, lgtext, mount, lam, PriceFive)
                }

        }

        function GetPriceSix(form, height, width, photos, lgtext, mount, lam, PriceFive)

        {

                if(form.lam.checked)
                {
                        PriceSix = PriceFive + PriceTwo * 5
                        GetPriceSeven(form, height, width, photos, lgtext, mount, lam, PriceSix)
                }
                else
                {
                        PriceSix = PriceFive
                        GetPriceSeven(form, height, width, photos, lgtext, mount, lam, PriceSix)
                }

        }


        function GetPriceSeven(form, height, width, photos, lgtext, mount, lam, PriceSix)

        {

        total = (photos * 4.95) + PriceSix
        WriteDocument(total)

        }

        function RoundToPennies(n)

        {

        pennies = n * 100;
        pennies = Math.round(pennies);
        strPennies = "" + pennies;
        len = strPennies.length;
        return strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len);

        }

        function WriteDocument(total)

        {

                alert("Estimated price of this collage is ONLY $" + RoundToPennies(total))
        }
//-->
</script>

If I want it to go into this text box, what do i need to do to the function?

<INPUT TYPE = Text NAME = "collageEstimate" SIZE = 25 />
<input type="button" value="Calculate Estimate" name="B1" onclick="calculate(this.form)" />

Please help! I've been at this for hours and have tried everything I know!!! Here is the actual page as it now, working with the alert-popup: http://procollage.com/site10/pricing/photo-collage-pricing.html

A: 

You can try :

document.getElementById('collageEstimate').value = yourvalue

EDIT : I see you have name=collageEstimate, you can change to id=collageEstimate.

fastcodejava
Tried yours too. I know it's me overlooking something. I've tried SOOOOO many freakin' things in the past 10 hours that I'm cross-eyed: http:/www.procollage.com/site10/pricing/pricing3.html
dg
thank you for the info
dg
+3  A: 

What is this line trying to do?

height = eval(form.height.value)

If you're just trying to read it as a number, then do that:

height = parseFloat(form.height.value);

Anyway, change the WriteDocument function to this:

function WriteDocument(total) {
    document.yourFormName.collageEstimate.value = "Estimated price of this college "
                                           + "is ONLY $" + RoundToPennies(total);
}

You'll need to have your HTML looking something like this:

<form name="yourFormName">
    <input type="text" name="collageEstimate" size="25" />
    <input type="button" value="Calculate Estimate" name="B1" onclick="calculate(this.form)" />
</form>
nickf
I've been a graphic designer and web designer for a while but am I'm new to javascript. Your code isn't working (i'm sure i'm overlooking something simple): http://procollage.com/site10/pricing/pricing1.html
dg
GOT IT! It's working procollage.com/site10/pricing/pricing1.html
dg
A: 

Ok, to show the calculated value on the text input, you should get a reference to it, for example:

function WriteDocument(total) {
  document.forms['myForm'].elements['collageEstimate'].value = RoundToPennies(total);
}

You just need to replace myForm with the name of your form.

But I have a couple of comments on your code, aside your main question:

  1. You don't need to use eval to convert a String value to Number, you can just use the unary plus operator, the Number constructor called as a function, or parseFloat:

    var height = +form.height.value; // or
    var height = Number(form.height.value); // or
    var height = parseFloat(form.height.value);
    
  2. You should declare your variables with the var statement, otherwise if they aren't found in the current scope, they will become global variables. e.g.:

    //...
    var total = (photos * 4.95) + PriceSix;
    
  3. I would also recommend you to use semicolons on assignments, function calls, and return statements.

CMS
Thank you for your answer and so fast! I've been a graphic designer and web designer for a while but am I'm new to javascript. Your code isn't working (i'm sure i'm overlooking something simple): http:/www.procollage.com/site10/pricing/pricing2.htmlI also want to 'clean up' the code and will try your 'var' suggestions as soon as i get this working.
dg
Damn... i really don't know what I'm doing. I HATE that I would have to ask for it to be completely written out. It seriously pisses me off.
dg