$(document).ready(function(){
    
    // Initial page load
    $("#invest-display").hide();
    
    $("#calc_type").bind("change",function(){
        
        $("#target-display").show();
        $("#year-display").show();
        $("#returnrate-display").show();
        $("#inflation-display").show();
        $("#invest-display").show();
        
        $("#target-display").val('');
        $("#year-display").val('');
        $("#returnrate-display").val('6%');
        $("#inflation-display").val('3%');
        $("#invest-display").val('');
        
        if ($(this).val() == "investment") { 
          $("#invest-display").hide();
          $("#calc-info").html("This will allow you to calculate how much you will need to invest every year to meet your savings goals.");
        }
        if ($(this).val() == "time") {
          $("#year-display").hide();
        }
        if ($(this).val() == "target") {
          $("#target-display").hide(); 
          $("#calc-info").html("This will allow you to calculate how much you will save over a specified amount of time.");
        }
    });

    
		$("#submit").click(function() {
			
			var target = moneyToFloat($("#target").val());
			var year = moneyToFloat($("#year").val());
			var returnrate = percentToFloat($("#returnrate").val());
			var inflation = percentToFloat($("#inflation").val());
			var invest = moneyToFloat($("#invest").val());
      var formValue;
      
      if ($("#calc_type").val() == "investment") {
        formValue = Math.round((target/((Math.pow(1+returnrate-inflation,year)-1)/returnrate-inflation))*100)/100;
        //alert(commify(formValue));
        $('#savings-result').html("<p>You will need to invest<br><strong class=\"large\">"+commify(formValue)+"</strong> / year<br>to reach your target amount.");
      }
      else if ($("#calc_type").val() == "time") {
      }
      else if ($("#calc_type").val() == "target") {
        var formValue = Math.round((invest*(Math.pow(1+returnrate-inflation,year)-1)/(returnrate-inflation))*100)/100;
        $('#savings-result').html("<p>This is how much you will have saved by investing "+commify(invest)+" annually:\n<br><strong class=\"large\">"+commify(formValue)+"</strong>");
      }
      $.scrollTo( "#savings-result", {speed:1500} );
			return false;
		});
});



