function createLoadingWheel(div, hdiv /*div to use to find width*/, wdiv /*div to use to find height*/) {
  $(div).append("<div id=\"loading-image\" style=\"position:absolute; top:" + $(hdiv).height()/2 + "px; left:" + $(wdiv).width()/2 + "px;\"><img src=\"/images/ajax-loader.gif\" alt=\"\" \/><\/div>");
}

function onAJAXError(xhr, ajaxOptions, thrownError) {
  switch (xhr.status) {
  case 500:
    $('body').html(xhr.responseText);
    break;
  case 503:
    window.location = "/error/dbdown_" + language + ".html";
    break;
  }
}

// Mantis item 10140: ensure that hash is accounted for when switching languages
function switchLang() {
  var newHost;
  var newPath;

  if (window.location.pathname.indexOf("_e") != -1) {
    newPath = window.location.pathname.replace('_e', '_f');
    newHost = window.location.hostname.replace('-e', '-f').replace('climate', 'climat').replace('weatheroffice', 'meteo');
  } else {
    newPath = window.location.pathname.replace('_f', '_e');
    newHost = window.location.hostname.replace('-f', '-e').replace('climat', 'climate').replace('meteo', 'weatheroffice');
  }

  var newUrl = "http://" + newHost + newPath;

  if (window.location.hash != '' && window.location.hash.search("=") > -1) {
    var values = window.location.hash.substr(1).split("&");
    var gets = window.location.search.substr(1).split("&");
    for (var i = 0; i < gets.length; i++) {
      for (var j = 0; j < values.length; j++) {
        if ((gets[i].split("="))[0] == (values[j].split("="))[0]) {
          gets[i] = values[j];
        }
      }
    }
  
  window.location = newUrl + "?" + gets.join("&");
  
  } else {
    window.location = newUrl + window.location.search;
  }

}

function hashToGet() {
  if (window.location.hash != '' && window.location.hash.search("=") > -1) {
    var values = window.location.hash.substr(1).split("&");
    var gets = window.location.search.substr(1).split("&");
    for (var i = 0; i < gets.length; i++) {
      for (var j = 0; j < values.length; j++) {
        if ((gets[i].split("="))[0] == (values[j].split("="))[0]) {
          gets[i] = values[j];
        }
      }
    }
    window.location.replace(window.location.pathname + "?" + gets.join("&"));
  }
}

function setAjaxParams(params) {
  var h5 = !(typeof history.pushState === 'undefined');
  //Non HTML5 Complaint (changing hash value)
  if (!h5) {
    var matches = true;
    for (var i in params) {
      matches = matches && (parent.location.search.search(i + "=" + params[i] + "(&|$)") != -1);
    }
    if (matches) {
      if (parent.location.hash != "") {
        parent.location.hash = "";
      }
    }
    else {
      var s = '';
      for (var i in params) {
        if (s != '') {
          s += "&";
        }
        s += i + "=" + params[i];
      }
      parent.location.hash = s;
    }
  }
  //HTML5 Complaint (using pushState)
  else {
    var s = window.location.search;
    for (var i in params) {
      var patt = new RegExp(i + "=[^&]+(&|$)");
      if (s.search(patt) == -1) {
        s += "&" + i + "=" + params[i];
      }
      else {
        s = s.replace(patt, i + "=" + params[i] + "&");
      }
    }
    if (s.substr(s.length - 1, 1) == "&") {
      s = s.substr(0, s.length - 1);
    }
    if (s != window.location.search) {
      window.history.pushState("", "", window.location.pathname + s);
    }
  }
}

