-
Notifications
You must be signed in to change notification settings - Fork 42
/
jquery.accrue.min.js
1 lines (1 loc) · 5.98 KB
/
jquery.accrue.min.js
1
!function(u){function _(t,e){return new Intl.NumberFormat(e.language,{style:"currency",currency:e.currency}).format(t)}u.extend(u.fn,{accrue:function(o){return o=u.extend({calculationMethod:t},u.fn.accrue.options,o),this.each(function(){var e,a,n=u(this);n.find(".form").length||n.append('<div class="form"></div>'),d(n,o,"amount"),d(n,o,"rate"),d(n,o,"term");switch("compare"==o.mode&&d(n,o,"rate_compare"),e=".results"===o.response_output_div?(0===n.find(".results").length&&n.append('<div class="results"></div>'),n.find(".results")):u(o.response_output_div),o.mode){case"basic":a=t;break;case"compare":a=r;break;case"amortization":a=m}a(n,o,e),"button"==o.operation?(0===n.find("button").length&&0===n.find("input[type=submit]").length&&0===n.find("input[type=image]").length&&n.find(".form").append('<button class="accrue-calculate">'+o.button_label+"</button>"),n.find("button, input[type=submit], input[type=image]").each(function(){u(this).click(function(t){t.preventDefault(),a(n,o,e)})})):n.find("input, select").each(function(){u(this).bind("keyup change",function(){a(n,o,e)})}),n.find("form").each(function(){u(this).submit(function(t){t.preventDefault(),a(n,o,e)})})})}}),u.fn.accrue.options={mode:"basic",operation:"keyup",currency:"USD",language:"en",default_values:{amount:"7,500",rate:"7%",rate_compare:"1.49%",term:"36m"},field_titles:{amount:"Loan Amount",rate:"Rate (APR)",rate_compare:"Comparison Rate",term:"Term"},button_label:"Calculate",field_comments:{amount:"",rate:"",rate_compare:"",term:"Format: 12m, 36m, 3y, 7y"},response_output_div:".results",response_basic:"<p><strong>Monthly Payment:</strong><br />%payment_amount%</p><p><strong>Number of Payments:</strong><br />%num_payments%</p><p><strong>Total Payments:</strong><br />%total_payments%</p><p><strong>Total Interest:</strong><br />%total_interest%</p>",response_compare:'<p class="total-savings">Save %savings% in interest!</p>',error_text:'<p class="error">Please fill in all fields.</p>',callback:function(t,e){}};var d=function(t,e,a){var n;return t.find(".accrue-"+a).length?n=t.find(".accrue-"+a):t.find("."+a).length?n=t.find("."+a):t.find("input[name~="+a+"]").length?t.find("input[name~="+a+"]"):n="","string"!=typeof n?n.val():"term_compare"!=a&&(t.find(".form").append('<div class="accrue-field-'+a+'"><p><label>'+e.field_titles[a]+':</label><input type="text" class="'+a+'" value="'+e.default_values[a]+'" />'+(0<e.field_comments[a].length?"<small>"+e.field_comments[a]+"</small>":"")+"</p></div>"),t.find("."+a).val())},t=function(t,e,a){var n,o=u.loanInfo({amount:d(t,e,"amount"),rate:d(t,e,"rate"),term:d(t,e,"term")});0!==o?(n=e.response_basic.replace("%payment_amount%",_(o.payment_amount_formatted,e)).replace("%num_payments%",o.num_payments).replace("%total_payments%",_(o.total_payments_formatted,e)).replace("%total_interest%",_(o.total_interest_formatted,e)),a.html(n)):a.html(e.error_text),e.callback(t,o)},r=function(t,e,a){var n=d(t,e,"term_compare"),o=("boolean"==typeof n&&(n=d(t,e,"term")),u.loanInfo({amount:d(t,e,"amount"),rate:d(t,e,"rate"),term:d(t,e,"term")})),n=u.loanInfo({amount:d(t,e,"amount"),rate:d(t,e,"rate_compare"),term:n}),r={loan_1:o,loan_2:n};0!==o&&0!==n?(0<o.total_interest-n.total_interest?r.savings=o.total_interest-n.total_interest:r.savings=0,n=e.response_compare.replace("%savings%",_(r.savings.toFixed(2),e)).replace("%loan_1_payment_amount%",_(n.payment_amount_formatted,e)).replace("%loan_1_num_payments%",n.num_payments).replace("%loan_1_total_payments%",n.total_payments_formatted).replace("%loan_1_total_interest%",_(n.total_interest_formatted,e)).replace("%loan_2_payment_amount%",_(o.payment_amount_formatted,e)).replace("%loan_2_num_payments%",o.num_payments).replace("%loan_2_total_payments%",o.total_payments_formatted).replace("%loan_2_total_interest%",_(o.total_interest_formatted,e)),a.html(n)):a.html(e.error_text),e.callback(t,r)},m=function(t,e,a){var n=u.loanInfo({amount:d(t,e,"amount"),rate:d(t,e,"rate"),term:d(t,e,"term")});if(0!==n){for(var o='<table class="accrue-amortization"><thead><tr><th class="accrue-payment-number">#</th><th class="accrue-payment-amount">Payment Amt.</th><th class="accrue-total-interest">Total Interest</th><th class="accrue-total-payments">Total Payments</th><th class="accrue-balance">Balance</th></tr></thead><tbody>',r=n.payment_amount-n.original_amount/n.num_payments,m=n.payment_amount-r,l=0,s=0,i=parseInt(n.original_amount,10),c=0;c<n.num_payments;c++){l+=r,s+=n.payment_amount,i-=m;var p="td",o=o+"<tr><"+(p=c==n.num_payments-1?"th":p)+' class="accrue-payment-number">'+(c+1)+"</"+p+"><"+p+' class="accrue-payment-amount">'+_(n.payment_amount_formatted,e)+"</"+p+"><"+p+' class="accrue-total-interest">'+_(l.toFixed(2),e)+"</"+p+"><"+p+' class="accrue-total-payments">'+_(s.toFixed(2),e)+"</"+p+"><"+p+' class="accrue-balance">'+_(i.toFixed(2),e)+"</"+p+"></tr>"}a.html(o+="</tbody></table>")}else a.html(e.error_text);e.callback(t,n)};u.loanInfo=function(t){var e=(void 0!==t.amount?t.amount:0).toString().replace(/[^\d.]/gi,""),a=(void 0!==t.rate?t.rate:0).toString().replace(/[^\d.]/gi,""),t=(t=void 0!==t.term?t.term:0).match("y")?12*parseInt(t.replace(/[^\d.]/gi,""),10):parseInt(t.replace(/[^\d.]/gi,""),10),n=a/100/12,o=Math.pow(1+n,t),n=e*o*n/(o-1);return 0<e*a*t?{original_amount:e,payment_amount:n,payment_amount_formatted:n.toFixed(2),num_payments:t,total_payments:n*t,total_payments_formatted:(n*t).toFixed(2),total_interest:n*t-e,total_interest_formatted:(n*t-e).toFixed(2)}:0},u.loanAmount=function(t){var e=(void 0!==t.payment?t.payment:0).toString().replace(/[^\d.]/gi,""),a=(void 0!==t.rate?t.rate:0).toString().replace(/[^\d.]/gi,""),t=(t=void 0!==t.term?t.term:0).match("y")?12*parseInt(t.replace(/[^\d.]/gi,""),10):parseInt(t.replace(/[^\d.]/gi,""),10),n=a/100,a=e*(1-Math.pow(1+a/100/12,-1*t))*(12/n);return 0<a?{principal_amount:a,principal_amount_formatted:a.toFixed(2),payment_amount:e,payment_amount_formatted:(+e).toFixed(2),num_payments:t,total_payments:e*t,total_payments_formatted:(e*t).toFixed(2),total_interest:e*t-a,total_interest_formatted:(e*t-a).toFixed(2)}:0}}(jQuery,(window,document));