function sumbit_fpl_calc(button){

  $('#fpl_results').hide();
  $('#fpl_calc_button').addClass('processing').removeClass('submit_button');

  $.post("/calculators/fpl_calc/fpl_calc.php", $("#fpl_calc").serialize(),
    function(data){
      if ( data['fpl_poverty_level'] != "&nbsp;")
        $('#fpl_poverty_level').html( "Your FPL (Federal Poverty Level) is <strong>" + data['fpl_poverty_level'] + "</strong>" );
      else
        $('#fpl_poverty_level').html( data['fpl_poverty_level'] );

      $('#fpl_error_message').html( data['fpl_error_message'] );
      $('#fpl_calc_button').removeClass('processing').addClass('submit_button');
      $('#fpl_results').show();
    }, "json"
  );

  button.blur();

}

function update_ui(e){

  if (this.id == 'office_visit_amount' || this.id == 'rx_amount') {
    $(this).val( validate_number( $(this).val() ) );
  } else {
    $(this).val( validate_currency( $(this).val() ) );
  }

  update_office_visit_total();
  update_rx_total();

  update_total_cost();
  update_percent();

}

function update_office_visit_total(){

  var copay = currency_to_number( $('#office_visit_copay').val() );
  var amount = $('#office_visit_amount').val();
  var deductable = currency_to_number( $('#office_visit_deductible').val() );

  var total = (copay * amount) + deductable;

  $('#office_visit_total').val( number_to_currency( total ) );

}

function update_rx_total(){

  var copay = currency_to_number( $('#rx_copay').val() );
  var amount = $('#rx_amount').val();
  var deductable = currency_to_number( $('#rx_deductible').val() );

  var total = (copay * amount) + deductable;

  $('#rx_total').val( number_to_currency( total ) );

}


function update_total_cost(){

  var total = 0;

  jQuery.each( $('.ui_cost'), function() {
    total += currency_to_number( this.value );
  });

  total += ( currency_to_number( $('#ui_monthly_premium').val() ) * 12 );

  $('#ui_total_cost').val( number_to_currency(total) );

}

function update_percent(){

  var annual_income = currency_to_number( $('#ui_annual_income').val() );

  var percent = (currency_to_number( $('#ui_total_cost').val() ) / annual_income ) * 100 + '';
  $('#ui_percent_of_income').val( (percent == 'Infinity') ? '%' : percent.substr(0,4) + '%' );
}


function validate_currency( string ){

  string = string.replace(/,/g,'')
  if (string.charAt(0) != '$')
    string = '$' + string;

  if ( string.match(/^\$?[\d\.]+$/) ){
    if ( string.match(/^\$?[\d]+\.\d{2}$/) ){
      return string;
    } else if ( string.match(/^\$?[\d]+\.\d{1}$/) ){
      return string + '0';
    } else if ( string.match(/^\$?[\d]+$/) ){
      return string + '.00';
    }
  }

  return '$';

}

function validate_number( string ){

  if ( string.match(/^\d+$/) ){
    return string
  } else {
    return '';
  }

}

function currency_to_number( string ){

  string = string.replace(/\$/,'');
  string = string.replace(/,/,'');
  return (string) ? string * 1 : 0;

}

function number_to_currency( number ){

  string = '$' + number;

  if ( string.match(/\.\d{2}$/) ){
    return string;
  } else if ( string.match(/\.\d{1}$/) ){
    return string + '0';
  } else {
    return string + '.00';
  }

}
