File size: 2,869 Bytes
19dc2fc
ecd9fb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19dc2fc
 
 
 
ecd9fb1
 
19dc2fc
 
 
 
 
 
9f439b3
ecd9fb1
9f439b3
ecd9fb1
 
 
 
9f439b3
77e60cf
9f439b3
 
 
 
 
ecd9fb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f439b3
19dc2fc
 
 
ecd9fb1
19dc2fc
 
ecd9fb1
 
19dc2fc
 
 
ecd9fb1
 
 
823a236
ecd9fb1
 
19dc2fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const stateTaxRates = {
  "Alabama": 0.04,
  "Alaska": 0.00,
  "Arizona": 0.056,
  "Arkansas": 0.065,
  "California": 0.0725,
  "Colorado": 0.029,
  "Connecticut": 0.0635,
  "Delaware": 0.00,
  "Florida": 0.06,
  "Georgia": 0.04,
  "Hawaii": 0.04,
  "Idaho": 0.06,
  "Illinois": 0.0625,
  "Indiana": 0.07,
  "Iowa": 0.06,
  "Kansas": 0.065,
  "Kentucky": 0.06,
  "Louisiana": 0.0445,
  "Maine": 0.055,
  "Maryland": 0.06,
  "Massachusetts": 0.0625,
  "Michigan": 0.06,
  "Minnesota": 0.06875,
  "Mississippi": 0.07,
  "Missouri": 0.04225,
  "Montana": 0.00,
  "Nebraska": 0.055,
  "Nevada": 0.0685,
  "New Hampshire": 0.00,
  "New Jersey": 0.06625,
  "New Mexico": 0.05125,
  "New York": 0.04,
  "North Carolina": 0.0475,
  "North Dakota": 0.05,
  "Ohio": 0.0575,
  "Oklahoma": 0.045,
  "Oregon": 0.00,
  "Pennsylvania": 0.06,
  "Rhode Island": 0.07,
  "South Carolina": 0.06,
  "South Dakota": 0.045,
  "Tennessee": 0.07,
  "Texas": 0.0625,
  "Utah": 0.0485,
  "Vermont": 0.06,
  "Virginia": 0.043,
  "Washington": 0.065,
  "West Virginia": 0.06,
  "Wisconsin": 0.05,
  "Wyoming": 0.04
};

window.onload = () => {
  const stateSelect = document.getElementById("state");

  // Populate dropdown
  for (const state in stateTaxRates) {
    const option = document.createElement("option");
    option.value = state;
    option.textContent = state;
    stateSelect.appendChild(option);
  }

  // Auto-detect user's state with fallback
  fetch("https://ipapi.co/json/")
    .then(response => {
      if (!response.ok) throw new Error("Primary API failed");
      return response.json();
    })
    .then(data => {
      const userState = data.region;
      if (stateTaxRates[userState]) {
        stateSelect.value = userState;
      }
    })
    .catch(error => {
      console.warn("Primary location detection failed:", error);
      // Fallback to ipinfo.io using your token
      fetch("https://ipinfo.io/json?token=98220602a25957")
        .then(response => {
          if (!response.ok) throw new Error("Fallback API failed");
          return response.json();
        })
        .then(data => {
          const userState = data.region;
          if (stateTaxRates[userState]) {
            stateSelect.value = userState;
          }
        })
        .catch(fallbackError => {
          console.warn("Fallback location detection also failed:", fallbackError);
        });
    });
};

function calculateTax() {
  const amount = parseFloat(document.getElementById("amount").value);
  const state = document.getElementById("state").value;

  if (isNaN(amount)) {
    alert("Please enter a valid amount.");
    return;
  }

  const taxRate = stateTaxRates[state];
  const taxAmount = amount * taxRate;
  const totalAmount = amount + taxAmount;

  document.getElementById("result").textContent =
    `Tax: $${taxAmount.toFixed(2)} | Total: $${totalAmount.toFixed(2)}`;
}