/** * HVAC Trainer Profile JavaScript * * @package HVAC_Community_Events * @version 2.0.0 */ jQuery(document).ready(function($) { // Cache DOM elements const $profileForm = $('#hvac-profile-form'); const $uploadButton = $('#hvac-upload-photo'); const $removeButton = $('#hvac-remove-photo'); const $photoIdField = $('#profile_photo_id'); const $currentPhoto = $('.hvac-current-photo'); // Form validation function validateProfileForm() { let isValid = true; const errors = []; // Clear previous errors $('.hvac-form-error').removeClass('hvac-form-error'); $('.hvac-error-message').remove(); // Required fields const requiredFields = [ { id: 'first_name', label: 'First Name' }, { id: 'last_name', label: 'Last Name' }, { id: 'display_name', label: 'Display Name' }, { id: 'email', label: 'Email Address' } ]; 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 const email = $('#email').val(); if (email && !isValidEmail(email)) { isValid = false; errors.push('Please enter a valid email address'); $('#email').addClass('hvac-form-error'); $('#email').after(''); } // Validate phone format if provided const phone = $('#phone').val(); if (phone && !isValidPhone(phone)) { isValid = false; errors.push('Please enter a valid phone number'); $('#phone').addClass('hvac-form-error'); $('#phone').after(''); } // Validate URLs if provided const website = $('#website').val(); if (website && !isValidURL(website)) { isValid = false; errors.push('Please enter a valid website URL'); $('#website').addClass('hvac-form-error'); $('#website').after(''); } const linkedin = $('#linkedin').val(); if (linkedin && !isValidURL(linkedin)) { isValid = false; errors.push('Please enter a valid LinkedIn URL'); $('#linkedin').addClass('hvac-form-error'); $('#linkedin').after(''); } // Validate years of experience const years = $('#years_experience').val(); if (years && (years < 0 || years > 50)) { isValid = false; errors.push('Years of experience must be between 0 and 50'); $('#years_experience').addClass('hvac-form-error'); $('#years_experience').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 profile form submission if ($profileForm.length) { $profileForm.on('submit', function(e) { e.preventDefault(); // Validate form if (!validateProfileForm()) { return false; } // Disable submit button const $submitButton = $profileForm.find('button[type="submit"]'); const originalText = $submitButton.text(); $submitButton.prop('disabled', true).text('Saving...'); // Gather form data const formData = { action: 'hvac_update_profile', nonce: hvacProfile.nonce, first_name: $('#first_name').val(), last_name: $('#last_name').val(), display_name: $('#display_name').val(), email: $('#email').val(), phone: $('#phone').val(), description: $('#description').val(), city: $('#city').val(), state: $('#state').val(), country: $('#country').val(), years_experience: $('#years_experience').val(), certifications: $('#certifications').val(), website: $('#website').val(), linkedin: $('#linkedin').val(), profile_photo_id: $('#profile_photo_id').val() }; // Send AJAX request $.ajax({ url: hvacProfile.ajax_url, type: 'POST', data: formData, success: function(response) { if (response.success) { showMessage(response.data || 'Profile updated successfully.', 'success'); } else { showMessage(response.data || 'An error occurred while updating your profile.', 'error'); } }, error: function() { showMessage('An error occurred. Please try again.', 'error'); }, complete: function() { // Re-enable submit button $submitButton.prop('disabled', false).text(originalText); } }); }); } // Handle photo 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 Profile Photo', button: { text: 'Use this photo' }, 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 photo preview $currentPhoto.html('