/** * HVAC Organizers JavaScript * * @package HVAC_Community_Events * @version 2.0.0 */ jQuery(document).ready(function($) { // Cache DOM elements const $organizerForm = $('#hvac-organizer-form'); const $uploadButton = $('#hvac-upload-logo'); const $removeButton = $('#hvac-remove-logo'); const $logoIdField = $('#org_logo_id'); const $currentLogo = $('.hvac-current-logo'); const $deleteButton = $('#hvac-delete-organizer'); // Form validation function validateOrganizerForm() { let isValid = true; const errors = []; // Clear previous errors $('.hvac-form-error').removeClass('hvac-form-error'); $('.hvac-error-message').remove(); // Required fields const requiredFields = [ { id: 'org_name', label: 'Organization Name' }, { id: 'hq_city', label: 'Headquarters City' }, { id: 'hq_state', label: 'Headquarters State/Province' }, { id: 'hq_country', label: 'Headquarters Country' } ]; requiredFields.forEach(field => { const $field = $('#' + field.id); const value = $field.val(); if (!value || value.trim() === '') { isValid = false; errors.push(field.label + ' is required'); $field.addClass('hvac-form-error'); $field.after(''); } }); // Validate email if provided const email = $('#org_email').val(); if (email && !isValidEmail(email)) { isValid = false; errors.push('Please enter a valid email address'); $('#org_email').addClass('hvac-form-error'); $('#org_email').after(''); } // Validate phone format if provided const phone = $('#org_phone').val(); if (phone && !isValidPhone(phone)) { isValid = false; errors.push('Please enter a valid phone number'); $('#org_phone').addClass('hvac-form-error'); $('#org_phone').after(''); } // Validate website URL if provided const website = $('#org_website').val(); if (website && !isValidURL(website)) { isValid = false; errors.push('Please enter a valid website URL'); $('#org_website').addClass('hvac-form-error'); $('#org_website').after(''); } return isValid; } // Email validation function isValidEmail(email) { const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return re.test(email); } // Phone validation function isValidPhone(phone) { const digits = phone.replace(/\D/g, ''); return digits.length >= 10 && digits.length <= 15; } // URL validation function isValidURL(url) { try { new URL(url); return true; } catch (_) { return false; } } // Show message function showMessage(message, type = 'success') { const messageClass = type === 'success' ? 'hvac-message-success' : 'hvac-message-error'; const $message = $('
'); // Remove any existing messages $('.hvac-message').remove(); // Add new message $('.hvac-page-header').after($message); // Auto-hide success messages after 5 seconds if (type === 'success') { setTimeout(function() { $message.fadeOut(function() { $(this).remove(); }); }, 5000); } // Scroll to top $('html, body').animate({ scrollTop: $('.hvac-page-header').offset().top - 100 }, 300); } // Handle organizer form submission if ($organizerForm.length) { $organizerForm.on('submit', function(e) { e.preventDefault(); // Validate form if (!validateOrganizerForm()) { return false; } // Disable submit button const $submitButton = $organizerForm.find('button[type="submit"]'); const originalText = $submitButton.text(); $submitButton.prop('disabled', true).text('Saving...'); // Gather form data const formData = { action: 'hvac_save_organizer', nonce: hvacOrganizers.nonce, organizer_id: $('input[name="organizer_id"]').val(), org_name: $('#org_name').val(), org_description: $('#org_description').val(), hq_city: $('#hq_city').val(), hq_state: $('#hq_state').val(), hq_country: $('#hq_country').val(), org_phone: $('#org_phone').val(), org_email: $('#org_email').val(), org_website: $('#org_website').val(), org_logo_id: $('#org_logo_id').val() }; // Send AJAX request $.ajax({ url: hvacOrganizers.ajax_url, type: 'POST', data: formData, success: function(response) { if (response.success) { showMessage(response.data.message, 'success'); // If creating new organizer, update form to edit mode if (!formData.organizer_id && response.data.organizer_id) { $('input[name="organizer_id"]').val(response.data.organizer_id); $('.hvac-page-header h1').text('Edit Organizer'); $('.hvac-breadcrumb').html('Trainer > Organizers > Edit'); // Add delete button if not present if (!$('#hvac-delete-organizer').length) { const deleteButton = ''; $('.hvac-form-actions').append(deleteButton); } } // Update button text $submitButton.text('Update Organizer'); } else { showMessage(response.data || 'An error occurred while saving the organizer.', 'error'); } }, error: function() { showMessage('An error occurred. Please try again.', 'error'); }, complete: function() { // Re-enable submit button $submitButton.prop('disabled', false).text(originalText); } }); }); } // Handle logo upload if ($uploadButton.length) { let mediaUploader; $uploadButton.on('click', function(e) { e.preventDefault(); // If the media uploader already exists, open it if (mediaUploader) { mediaUploader.open(); return; } // Create the media uploader mediaUploader = wp.media({ title: 'Choose Organization Logo', button: { text: 'Use this logo' }, multiple: false, library: { type: 'image' } }); // When an image is selected, run a callback mediaUploader.on('select', function() { const attachment = mediaUploader.state().get('selection').first().toJSON(); // Update the logo preview $currentLogo.html('