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

Hello,

I have a simple order form which uses a function to calculate totals, sales tax and shipping and another function to auto-complete the fields for shipping address if a box is checked that the shipping address is the same as the billing address. All works fine, except that I cannot get the sales tax to calculate correctly. Only orders shipped to Nevada will have sales tax, and if someone selects that state within the drop-down menu of states under shipping, the sales tax is calculated correctly. However, if someone enters a billing address with Nevada as the state, and they check the box that the shipping address is the same as the billing address, the shipping fields are populated correctly, including Nevada being selected in the drop-down menu, but for some reason, the sales tax isn't registering for this. It only calculates if someone manually selects Nevada in the shipping drop-down menu of states. I don't know if there is a simple solution for getting the shipping state to register the sales tax when the shipping address is the same as the billing address (checking the box), but I have been trying to attack it from a different angle and check to see if the billing state is Nevada AND if the shipsame checkbox is checked, in order to trigger the sales tax, but this isn't working either. I think I need to somehow reference the function that is used to auto-fill the shipping fields, as that function is what is used for that checkbox, from within my other function for calculating totals, but I must be doing something wrong. Here is what I have so far (the pertinent code):

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.saddress1.value = (isChecked)? f.baddress1.value:"";
  f.saddress2.value = (isChecked)? f.baddress2.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!
tam423Asked:
Who is Participating?
 
walkerkeCommented:
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
                                               // billing address fields.
  }     // 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
 
walkerkeCommented:
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.saddress1.value = f.baddress1.value:"";
  f.saddress2.value = f.baddress2.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
 
tam423Author Commented:
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.saddress1.value = f.baddress1.value;
        f.saddress2.value = f.baddress2.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
 
tam423Author Commented:
walkerke,

Thank you so much! It works great now. I really appreciate your assistance with this. :-)
0
 
walkerkeCommented:
You're welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.