function searchOrderByDidChange(sender) {
  if (!sender)
    sender = document.getElementById('search_order_by_select');
  
  var newOrderBy = sender.value;
  
  var url = document.location.toString();
  url = url.replace(/&?order=[^&]+/, '');
  
  if (url.indexOf('?') == -1) url += '?';
  else url += '&';
  url = url.replace(/\?&/, '?');
  
  url += 'order='+newOrderBy;
  
  document.location = url;
}

function searchRegionDidChange() {
  var idregion = x$('idregion').value();
  
  sendAjaxMessage('Search', 'fetchSubregionsRegion', idregion, fetchSubregionsForRegion);
}

function fetchSubregionsForRegion(subregions) {
  
  // clear the select box
  var selectEl = document.getElementById('idsubregions');
  selectEl.options.length = 0;
  selectEl.options[selectEl.options.length] = new Option('All Sub Regions','');
  
  // there were no subregions in this region (probably because they selected 'all regions')
  if (!subregions) {
    return;
  }
  
  for (var i = 0; i < subregions.length; i++) {
    var subregion = subregions[i];
    
    selectEl.options[selectEl.options.length] = new Option(subregion.display_title,subregion.search_value);
  }
}