<?php
defined( 'ABSPATH' ) || exit;
$avi_lang = 'en';
if ( function_exists( 'avi_get_current_lang' ) ) { $avi_lang = avi_get_current_lang(); }
if ( $avi_lang === 'en' && isset( $_SERVER['REQUEST_URI'] ) ) {
    $req = $_SERVER['REQUEST_URI'];
    if ( strpos( $req, '/fr/' ) === 0 ) { $avi_lang = 'fr'; }
    elseif ( strpos( $req, '/es/' ) === 0 ) { $avi_lang = 'es'; }
}
if ( $avi_lang !== 'en' ) { set_query_var( 'avi_lang', $avi_lang ); }
$is_fr = ( $avi_lang === 'fr' );
$is_es = ( $avi_lang === 'es' );

$avi_meta_title = function_exists( 'avi_get_content' )
	? avi_get_content( 'nonprofits', 'meta_title', 'Nonprofits — Avision' )
	: 'Nonprofits — Avision';
$avi_meta_desc  = function_exists( 'avi_get_content' )
	? avi_get_content( 'nonprofits', 'meta_desc', 'Support for nonprofits that rely on local visibility to serve their communities.' )
	: 'Support for nonprofits that rely on local visibility to serve their communities.';

add_action(
	'wp_enqueue_scripts',
	function () {
		wp_enqueue_style(
			'avi-np-caveat',
			'https://fonts.googleapis.com/css2?family=Caveat:wght@500;600&display=swap',
			array(),
			null
		);
	},
	20
);

avi_get_layout( 'header' );
?>

<?php
avi_component(
	'hero',
	array(
		'badge'       => 'Nonprofit Program',
		'title'       => function_exists( 'avi_get_content' ) ? avi_get_content( 'nonprofits', 'hero_title', 'Free access for nonprofits and associations.' ) : 'Free access for nonprofits and associations.',
		'subtitle'    => function_exists( 'avi_get_content' ) ? avi_get_content( 'nonprofits', 'hero_sub', 'AVI Access is completely free for registered nonprofits and associations. Get 50% off premium options — and up to 100% free as you grow the Avision community.' ) : 'AVI Access is completely free for registered nonprofits and associations. Get 50% off premium options — and up to 100% free as you grow the Avision community.',
		'align' => 'center',
		'size'  => 'small',
	)
);
?>

<div style="text-align:center;padding:8px 0 4px;margin-bottom:8px;">
<span style="font-size:56px;line-height:1;display:inline-block;animation:avi-np-handshake 2s ease-in-out infinite;">🤝</span>
</div>
<style>
@keyframes avi-np-handshake {
  0%,100% { transform: scale(1); }
  50%      { transform: scale(1.08); }
}
</style>

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Caveat:wght@500;600&display=swap" rel="stylesheet">
<div style="text-align:center;max-width:600px;margin:0 auto 32px;padding:0 24px;">
<p style="font-family:'Caveat','Segoe Script','Comic Sans MS','Bradley Hand',cursive;font-size:22px;font-weight:500;color:#cbd5e1;line-height:1.7;margin:0 0 12px;font-style:normal;">
"Every organization serving a community deserves to be seen. This program is my commitment to you."
</p>
<p style="font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;font-style:normal;font-size:13px;font-weight:600;color:#DAA520;margin:0;">
— Anthony, founder of Avision
</p>
</div>

