(function(){
  'use strict';
  var d=document,w=window;
  var ready=function(fn){d.readyState!=='loading'?fn():d.addEventListener('DOMContentLoaded',fn)};
  function wrap(){var el=d.querySelector('.mj428-toast-wrap'); if(!el){el=d.createElement('div');el.className='mj428-toast-wrap';d.body.appendChild(el)} return el}
  function toast(opts,type){
    if(typeof opts==='string') opts={message:opts,type:type||'info'}; opts=opts||{};
    var t=d.createElement('div'); var ty=opts.type||type||'info';
    t.className='mj428-toast is-'+ty; t.setAttribute('role','status');
    var title=opts.title || (ty==='success'?'Success':(ty==='error'?'Error':(ty==='warning'?'Please check':'Notice')));
    t.innerHTML='<button type="button" class="mj428-toast-close" aria-label="Close">×</button><div class="mj428-toast-title"></div><div class="mj428-toast-msg"></div>';
    t.querySelector('.mj428-toast-title').textContent=title;
    t.querySelector('.mj428-toast-msg').textContent=opts.message||'';
    if(opts.actions&&opts.actions.length){var a=d.createElement('div');a.className='mj428-toast-actions';opts.actions.forEach(function(x){var link=d.createElement(x.href?'a':'button');if(x.href)link.href=x.href;link.textContent=x.label||'Open';a.appendChild(link)});t.appendChild(a)}
    t.querySelector('.mj428-toast-close').onclick=function(){t.remove()}; wrap().appendChild(t);
    setTimeout(function(){if(t&&t.parentNode)t.remove()}, opts.timeout||5600); return t;
  }
  function loader(msg,sub){var el=d.querySelector('.mj428-page-loader'); if(!el){el=d.createElement('div');el.className='mj428-page-loader';el.innerHTML='<div class="mj428-loader-card"><div class="mj428-spinner"></div><div class="mj428-loading-text">Processing...</div><div class="mj428-loading-sub">Please wait.</div></div>';d.body.appendChild(el)} el.querySelector('.mj428-loading-text').textContent=msg||'Processing...';el.querySelector('.mj428-loading-sub').textContent=sub||'Please wait, do not close this page.';el.classList.add('is-active');return el}
  function hideLoader(){var el=d.querySelector('.mj428-page-loader'); if(el)el.classList.remove('is-active')}

  function isAuthForm(form){
    var action=(form.getAttribute('action')||'').toLowerCase();
    var path=(location.pathname||'').toLowerCase();
    if(form.dataset.mjAuthForm==='1'||form.classList.contains('mj-auth-form'))return true;
    if(/login|register|signup|forgot|reset-password|admin-login/.test(action+' '+path))return true;
    if(form.querySelector('input[name="password"]') && form.querySelector('input[name="email"]') && !form.querySelector('input[type="file"]'))return true;
    return false;
  }
  function messageFromAlert(el){var txt=(el.textContent||'').replace(/×/g,'').trim(); if(!txt)return; var type='info'; if(/alert-success|bg-success/.test(el.className))type='success'; else if(/alert-danger|alert-error|bg-danger/.test(el.className))type='error'; else if(/alert-warning|bg-warning/.test(el.className))type='warning'; toast({type:type,title:type==='success'?'Done':(type==='error'?'Action needed':'Notice'),message:txt}); el.style.display='none'; }
  function isEditForm(form){
    try{
      var path=(location.pathname||'').toLowerCase();
      if(/(^|\/)(edit-[^\/]+|[^\/]+-edit|deal-edit|company-edit)\.php$/.test(path))return true;
      if(/(^|\/)edit(\-|_|\/)/.test(path))return true;
      var id=form.querySelector('input[name="id"],input[name="listing_id"],input[name="company_id"],input[name="deal_id"],input[name="item_id"],input[name="record_id"],input[name="edit_id"]');
      if(id && parseInt(id.value||'0',10)>0)return true;
      if(form.dataset.mjEditForm==='1')return true;
    }catch(e){}
    return false;
  }
  function validate(form){var ok=true,first=null;form.querySelectorAll('.mj428-field-error').forEach(function(x){x.classList.remove('mj428-field-error')});form.querySelectorAll('.mj428-field-error-text').forEach(function(x){x.remove()});form.querySelectorAll('input,select,textarea').forEach(function(f){if(f.disabled||f.type==='hidden'||f.name==='website_url'||f.name==='company_website_confirm')return;var v=(f.value||'').trim();var msg='';if(f.required&&!v)msg='This field is required.';else if(v&&f.type==='email'&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v))msg='Enter a valid email address.';else if(v&&(f.type==='tel'||/phone|mobile|whatsapp/i.test(f.name))&&!/^[+()\d\s\-]{6,30}$/.test(v))msg='Enter a valid phone number.';else if(v&&f.type==='url'&&!/^https?:\/\//i.test(v))msg='URL must start with http:// or https://';if(f.type==='file'&&f.files&&f.files.length){[].forEach.call(f.files,function(file){if(/\.(php|phtml|phar|js|html?|svg|exe|sh|bat|cmd)$/i.test(file.name||''))msg='Unsafe file type blocked.'; if(file.size>10*1024*1024)msg='File too large. Maximum 10MB.'})} if(msg){ok=false;first=first||f;f.classList.add('mj428-field-error');var e=d.createElement('div');e.className='mj428-field-error-text';e.textContent=msg;if(f.parentNode)f.parentNode.appendChild(e)}});if(first){try{first.focus();first.scrollIntoView({behavior:'smooth',block:'center'})}catch(e){}} return ok}
  function hidden(form,name,val){var x=form.querySelector('input[name="'+name+'"]'); if(!x){x=d.createElement('input');x.type='hidden';x.name=name;form.appendChild(x)} x.value=val; return x}
  function resetSubmitting(form,msg){try{if(!form)return;form.dataset.mjSubmitting='0';var btn=form.querySelector('button[type=submit],input[type=submit]');if(btn){btn.disabled=false;btn.classList.remove('mj428-btn-loading');if(btn.dataset.mjOld){if(btn.tagName==='INPUT')btn.value=btn.dataset.mjOld;else btn.innerHTML=btn.dataset.mjOld;}}hideLoader();if(msg)toast({type:'warning',title:'Still processing?',message:msg});}catch(e){}}
  function enhance(form){if(form.dataset.mjUnifiedEnhanced==='1'||form.dataset.mj428Enhanced==='1'||form.dataset.mjNoEnhance==='1'||isAuthForm(form))return;form.dataset.mjUnifiedEnhanced='1';form.dataset.mj428Enhanced='1';var method=(form.method||'get').toLowerCase();if(method==='post'||form.querySelector('input[type=file]')){hidden(form,'_mj_form_guard','1');hidden(form,'mj_form_token',w.MJ_FORM_TOKEN||w.MJ_CSRF_TOKEN||'');hidden(form,'_mj_form_key',(location.pathname.replace(/^\//,'')||'form'));if(!form.querySelector('input[name=website_url]')){var hp=d.createElement('div');hp.className='mj428-honeypot';hp.innerHTML='<label>Website <input type="text" name="website_url" tabindex="-1" autocomplete="off"></label>';form.appendChild(hp)}}form.addEventListener('submit',function(e){if(form.dataset.mjSubmitting==='1'){e.preventDefault();toast({type:'warning',title:'Please wait',message:'This form is already submitting.'});return false}if(!validate(form)){e.preventDefault();toast({type:'error',title:'Please fix the form',message:'Please check the highlighted fields.'});return false}try{if(!isEditForm(form)){var key=location.pathname+'|'+Array.from(new FormData(form).entries()).filter(function(p){return !/^(_mj|mj_form_token|csrf_token|password)/.test(p[0])}).slice(0,40).map(function(p){return p[0]+'='+p[1]}).join('&');var prev=JSON.parse(sessionStorage.getItem('mj428:lastSubmit')||'{}');if(prev.key===key&&Date.now()-prev.at<9000){e.preventDefault();toast({type:'warning',title:'Already submitted',message:'Same form is already submitted. Please wait.'});return false}sessionStorage.setItem('mj428:lastSubmit',JSON.stringify({key:key,at:Date.now()}))}}catch(ex){}form.dataset.mjSubmitting='1';var btn=form.querySelector('button[type=submit],input[type=submit]');if(btn){btn.dataset.mjOld=btn.tagName==='INPUT'?btn.value:btn.innerHTML;btn.disabled=true;btn.classList.add('mj428-btn-loading');if(btn.tagName==='INPUT')btn.value='Submitting...';else btn.innerHTML='Submitting...'}if(form.querySelector('input[type=file]')||form.dataset.mjLongProcess==='1'||/install|update|import|payment|wallet|checkout|deal|listing|company/i.test(location.pathname)){loader(form.dataset.loadingText||'Processing...')}clearTimeout(form._mjSubmitWatch);form._mjSubmitWatch=setTimeout(function(){if(form.dataset.mjSubmitting==='1'){resetSubmitting(form,'The page did not finish after 90 seconds. Please check the required fields or server error log, then try again.')}},90000);},true)}
  function imgFallback(){d.querySelectorAll('img').forEach(function(img){if(!img.hasAttribute('loading'))img.setAttribute('loading','lazy'); if(!img.hasAttribute('decoding'))img.setAttribute('decoding','async'); img.addEventListener('error',function(){if(img.dataset.mjBroken)return;img.dataset.mjBroken='1';img.classList.add('mj428-broken-image');var svg='<svg xmlns="http://www.w3.org/2000/svg" width="800" height="500"><rect width="100%" height="100%" fill="%23f8fafc"/><text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" fill="%2364758b" font-family="Arial" font-size="28">Image not available</text></svg>';img.src='data:image/svg+xml;charset=utf-8,'+svg})})}
  function smartPolling(){if(!w.MJ_NOTIFICATION_POLL_MS)return;var active=true;d.addEventListener('visibilitychange',function(){active=!d.hidden;w.MJ_NOTIFICATION_POLL_MS=active?25000:90000})}
  ready(function(){w.MJ428={toast:toast,loader:loader,hideLoader:hideLoader,resetSubmitting:resetSubmitting};d.querySelectorAll('.alert').forEach(messageFromAlert);d.querySelectorAll('form').forEach(enhance);imgFallback();smartPolling();var p=new URLSearchParams(location.search);if(p.get('saved')==='1'||p.get('success')==='1')toast({type:'success',message:'Action completed successfully.'});});w.addEventListener('pageshow',function(){d.querySelectorAll('form').forEach(function(f){if(f.dataset.mjSubmitting==='1')resetSubmitting(f,'');});hideLoader();});
})(window,document);
