// Function to check the Test API button state when the page loads
document.addEventListener('DOMContentLoaded', function() {
    smap_checkTestButton();
});

function smap_updateHiddenFields() {
    const apiKey = smap_sanitizeInput(document.getElementById('smap_api_key').value.trim());
    const phoneNumber = smap_sanitizeInput(document.getElementById('smap_test_phone').value.trim());
    
    document.getElementById('smap_api_key_hidden').value = apiKey;
    document.getElementById('smap_test_phone_hidden').value = phoneNumber;
    smap_checkTestButton();
}

// Basic sanitization function to remove potentially harmful characters
function smap_sanitizeInput(input) {
    return input.replace(/[<>]/g, ''); // Remove any < or > symbols to avoid script injections
}

function smap_checkTestButton() {
    const apiKey = smap_sanitizeInput(document.getElementById('smap_api_key_hidden').value.trim());
    const phoneNumber = smap_sanitizeInput(document.getElementById('smap_test_phone_hidden').value.trim());
    const testButton = document.getElementById('smap_test_api');

    if (apiKey && phoneNumber) {
        testButton.disabled = false;
    } else {
        testButton.disabled = true;
    }
}

// Function to validate the form before submission
function smap_validateForm() {
    const apiKey = smap_sanitizeInput(document.getElementById('smap_api_key').value.trim());
    const phoneNumber = smap_sanitizeInput(document.getElementById('smap_test_phone').value.trim());

    // You can add additional validation checks here if needed
    // For example, to validate if the phone number is in a valid format:
    const phoneNumberRegex = /^[0-9+\- ]+$/; // Adjust this regex according to your needs

    if (apiKey && phoneNumberRegex.test(phoneNumber)) {
        return true;
    } else {
        alert('Please fill in all required fields and ensure the phone number is valid before submitting.');
        return false;
    }
}
