<?php
session_start();
include 'config/db.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    $mobile = trim($_POST['mobile']);
    $address = trim($_POST['address']);
    
    // Validation
    if (empty($name) || empty($email) || empty($password)) {
        $error = "Please fill all required fields";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = "Please enter a valid email address";
    } elseif (strlen($password) < 6) {
        $error = "Password must be at least 6 characters long";
    } elseif ($password !== $confirm_password) {
        $error = "Passwords do not match";
    } else {
        // Check if email already exists
        $check = $conn->prepare("SELECT id FROM users WHERE email = ?");
        $check->bind_param("s", $email);
        $check->execute();
        $check->store_result();
        
        if ($check->num_rows > 0) {
            $error = "Email already registered. Please login.";
        } else {
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            $stmt = $conn->prepare("INSERT INTO users (name, email, password, mobile, address, role, kyc_status, created_at) VALUES (?, ?, ?, ?, ?, 'user', 'pending', NOW())");
            $stmt->bind_param("sssss", $name, $email, $hashed_password, $mobile, $address);
            
            if ($stmt->execute()) {
                $user_id = $conn->insert_id;
                
                // Auto login after registration
                $_SESSION['user_id'] = $user_id;
                $_SESSION['name'] = $name;
                $_SESSION['email'] = $email;
                $_SESSION['role'] = 'user';
                $_SESSION['logged_in'] = true;
                $_SESSION['kyc_status'] = 'pending';
                
                $success = "Registration successful! Redirecting to your dashboard...";
                echo "<script>
                        setTimeout(function(){ 
                            window.location.href = 'user/dashboard.php'; 
                        }, 1500);
                      </script>";
            } else {
                $error = "Registration failed: " . $conn->error;
            }
        }
        $check->close();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register - BMYA SHELDANG</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: #f5f5f5;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 2rem;
        }

        /* Main container */
        .register-container {
            width: 100%;
            max-width: 480px;
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
            overflow: hidden;
            animation: fadeIn 0.5s ease-out;
            position: relative;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* Header */
        .register-header {
            background: white;
            padding: 2rem 2rem 1rem 2rem;
            text-align: center;
            border-bottom: 1px solid #eee;
        }

        .header-logo {
            width: 70px;
            height: 70px;
            object-fit: contain;
            margin-bottom: 1rem;
        }

        .register-header h1 {
            font-size: 1.5rem;
            font-weight: 600;
            color: #333;
            margin-bottom: 0.25rem;
        }

        .register-header p {
            font-size: 0.85rem;
            color: #888;
        }

        .home-btn {
            position: absolute;
            top: 20px;
            left: 20px;
            background: #f0f0f0;
            color: #666;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            transition: all 0.3s ease;
            font-size: 1rem;
            z-index: 10;
        }

        .home-btn:hover {
            background: #e0e0e0;
            transform: translateY(-2px);
        }

        .register-body {
            padding: 1.5rem 2rem 2rem 2rem;
        }

        .form-group {
            margin-bottom: 1.25rem;
            position: relative;
        }

        .form-group i {
            position: absolute;
            left: 14px;
            top: 50%;
            transform: translateY(-50%);
            color: #999;
            font-size: 1rem;
            pointer-events: none;
        }

        .form-group textarea + i {
            top: 18px;
            transform: none;
        }

        input, textarea {
            width: 100%;
            padding: 12px 15px 12px 42px;
            border: 1px solid #ddd;
            border-radius: 10px;
            font-size: 0.9rem;
            font-family: 'Poppins', sans-serif;
            transition: all 0.3s;
            outline: none;
            background: #fafafa;
        }

        textarea {
            padding-top: 12px;
            resize: vertical;
            min-height: 80px;
        }

        input:focus, textarea:focus {
            border-color: #2c5f2d;
            background: white;
            box-shadow: 0 0 0 3px rgba(44, 95, 45, 0.05);
        }

        .password-strength {
            margin-top: 0.35rem;
            font-size: 0.7rem;
        }

        .strength-text {
            color: #888;
        }

        .btn-register {
            width: 100%;
            background: #2c5f2d;
            color: white;
            border: none;
            padding: 12px;
            border-radius: 10px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            margin-bottom: 1rem;
        }

        .btn-register:hover {
            background: #1e4a1f;
            transform: translateY(-1px);
        }

        .login-link {
            text-align: center;
            padding-top: 1rem;
            border-top: 1px solid #eee;
        }

        .login-link p {
            color: #888;
            font-size: 0.85rem;
        }

        .login-link a {
            color: #2c5f2d;
            text-decoration: none;
            font-weight: 500;
        }

        .alert {
            padding: 12px 15px;
            border-radius: 10px;
            margin-bottom: 1.25rem;
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 0.85rem;
            animation: slideDown 0.3s ease-out;
        }

        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .alert-success {
            background: #e8f5e9;
            color: #2e7d32;
            border-left: 3px solid #4caf50;
        }

        .alert-error {
            background: #ffebee;
            color: #c62828;
            border-left: 3px solid #f44336;
        }

        .terms {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 1.25rem;
            font-size: 0.8rem;
        }

        .terms input {
            width: 16px;
            height: 16px;
            padding: 0;
            margin: 0;
            accent-color: #2c5f2d;
        }

        .loading-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.7);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            flex-direction: column;
            gap: 1rem;
        }

        .loading-overlay.active {
            display: flex;
        }

        .spinner {
            width: 50px;
            height: 50px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid #2c5f2d;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* ============== REGISTRATION NOTIFICATION STYLES ============== */
        .registration-notification {
            position: fixed;
            bottom: 30px;
            right: 30px;
            background: linear-gradient(135deg, #ff6b6b, #ee5a24);
            color: white;
            padding: 15px 20px;
            border-radius: 50px;
            display: none;
            align-items: center;
            gap: 12px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.2);
            z-index: 10000;
            animation: bounceIn 0.5s ease-out;
            cursor: pointer;
            transition: transform 0.3s;
        }

        .registration-notification:hover {
            transform: scale(1.05);
        }

        .registration-notification i:first-child {
            font-size: 1.5rem;
            animation: bellShake 1s infinite;
        }

        .registration-notification .notification-text {
            font-size: 0.9rem;
            font-weight: 500;
        }

        .registration-notification .notification-text small {
            font-size: 0.7rem;
            opacity: 0.9;
        }

        .registration-notification .close-noti {
            background: rgba(255,255,255,0.2);
            border: none;
            color: white;
            width: 28px;
            height: 28px;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: background 0.3s;
        }

        .registration-notification .close-noti:hover {
            background: rgba(255,255,255,0.4);
        }

        @keyframes bounceIn {
            from {
                opacity: 0;
                transform: translateX(100px);
            }
            to {
                opacity: 1;
                transform: translateX(0);
            }
        }

        @keyframes bellShake {
            0%, 100% { transform: rotate(0); }
            25% { transform: rotate(15deg); }
            75% { transform: rotate(-15deg); }
        }

        /* Audio Control Button */
        .audio-control {
            position: fixed;
            bottom: 30px;
            left: 30px;
            background: #2c5f2d;
            color: white;
            width: 50px;
            height: 50px;
            border-radius: 50%;
            display: none;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            box-shadow: 0 3px 10px rgba(0,0,0,0.2);
            z-index: 10000;
            transition: all 0.3s;
        }

        .audio-control:hover {
            transform: scale(1.1);
            background: #1e4a1f;
        }

        .audio-control.playing {
            background: #dc3545;
            animation: pulse 1s infinite;
        }

        @keyframes pulse {
            0% { box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.4); }
            70% { box-shadow: 0 0 0 10px rgba(220, 53, 69, 0); }
            100% { box-shadow: 0 0 0 0 rgba(220, 53, 69, 0); }
        }

        @media (max-width: 560px) {
            body {
                padding: 1rem;
            }
            
            .register-header {
                padding: 1.5rem 1.5rem 1rem 1.5rem;
            }
            
            .registration-notification {
                bottom: 20px;
                right: 20px;
                padding: 10px 15px;
            }
            
            .audio-control {
                bottom: 20px;
                left: 20px;
                width: 42px;
                height: 42px;
            }
        }
    </style>
