In this step, we'll add some fields to get input we'll need for calculating state withholding.
document.addEventListener("DOMContentLoaded", function() {
loadStates();
});
function compute() {
var earnings = getFloat('earnings');
var filingstatus = getInt('filingstatus');
var payperiods = getInt('payperiods');
getFederal(earnings, filingstatus, payperiods);
}
function getFederal(earnings, filingstatus, payperiods) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
let res = JSON.parse(this.responseText);
if (res.status == "ok") {
var out_federal = document.getElementById('out_federal');
out_federal.value = res.value.toFixed(2);
}
}
};
var uri = "http://localhost:8000/taxamount/federal income tax"
+ "?earnings=" + earnings.toString()
+ "&filingstatus=" + filingstatus.toString()
+ "&payperiods=" + payperiods.toString();
xhttp.open("GET", uri, true);
xhttp.send();
}
function loadStates() {
var select = document.getElementById('state');
var states = [["AL", "Alabama"], ["AK", "Alaska"], ["AZ", "Arizona"],
["AR", "Arkansas"], ["CA", "California"], ["CO", "Colorado"],
["CT", "Connecticut"], ["DE", "Delaware"], ["FL", "Florida"],
["GA", "Georgia"], ["HI", "Hawaii"], ["ID", "Idaho"],
["IL", "Illinois"], ["IN", "Indiana"], ["IA", "Iowa"],
["KS", "Kansas"], ["KY", "Kentucky"], ["LA", "Louisiana"],
["ME", "Maine"], ["MD", "Maryland"], ["MA", "Massachusetts"],
["MI", "Michigan"], ["MN", "Minnesota"], ["MS", "Mississippi"],
["MO", "Missouri"], ["MT", "Montana"], ["NE", "Nebraska"],
["NV", "Nevada"], ["NH", "New Hampshire"], ["NJ", "New Jersey"],
["NM", "New Mexico"], ["NY", "New York"], ["NC", "North Carolina"],
["ND", "North Dakota"], ["OH", "Ohio"], ["OK", "Oklahoma"],
["OR", "Oregon"], ["PA", "Pennsylvania"], ["RI", "Rhode Island"],
["SC", "South Carolina"], ["SD", "South Dakota"],
["TN", "Tennessee"], ["TX", "Texas"], ["UT", "Utah"],
["VT", "Vermont"], ["VA", "Virginia"], ["WA", "Washington"],
["WV", "West Virginia"], ["WI", "Wisconsin"], ["WY", "Wyoming"]];
states.forEach(function(state) {
var option = document.createElement('option');
option.setAttribute('value', state[0]);
option.appendChild(document.createTextNode(state[1]));
select.appendChild(option);
});
select.addEventListener("change", function() {
updateStateDiv(select);
});
updateStateDiv(select);
}
function updateStateDiv(state_select) {
getProperties(state_select.value, function (response) {
if (response.status == "ok") {
var tax_props = response.value;
var div = document.getElementById("state_div");
if (!tax_props.has_tax) {
div.style.display = "none";
state_select.setAttribute("title", "No state tax");
} else {
div.style.display = "block";
state_select.setAttribute("title", "");
}
} else {
alert(response.value);
}
});
}
function getProperties(taxname, callback) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
let res = JSON.parse(this.responseText);
callback(res);
}
};
var uri = "http://localhost:8000/tax/" + taxname;
xhttp.open("GET", uri, true);
xhttp.send();
}
function getFloat(id) {
var elem = document.getElementById(id).value;
var f = parseFloat(elem);
if (isNaN(f)) {
return 0.00;
} else {
return f;
}
}
function getInt(id) {
var elem = document.getElementById(id).value;
var f = parseInt(elem);
if (Number.isInteger(f)) {
return f;
} else {
return 0;
}
}
Here are two new functions:
getProperties - a helper function for fetching tax properties from TaxServer, and
updateStateDiv - a function which displays or hides the state_div section based on whether TaxServer says the state has a tax.
We've added some code to loadStates to call updateStateDiv whenever the state selection changes. We also call the function initially to make sure the form is in sync with the initial state select box value.