jQuery(document).ready(function($) {
    // Add validation to Divi contact forms
    // Note: We flag suspicious submissions but don't hard-block them
    $('.et_pb_contact_form').on('submit', function(e) {
        var form = $(this);
        var isValid = true;
        
        // Check honeypot fields - only block if honeypot is filled (definite bot)
        form.find('input[name*="honeypot"]').each(function() {
            if ($(this).val() !== '') {
                // Honeypot filled - definitely a bot, block it
                isValid = false;
                e.preventDefault();
                return false;
            }
        });
        
        // Check for rapid submission (< 3 seconds) - flag but don't block
        var lastSubmission = localStorage.getItem('formshield_last_submission');
        var now = Date.now();
        if (lastSubmission && (now - parseInt(lastSubmission)) < 3000) {
            // Too fast - suspicious but let it pass (will be flagged server-side)
            // Don't prevent submission, just log it
        }
        
        // Store submission time for rate limiting check
        localStorage.setItem('formshield_last_submission', now.toString());
    });
    
    // Add badge to protected Divi forms
    if (typeof formshieldDiviData !== 'undefined') {
        $('.et_pb_contact_form').each(function() {
            var $form = $(this);
            var formId = $form.attr('id') || 'divi_form_' + Math.random();
            
            if (formshieldDiviData.protectedForms.indexOf(formId) !== -1) {
                var $existingBadge = $form.find('.formshield-protected-badge');
                if ($existingBadge.length === 0) {
                    $form.append(formshieldDiviData.badge);
                }
            }
        });
    }
});
