<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Spain vs UK Tax Comparison</title>

    <style>

        body {

            font-family: Arial, sans-serif;

            margin: 0;

            padding: 0;

            background-color: #f9f9f9;

            color: #333;

        }

        header {

    background-color: white;

    border-bottom: 2px solid #ddd;

    padding: 10px 20px;

    display: flex;

    justify-content: space-between;

    align-items: center;

    font-family: Arial, sans-serif;

}


.branding {

    display: flex;

    align-items: center;

}


.logo-container {

    display: flex;

    align-items: center;

    font-size: 1.5rem;

    font-weight: bold;

}


.logo-text {

    background-color: #4cffb5;

    padding: 5px 10px;

    color: black;

    border-radius: 5px 0 0 5px;

}


.logo-highlight {

    background-color: #333;

    padding: 5px 10px;

    color: #4cffb5;

    border-radius: 0 5px 5px 0;

}


.nav-links {

    margin-left: 20px;

    display: flex;

    gap: 15px;

}


.nav-links a {

    text-decoration: none;

    color: black;

    font-size: 1rem;

    transition: color 0.3s;

}


.nav-links a:hover {

    color: #4cffb5;

}


.cta-button {

    background-color: #4cffb5;

    color: black;

    text-decoration: none;

    padding: 10px 15px;

    font-weight: bold;

    border-radius: 5px;

    font-size: 1rem;

    transition: background-color 0.3s;

}


.cta-button:hover {

    background-color: #3ae09d;

}

        header h1 {

            margin: 0;

            font-size: 2rem;

        }

        .form-container {

            max-width: 900px;

            margin: 30px auto;

            padding: 20px;

            background: #fff;

            border-radius: 10px;

            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

        }

        .form-container form {

            display: flex;

            flex-wrap: wrap;

            gap: 20px;

            justify-content: center;

            align-items: center;

        }

        .form-container label {

            font-size: 1rem;

        }

        .form-container input[type="number"], 

        .form-container button {

            padding: 10px;

            border: 1px solid #ddd;

            border-radius: 5px;

            font-size: 1rem;

        }

        .form-container button {

            background-color: #2c3e50;

            color: #fff;

            cursor: pointer;

            transition: background-color 0.3s;

        }

        .form-container button:hover {

            background-color: #1a252f;

        }

        .results-container {

            display: flex;

            flex-wrap: wrap;

            justify-content: space-around;

            gap: 20px;

            margin: 20px auto;

            max-width: 1200px;

        }

        .box {

            background: #fff;

            border-radius: 10px;

            padding: 20px;

            width: calc(50% - 20px);

            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

        }

        .box h2 {

            font-size: 1.5rem;

            color: #2c3e50;

        }

        table {

            width: 100%;

            border-collapse: collapse;

            margin-top: 15px;

        }

        table th, table td {

            border: 1px solid #ddd;

            padding: 10px;

            text-align: center;

        }

        table th {

            background-color: #f4f4f4;

            color: #333;

            font-weight: bold;

        }

        .highlight-section {

            max-width: 900px;

            margin: 40px auto;

            text-align: center;

        }

        .highlight-section h2 {

            font-size: 1.8rem;

            margin-bottom: 20px;

            color: #2c3e50;

        }

        .highlight-container {

            display: flex;

            justify-content: space-around;

            gap: 10px;

        }

        .highlight-box {

            border: 2px solid transparent;

            border-radius: 10px;

            padding: 20px;

            width: 200px;

            font-size: 1.2rem;

            font-weight: bold;

            text-align: center;

        }

        .better-off {

            border-color: #4cffb5;

            background-color: #4cffb5;

            color: green;

        }

        .worse-off {

            border-color: red;

            background-color: #fbeaea;

            color: red;

        }

        footer {

            background-color: #f9f9f9;

             text-align: center;

            padding: 20px;

            border-top: 2px solid #ddd;

            font-size: 0.9rem;

            color: #333;

            font-family: Arial, sans-serif;

            }

    </style>

</head>

