// Thank you http://stackoverflow.com/a/11677514/117092
function getUrlVars(url) {
  // exctract query args keys and values
  // return a list-object : list of keyworks. Each keyword is asscessible has a member variable of the list
  var vars = [], hash;
  if (url.indexOf('?') !== -1) {
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
  }
  return vars;
}

function patchUrl(page, url) {
  // return new url with same querystring than the current one but with the new page number
  var oldUrl = url?url:window.location.href;
  var urlVars = getUrlVars(oldUrl);
  var newUrl = oldUrl.split('?')[0];
  newUrl += "?page=" + page;
  for (var i=0, l=urlVars.length; i<l; i++) {
    var varName = urlVars[i];
    if (varName !== 'page') {
      newUrl += '&' + varName + '=' + urlVars[varName];
    }
  }
  return newUrl;
}