${t('success.title')}
${esc(t('success.name',{name:'\u0000', program:name})).replace('\u0000', `${esc(L.name)}`)}
${esc(t('success.callOn',{phone:'\u0000'})).replace('\u0000', `${esc(prettyPhone(L.phone))}`)}
${ICON_WA}${t('success.wa')}
${t('success.home')}
`;
}
function wireSuccess(){
document.getElementById('regAnother').onclick = ()=>{
regDone = null; regState = { name:'', phone:'', d:'', m:'', y:'', place:'', notes:'' };
Object.keys(regErrors).forEach(k=> delete regErrors[k]); regSummary = []; regBanner = null;
document.getElementById('formCard').innerHTML = registrationFormHTML(); wireForm(program.id);
document.getElementById('regName').focus();
};
}
function updateRegCount(){
const ta = document.getElementById('regNotes'), c = document.getElementById('c_regNotes'); if(!ta||!c) return;
c.textContent = ta.value.length + ' / ' + ta.maxLength;
}
// The API answers in hardcoded Arabic: route what we can to the right field; everything else goes through
// shared.js's dictionary, and a non-Arabic visitor never sees a raw Arabic sentence.
function regServerError(status, raw){
raw = String(raw || '');
if(status === 429) return regBannerSet('err.rate');
if(status === 404) return regBannerSet('err.gone');
if(status >= 500) return regBannerSet('err.server');
const route = (key, errKey)=>{ regErrors[key] = errKey; regSummary = [key]; regPaint(key); regShowMessage(); regFocus(key); };
if(/رقم الهاتف/.test(raw)) return route('phone','err.phone');
if(/الاسم/.test(raw)) return route('name', /طويل/.test(raw) ? 'e.nameShort' : 'e.nameReq');
const msg = translateServerError(raw);
if(!msg || (CUR_LANG !== 'ar' && msg === raw)) return regBannerSet('err.generic');
regBannerSet('err.generic', msg);
}
function wireForm(programId){
const g = id=>document.getElementById(id);
populateDobSelects('regBirthDay','regBirthMonth','regBirthYear');
g('regName').value = regState.name; g('regPhone').value = regState.phone;
g('regBirthMonth').value = regState.m; g('regBirthYear').value = regState.y; refreshDobDays('regBirthDay','regBirthMonth','regBirthYear', regState.d);
g('regBirthPlace').value = regState.place; g('regNotes').value = regState.notes; updateRegCount();
Object.keys(regErrors).forEach(regPaint); regShowMessage();
const touched = {};
const live = (key, ids, evs)=> ids.forEach(id=> evs.forEach(ev=> g(id).addEventListener(ev, ()=>{
if(ev==='blur' || ev==='change') touched[key] = true;
if(touched[key]){ regCheck(key, true); if(regSummary.length) regShowMessage(); }
})));
const sync = ()=>{
regState.name = g('regName').value; regState.phone = g('regPhone').value; regState.place = g('regBirthPlace').value; regState.notes = g('regNotes').value;
regState.d = g('regBirthDay').value; regState.m = g('regBirthMonth').value; regState.y = g('regBirthYear').value;
saveRegDraft();
};
['regName','regPhone','regBirthPlace','regNotes','regBirthDay','regBirthMonth','regBirthYear'].forEach(id=> g(id).addEventListener('input', sync));
['regBirthDay','regBirthMonth','regBirthYear'].forEach(id=> g(id).addEventListener('change', sync));
g('regBirthMonth').addEventListener('change', ()=>{ refreshDobDays('regBirthDay','regBirthMonth','regBirthYear'); sync(); });
g('regBirthYear').addEventListener('change', ()=>{ refreshDobDays('regBirthDay','regBirthMonth','regBirthYear'); sync(); });
g('regNotes').addEventListener('input', updateRegCount);
live('name',['regName'],['blur','input']);
live('phone',['regPhone'],['blur','input']);
// month -> year -> day order: don't nag "incomplete" half-way through; the submit check reports it.
['regBirthDay','regBirthMonth','regBirthYear'].forEach(id=> g(id).addEventListener('change', ()=>{
sync(); const res = REG_V.dob();
if(res==='e.dobIncomplete' && !touched.dobStrict){ delete regErrors.dob; regPaint('dob'); }
else { regCheck('dob', true); if(regSummary.length) regShowMessage(); }
}));
g('regPhone').addEventListener('blur', ()=>{ const p = normalizePhone(g('regPhone').value); if(p){ g('regPhone').value = prettyPhone(p); sync(); } });
g('regForm').addEventListener('submit', async (e)=>{
e.preventDefault();
if(regSubmitting) return; // duplicate-submit guard: Enter key, double tap, slow network
regBanner = null; sync(); touched.dobStrict = true;
const bad = ['name','phone','dob'].filter(k=> regCheck(k, true));
regSummary = bad; regShowMessage();
if(bad.length){ regFocus(bad[0]); return; }
const btn = g('regSubmitBtn'), phone = normalizePhone(regState.phone), name = regState.name.replace(/\s+/g,' ').trim();
const dobVal = (regState.d && regState.m && regState.y) ? regState.y+'-'+String(regState.m).padStart(2,'0')+'-'+String(regState.d).padStart(2,'0') : null;
regSubmitting = true; btn.disabled = true; btn.setAttribute('aria-busy','true');
btn.innerHTML = `