document.getElementById("contactForm").addEventListener("submit", function (e) {
    e.preventDefault();

    let valid = true;

    // Fields
    const name = document.getElementById("name");
    const email = document.getElementById("email");
    const phone = document.getElementById("phone");
    const message = document.getElementById("message");

    // Clear errors
    document.querySelectorAll(".error-msg").forEach(el => el.textContent = "");

    // Name
    if (name.value.trim() === "") {
        setError(name, "Name is required");
        valid = false;
    }

    // Email
    if (!/^\S+@\S+\.\S+$/.test(email.value)) {
        setError(email, "Valid email is required");
        valid = false;
    }

    // Phone
    if (!/^[0-9]{10}$/.test(phone.value)) {
        setError(phone, "Enter valid 10-digit phone number");
        valid = false;
    }

    // Message
    if (message.value.trim().length < 10) {
        setError(message, "Message must be at least 10 characters");
        valid = false;
    }

    if (valid) {
        document.getElementById("successMsg").textContent =
            "Thank you! Your message has been sent successfully.";

        this.reset();
    }
});

function setError(input, message) {
    input.nextElementSibling.textContent = message;
}
