$(document).ready(function() {
  $("#Staying-With").attr({disabled: true});
  
  // Only allow input of a student name if they've selected to stay
  // with a specific student
  $("input[value=With_This_Student]").click(function() {
    $("#Staying-With").attr({disabled: false});
  });
  $("input[value=Arrange_For_Me]").click(function() {
    $("#Staying-With").attr({disabled: true});
  });
  
  // For options we auto tick the box if they enter text
  $(".check-with-text input[type=text]").bind('change', function() {
    $(this).siblings("input[type=checkbox]").attr({'checked': this.value != ''});
  });
  
  // On the main page, make the month dropdown automatically take you to the
  // selected month
  var $month_form = $('#month-select');
  if ($month_form.length > 0) {
    $('label, input[type=submit]', $month_form).hide();
    $('select', $month_form).change(function () {
      window.location = './?date=' + $(this).val() + '#calendar';
    })
  }
  
  // By default we hide the full housing details form
  // we only enable it if the user says they want on-campus housing.
  // If they then change their mind, we empty that part of the form.
  $('#housing-details').hide();
  $('#on-campus input').click(function (event) {
    if ($(event.target).val() == 'Yes') {
      $('#housing-details').show();
    } else {
      $('#housing-details').hide();
      $('#housing-details input').val('').removeAttr('selected').removeAttr('checked');
    }
  });
  
  // On the Department Visit Days form only show the 'area of interest' box if they are
  // coming for the Visit Day
  var $visit_day_info = $('input[value=Visit_Day]');
  if ($visit_day_info.length > 0 && ! $visit_day_info[0].checked) {
    $('#area-of-interest').hide();
  }
  
  $('input[name=Fridays_Or_Department]').click(function () {
    if (this.value == 'Visit_Day') {
      $('#area-of-interest').show();
    } else {
      $('#area-of-interest input').val('');
      $('#area-of-interest').hide();
    }
  })
});