<body>

    <header>

        <div class="branding">

            <div class="logo-container">

                <span class="logo-text">SPAIN vs UK</span>

                <span class="logo-highlight">TAX</span>

            </div>

        </div>

        <nav class="nav-links">

            <a href="#">Beckham Law</a>

            <a href="#">Moving to Spain?</a>

            <a href="#">Living in Spain?</a>

        </nav>

        <a href="#" class="cta-button">Request Free Consultation</a>

    </header>

    <div class="form-container">

        <form id="tax-form">

            <label for="salary">Enter Salary (£):</label>

            <input type="number" id="salary" placeholder="e.g., 50000" required>

            <label>

                <input type="checkbox" id="beckham-law">

                Apply Beckham Law (Spain)

            </label>

            <button type="submit">Compare Taxes</button>

        </form>

    </div>

        </div>

    <div class="highlight-section">

        <h2>Total Tax Difference</h2>

        <div id="total-difference" style="font-size: 2rem; font-weight: bold; margin-bottom: 20px;"></div>

        <div class="highlight-container">

            <div id="highlight-week" class="highlight-box"></div>

            <div id="highlight-month" class="highlight-box"></div>

            <div id="highlight-year" class="highlight-box"></div>

            <div id="highlight-five-years" class="highlight-box"></div>

        </div>

    <div class="results-container">

        <div class="box">

            <h2>Spain Tax Breakdown</h2>

            <div id="spain-results"></div>

        </div>

        <div class="box">

            <h2>UK Tax Breakdown</h2>

            <div id="uk-results"></div>

        </div>

    </div>

    <div class="results-container">

        <div class="box" style="width: 100%;">

            <h2>Comparison Table</h2>

            <div id="comparison-results"></div>

        </div>

    </div>


    </div>

    <footer>

    <p>© 2024 Compare Tax Spain. All rights reserved.</p>

    </footer>

    <script>

        let euroConversionRate = 1.17; // Default rate (fallback)


        async function fetchConversionRate() {

            const apiKey = "fca_live_Cw4QAexNPp5N5VKdbPFHq8bdYtcRoigNwCr0Rsg1";

            const url = `https://api.freecurrencyapi.com/v1/latest?apikey=${apiKey}&currencies=EUR&base_currency=GBP`;


            try {

                const response = await fetch(url);

                const data = await response.json();


                if (data && data.data && data.data.EUR) {

                    euroConversionRate = data.data.EUR;

                    console.log("Updated Conversion Rate:", euroConversionRate);

                } else {

                    throw new Error("Invalid response format");

                }

            } catch (error) {

                console.error("Error fetching conversion rate:", error);

            }

        }


        document.addEventListener("DOMContentLoaded", fetchConversionRate);


        document.getElementById("tax-form").addEventListener("submit", function (event) {

            event.preventDefault();


            const salaryGBP = parseFloat(document.getElementById("salary").value);

            if (isNaN(salaryGBP) || salaryGBP <= 0) {

                alert("Please enter a valid salary.");

                return;

            }


            const salaryEUR = salaryGBP * euroConversionRate;

            const applyBeckhamLaw = document.getElementById("beckham-law").checked;


            const spainTaxes = calculateSpainTaxes(salaryEUR, applyBeckhamLaw);

            const ukTaxes = calculateUKTaxes(salaryGBP);


            displayResults("spain-results", spainTaxes, salaryGBP, salaryEUR);

            displayResults("uk-results", ukTaxes, salaryGBP, salaryEUR);

            displayComparison(spainTaxes, ukTaxes, salaryGBP, salaryEUR, euroConversionRate);

            displayHighlights(spainTaxes, ukTaxes, euroConversionRate);

        });


        function calculateSpainTaxes(incomeEUR, beckhamLaw) {

            const taxRates = beckhamLaw ? { incomeTaxRate: 0.24, socialSecurityCap: 4500 } : {

                incomeTaxRate: incomeEUR <= 12450 ? 0.19 :

                               incomeEUR <= 20200 ? 0.24 :

                               incomeEUR <= 35200 ? 0.3 :

                               incomeEUR <= 60000 ? 0.37 : 0.45,

                socialSecurityRate: 0.0635,

                socialSecurityCap: 4500,

            };


            const socialSecurityTax = Math.min(incomeEUR * (taxRates.socialSecurityRate || 0.0635), taxRates.socialSecurityCap);

            const incomeTax = incomeEUR * taxRates.incomeTaxRate;

            const totalTax = incomeTax + socialSecurityTax;

            const netIncome = incomeEUR - totalTax;


            return {

                taxableIncome: incomeEUR,

                incomeTax: incomeTax,

                socialSecurityTax: socialSecurityTax,

                totalTax: totalTax,

                annualNetIncome: netIncome,

                monthlyNetIncome: netIncome / 12,

                weeklyNetIncome: netIncome / 52,

            };

        }


        function calculateUKTaxes(incomeGBP) {

            const incomeTaxRate = incomeGBP <= 12570 ? 0 :

                                  incomeGBP <= 50270 ? 0.2 :

                                  incomeGBP <= 150000 ? 0.4 : 0.45;


            const incomeTax = incomeGBP <= 12570 ? 0 :

                              incomeGBP <= 50270 ? (incomeGBP - 12570) * 0.2 :

                              incomeGBP <= 150000 ? (incomeGBP - 50270) * 0.4 + 7540 :

                              (incomeGBP - 150000) * 0.45 + 7540 + 39892;


            const nationalInsuranceRate = incomeGBP <= 12570 ? 0 :

                                          incomeGBP <= 50270 ? 0.12 : 0.02;


            const nationalInsurance = incomeGBP <= 50270 ? (incomeGBP - 12570) * 0.12 :

                                       (50270 - 12570) * 0.12 + (incomeGBP - 50270) * 0.02;


            const totalTax = incomeTax + nationalInsurance;

            const netIncome = incomeGBP - totalTax;


            return {

                taxableIncome: incomeGBP,

                incomeTax: incomeTax,

                socialSecurityTax: nationalInsurance,

                totalTax: totalTax,

                annualNetIncome: netIncome,

                monthlyNetIncome: netIncome / 12,

                weeklyNetIncome: netIncome / 52,

            };

        }


        function displayResults(containerId, data, salaryGBP, salaryEUR) {

            const container = document.getElementById(containerId);

            container.innerHTML = `

                <table>

                    <thead>

                        <tr>

                            <th>Concept</th>

                            <th>Amount (GBP)</th>

                            <th>Amount (EUR)</th>

                        </tr>

                    </thead>

                    <tbody>

                        <tr><td>Taxable Income</td><td>£${salaryGBP.toFixed(2)}</td><td>€${salaryEUR.toFixed(2)}</td></tr>

                        <tr><td>Total Income Tax</td><td>£${(data.incomeTax / euroConversionRate).toFixed(2)}</td><td>€${data.incomeTax.toFixed(2)}</td></tr>

                        <tr><td>Social Security Tax</td><td>£${(data.socialSecurityTax / euroConversionRate).toFixed(2)}</td><td>€${data.socialSecurityTax.toFixed(2)}</td></tr>

                        <tr><td>Total Tax</td><td>£${(data.totalTax / euroConversionRate).toFixed(2)}</td><td>€${data.totalTax.toFixed(2)}</td></tr>

                        <tr><td>Year Net Income</td><td>£${(data.annualNetIncome / euroConversionRate).toFixed(2)}</td><td>€${data.annualNetIncome.toFixed(2)}</td></tr>

                        <tr><td>Month Net Income</td><td>£${(data.monthlyNetIncome / euroConversionRate).toFixed(2)}</td><td>€${data.monthlyNetIncome.toFixed(2)}</td></tr>

                    </tbody>

                </table>

            `;

        }


        function displayComparison(spainData, ukData, salaryGBP, salaryEUR, euroConversionRate) {

            const comparisonContainer = document.getElementById("comparison-results");

            comparisonContainer.innerHTML = `

                <table>

                    <thead>

                        <tr>

                            <th>Concept</th>

                            <th>Spain (GBP / EUR)</th>

                            <th>UK (GBP / EUR)</th>

                        </tr>

                    </thead>

                    <tbody>

                        <tr>

                            <td>Taxable Income</td>

                            <td>£${(spainData.taxableIncome / euroConversionRate).toFixed(2)} / €${spainData.taxableIncome.toFixed(2)}</td>

                            <td>£${ukData.taxableIncome.toFixed(2)} / €${(ukData.taxableIncome * euroConversionRate).toFixed(2)}</td>

                        </tr>

                        <tr>

                            <td>Total Income Tax</td>

                            <td>£${(spainData.incomeTax / euroConversionRate).toFixed(2)} / €${spainData.incomeTax.toFixed(2)}</td>

                            <td>£${ukData.incomeTax.toFixed(2)} / €${(ukData.incomeTax * euroConversionRate).toFixed(2)}</td>

                        </tr>

                        <tr>

                            <td>Social Security Tax</td>

                            <td>£${(spainData.socialSecurityTax / euroConversionRate).toFixed(2)} / €${spainData.socialSecurityTax.toFixed(2)}</td>

                            <td>£${ukData.socialSecurityTax.toFixed(2)} / €${(ukData.socialSecurityTax * euroConversionRate).toFixed(2)}</td>

                        </tr>

                        <tr>

                            <td>Total Tax</td>

                            <td>£${(spainData.totalTax / euroConversionRate).toFixed(2)} / €${spainData.totalTax.toFixed(2)}</td>

                            <td>£${ukData.totalTax.toFixed(2)} / €${(ukData.totalTax * euroConversionRate).toFixed(2)}</td>

                        </tr>

                        <tr>

                            <td>Year Net Income</td>

                            <td>£${(spainData.annualNetIncome / euroConversionRate).toFixed(2)} / €${spainData.annualNetIncome.toFixed(2)}</td>

                            <td>£${ukData.annualNetIncome.toFixed(2)} / €${(ukData.annualNetIncome * euroConversionRate).toFixed(2)}</td>

                        </tr>

                        <tr>

                            <td>Month Net Income</td>

                            <td>£${(spainData.monthlyNetIncome / euroConversionRate).toFixed(2)} / €${spainData.monthlyNetIncome.toFixed(2)}</td>

                            <td>£${ukData.monthlyNetIncome.toFixed(2)} / €${(ukData.monthlyNetIncome * euroConversionRate).toFixed(2)}</td>

                        </tr>

                    </tbody>

                </table>

            `;

        }


        function displayHighlights(spainData, ukData, euroConversionRate) {

    const spainTotalTax = spainData.totalTax;

    const ukTotalTax = ukData.totalTax;


    const taxDifferenceGBP = ukTotalTax - (spainTotalTax / euroConversionRate);


    const weeklyDifference = taxDifferenceGBP / 52;

    const monthlyDifference = taxDifferenceGBP / 12;

    const yearlyDifference = taxDifferenceGBP;

    const fiveYearDifference = taxDifferenceGBP * 5;


    function formatHighlightBox(title, value) {

        const isBetter = value >= 0;

        return `

            <div>

                <div style="font-size: 1.5rem; font-weight: bold;">£${Math.abs(value.toFixed(2))}</div>

                <div style="font-size: 1rem; margin-top: 5px;">${isBetter ? "Better off" : "Worse off"}</div>

            </div>

            <div style="margin-top: 10px; font-weight: bold;">${title}</div>

        `;

    }


    const setHighlightBoxStyle = (elementId, title, value) => {

        const element = document.getElementById(elementId);

        const isBetter = value >= 0;

        element.className = `highlight-box ${isBetter ? "better-off" : "worse-off"}`;

        element.innerHTML = formatHighlightBox(title, value);

    };


    setHighlightBoxStyle("highlight-week", "Per Week", weeklyDifference);

    setHighlightBoxStyle("highlight-month", "Per Month", monthlyDifference);

    setHighlightBoxStyle("highlight-year", "Per Year", yearlyDifference);

    setHighlightBoxStyle("highlight-five-years", "Per 5 Years", fiveYearDifference);

}



    </script>

</body>

</html>