$(document).ready(function(){
  
  //Initialize variables
  var base_cost = 125;
  var add_ons = 0;
  
  // Cost constants
  var group_discount = 50;
  var bonfire = 50;
  var meal = 6;
  var day = 15;
  
  // Limits inputs to numeric values
  $('#group').numeric();
  $('#add-meals').numeric();
  $('#add-days').numeric();
  $('#subtract-meals').numeric();
  $('#subtract-days').numeric();
  
  // Initialize fields
  $('#group').val('15');
  $("select option[value='0']").attr("selected", "selected");
  $('#add-meals').val(0);
  $('#add-days').val(0);
  $('#subtract-meals').val(0);
  $('#subtract-days').val(0);
  
  $('#group').change(function(){
    if ($(this).val() < 15) {
      alert('Please enter a group greater than or equal to 15 people.');
      $(this).val('15');
      $('#I-1-Sub').html('$125');
    } else if ($(this).val() < group_discount) {
		  $('#I-1-Sub').html('$125');
		} else {
		  $('#I-1-Sub').html('$115');
		}
		
	 // Calculate total estimate
	 calc();
		
		$('#IIA-1').focus();
	});
	
	$('#section-IIIa select, #section-IIIc select').change(function(){
	 
	 // Calculate total estimate
	 calc();
	});
	
	$('#section-IV input').change(function(){
	 if ($(this).val() == '') {
	   $(this).val(0);
	 }
 
	 // Calculate total estimate
	 calc();
	});
	
	
	function calc() {
   
    //base_cost = parseFloat($('#I-1-Sub').html().replace(/[$,]+/g,""));
     
    // Calculate subtotals for additional activities A
    //$('select#IIIA-1, select#IIIA-2, select#IIIA-3').each(function(){
    //  $('#' + $(this).attr('id') + '-Sub').html('$' + $(this).val());
    //});
    
    add_ons = parseFloat($('#IIIA-1').val()) + parseFloat($('#IIIA-2').val()) + parseFloat($('#IIIA-3').val());
    
    // Calculate subtotals for additional activities B
    /*$('select#IIIB-1, select#IIIB-2').each(function(){
     if ($(this).val() == 9999) {
        $('#' + $(this).attr('id') + '-Sub').html('$' + Math.round((bonfire + parseFloat($('#group').val())) / parseFloat($('#group').val())*100)/100);
      } else {
        $('#' + $(this).attr('id') + '-Sub').html('$' + Math.round($(this).val() / parseFloat($('#group').val())*100)/100);
      }
    });*/
    
    if ($('#IIIB-1').val() == 9999) {
      add_ons += (bonfire + parseFloat($('#group').val())) / parseFloat($('#group').val());
    } else {
      add_ons += parseFloat($('#IIIB-1').val()) / parseFloat($('#group').val());
    }  
    
    if ($('#IIIB-2').val() == 9999) {
      add_ons += (bonfire + parseFloat($('#group').val())) / parseFloat($('#group').val());
    } else {
      add_ons += parseFloat($('#IIIB-2').val()) / parseFloat($('#group').val());
    }
    
    // Calculate subtotals for add / subtract meals and days
    add_ons += parseFloat($('#add-meals').val()) * meal;
    add_ons += parseFloat($('#add-days').val()) * day;
    
    add_ons -= parseFloat($('#subtract-meals').val()) * meal;
    add_ons -= parseFloat($('#subtract-days').val()) * day;
    
    /*$('#section-IV input').each(function(){
      if ($(this).attr('id') == 'add-meals') {
       $('#' + $(this).attr('id') + '-Sub').html('$' + $(this).val() * meal);
      }
      
      if ($(this).attr('id') == 'add-days') {
       $('#' + $(this).attr('id') + '-Sub').html('$' + $(this).val() * day);
      }
      
      if ($(this).attr('id') == 'subtract-meals') {
       $('#' + $(this).attr('id') + '-Sub').html('-$' + $(this).val() * meal);
      }
      
      if ($(this).attr('id') == 'subtract-days') {
       $('#' + $(this).attr('id') + '-Sub').html('-$' + $(this).val() * day);
      }
    });*/
    
    //Calculate section subtotals
    
    /*$('#IIIA-Total').html('$' + (parseFloat($('#IIIA-1-Sub').html().replace(/[$,]+/g,"")) + parseFloat($('#IIIA-2-Sub').html().replace(/[$,]+/g,"")) + parseFloat($('#IIIA-3-Sub').html().replace(/[$,]+/g,""))));
    
    $('#IIIB-Total').html('$' + (parseFloat($('#IIIB-1-Sub').html().replace(/[$,]+/g,"")) + parseFloat($('#IIIB-2-Sub').html().replace(/[$,]+/g,""))));
    
    $('#IVA-Add-Total').html('$' + (parseFloat($('#add-meals-Sub').html().replace(/[$,]+/g,"")) + parseFloat($('#add-days-Sub').html().replace(/[$,]+/g,""))));
    
    $('#IVA-Sub-Total').html('$' + (parseFloat($('#subtract-meals-Sub').html().replace(/[$,]+/g,"")) + parseFloat($('#subtract-days-Sub').html().replace(/[$,]+/g,""))));*/
    
    // Calculate total estimate
    total = base_cost + add_ons;
    
    $('#Est-Total').html('$' + Math.round(total*100)/100);
  }
});


