//  Filename: calc_savings.js  //  Function: functions to calculate how long it would take to save a target amount or how much can be saved in a set number of years based on interest and inflation rates  //  Author: Barry Hood -- design@delinear.co.uk  //  Date: 2005.05.06
function savings_amount_calculator(form_name){var a_starting_balance=strip_common_elements(document.getElementById(form_name).a_starting_balance.value);var a_monthly_deposit=strip_common_elements(document.getElementById(form_name).a_monthly_deposit.value);var a_interest=strip_common_elements(document.getElementById(form_name).a_interest.value);var a_inflation=strip_common_elements(document.getElementById(form_name).a_inflation.value);var a_target=strip_common_elements(document.getElementById(form_name).a_target.value);var a_div=0;var a_diff=0;var a_monthly_real_value=0;var a_monthly_interest=0;var a_years=0;var a_real_value=0;var error_array=new Array;var error_count=0;if(!check_number_valid(a_starting_balance)){error_array[error_count]="\tThe starting balance field";error_count++;}if(!check_number_valid(a_monthly_deposit)){error_array[error_count]="\tThe monthly deposit field";error_count++;}if(!check_number_valid(a_interest)){error_array[error_count]="\tThe interest rate field";error_count++;}if(!check_number_valid(a_inflation)){error_array[error_count]="\tThe inflation rate field";error_count++;}if(!check_number_valid(a_target)){error_array[error_count]="\tThe target amount field";error_count++;}if(error_array.length>0){var error_message;error_message=(error_array.length>1)?"The following fields are incorrect. Please check the fields and try again:\n\n":"The following field is incorrect. Please check the fields and try again:\n\n";for(i=0;i<error_array.length;i++){error_message+=error_array[i];error_message+="\n";}
document.getElementById(form_name).a_years.value='';document.getElementById(form_name).a_real_value.value='';alert(error_message);return false;}a_starting_balance=parseFloat(a_starting_balance);a_target=parseFloat(a_target);a_inflation=parseFloat(a_inflation);a_years=parseFloat(a_years);a_interest=(a_interest/100);a_inflation=(a_inflation/100);a_monthly_interest=(a_interest/12);a_monthly_real_value=(a_monthly_deposit/a_monthly_interest);a_diff=((a_target+a_monthly_real_value)/(a_starting_balance+a_monthly_real_value));a_div=((Math.log(a_diff))/( Math.log(1+a_monthly_interest)));a_years=(a_div/12);a_real_value=(a_target/(Math.pow((1+a_inflation),a_years)));a_years=Math.round(a_years*10)/10;document.getElementById(form_name).a_years.value=format_number_output(a_years,'',',')+" years";document.getElementById(form_name).a_real_value.value=format_number_output(a_real_value,2,',','£');return false;}function savings_year_calculator(form_name){var y_starting_balance=strip_common_elements(document.getElementById(form_name).y_starting_balance.value);var y_monthly_deposit=strip_common_elements(document.getElementById(form_name).y_monthly_deposit.value);var y_interest=strip_common_elements(document.getElementById(form_name).y_interest.value);var y_inflation=strip_common_elements(document.getElementById(form_name).y_inflation.value);var y_years=strip_common_elements(document.getElementById(form_name).y_years.value);var y_monthly_interest=0;var y_div=0;var y_target=0;var y_real_value=0;var error_array=new Array;var error_count=0;
if(!check_number_valid(y_starting_balance)){error_array[error_count]="\tThe starting balance field";error_count++;}if(!check_number_valid(y_monthly_deposit)){error_array[error_count]="\tThe monthly deposit field";error_count++;}if(!check_number_valid(y_interest)){error_array[error_count]="\tThe interest rate field";error_count++;}if(!check_number_valid(y_inflation)){error_array[error_count]="\tThe inflation rate field";error_count++;}if(!check_number_valid(y_years)){error_array[error_count]="\tThe target number of years";error_count++;}if(error_array.length>0){var error_message;error_message=(error_array.length>1)?"The following fields are incorrect. Please check the fields and try again:\n\n":"The following field is incorrect. Please check the fields and try again:\n\n";for(i=0;i<error_array.length;i++){error_message+=error_array[i];error_message+="\n";}document.getElementById(form_name).y_target.value='';document.getElementById(form_name).y_real_value.value='';alert(error_message);return false;}y_starting_balance=parseInt(y_starting_balance);y_monthly_deposit=parseInt(y_monthly_deposit);y_inflation=parseInt(y_inflation);y_years=parseInt(y_years);y_interest=(y_interest/100);y_monthly_interest=(y_interest/12);y_div=y_years*12;y_target=(y_starting_balance*(Math.pow(1+y_monthly_interest,y_div)))+(((y_monthly_deposit*((Math.pow(1+y_monthly_interest,y_div))- 1))/y_monthly_interest));y_real_value=(y_target/(Math.pow(1+(y_inflation/100),y_years)));document.getElementById(form_name).y_target.value=format_number_output(y_target,2,',','£');
document.getElementById(form_name).y_real_value.value=format_number_output(y_real_value,2,',','£');return false;}