//  Filename: calc_loan.js  //  Function: functions to calculate loan repayments, APR and nominal interest  //  Author: Barry Hood -- design@delinear.co.uk  //  Date: 2005.05.06
function loan_calculator(form_name){var loan_amount=strip_common_elements(document.getElementById(form_name).loan_amount.value);var apr_amount=strip_common_elements(document.getElementById(form_name).apr_amount.value);var loan_period=strip_common_elements(document.getElementById(form_name).loan_period.value);var number_payments=0;var monthly_amount=0;var total_to_pay=0;var total_interest=0;var monthly_rate=0;var interest_rate=0;var error_array=new Array;var error_count=0;if(!check_number_valid(loan_amount)){error_array[error_count]="\tThe loan amount field";error_count++;}if(!check_number_valid(apr_amount)){error_array[error_count]="\tThe APR amount field";error_count++;}if(!check_number_valid(loan_period)){error_array[error_count]="\tThe loan period";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";}alert(error_message);document.getElementById(form_name).number_payments.value='';
document.getElementById(form_name).monthly_amount.value='';document.getElementById(form_name).total_to_pay.value='';document.getElementById(form_name).total_interest.value='';return false;}apr_amount/=100;interest_rate=12*(Math.pow(apr_amount+1,1/12)-1);monthly_rate=interest_rate/12;number_payments=12*loan_period;var q=1/(1+monthly_rate);monthly_amount=(loan_amount*(q-1))/(q*((Math.pow(q,number_payments))-1));total_to_pay=(number_payments*monthly_amount);total_interest=(total_to_pay-loan_amount);document.getElementById(form_name).number_payments.value=format_number_output(number_payments,0,',');document.getElementById(form_name).monthly_amount.value=format_number_output(monthly_amount,2,',','£');document.getElementById(form_name).total_to_pay.value=format_number_output(total_to_pay,2,',','£');document.getElementById(form_name).total_interest.value=format_number_output(total_interest,2,',','£');return false;}function apr_calculator(form_name){var apr_nom_int=strip_common_elements(document.getElementById(form_name).apr_nom_int.value);var apr_apr_int=0;if(!check_number_valid(apr_nom_int)){alert("You must fill out the nominal interest form field to calculate the APR. Please try again.");return false;}
apr_nom_int/=100;apr_apr_int=(Math.pow((1+(apr_nom_int/12)),12))-1;apr_apr_int*=100;document.getElementById(form_name).apr_apr_int.value=format_number_output(apr_apr_int,2,',');return false;}function nominal_calculator(form_name){var nom_apr_int=strip_common_elements(document.getElementById(form_name).nom_apr_int.value);var nom_nom_int=0;if(!check_number_valid(nom_apr_int)){alert("You must fill out the APR form field to calculate the nominal interest rate. Please try again.");return false;}nom_apr_int/=100;nom_nom_int=(12*((Math.pow(nom_apr_int+1,1/12))-1));nom_nom_int*=100;document.getElementById(form_name).nom_nom_int.value=format_number_output(nom_nom_int,2,',');return false;}