var packageCosts = { 2: 4.95, 3: 22.95, 4: 69.95, 5: 149.95, 6: 316.00, 7: 483.00, 8: 590.00 };

$(function() {
  function updateCost() {
    var packageNum = $('#forum_posting_product').val();
    var repeats = $('#forum_posting_occurrences').val();
    if (packageNum == 'select') {
      $('.cost').text('Please select a package');
    } else {
      $('.cost').text((repeats * packageCosts[packageNum]).toFixed(2) + ' credits');
    }
  }
  
  function displayCorrectField() {
    var subscription = $('#forum_posting_subscription:checked').val();
    if (subscription != null) {
      $('#forum_posting_time_completion').val(4);
      $('#forum_posting_time_completion_box').hide();
      $('#forum_posting_time_completion_error_box').hide();
      $('#forum_posting_occurrences_box').show();
      $('#forum_posting_occurrences_error_box').show();
    } else {
      $('#forum_posting_time_completion_box').show();
      $('#forum_posting_time_completion_error_box').show();
      $('#forum_posting_occurrences').val(1);
      updateCost();
      $('#forum_posting_occurrences_box').hide();
      $('#forum_posting_occurrences_error_box').hide();
    }
  }
  
  $('#forum_posting_product').change(function() {
      updateCost();
  });
  
  $('#forum_posting_occurrences').keyup(function() {
      updateCost();
  });
  
  $('#forum_posting_subscription').click(function() {
      displayCorrectField();
  });
  
  updateCost();
  displayCorrectField();
});