<section class="avi-content-section">
	<div class="avi-content-block">
		<?php
		$avi_body_override = function_exists( 'avi_get_content' ) ? (string) avi_get_content( 'nonprofits', 'body', '' ) : '';
		if ( $avi_body_override !== '' ) {
			echo wp_kses_post( $avi_body_override );
		} else {
			?>
			<!-- Bloc offre principale -->
			<div style="background:rgba(108,63,197,0.08);border-left:4px solid #6C3FC5;border-radius:12px;padding:32px 36px;margin-bottom:32px;">

			<h2 style="margin-top:0;color:#fff;font-size:22px;">
			AVI Access — free for nonprofits and associations
			</h2>

			<p style="color:#cbd5e1;line-height:1.8;">
			If your organization serves a community locally — charity, association, foundation, social enterprise — your Google Business Profile matters as much as any commercial business. People search for you. Supporters look you up. Beneficiaries need to find you.
			</p>

			<p style="color:#cbd5e1;line-height:1.8;">
			AVI Access gives your organization a full visibility audit, a score /100, a guided action plan, and AVI Intelligence — completely free. No credit card. No time limit.
			</p>

			<ul style="color:#cbd5e1;list-style:none;padding:0;margin:20px 0;line-height:2;">
			<li>✅ Full local visibility audit</li>
			<li>✅ GBP optimization report</li>
			<li>✅ AVI Intelligence — full access</li>
			<li>✅ Visibility score /100 + priority actions</li>
			<li>✅ Growth Radar — 20 keywords monthly</li>
			<li>✅ Guided progression system</li>
			<li>✅ Google Updates feed</li>
			</ul>

			</div>

			<div style="background:linear-gradient(135deg,rgba(108,63,197,0.12) 0%,rgba(218,165,32,0.08) 100%);border:1px solid rgba(218,165,32,0.25);border-radius:12px;padding:28px 32px;margin-bottom:32px;text-align:center;">
			<p style="color:#e2e8f0;font-size:18px;font-weight:600;line-height:1.6;margin:0 0 8px;">
			Behind every Google search,<br>
			someone needs you.
			</p>
			<p style="color:#DAA520;font-size:20px;font-weight:700;line-height:1.4;margin:0 0 24px;">
			Your organization deserves to be found.
			</p>
			<a href="#avi-np-form-wrap" onclick="document.getElementById('avi-np-form-wrap').scrollIntoView({behavior:'smooth'});return false;" style="display:inline-block;background:linear-gradient(135deg,#B8860B,#DAA520);color:#0a1628;font-weight:700;padding:14px 32px;border-radius:10px;font-size:16px;text-decoration:none;box-shadow:0 4px 20px rgba(218,165,32,0.3);transition:opacity 0.2s;">
			Join the program for free →
			</a>
			</div>

			<!-- Bloc addons 50% -->
			<div style="background:#0f1e35;border-radius:12px;padding:32px 36px;margin-bottom:32px;">

			<h2 style="margin-top:0;color:#fff;font-size:22px;">
			Premium options — from 50% to 100% off
			</h2>

			<p style="color:#cbd5e1;line-height:1.8;">
			When your organization wants to go further, our premium options (automation, position tracking, profile protection) are available at 50% off the standard price — with no obligation. Your dashboard access remains 100% free, with or without premium options.
			</p>

			<p style="color:#cbd5e1;line-height:1.8;">
			The more local businesses you refer to Avision, the higher your discount — up to 100% free for the most active ambassadors.
			</p>

			<p style="color:#cbd5e1;line-height:1.8;">
			And for every local business you refer, your organization receives 50% of the revenue generated as a donation.
			</p>

			<div style="background:rgba(218,165,32,0.06);border:1px solid rgba(218,165,32,0.2);border-radius:10px;padding:20px 24px;margin-top:20px;">
			<p style="margin:0 0 12px;color:#DAA520;font-weight:600;font-size:14px;">
			💡 The more you refer, the more you earn:
			</p>
			<ul style="color:#cbd5e1;list-style:none;padding:0;margin:0;line-height:2.2;font-family:monospace;font-size:13px;">
			<li>1&nbsp;&nbsp;referral&nbsp;&nbsp;× $69 = $69&nbsp;&nbsp;&nbsp;&nbsp;→ $34 donated + 70% off options</li>
			<li>11 referrals × $69 = $759&nbsp;&nbsp;→ $379 donated + 80% off options</li>
			<li>21 referrals × $69 = $1,449 → $724 donated + 90% off options</li>
			<li>30 referrals × $69 = $2,070 → $1,035 donated + 100% free options 🎉</li>
			</ul>
			<p style="color:#64748b;font-size:12px;margin:12px 0 0;">
			Each referral generates a one-time donation (50% of the purchase price). The more businesses you refer, the more your organization receives — and the more your premium discount grows.<br><br><em>Your dashboard is your most powerful tool for local visibility. Share it. Grow with it.</em>
			</p>

			<!-- Engagement partage réseau -->
			<div style="background:rgba(218,165,32,0.06);border:1px solid rgba(218,165,32,0.15);border-radius:10px;padding:20px 24px;margin-top:20px;">
			<p style="color:#DAA520;font-weight:700;font-size:14px;margin:0 0 8px;">
			📣 Network sharing commitment
			</p>
			<p style="color:#94a3b8;font-size:13px;line-height:1.7;margin:0 0 12px;">
			As an Avision Ambassador, we ask that you share one post about Avision on your networks (Facebook, Instagram, LinkedIn or any active channel) <strong style="color:#e2e8f0;">twice a year</strong>. This keeps your organization active and maintains your premium discount.
			</p>
			<p style="color:#64748b;font-size:12px;margin:0;">
			Submit your proof via your dashboard once logged in — or email a link to <a href="mailto:contact@avision.org" style="color:#DAA520;">contact@avision.org</a>
			</p>
			</div>
			</div>

			</div>

			<div style="background:#0f1e35;border-radius:12px;padding:32px 36px;margin-bottom:32px;">
			<h2 style="margin-top:0;color:#fff;font-size:20px;">
			How to access your free account
			</h2>
			<p style="color:#cbd5e1;line-height:1.8;">
			Fill in the form below with your organization's official registration number. We verify your status and activate your free access within 48 hours.
			</p>
			</div>

			<!-- Bloc formulaire candidature -->
			<div id="avi-np-form-wrap" class="avi-nonprofit-page" style="background:#0f1e35;border-radius:12px;padding:32px 36px;margin-bottom:32px;">

			<h2 style="margin-top:0;color:#fff;font-size:20px;">
			Apply for free nonprofit access
			</h2>

			<p style="color:#cbd5e1;line-height:1.8;margin-bottom:24px;">
			Fill in the form below. We verify your nonprofit status and send your access within 48 hours.
			</p>

			<div id="avi-np-form-fields">

			<div style="margin-bottom:16px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			Organization name *
			</label>
			<input type="text" id="avi-np-org-name" placeholder="Red Cross London Chapter" style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<div style="margin-bottom:16px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			Country *
			</label>
			<select id="avi-np-country" style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			<option value="">— Select your country —</option>
			<option value="US">🇺🇸 United States (EIN)</option>
			<option value="CA">🇨🇦 Canada (CRA)</option>
			<option value="GB">🇬🇧 United Kingdom (Charity / CIC)</option>
			<option value="FR">🇫🇷 France (RNA)</option>
			<option value="AU">🇦🇺 Australia (ABN)</option>
			<option value="ES">🇪🇸 Spain</option>
			<option value="DE">🇩🇪 Germany</option>
			<option value="IT">🇮🇹 Italy</option>
			<option value="NL">🇳🇱 Netherlands</option>
			<option value="BE">🇧🇪 Belgium</option>
			<option value="CH">🇨🇭 Switzerland</option>
			<option value="PT">🇵🇹 Portugal</option>
			<option value="SE">🇸🇪 Sweden</option>
			<option value="NO">🇳🇴 Norway</option>
			<option value="DK">🇩🇰 Denmark</option>
			<option value="OTHER">🌍 Other country</option>
			</select>
			<p style="font-size:12px;color:#64748b;margin:6px 0 0;line-height:1.5;">
			⏳ Verification within 48 hours after your account is created.
			</p>
			<div id="avi-np-other-country-wrap" style="display:none;margin-top:10px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;" id="avi-np-other-country-label">
			Specify your country *
			</label>
			<input type="text" id="avi-np-other-country" placeholder="e.g. Brazil, Japan, Mexico..." style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			<p style="font-size:11px;color:#64748b;margin:6px 0 0;">
			Your country will be added to our verification list.
			</p>
			</div>
			</div>

			<div style="margin-bottom:16px;" id="avi-np-number-wrap">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;" id="avi-np-number-label">
			Registration number *
			<span style="color:#64748b;font-size:11px;" id="avi-np-number-hint"></span>
			</label>
			<input type="text" id="avi-np-org-number" placeholder="EIN, charity number, RNA..." style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<div style="margin-bottom:16px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			Google Business Profile name *
			</label>
			<input type="text" id="avi-np-gbp-name" placeholder="Exact name as it appears on Google Maps" style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<div style="margin-bottom:16px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			Organization website
			</label>
			<input type="url" id="avi-np-website" placeholder="https://yourorganization.org" style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<div style="margin-bottom:16px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			Contact email *
			</label>
			<input type="email" id="avi-np-email" placeholder="contact@yourorganization.org" style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<div style="margin-bottom:16px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			City *
			</label>
			<input type="text" id="avi-np-city" placeholder="London" style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<div style="margin-bottom:16px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			Neighborhood / District *
			</label>
			<input type="text" id="avi-np-neighborhood" placeholder="Westminster, Shoreditch..." style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<div style="margin-bottom:16px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			Password * (min. 8 characters)
			</label>
			<input type="password" id="avi-np-password" placeholder="••••••••" style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<div style="margin-bottom:20px;">
			<label style="color:#94a3b8;font-size:13px;display:block;margin-bottom:6px;">
			Confirm password *
			</label>
			<input type="password" id="avi-np-password-confirm" placeholder="••••••••" style="width:100%;padding:10px 14px;border-radius:8px;border:1px solid #1e3a5f;background:#0a1628;color:#fff;font-size:14px;box-sizing:border-box;">
			</div>

			<?php
			$avi_lang       = 'en';
			$avi_terms_mode = 'nonprofit';
			include __DIR__ . '/../partials/avi-terms-checkbox.php';
			?>

			<input type="text" id="avi-np-hp" name="avi-np-hp" value="" tabindex="-1" autocomplete="off" style="position:absolute;left:-9999px;opacity:0;height:0;width:0;" aria-hidden="true">

			<div id="avi-np-form-error" style="display:none;color:#f87171;font-size:13px;margin-bottom:12px;padding:10px 14px;background:rgba(248,113,113,0.08);border-radius:8px;"></div>

			<button type="button" id="avi-np-submit" style="background:linear-gradient(135deg,#B8860B,#DAA520);color:#0a1628;font-weight:700;padding:12px 28px;border:none;border-radius:8px;font-size:15px;cursor:pointer;width:100%;">
			Submit my application →
			</button>

			<p style="color:#64748b;font-size:12px;margin-top:12px;text-align:center;">
			No credit card required. Verification within 48 hours.
			</p>
			</div>

			<div id="avi-np-form-success" style="display:none;text-align:center;padding:24px 0;">
			<div style="font-size:48px;margin-bottom:12px;">✅</div>
			<h3 style="color:#fff;margin-bottom:8px;">
			Application received!
			</h3>
			<p style="color:#cbd5e1;line-height:1.8;">
			We've sent you an email with the link to connect your Google Business Profile and complete your registration.
			</p>
			<p style="color:#cbd5e1;line-height:1.8;">
			Check your inbox — the email comes from <strong>no-reply@avision.org</strong>.
			</p>
			<p style="color:#64748b;font-size:13px;margin-top:8px;">
			Don't have a Google Business Profile yet? <a href="https://business.google.com/create" target="_blank" rel="noopener" style="color:#DAA520;">Create one here</a> then use the link in your email to continue.
			</p>
			</div>

			</div>

			<!-- Section GBP manquante -->
			<div class="avi-nonprofit-page" style="background:rgba(184,134,11,0.08);border-left:4px solid #DAA520;border-radius:12px;padding:24px 28px;margin-bottom:32px;">

			<h3 style="margin-top:0;color:#DAA520;font-size:16px;">
			📍 Don't have a Google Business Profile yet?
			</h3>

			<p style="color:#cbd5e1;line-height:1.8;margin-bottom:16px;">
			Avision works with your existing Google Business Profile. If your organization isn't on Google Maps yet, you'll need to create your profile first — it's free and takes about 10 minutes.
			</p>

			<a href="https://business.google.com/create" target="_blank" rel="noopener" style="display:inline-block;background:transparent;border:1px solid #DAA520;color:#DAA520;padding:10px 20px;border-radius:8px;font-size:14px;text-decoration:none;font-weight:600;">
			Create your Google Business Profile →
			</a>

			<p style="color:#64748b;font-size:13px;margin-top:12px;margin-bottom:0;">
			Once your profile is live (usually 3–7 days after verification), come back here to complete your nonprofit registration.
			</p>

			</div>

			<!-- Bloc éligibilité -->
			<div class="avi-info-card" style="margin-top:0;">
			<p style="margin:0;color:#94a3b8;font-size:14px;">
			<strong style="color:#fff;">Who qualifies:</strong> registered charities, nonprofits, associations, foundations, NGOs, social enterprises, community organizations, religious organizations, animal shelters, food banks, hospices, youth organizations, mutual aid societies, cooperatives, and CICs (UK). No revenue threshold — if you serve a community, you qualify. Verification via official register.
			</p>
			</div>
			<?php
		}
		?>
	</div>
