?
Solved

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

Posted on 2007-04-10
5
Medium Priority
?
1,205 Views
Last Modified: 2008-04-21
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!
0
Comment
Question by:tam423
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:walkerke
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.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
 

Author Comment

by:tam423
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.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
 
LVL 11

Accepted Solution

by:
walkerke earned 500 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
                                               // 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
 

Author Comment

by:tam423
ID: 18895037
walkerke,

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

Expert Comment

by:walkerke
ID: 18897013
You're welcome.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question