(function(){
  if (window.jQuery) {
    jQuery(function($){
      $('.mj-data-table').each(function(){
        if ($.fn.DataTable) {
          $(this).DataTable({responsive:true, pageLength:25, order:[], autoWidth:false});
        }
      });
      $('[data-toggle="tooltip"]').tooltip && $('[data-toggle="tooltip"]').tooltip();
    });
  }
})();
async function loadStates(countryId, targetId){
  var target=document.getElementById(targetId); if(!target) return;
  target.innerHTML='<option value="">Loading...</option>';
  var base=(window.MJ_BASE_URL||'').replace(/\/$/,'');
  try { var r=await fetch(base+'/ajax/states.php?country_id='+encodeURIComponent(countryId)); target.innerHTML=await r.text(); }
  catch(e){ target.innerHTML='<option value="">Unable to load</option>'; }
}
async function loadCities(stateId, targetId){
  var target=document.getElementById(targetId); if(!target) return;
  target.innerHTML='<option value="">Loading...</option>';
  var base=(window.MJ_BASE_URL||'').replace(/\/$/,'');
  try { var r=await fetch(base+'/ajax/cities.php?state_id='+encodeURIComponent(stateId)); target.innerHTML=await r.text(); }
  catch(e){ target.innerHTML='<option value="">Unable to load</option>'; }
}