</section>

<script>
(function() {
	var countryHints = {
		'US':    'EIN (e.g. 12-3456789)',
		'CA':    'CRA number (9 digits)',
		'GB':    'Charity number (6-7 digits) or CIC number',
		'FR':    'RNA number (e.g. W751234567)',
		'AU':    'ABN (11 digits)',
		'ES':    'NIF / CIF (e.g. G12345678)',
		'DE':    'Vereinsregisternummer',
		'IT':    'Codice Fiscale',
		'NL':    'KvK number',
		'BE':    'Numéro d\'entreprise (BCE)',
		'CH':    'UID / IDE (CHE-xxx.xxx.xxx)',
		'PT':    'NIF (9 digits)',
		'SE':    'Organisationsnummer',
		'NO':    'Organisasjonsnummer',
		'DK':    'CVR number',
		'OTHER': 'Official registration number'
	};

	var sel = document.getElementById('avi-np-country');
	if (sel) {
		sel.addEventListener('change', function() {
			var hint = document.getElementById('avi-np-number-hint');
			if (hint) {
				hint.textContent = countryHints[this.value]
					? '(' + countryHints[this.value] + ')' : '';
			}
			var otherWrap = document.getElementById('avi-np-other-country-wrap');
			if (otherWrap) {
				otherWrap.style.display =
					this.value === 'OTHER' ? 'block' : 'none';
			}
		});
	}

	var submitLabel  = 'Submit my application →';
	var sendingLabel = 'Creating your account...';
	var submitErrors = {
		required: 'Please fill in all required fields.',
		email: 'Please enter a valid email address.',
		password_short: 'Password must be at least 8 characters.',
		password_mismatch: 'Passwords do not match.',
		email_exists: 'An account already exists with this email.',
		generic: 'An error occurred. Please try again.',
		connection: 'Connection error. Please try again.'
	};
	var btn = document.getElementById('avi-np-submit');
	if (!btn) return;

	function showNpError(msg) {
		var box = document.getElementById('avi-np-form-error');
		if (box) {
			box.textContent = msg;
			box.style.display = 'block';
		}
	}

	btn.addEventListener('click', function() {
		var orgName  = (document.getElementById('avi-np-org-name').value || '').trim();
		var country  = (document.getElementById('avi-np-country').value || '').trim();
		var otherCountry = '';
		if (country === 'OTHER') {
			otherCountry = (document.getElementById('avi-np-other-country') || {}).value || '';
			otherCountry = otherCountry.trim();
		}
		var orgNum   = (document.getElementById('avi-np-org-number').value || '').trim();
		var gbpName  = (document.getElementById('avi-np-gbp-name').value || '').trim();
		var website  = (document.getElementById('avi-np-website').value || '').trim();
		var email    = (document.getElementById('avi-np-email').value || '').trim();
		var city     = (document.getElementById('avi-np-city').value || '').trim();
		var neighborhood = (document.getElementById('avi-np-neighborhood').value || '').trim();
		var password = (document.getElementById('avi-np-password').value || '');
		var passwordConfirm = (document.getElementById('avi-np-password-confirm').value || '');
		var errBox   = document.getElementById('avi-np-form-error');
		var hp       = (document.getElementById('avi-np-hp') || {}).value || '';

		errBox.style.display = 'none';

		if (hp !== '') {
			document.getElementById('avi-np-form-fields').style.display = 'none';
			document.getElementById('avi-np-form-success').style.display = 'block';
			return;
		}

		if (!orgName || !country || !orgNum || !gbpName || !email || !city || !neighborhood || !password) {
			errBox.textContent = submitErrors.required;
			errBox.style.display = 'block';
			return;
		}
		if (country === 'OTHER' && !otherCountry) {
			errBox.textContent = submitErrors.required;
			errBox.style.display = 'block';
			return;
		}
		if (email.indexOf('@') < 0) {
			errBox.textContent = submitErrors.email;
			errBox.style.display = 'block';
			return;
		}
		if (password.length < 8) {
			errBox.textContent = submitErrors.password_short;
			errBox.style.display = 'block';
			return;
		}
		if (password !== passwordConfirm) {
			errBox.textContent = submitErrors.password_mismatch;
			errBox.style.display = 'block';
			return;
		}
		if (!document.getElementById('avi_accept_terms') || !document.getElementById('avi_accept_terms').checked) {
			showNpError('Please accept the Terms & Conditions.');
			return;
		}
		if (!document.getElementById('avi_accept_ambassador') || !document.getElementById('avi_accept_ambassador').checked) {
			showNpError('Please accept the Ambassador Program rules.');
			return;
		}

		btn.disabled = true;
		btn.textContent = sendingLabel;

		var fd = new FormData();
		fd.append('action', 'avision_nonprofit_apply');
		fd.append('nonce', '<?php echo esc_js( wp_create_nonce( 'avision_nonprofit_apply' ) ); ?>');
		fd.append('org_name', orgName);
		fd.append('country', country);
		fd.append('other_country', otherCountry);
		fd.append('org_number', orgNum);
		fd.append('gbp_name', gbpName);
		fd.append('website', website);
		fd.append('email', email);
		fd.append('lang', '<?php echo esc_js( $avi_lang ); ?>');
		fd.append('hp', hp);

		fetch('<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>', { method: 'POST', body: fd })
			.then(function(r) { return r.json(); })
			.then(function(res1) {
				var npId = (res1.success && res1.data && res1.data.np_id) ? res1.data.np_id : 0;
				return fetch('<?php echo esc_js( rest_url( 'avision/v1/nonprofit-register' ) ); ?>', {
					method: 'POST',
					headers: { 'Content-Type': 'application/json' },
					credentials: 'include',
					body: JSON.stringify({
						email: email,
						org_name: orgName,
						org_number: orgNum,
						country: country,
						city: city,
						neighborhood: neighborhood,
						gbp_name: gbpName,
						website: website,
						password: password,
						password_confirm: passwordConfirm,
						language: '<?php echo esc_js( $avi_lang ); ?>',
						np_id: npId
					})
				});
			})
			.then(function(r) { return r.json(); })
			.then(function(res2) {
				if (res2.success && res2.redirect) {
					window.location.href = res2.redirect;
					return;
				}
				var msg = res2.message || res2.error || '';
				if (msg === 'email_exists') {
					msg = submitErrors.email_exists;
				} else if (msg === 'password_mismatch') {
					msg = submitErrors.password_mismatch;
				} else if (msg === 'password_too_short') {
					msg = submitErrors.password_short;
				} else if (!msg) {
					msg = submitErrors.generic;
				}
				errBox.textContent = msg;
				errBox.style.display = 'block';
				btn.disabled = false;
				btn.textContent = submitLabel;
			})
			.catch(function() {
				errBox.textContent = submitErrors.connection;
				errBox.style.display = 'block';
				btn.disabled = false;
				btn.textContent = submitLabel;
			});
	});
})();
</script>

<?php $GLOBALS['avi_template_footer_rendered'] = true; avi_get_layout( 'footer' ); ?>

