var license_choice = null;
var support_choice = null;
var total_users = null;

var effective_user_count = function(count) {
  if (count > 500) {
    return count * 0.5;
  } else if (count > 245) {
    return count * 0.65;
  } else if (count > 95) {
    return count * 0.8;
  } else if (count > 75) {
    return count * 0.9;
  } else {
    return count;
  }
};

var recalculate = function() {
  var costs = new Array();
  if (license_choice != null && support_choice != null && total_users != null) {
    var user_count = parseInt(total_users, 10);
    user_count = effective_user_count(user_count);
    var maintenance;
    if (support_choice == 'yes') {
      maintenance = (user_count * 225).toFixed(0);
    } else {
      maintenance = 0;
    }
    if (license_choice == 'reg') {
      var cost = (user_count * 1500).toFixed(0);
      costs.push(cost);
      costs.push(maintenance);
      costs.push(maintenance);
    } else {
      var cost = (user_count * 750).toFixed(0);
      costs.push(cost);
      costs.push(cost);
      costs.push(cost);
    }
    for (var i = 0; i < 7; i ++) {
      costs.push(maintenance);
    }
    var i = 0;
    var first_five = 0.0;
    $('#costs_table td.costs').each(function() {
      first_five += parseFloat(costs[i], 10);
      $(this).text('$' + costs[i]);
      i += 1;
    });
    var second_five = maintenance * 5 + 0.0;
    $('#totals_table td#first_five').text('$' + first_five.toFixed(0));
    $('#totals_table td#second_five').text('$' + second_five.toFixed(0));
  }
};

$('.num_users').change(function() {
  total_users = $(this).val();
  recalculate();
});

$('.license').change(function() {
  var this_id = $(this).attr('id');
  if (this_id == 'license_reg') {
    $('#license_three').attr('checked', false);
    license_choice = 'reg';
  } else if (this_id == 'license_three') {
    $('#license_reg').attr('checked', false);
    license_choice = 'three';
  }
  recalculate();
});

$('.support').change(function() {
  var this_id = $(this).attr('id');
  if (this_id == 'contract_yes') {
    $('#contract_no').attr('checked', false);
    support_choice = 'yes';
  } else if (this_id == 'contract_no') {
    $('#contract_yes').attr('checked', false);
    support_choice = 'no';
  }
  recalculate();
});


