(function (){
var ans={};
function sqProgress(step){
document.querySelectorAll('#sq-progress .sq-progress-step').forEach(function (s, i){
s.classList.toggle('done', i < step);
});
}
function sqShow(id, step){
document.querySelectorAll('.sq-screen').forEach(function (el){
el.classList.remove('active');
});
document.getElementById('sq-' + id).classList.add('active');
var prog=document.getElementById('sq-progress');
if(id==='goodbye'||id==='success'){
prog.style.display='none';
}else{
prog.style.display='flex';
sqProgress(step||0);
}}
sqProgress(1);
window.sqGo=function (q, v){
ans[q]=v;
if(q==='q1')   sqShow(v==='da' ? 'q2a':'q2b',    2);
else if(q==='q2a')  sqShow(v==='da' ? 'q3a':'q4a',    v==='da' ? 3:4);
else if(q==='q3a')  sqShow(v==='da' ? 'email':'q4a',    4);
else if(q==='q4a')  sqShow(v==='da' ? 'email':'goodbye', 4);
else if(q==='q2b')  sqShow(v==='da' ? 'q3ba':'q3bb',   3);
else if(q==='q3ba') sqShow(v==='da' ? 'email':'goodbye', 4);
else if(q==='q3bb') sqShow(v==='da' ? 'email':'goodbye', 4);
};
window.sqSubmit=function (){
var input=document.getElementById('sq-email-input');
var email=input.value.trim();
var err=document.getElementById('sq-err');
var btn=document.getElementById('sq-submit-btn');
if(!email||!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)){
err.style.display='block';
return;
}
err.style.display='none';
btn.disabled=true;
btn.textContent='Šaljemo...';
var fd=new FormData();
fd.append('nl[]',   String(evQuiz.newsletterListId));
fd.append('nr',     'minimal');
fd.append('nlang',  '');
fd.append('ne',     email);
var nlForm=document.querySelector('form[action*="action=tnp"]');
if(nlForm){
var ctToken=nlForm.querySelector('[name="ct_bot_detector_event_token"]');
var apbct=nlForm.querySelector('[name="apbct_visible_fields"]');
if(ctToken) fd.append('ct_bot_detector_event_token', ctToken.value);
if(apbct)   fd.append('apbct_visible_fields',        apbct.value);
}
fetch('/wp-admin/admin-ajax.php?action=tnp&na=s', { method: 'POST', body: fd })
.then(function (){
if(ans['q1']==='ne'){
var fd2=new FormData();
fd2.append('action', 'ev_quiz_path_b');
fd2.append('email',  email);
fd2.append('nonce',  evQuiz.nonce);
return fetch(evQuiz.ajaxUrl, { method: 'POST', body: fd2 });
}})
.catch(function (){  })
.then(function (){
sqShow('success');
});
};})();