function gup(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function updateTourSelections(tourTypeId, tourSelectionId) {
  $('tour_selection_id').childElements().invoke("remove");
  var url = '/search?tour_type_id=' + tourTypeId;

  new Ajax.Request(url, {
    method: 'get',
    onSuccess: function(transport) {
      //var option = new Element('option', { 'value': 0 }).update("All tour selections");
      //$('tour_selection_id').insert(option);

      var result = eval(transport.responseText);
      result.each(function(item) {
        var option = new Element('option', { 'value': item[1] }).update(item[0]);
        if (tourSelectionId == item[1]) {
          // Beholder den valgte tour selectionen
          option.setAttribute("selected", "selected");
        }
        $('tour_selection_id').insert(option);
      });
    }});
}

document.observe("dom:loaded", function() {
  var tourSelectionId = gup("tour_selection_id");

  if (tourSelectionId != 0) {
    updateTourSelections($('tour_type_id').getValue(), tourSelectionId);
  }

  $('tour_type_id').observe("change", function() {
    updateTourSelections($('tour_type_id').getValue(), tourSelectionId);
  });
});