Solved

# calculating sales tax based on state selected (function within a function?)

Posted on 2007-04-10
1,174 Views
Hello,

function setfields(obj){
var isChecked = obj.checked;
var f=obj.form; // get the form belonging to the checkbox
f.sname.value = (isChecked)? f.bname.value:"";
f.scity.value = (isChecked)? f.bcity.value:"";
f.sstate.selectedIndex = f.bstate.selectedIndex;
f.szip.value = (isChecked)? f.bzip.value:"";
f.scountry.value = (isChecked)? f.bcountry.value:"";

}

function compute(obj)
{
var form = obj.form;
//NV State sales tax cost
if (form.sstate.selectedIndex==31)
{form.salestax.value = fmtPrice((parseFloat(form.subtotal.value) * 0.07125)) }
else if ((form.bstate.selectedIndex==31) && (setfields.isChecked))
{form.salestax.value = fmtPrice((parseFloat(form.subtotal.value) * 0.07125)) }
--------
This is the checkbox for indicating that the shipping address is the same as billing:
<input type="CHECKBOX" onClick="setfields(this)" name="shipsame" value="true">

This is how the billing state drop-down menu begins:
<select size="1" onChange="compute(this)" name="bstate">

This is how the shipping drop-down menu begins:
<select size="1"  onChange="compute(this)" name="sstate">

I thought that referencing the shipsame checkbox "setfields(isChecked)" would be the answer, but I think my syntax is wrong -- or maybe I am totally off on how to achieve this, altogether! I am not too savvy with javascript. Any help would be most appreciated!
0
Question by:tam423
• 3
• 2

LVL 11

Expert Comment

ID: 18887708
I'm not sure if there is a way for the function "compute" to access the variable "isChecked" which is local to the setfields function. "setfields.isChecked" obviously doesn't work. If you make the compute function alert the value of setfields.isChecked, it will report "undefined". The solution that comes to mind is to make "isChecked" a global variable by setting it to false outside the function definition by changing a couple of lines at the beginning:

var isChecked = false;
function setfields(obj){
isChecked = obj.checked;
...etc.

and reference isChecked in the compute function instead of setfields.isChecked.

You should be aware that the shipping state is set to match the billing state whether the checkbox is checked or unchecked. If the user checks the box, then decides to enter a shipping address THEN uncheck the box, the shipping state will be changed to match the billing state when the box is unchecked.

It also might use fewer resources to evaluate isChecked once and react accordingly rather than evaluate it for each variable:

if (isChecked) {
f.sname.value = f.bname.value:"";
f.scity.value = f.bcity.value:"";
f.sstate.selectedIndex = f.bstate.selectedIndex;
f.szip.value = f.bzip.value:"";
f.scountry.value = f.bcountry.value:"";
}
0

Author Comment

ID: 18890931
walkerke,

Thanks so much for your response. I made the changes as you suggest, but unfortunately, there was no change. Checking the shipsame box still does not prompt the sales tax to be calculated when Nevada is selected in the billing drop-down menu. The code where I made changes now looks like:

var isChecked = false;

function setfields(obj){
var isChecked = obj.checked;
var f=obj.form; // get the form belonging to the checkbox
if (isChecked) {
f.sname.value = f.bname.value;
f.scity.value = f.bcity.value;
f.sstate.selectedIndex = f.bstate.selectedIndex;
f.szip.value = f.bzip.value;
f.scountry.value = f.bcountry.value;
}
}

function compute(obj)
{
var form = obj.form;

//NV State sales tax cost
if (form.sstate.selectedIndex==31)
{form.salestax.value = fmtPrice((parseFloat(form.subtotal.value) * 0.07125)) }
else if ((form.bstate.selectedIndex==31) && (isChecked))
{form.salestax.value = fmtPrice((parseFloat(form.subtotal.value) * 0.07125)) }

Did I not set up the global variable properly? Or, any other suggestions? I can provide the entire code for copying and testing, if necessary. Thank you!
0

LVL 11

Accepted Solution

walkerke earned 125 total points
ID: 18894541
Remove the "var" declaration from the function setfields which is declaring a new local variable named "isChecked" which is independent of the global one.

var isChecked = false;   // declare global variable "isChecked"

function setfields(obj){
isChecked = obj.checked;  // change/set global variable "isChecked" to the value of obj.checked
var f=obj.form;                     // declare local variable "f" and set to hold obj.form
if (isChecked) {                    // since there is not a local variable "isChecked", the global one will be
etc., etc.,                            // evaluated. If true, shipping address fields will be populated to match
}     // end conditional
compute(obj);                  // compute sales tax if applicable
}    // end function setfields

I have also added the call to the compute function at the end of the setfields function to compute the sales tax after the shipping address fields are populated.
0

Author Comment

ID: 18895037
walkerke,

Thank you so much! It works great now. I really appreciate your assistance with this. :-)
0

LVL 11

Expert Comment

ID: 18897013
You're welcome.
0

## Featured Post

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…