Changed around line 1
+ document.addEventListener('DOMContentLoaded', function() {
+ const calculateBtn = document.getElementById('calculate');
+ const resultsDiv = document.querySelector('.results');
+ let chart = null;
+
+ calculateBtn.addEventListener('click', calculateSalary);
+
+ function calculateSalary() {
+ const grossSalary = parseFloat(document.getElementById('gross-salary').value);
+
+ if (isNaN(grossSalary) || grossSalary <= 0) {
+ alert('Please enter a valid salary amount');
+ return;
+ }
+
+ // Simple tax calculation (estimated)
+ const taxRate = calculateTaxRate(grossSalary);
+ const netSalary = grossSalary * (1 - taxRate);
+ const monthlyNet = netSalary / 12;
+
+ updateDisplay(grossSalary, netSalary, monthlyNet);
+ drawChart(netSalary, grossSalary - netSalary);
+
+ resultsDiv.classList.remove('hidden');
+ }
+
+ function calculateTaxRate(gross) {
+ // Progressive tax rate simulation
+ if (gross <= 9950) return 0.10;
+ if (gross <= 40525) return 0.12;
+ if (gross <= 86375) return 0.22;
+ if (gross <= 164925) return 0.24;
+ if (gross <= 209425) return 0.32;
+ if (gross <= 523600) return 0.35;
+ return 0.37;
+ }
+
+ function updateDisplay(gross, net, monthly) {
+ document.getElementById('gross-amount').textContent = formatCurrency(gross);
+ document.getElementById('net-amount').textContent = formatCurrency(net);
+ document.getElementById('monthly-amount').textContent = formatCurrency(monthly);
+ }
+
+ function formatCurrency(amount) {
+ return new Intl.NumberFormat('en-US', {
+ style: 'currency',
+ currency: 'USD',
+ maximumFractionDigits: 0
+ }).format(amount);
+ }
+
+ function drawChart(net, tax) {
+ const ctx = document.getElementById('pie-chart').getContext('2d');
+
+ if (chart) {
+ chart.destroy();
+ }
+
+ const centerX = ctx.canvas.width / 2;
+ const centerY = ctx.canvas.height / 2;
+ const radius = Math.min(centerX, centerY) * 0.8;
+
+ const total = net + tax;
+ const netAngle = (net / total) * Math.PI * 2;
+
+ ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
+
+ // Draw net salary portion
+ ctx.beginPath();
+ ctx.moveTo(centerX, centerY);
+ ctx.arc(centerX, centerY, radius, 0, netAngle);
+ ctx.fillStyle = '#00B894';
+ ctx.fill();
+
+ // Draw tax portion
+ ctx.beginPath();
+ ctx.moveTo(centerX, centerY);
+ ctx.arc(centerX, centerY, radius, netAngle, Math.PI * 2);
+ ctx.fillStyle = '#636E72';
+ ctx.fill();
+ }
+ });