function trip_sort(sort_type, descending) {
  if (sort_type == "date") {
    sort_order = trip_date_sort_order;
  }
  else {
    sort_order = trip_cost_sort_order;
  }

  if (descending) {
    sort_order = sort_order.slice().reverse();
  }

  ul = $('trip_list');
  for(i=0;i<sort_order.length;i++) {
    li = $(sort_order[i]).remove();
    ul.appendChild(li);
  }
}

function sort_trip_list(value) {
  switch(value) {
    case 1: trip_sort('cost', false); break;
    case 2: trip_sort('cost', true); break;
    case 3: trip_sort('date', false); break;
    case 4: trip_sort('date', true); break;
  }
}

function safe_hide(id) {
  if ($(id) != null) {
    $(id).hide();
  }
}

function confirm_cancel_insurance(element, value) {
  var confirm_text = 'Purchasing RBC Insurance as part of your trip package means that you will be fully reimbursed if you have to cancel your trip (for a valid reason), if you require medical attention on the trip, if your baggage gets lost, and a whole lot more! For full policy information, click on the link at the bottom of our website ‘RBC Insurance’. Campus Vacations strongly recommends that you keep this option selected. Press "OK" to cancel the insurance, and "Cancel" to keep it.'
  if (value == null) {
    if (!confirm(confirm_text)) {
      element.checked = true;
    }
  }
}

function confirm_extra_lift_ticket() {
  if ($('3DLIFT') != null && !$('3DLIFT').checked) {
    alert('You\'ve chosen to rent equipment on Friday, but you haven\'t opted into the Friday lift-ticket.  We\'ll select that for you now.');
    $('3DLIFT').checked = true;
  }
}

function confirm_friday_rentals(element) {
  if (!element.checked && $('Friday').checked) {
    if (!confirm('You have selected rentals for Friday, and your lift tickets only provide you with skiing for Saturday and Sunday. In order to ski on Friday you will need an extra day lift ticket. Choose "Cancel" to keep the lift ticket, or "OK" to continue with unselecting it.')) {
      element.checked = true;
    }
  }
}

function toggle_shipping(value) {
  if (value == '1') {
    new Effect.BlindUp("shipping_address",{});
  }
  else {
    new Effect.BlindDown("shipping_address",{});
  }
}

function toggle_paypal(value) {
  include_shipping = ($('shipping_address') != null);
  if (value == 'paypal') {
    $('billing_address').hide();
    $('credit_card_information').hide();
    $('interac_information').hide();
    $('paypal_information').show();
  }
  else if (value == 'interac') {
    $('billing_address').show();
    $('billing_address_form').hide();
    if (include_shipping) {
      $('same_as_billing').hide();
      $('shipping_address').show();
    }
    $('payment_button').hide();
    $('credit_card_information').hide();
    $('paypal_information').hide();
    $('interac_information').show();
  }
  else {
    $('billing_address_form').show();
    if (include_shipping) {
      $('same_as_billing').show();
      if ($('shipping_same_as_billing').checked) {
        $('shipping_address').hide();
      }
    }
    $('payment_button').show();
    $('billing_address').show();
    $('credit_card_information').show();
    $('paypal_information').hide();
    $('interac_information').hide();
  }
}

function pay_with_interac(path) {
  $('interac_button').disabled = true;
  $('interac_button').value = "Processing...";
  $('interac_button').form.action = path;
  $('interac_button').form.submit();
  return false;
}

function update_bus_availability(school_region_id, wants_to_drive) {
  if (sold_out[parseInt(school_region_id)]) {
    $('choose_bus').disabled = 'disabled';
    $('choose_bus_label').addClassName('disabled');
    $('choose_driving').checked = 'checked';
    $('sold_out_message').show();
  }
  else {
    $('choose_bus').disabled = '';
    $('choose_bus_label').removeClassName('disabled');
    if (!wants_to_drive) {
      $('choose_bus').checked = 'checked';
    }
    $('sold_out_message').hide();
  }
}

function show_all(selector) {
  $$(selector).each(function(elem, index){
    elem.show();
  });
}

function hide_all(selector) {
  $$(selector).each(function(elem, index){
    elem.hide();
  });
}