</head>
<body>
    <div class="register-container">
        <a href="index.php" class="home-btn" title="Back to Home">
            <i class="fas fa-arrow-left"></i>
        </a>

        <div class="register-header">
            <img src="https://bmyouth.org/fund/assets/logo.png" 
                 alt="BMYA FUND Logo" 
                 class="header-logo"
                 onerror="this.src='data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 100 100%27%3E%3Ccircle cx=%2750%27 cy=%2750%27 r=%2745%27 fill=%27%232c5f2d%27/%3E%3Ctext x=%2750%27 y=%2770%27 text-anchor=%27middle%27 fill=%27white%27 font-size=%2736%27 font-weight=%27bold%27%3EB%3C/text%3E%3C/svg%3E'">
            <h1>Create Account</h1>
            <p>Join BMYA SHELDANG</p>
        </div>
        
        <div class="register-body">
            <?php if($error): ?>
                <div class="alert alert-error">
                    <i class="fas fa-exclamation-circle"></i>
                    <?= htmlspecialchars($error) ?>
                </div>
            <?php endif; ?>
            
            <?php if($success): ?>
                <div class="alert alert-success">
                    <i class="fas fa-check-circle"></i>
                    <?= htmlspecialchars($success) ?>
                </div>
            <?php endif; ?>
            
            <form method="POST" id="registerForm">
                <div class="form-group">
                    <i class="fas fa-user"></i>
                    <input type="text" name="name" id="name" placeholder="Full Name *" required 
                           value="<?= isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>">
                </div>
                
                <div class="form-group">
                    <i class="fas fa-envelope"></i>
                    <input type="email" name="email" id="email" placeholder="Email Address *" required
                           value="<?= isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>">
                </div>
                
                <div class="form-group">
                    <i class="fas fa-lock"></i>
                    <input type="password" name="password" id="password" placeholder="Password *" required>
                    <div class="password-strength">
                        <span class="strength-text" id="strengthText"></span>
                    </div>
                </div>
                
                <div class="form-group">
                    <i class="fas fa-lock"></i>
                    <input type="password" name="confirm_password" id="confirm_password" placeholder="Confirm Password *" required>
                </div>
                
                <div class="form-group">
                    <i class="fas fa-phone"></i>
                    <input type="tel" name="mobile" id="mobile" placeholder="Mobile Number (Required)"
                           value="<?= isset($_POST['mobile']) ? htmlspecialchars($_POST['mobile']) : '' ?>">
                </div>
                
                <div class="form-group">
                    <i class="fas fa-map-marker-alt"></i>
                    <textarea name="address" id="address" placeholder="Your Address (Required)"><?= isset($_POST['address']) ? htmlspecialchars($_POST['address']) : '' ?></textarea>
                </div>
                
                <div class="terms">
                    <input type="checkbox" id="terms" required>
                    <label for="terms">
                        I agree to the <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>
                    </label>
                </div>
                
                <button type="submit" class="btn-register" id="registerBtn">
                    <i class="fas fa-user-plus"></i>
                    Register
                </button>
            </form>
            
            <div class="login-link">
                <p>Already have an account? <a href="login.php">Sign In</a></p>
            </div>
        </div>
    </div>

    <!-- Loading Overlay -->
    <div id="loadingOverlay" class="loading-overlay">
        <div class="spinner"></div>
        <p>Registration successful! Redirecting to your dashboard...</p>
    </div>

    <!-- Registration Notification Popup -->
    <div id="registrationNotification" class="registration-notification">
        <i class="fas fa-id-card"></i>
        <div class="notification-text">
            <strong>⚠️ Registration Pending!</strong><br>
        </div>
        <div class="close-noti" onclick="event.stopPropagation(); closeRegistrationNotification()">
            <i class="fas fa-times"></i>
        </div>
    </div>

    <!-- Audio Control Button -->
    <div id="audioControl" class="audio-control">
        <i class="fas fa-volume-up"></i>
    </div>

    <!-- Hidden Audio Element - Using register-reminder.mp3 -->
    <audio id="reminderAudio" preload="auto" loop>
        <!-- Try multiple possible locations for register-reminder.mp3 -->
        <source src="../assets/audio/register-reminder.mp3" type="audio/mpeg">
        <source src="../assets/audio/register-reminder.mp3" type="audio/mpeg">
        <source src="audio/register-reminder.mp3" type="audio/mpeg">
        <source src="register-reminder.mp3" type="audio/mpeg">
    </audio>

    <script>
        // =============================================
        // REGISTRATION REMINDER AUDIO FUNCTIONALITY
        // Message: "आपका रजिस्ट्रेशन पूरा नहीं हुआ है कृपया पूरा करें"
        // =============================================
        
        // Get elements
        const audioElement = document.getElementById('reminderAudio');
        const audioControlBtn = document.getElementById('audioControl');
        const registrationNotificationDiv = document.getElementById('registrationNotification');
        
        let isAudioPlaying = false;
        let audioCheckInterval = null;
        let speechInterval = null;
        let fallbackMode = false;
        
        // ========== TEXT TO SPEECH FUNCTION (Hindi) ==========
        // Message: "आपका रजिस्ट्रेशन पूरा नहीं हुआ है कृपया पूरा करें"
        function speakReminderAlert() {
            if ('speechSynthesis' in window) {
                // Cancel any ongoing speech
                window.speechSynthesis.cancel();
                
                const utterance = new SpeechSynthesisUtterance('आपका रजिस्ट्रेशन पूरा नहीं हुआ है कृपया पूरा करें');
                utterance.lang = 'hi-IN';
                utterance.rate = 0.85;
                utterance.pitch = 1;
                utterance.volume = 1;
                
                // Try to get Hindi voice if available
                let voices = window.speechSynthesis.getVoices();
                let hindiVoice = voices.find(voice => voice.lang === 'hi-IN' || voice.lang === 'hi');
                if (hindiVoice) {
                    utterance.voice = hindiVoice;
                }
                
                window.speechSynthesis.speak(utterance);
            } else {
                console.log("Speech synthesis not supported");
            }
        }
        
        // ========== PLAY AUDIO FILE ==========
        function playAudioFile() {
            if (audioElement && audioElement.src) {
                audioElement.volume = 0.8;
                audioElement.loop = true;
                
                const playPromise = audioElement.play();
                if (playPromise !== undefined) {
                    playPromise.then(() => {
                        console.log("Audio playing successfully");
                        return true;
                    }).catch(error => {
                        console.log("Audio autoplay blocked:", error);
                        // Use speech synthesis as fallback
                        useSpeechFallback();
                        return false;
                    });
                }
            } else {
                useSpeechFallback();
            }
        }
        
        // ========== USE SPEECH SYNTHESIS FALLBACK ==========
        function useSpeechFallback() {
            if (fallbackMode) return;
            fallbackMode = true;
            console.log("Using speech synthesis fallback");
            
            // Speak immediately
            speakReminderAlert();
            
            // Repeat every 7 seconds
            if (speechInterval) clearInterval(speechInterval);
            speechInterval = setInterval(() => {
                if (isAudioPlaying && document.getElementById('registrationNotification') && 
                    document.getElementById('registrationNotification').style.display !== 'none') {
                    speakReminderAlert();
                } else if (!isAudioPlaying) {
                    clearInterval(speechInterval);
                    speechInterval = null;
                }
            }, 7000);
        }
        
        // ========== START REMINDER ==========
        function startReminder() {
            if (isAudioPlaying) return;
            
            isAudioPlaying = true;
            
            // Show audio control button
            audioControlBtn.style.display = 'flex';
            audioControlBtn.classList.add('playing');
            audioControlBtn.innerHTML = '<i class="fas fa-volume-mute"></i>';
            
            // Show notification
            registrationNotificationDiv.style.display = 'flex';
            
            // Try to play audio file
            playAudioFile();
        }
        
        // ========== STOP REMINDER ==========
        function stopReminder() {
            isAudioPlaying = false;
            fallbackMode = false;
            
            // Stop audio element
            if (audioElement) {
                audioElement.pause();
                audioElement.currentTime = 0;
            }
            
            // Stop speech synthesis
            if ('speechSynthesis' in window) {
                window.speechSynthesis.cancel();
            }
            
            // Clear speech interval
            if (speechInterval) {
                clearInterval(speechInterval);
                speechInterval = null;
            }
            
            // Update UI
            audioControlBtn.classList.remove('playing');
            audioControlBtn.innerHTML = '<i class="fas fa-volume-up"></i>';
            registrationNotificationDiv.style.display = 'none';
        }
        
        // ========== TOGGLE AUDIO ==========
        function toggleAudio() {
            if (isAudioPlaying) {
                stopReminder();
            } else {
                startReminder();
            }
        }
        
        // ========== CLOSE NOTIFICATION ==========
        function closeRegistrationNotification() {
            registrationNotificationDiv.style.display = 'none';
        }
        
        // ========== GO TO KYC PAGE ==========
        function goToKYC() {
            window.location.href = 'user/kyc_verification.php';
        }
        
        // ========== CHECK REGISTRATION STATUS ==========
        function checkRegistrationStatus() {
            // Simple check via session storage
            let registrationCompleted = sessionStorage.getItem('registration_completed');
            if (registrationCompleted === 'true') {
                stopReminder();
                if (audioCheckInterval) {
                    clearInterval(audioCheckInterval);
                    audioCheckInterval = null;
                }
            }
        }
        
        // ========== INITIALIZE AUDIO REMINDER ==========
        function initAudioReminder() {
            // Set session storage flag
            sessionStorage.setItem('registration_completed', 'false');
            
            // Start reminder after page load
            setTimeout(() => {
                startReminder();
            }, 1000);
            
            // Check registration status periodically
            audioCheckInterval = setInterval(checkRegistrationStatus, 5000);
        }
        
        // ========== LOAD VOICES AND SETUP ==========
        function setupVoices() {
            if ('speechSynthesis' in window) {
                // Load voices
                window.speechSynthesis.getVoices();
                // Some browsers need time to load voices
                if (window.speechSynthesis.onvoiceschanged !== undefined) {
                    window.speechSynthesis.onvoiceschanged = function() {
                        window.speechSynthesis.getVoices();
                    };
                }
            }
        }
        
        // ========== EVENT LISTENERS ==========
        document.addEventListener('DOMContentLoaded', function() {
            // Setup speech voices
            setupVoices();
            
            // Start reminder (since user just registered, KYC is pending)
            initAudioReminder();
            
            // Audio control button click
            audioControlBtn.addEventListener('click', toggleAudio);
            
            // Notification click - go to KYC
            registrationNotificationDiv.addEventListener('click', goToKYC);
            
            // Try to preload audio on user interaction (some browsers need this)
            document.body.addEventListener('click', function() {
                if (isAudioPlaying && audioElement && audioElement.paused && !fallbackMode) {
                    audioElement.play().catch(e => console.log("Playback still blocked"));
                }
            }, { once: true });
        });
        
        // ========== CLEANUP ON PAGE UNLOAD ==========
        window.addEventListener('beforeunload', function() {
            if (audioElement) {
                audioElement.pause();
            }
            if ('speechSynthesis' in window) {
                window.speechSynthesis.cancel();
            }
            if (speechInterval) {
                clearInterval(speechInterval);
            }
            if (audioCheckInterval) {
                clearInterval(audioCheckInterval);
            }
        });
        
        // Password strength checker
        const passwordInput = document.getElementById('password');
        const strengthText = document.getElementById('strengthText');
        
        function checkPasswordStrength(password) {
            let strength = 0;
            if (password.length >= 6) strength++;
            if (password.length >= 10) strength++;
            if (password.match(/[a-z]+/)) strength++;
            if (password.match(/[A-Z]+/)) strength++;
            if (password.match(/[0-9]+/)) strength++;
            if (password.match(/[$@#&!]+/)) strength++;
            
            if (password.length === 0) return '';
            if (strength <= 2) return 'Weak';
            if (strength <= 4) return 'Medium';
            return 'Strong';
        }
        
        if (passwordInput) {
            passwordInput.addEventListener('input', function() {
                const strength = checkPasswordStrength(this.value);
                if (strength === 'Weak') {
                    strengthText.innerHTML = '⚠️ Weak password';
                    strengthText.style.color = '#e74c3c';
                } else if (strength === 'Medium') {
                    strengthText.innerHTML = '👍 Medium strength';
                    strengthText.style.color = '#f39c12';
                } else if (strength === 'Strong') {
                    strengthText.innerHTML = '✅ Strong password';
                    strengthText.style.color = '#27ae60';
                } else {
                    strengthText.innerHTML = '';
                }
            });
        }
        
        // Form validation
        const nameInput = document.getElementById('name');
        const emailInput = document.getElementById('email');
        const confirmPassword = document.getElementById('confirm_password');
        
        if (nameInput) {
            nameInput.addEventListener('input', function() {
                if (this.value.length >= 3) {
                    this.style.borderColor = '#4caf50';
                } else if (this.value.length > 0) {
                    this.style.borderColor = '#f44336';
                } else {
                    this.style.borderColor = '#ddd';
                }
            });
        }
        
        if (emailInput) {
            emailInput.addEventListener('input', function() {
                const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
                if (emailRegex.test(this.value)) {
                    this.style.borderColor = '#4caf50';
                } else if (this.value.length > 0) {
                    this.style.borderColor = '#f44336';
                } else {
                    this.style.borderColor = '#ddd';
                }
            });
        }
        
        if (confirmPassword) {
            confirmPassword.addEventListener('input', function() {
                if (this.value === passwordInput.value && this.value !== '') {
                    this.style.borderColor = '#4caf50';
                } else if (this.value.length > 0) {
                    this.style.borderColor = '#f44336';
                } else {
                    this.style.borderColor = '#ddd';
                }
            });
        }
        
        // Form submission
        const registerForm = document.getElementById('registerForm');
        if (registerForm) {
            registerForm.addEventListener('submit', function(e) {
                const password = passwordInput.value;
                const confirm = confirmPassword.value;
                
                if (password !== confirm) {
                    e.preventDefault();
                    alert('Passwords do not match!');
                    return false;
                }
                
                if (password.length < 6) {
                    e.preventDefault();
                    alert('Password must be at least 6 characters long!');
                    return false;
                }
                
                if (!document.getElementById('terms').checked) {
                    e.preventDefault();
                    alert('Please accept the Terms and Conditions!');
                    return false;
                }
                
                const registerBtn = document.getElementById('registerBtn');
                registerBtn.innerHTML = '<i class="fas fa-spinner fa-pulse"></i> Registering...';
                registerBtn.disabled = true;
                
                return true;
            });
        }
        
        <?php if($success): ?>
        document.getElementById('loadingOverlay').classList.add('active');
        <?php endif; ?>
    </script>
</body>
</html>