jQuery.ajaxSetup({
  headers : {
    'X-CSRF-TOKEN' : jQuery('meta[name="x-header-csrf"]').attr('content')
  }
});

function mswExp(url,pg) {
  mswShowSpin();
  jQuery(function() {
    jQuery.ajax({
      url      : 'index.php',
      data     : 'ajax-ops=' + pg + (url ? url : ''),
      dataType : 'json',
      success  : function (data) {
		    mswCloseSpin();
        switch(data['status']) {
          case 'ok':
            mswWin('', data['loc']);
            break;
          case 'err':
            mswDialog(data['txt'][0], data['txt'][1], data['status']);
            break;
          default:
            break;
        }
      }
    });
  });
  return false;
}

function mswKey() {
  mswBoxSpin('fm[apikey]', 'spin', 'right');
  jQuery(function() {
    jQuery.ajax({
      url      : 'index.php',
      data     : 'ajax-ops=apikey',
      dataType : 'json',
      cache    : false,
      success  : function (data) {
        mswBoxSpin('fm[apikey]', 'stop', 'right');
        jQuery('input[name="fm[apikey]"]').val(data['key']);
      }
    });
  });
  return false;
}

function mswPassClr() {
  jQuery('span[class="passGenReveal"]').html('&nbsp;');
}
function mswPassGen() {
  jQuery('input[name="fm[pass]"]').addClass('msw-box-spinner-right');
	jQuery(function() {
    jQuery.ajax({
      url      : 'index.php',
      data     : 'ajax-ops=pass-gen',
      dataType : 'json',
      success  : function (data) {
		    jQuery('input[name="fm[pass]"]').removeClass('msw-box-spinner-right');
		    jQuery('span[class="passGenReveal"]').html(data['pass']);
		    jQuery('input[name="fm[pass]"]').val(data['pass']);
      }
    });
  });
  return false;
}

function mswAction(act, fld, slt, cnf) {
  if (typeof cnf != 'undefined') {
    mswDialog(mswlang.pls, mswlang.aus, 'confirm', {
      'act' : act, 
      'fld' : fld,
      'slt' : slt
    });
    return false;
  }
  if (typeof slt == 'undefined') {
    var slt = 'no';
  }
  if (typeof fld == 'undefined') {
    var fld = '.mainmswarea';
  }
  jQuery(function() {
    if (slt == 'no') {
      // For version check, we remove the menu..
      if (act == 'vc') {
        jQuery('body').removeClass('pushy-open-left');
        jQuery('body').removeClass('pushy_html_body');
        jQuery('html').removeClass('pushy_html_body');
        jQuery('i[class="fa fa-chevron-left fa-fw menu-btn pushy-rotate-start"]').attr('class', 'fa fa-bars fa-fw menu-btn');
        jQuery('div[class="site-overlay"]').hide();
        setTimeout(function() {
          mswShowSpin();
        }, 300);
      } else {
        mswShowSpin();
      }
    }
    jQuery.ajax({
      type     : 'POST',
      url      : 'index.php?ajax-ops=' + act,
      data     : jQuery((fld ? fld : '.mainmswarea') + ' > form').serialize(),
      cache    : false,
      dataType : 'json',
      success  : function (data) {
        if (slt == 'no') {
          mswCloseSpin();
          switch(data['status']) {
            case 'ok':
            case 'err':
              mswDialog(data['txt'][0], data['txt'][1], data['status']);
              if (data['tr-ds']) {
                for (var i=0; i<data['tr-ds'].length; i++) {
                  jQuery('#idr_' + data['tr-ds'][i]).remove();
                }
                jQuery('#delb_span').hide();
                jQuery('#delb').addClass('disabled');
              }
              if (act == 'vc') {
                jQuery('div[class="site-overlay"]').show();
              }
              break;
            case 'rdr':
              mswWin('', data['loc']);
              break;
            default:
              break;
          }
        }
      }
    });
  });
  return false;
}

function mswMultiAction(act, fld, slt, cnf) {
  if (cnf) {
    if (!confirm(mswlang.aus)) {
      return false;
    }
  }
  if (typeof slt == 'undefined') {
    var slt = 'no';
  }
  if (typeof fld == 'undefined') {
    var fld = '.mainmswarea';
  }
  jQuery(function() {
    if (slt == 'no') {
      mswShowSpin();
    }
    var fd = new FormData(jQuery('#sform')[0]);
    jQuery.ajax({
      type        : 'POST',
      url         : 'index.php?ajax-ops=' + act,
      data        : fd,
      processData : false,
      contentType : false,
      cache       : false,
      dataType    : 'json',
      success     : function (data) {
        if (slt == 'no') {
          mswCloseSpin();
          switch(data['status']) {
            case 'ok':
            case 'err':
              mswDialog(data['txt'][0], data['txt'][1], data['status']);
              if (data['tr-ds']) {
                for (var i=0; i<data['tr-ds'].length; i++) {
                  jQuery('#idr_' + data['tr-ds'][i]).remove();
                }
                jQuery('#delb_span').hide();
                jQuery('#delb').addClass('disabled');
              }
              break;
            case 'rdr':
              mswWin('', data['loc']);
              break;
            default:
              break;
          }
        }
      }
    });
  });
  return false;
}

function mswPanel(panl) {
  jQuery(function() {
    jQuery.ajax({
      url      : 'index.php',
      data     : 'ajax-ops=menu-panel&pnl=' + panl,
      dataType : 'json',
      cache    : false,
      success  : function (data) {
      }
    });
  });
  return false;
}

function mswMail() {
  mswShowSpin();
  jQuery(function() {
    jQuery.ajax({
      url      : 'index.php',
      data     : 'ajax-ops=mail',
      dataType : 'json',
      cache    : false,
      success  : function (data) {
        mswCloseSpin();
        mswDialog(data['txt'][0], data['txt'][1], 'ok');
      }
    });
  });
  return false;
}

function mswRDR(act) {
  jQuery(function() {
    mswShowSpin();
    setTimeout(function() {
      jQuery.ajax({
        url      : 'index.php',
        data     : 'ajax-ops=' + act,
        dataType : 'json',
        cache    : false,
        success  : function (data) {
          mswWin('', data['rdr']);
        }
      });
    }, 1500);
  });
  return false;
}

function mswEnter() {
  if (jQuery('input[name="usr"]').val() == '') {
    jQuery('input[name="usr"]').focus();
  } else if (jQuery('input[name="pw"]').val() == '') {
    jQuery('input[name="pw"]').focus();
  } else {
    mswBoxSpin('usr', 'spin', 'right');
    jQuery(function() {
      jQuery.ajax({
        type     : 'POST',
        url      : 'index.php?ajax-ops=login',
        data     : jQuery('.mainmswarea > form').serialize(),
        cache    : false,
        dataType : 'json',
        success  : function (data) {
          switch(data['status']) {
            case 'ok':
              mswWin('', data['rdr']);
              break;
            case 'err':
              mswBoxSpin('usr', 'stop', 'right');
              mswDialog(data['txt'][0], data['txt'][1], 'err');
              break;
          }
        }
      });
    });
    return false;
  }
  return false;
}