فرم عضویت در موسسه فرهنگی خانواده امین
95%
document.addEventListener('DOMContentLoaded', function() { // Populate day/month/year (Persian years range) const daySel = document.getElementById('BirthDay'); const monthSel = document.getElementById('BirthMonth'); const yearSel = document.getElementById('BirthYear');
if (daySel && monthSel && yearSel) { // Clear existing options daySel.innerHTML = ''; monthSel.innerHTML = ''; yearSel.innerHTML = '';
// Add days for(let d = 1; d <= 31; d++) { daySel.add(new Option(d, d)); } // Add months for(let m = 1; m <= 12; m++) { monthSel.add(new Option(m, m)); } // Add years for(let y = 1320; y <= 1405; y++) { yearSel.add(new Option(y, y)); } } // Helpers function isValidPhone(v){ return /^\d{11}$/.test(v); } function isValidEmail(v){ return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v); } // Required single-field IDs const requiredIds = [ 'PaymentReceipt','PaymentDate', 'FirstName','LastName','FatherName', 'BirthDay','BirthMonth','BirthYear', 'Sex','Religion','Province','City','Address', 'Field1','Degree1', 'Phone_Home','Phone_Mobile','Phone_Work','Email_Address', 'Work1','Height1','weight1','Hair_Color1','Eyes_Color1','Field1b','Degree1b','Parents','Help','Cost', 'Eyes_Color2','Weight2','Favorite','Unfavorite','DivorceOk','WidowOk','Engagement', 'Dowry','CeremonyDetails','Degree2','Home2','Kids','Busy','Learning', 'Extra' ]; const radioGroups = []; // Progress calculation: count only truly filled fields (no placeholders) function updateProgress(){ let total = 0, filled = 0; // count required single fields requiredIds.forEach(id=>{ const el = document.getElementById(id); if(!el){ return; } total++; const val = (el.value || '').toString().trim(); if(val === '') return; // special validation for phones and email if(id==='Phone_Home' || id==='Phone_Mobile' || id==='Phone_Work'){ if(isValidPhone(val)) filled++; } else if(id==='Email_Address'){ if(isValidEmail(val)) filled++; } else { filled++; } });
const pct = total ? Math.round((filled/total)*100) : 0; document.getElementById('formProgress').style.width = pct + '%'; document.getElementById('progressPercent').textContent = pct + '%'; return {pct, total, filled}; }
// Show/hide error helpers function showError(id, msg){ const el = document.getElementById(id); if(el) el.classList.add('is-invalid'); const err = document.getElementById('err-' + id); if(err){ err.textContent = msg; err.style.display = 'block'; } } function hideError(id){ const el = document.getElementById(id); if(el) el.classList.remove('is-invalid'); const err = document.getElementById('err-' + id); if(err){ err.style.display = 'none'; } }
// Attach listeners to inputs for progress and autosave const form = document.getElementById('marriageForm'); if (form) { const allEls = form.querySelectorAll('input,select,textarea'); allEls.forEach(el=>{ el.addEventListener('input', function(){ updateProgress(); saveDraft(); hideError(el.id); }); el.addEventListener('change', function(){ updateProgress(); saveDraft(); hideError(el.id); }); });
// Validation on submit form.addEventListener('submit', function(e){ let ok = true;
// validate single fields requiredIds.forEach(id=>{ const el = document.getElementById(id); if(!el) return; const val = (el.value || '').toString().trim(); if(val === ''){ ok = false; showError(id, 'این فیلد الزامی است.'); } else { // special checks if((id==='Phone_Home' || id==='Phone_Mobile' || id==='Phone_Work') && !isValidPhone(val)){ ok = false; showError(id, 'شماره باید دقیقاً 11 رقم (فقط عدد) باشد.'); } else if(id==='Email_Address' && !isValidEmail(val)){ ok = false; showError(id, 'ایمیل معتبر نیست.'); } else { hideError(id); } } });
if(!ok){ e.preventDefault(); // scroll to first invalid field const firstInvalid = document.querySelector('.is-invalid, .error[style*="block"]'); if(firstInvalid) firstInvalid.scrollIntoView({behavior:'smooth', block:'center'}); } else { // success -> clear draft localStorage.removeItem('aifci_marriage_form_v1'); } }); }
// Autosave const STORAGE_KEY = 'aifci_marriage_form_v1'; function saveDraft(){ const form = document.getElementById('marriageForm'); if (!form) return;
const data = {}; Array.from(form.elements).forEach(el=>{ if(!el.name) return; if(el.type === 'radio'){ if(el.checked) data[el.name] = el.value; } else if(el.type === 'checkbox'){ data[el.name] = el.checked ? '1' : ''; } else { data[el.name] = el.value; } }); try { localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); } catch(e){ /* ignore storage errors */ } }
function restoreDraft(){ try { const raw = localStorage.getItem(STORAGE_KEY); if(!raw) return; const data = JSON.parse(raw); const form = document.getElementById('marriageForm'); if (!form) return;
Array.from(form.elements).forEach(el=>{ if(!el.name) return; const val = data[el.name]; if(typeof val === 'undefined') return; if(el.type === 'radio'){ el.checked = (el.value === val); } else if(el.type === 'checkbox'){ el.checked = !!val; } else { el.value = val; } }); } catch(e){ console.error('restoreDraft', e); } }
// Init restoreDraft(); updateProgress(); setInterval(saveDraft, 5000); });