function ajax_failed(msg) {
    $.toast({
      heading: 'Fail to load!',
      showHideTransition: 'slide',
      text: msg,
      icon: 'error',
      position: {
        right: 35,
        bottom: 65
      },
      hideAfter: 8000   // in milli seconds  
    });
}

function ajax_status(response) {
  if (response.status >= 200 && response.status < 300) {
    return Promise.resolve( response );
  } else {
    return Promise.reject(new Error(response.statusText));
  }
}

function ajax_content(response) {
   return response.text();
}

function ajax_loader(mydata, url, id) {
  if ( empty(url) ) {
    return Promise.reject('No URL given!');
  }  
  
  var request = $.ajax({
    url: url,
    type: 'POST', 
    data: mydata,
    global: false,
    async: true,
    cache: false,
    dataType: "html",
  });
  request.done(function (data) {
    $(function() {
      $("#" + id).html(data);
    });
  }).fail(function (err) {
    var d = JSON.stringify(mydata, null);
    console.log('data=' + d + ';url=' + url + ';id=' + id);
    console.log('AJAX Load Error: ' + err.status + " " + err.statusText);
  });
  return request;
}

function ajax_failed(msg) {
    $.toast({
      heading: 'Fail to load!',
      showHideTransition: 'slide',
      text: msg,
      icon: 'error',
      position: {
        right: 35,
        bottom: 65
      },
      hideAfter: 8000   // in milli seconds  
    });
}

function ajax_get_loader(mydata, url, id) {
  
  var query = "";
  var alc = 0;
  for (key in mydata) {
    if (alc !== 0) {
      url += "&";
    }
    url += encodeURIComponent(key)+"="+encodeURIComponent(mydata[key]);
    alc++;
  }

  var promise = new Promise(function(resolve, reject) {

  fetch(url, {
    method: 'GET',
    mode: 'cors',
    credentials: 'include', //'same-origin',
    headers: {
      "Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
      'accept': 'text/html'
    },
  })
  .then(ajax_status)
  .then(ajax_content)
  .then(htmlText => {
    $(function() {
      $("#" + id).html(htmlText);
    });
  })
  .then(function() {
    return resolve('worked');
  })
  .catch(err => {
    console.log("\r\nAJAX Alert: \r\n");
    console.warn("AJAX Loader URL: " + url);
    console.error(err);
    
    ajax_failed("Refresh this page!");
    
    return reject(err);
  });
  
  }); // End of promise...
  
  return promise;
}


function get_json_data(mydata, url) {
  var promise = new Promise(function(resolve, reject) {
      
  var myHeaders = new Headers({
    "Content-Type": "application/json",
  });    

  fetch(url, {
    method: 'POST',
    credentials: 'omit', // Only will work with omit credentials!!!
//    mode: 'no-cors',
    headers: myHeaders,
    body: JSON.stringify(mydata)
  })
  .then(response => response.json())
  .then(data => {
    return resolve(data);
  })
  .catch(err => {
    console.log("\r\n JSON Alert: \r\n");
    console.warn("URL Failed: " + url);
    console.error(err);
    return reject(err);
  });
  
  }); // End of promise...
  
  return promise;
}