var lifeExpectency = { 'male' : { 0 : 79.34, 1 : 78.75, 2 : 77.78, 3 : 76.80, 4 : 75.81, 5 : 74.83, 6 : 73.84, 7 : 72.85, 8 : 71.85, 9 : 70.86, 10 : 69.86, 11 : 68.87, 12 : 67.87, 13 : 66.88, 14 : 65.90, 15 : 64.92, 16 : 63.94, 17 : 62.98, 18 : 62.02, 19 : 61.07, 20 : 60.13, 21 : 59.19, 22 : 58.24, 23 : 57.30, 24 : 56.35, 25 : 55.39, 26 : 54.44, 27 : 53.47, 28 : 52.51, 29 : 51.55, 30 : 50.58, 31 : 49.62, 32 : 48.65, 33 : 47.69, 34 : 46.73, 35 : 45.77, 36 : 44.82, 37 : 43.86, 38 : 42.91, 39 : 41.97, 40 : 41.02, 41 : 40.08, 42 : 39.14, 43 : 38.20, 44 : 37.26, 45 : 36.33, 46 : 35.41, 47 : 34.48, 48 : 33.56, 49 : 32.65, 50 : 31.74, 51 : 30.83, 52 : 29.93, 53 : 29.04, 54 : 28.15, 55 : 27.27, 56 : 26.39, 57 : 25.52, 58 : 24.66, 59 : 23.80, 60 : 22.96, 61 : 22.11, 62 : 21.28, 63 : 20.46, 64 : 19.64, 65 : 18.84, 66 : 18.05, 67 : 17.27, 68 : 16.50, 69 : 15.74, 70 : 15.00, 71 : 14.28, 72 : 13.56, 73 : 12.87, 74 : 12.18, 75 : 11.52, 76 : 10.87, 77 : 10.24, 78 : 9.63, 79 : 9.03, 80 : 8.46, 81 : 7.91, 82 : 7.38, 83 : 6.87, 84 : 6.40, 85 : 5.95, 86 : 5.53, 87 : 5.14, 88 : 4.77, 89 : 4.44, 90 : 4.13, 91 : 3.83, 92 : 3.55, 93 : 3.29, 94 : 3.04, 95 : 2.82, 96 : 2.61, 97 : 2.41, 98 : 2.24, 99 : 2.08, 100 : 1.93, 101 : 1.80, 102 : 1.68, 103 : 1.58, 104 : 1.49, 105 : 1.40 }, 'female' : { 0 : 83.01, 1 : 82.34, 2 : 81.37, 3 : 80.39, 4 : 79.40, 5 : 78.41, 6 : 77.42, 7 : 76.42, 8 : 75.43, 9 : 74.43, 10 : 73.44, 11 : 72.44, 12 : 71.45, 13 : 70.45, 14 : 69.47, 15 : 68.48, 16 : 67.50, 17 : 66.52, 18 : 65.54, 19 : 64.56, 20 : 63.59, 21 : 62.61, 22 : 61.64, 23 : 60.66, 24 : 59.68, 25 : 58.70, 26 : 57.72, 27 : 56.75, 28 : 55.77, 29 : 54.79, 30 : 53.81, 31 : 52.83, 32 : 51.86, 33 : 50.88, 34 : 49.91, 35 : 48.94, 36 : 47.97, 37 : 47.00, 38 : 46.03, 39 : 45.07, 40 : 44.11, 41 : 43.15, 42 : 42.19, 43 : 41.23, 44 : 40.28, 45 : 39.33, 46 : 38.39, 47 : 37.45, 48 : 36.51, 49 : 35.58, 50 : 34.65, 51 : 33.73, 52 : 32.80, 53 : 31.88, 54 : 30.97, 55 : 30.05, 56 : 29.14, 57 : 28.24, 58 : 27.34, 59 : 26.44, 60 : 25.55, 61 : 24.67, 62 : 23.79, 63 : 22.93, 64 : 22.07, 65 : 21.22, 66 : 20.39, 67 : 19.56, 68 : 18.74, 69 : 17.93, 70 : 17.13, 71 : 16.34, 72 : 15.56, 73 : 14.79, 74 : 14.03, 75 : 13.29, 76 : 12.55, 77 : 11.84, 78 : 11.13, 79 : 10.45, 80 : 9.79, 81 : 9.14, 82 : 8.52, 83 : 7.92, 84 : 7.35, 85 : 6.81, 86 : 6.31, 87 : 5.83, 88 : 5.39, 89 : 4.97, 90 : 4.58, 91 : 4.22, 92 : 3.87, 93 : 3.56, 94 : 3.27, 95 : 3.00, 96 : 2.76, 97 : 2.54, 98 : 2.34, 99 : 2.17, 100 : 2.01, 101 : 1.86, 102 : 1.74, 103 : 1.62, 104 : 1.52, 105 : 1.42 } }; function pmt(rate, nper, pv, fv, type) { var pmt; fv = fv ? fv : 0; type = type ? type : 0; if(rate == 0) { pmt = (pv + fv) / nper; } else { pmt = (pv * Math.pow((1 + rate), nper) + fv) / ((1 + rate * type) * ( Math.pow((1 + rate), nper) - 1) / rate); } return Math.round((pmt * -1) * 100) / 100; } $(function() { $('#resident_last_5').bind('click', function() { //$('input[type=checkbox][name=resident_last_5]').is(':checked') $('#exchange-rate-wrapper').toggle(); }); $('input[type=submit][name=calculate]').bind('click', function() { var gender = $('select[name=gender]').val(); var age = $('select[name=age]').val(); var frequency = $('select[name=frequency]').val(); var fund_amount = $('input[type=text][name=fund_amount]').val(); if($('input[type=checkbox][name=resident_last_5]').is(':checked')) { var exchange_rate = $('input[type=text][name= exchange_rate]').val(); var rate = $('input[type=hidden][name= rate]').val(); // Clean up user entered fund amount fund_amount = fund_amount.replace(/[^\d.]/g, ''); fund_amount = Math.round(fund_amount * 100) / 100; $('input[type=text][name=fund_amount]').val(parseFloat(fund_amount).toFixed(2)); exchange_rate = Math.round(exchange_rate * 10000) / 10000; exchange_rate = parseFloat(exchange_rate).toFixed(4) $('input[type=text][name=exchange_rate]').val(exchange_rate); if(!gender.length || !age.length || !frequency.length || fund_amount == 0 || exchange_rate == 0 || !$('input[type=checkbox][name=resident_last_5]').is(':checked')) { // Form incomplete alert('Please complete all fields'); return false; } $.ajax({ dataType: "json", type: "POST", url: 'calculator/calculator.v2.js', data: { rate: rate, gender: gender, age: age, frequency: frequency, fund_amount: fund_amount, exchange_rate: exchange_rate }, success: function(data){ $('input[type=text][name=result_minimum]').val(data.min_amount); $('input[type=text][name=result]').val(data.base_amount); $('input[type=text][name=result_maximum]').val(data.max_amount); }, error: function(){ alert('There was a problem during the calculation'); }, }); } else { var rate = 0.036; // 10-year bond rate var nper = 0; // Remaining life expectency var pv = 0; // Fund amount entered by user var fv = 0; // Always be zero var type = 1; // Payments will always be in advance var result = 0; // What we need to work out! // Clean up user entered fund amount fund_amount = fund_amount.replace(/[^\d.]/g, ''); fund_amount = Math.round(fund_amount * 100) / 100; $('input[type=text][name=fund_amount]').val(parseFloat(fund_amount).toFixed(2)); if(!gender.length || !age.length || !frequency.length) { // Form incomplete return false; } if(!lifeExpectency[gender][age]) { // Unable to source remaining life expectency window.alert('Unable to source remaining life expectency'); return false; } nper = Math.round(lifeExpectency[gender][age] * 100) / 100; pv = fund_amount; // Adjust rate and nper to suit frequency switch(frequency.toLowerCase()) { case 'monthly': rate = rate / 12; nper = nper * 12; break; case 'quarterly': rate = rate / 4; nper = nper * 4; break; case 'six_monthly': rate = rate / 2; nper = nper * 2; break; case 'annually': // Nothing required break; } result = pmt(rate, nper, pv, fv, type) * -1; result = Math.round(result * 100) / 100; if($('input[type=text][name=result_minimum]')) { $('input[type=text][name=result_minimum]').val(parseFloat(result * 0).toFixed(2)); } if($('input[type=text][name=result]')) { $('input[type=text][name=result]').val(parseFloat(result).toFixed(2)); } if($('input[type=text][name=result_maximum]')) { $('input[type=text][name=result_maximum]').val(parseFloat(result * 1.5).toFixed(2)); } } return false; }); });