var fasoon = fasoon || {}; (function (ns, $) { "use strict"; ns.QuotePriceStep5 = function () { this.construct.apply(this, arguments); }; var proto = ns.QuotePriceStep5.prototype; proto.construct = function (options) { this.options = $.extend({ form_id: undefined, shares_number_id: undefined, equity_capital_id: undefined, share_price_id: undefined, share_text: {}, currency: 'CHF' }, options); this.form = $('#' + this.options.form_id); if (!this.form.length) { alert('Form #' + this.options.form_id + ' is not found.'); return; } this.shares_number = $('#' + this.options.shares_number_id, this.form); if (this.shares_number.length) { this.equity_capital = $('#' + this.options.equity_capital_id, this.form); this.share_price = $('#' + this.options.share_price_id, this.form); if (this.equity_capital.length && this.share_price.length) { this.equity_capital.on('change', $.proxy(this.calculate_shares_number, this)); this.share_price.on('change', $.proxy(this.calculate_shares_number, this)); this.calculate_shares_number(); } } $('.price', this.form).autoNumeric( 'init', { /*mRound: this.options.currency,*/ aSign: this.options.currency + ' ', pSign: 'p', aSep: "'", lZero: 'deny', vMin: 0.00, vMax: 999999999.99, mDec: 2 } ).on('focus', function () { this.setSelectionRange(4, this.value.length); }); $('.number', this.form).autoNumeric( 'init', { aSep: "'", lZero: 'deny', vMin: '1', vMax: '999999999.99', mDec: 0 } ).on('focus', function () { this.setSelectionRange(0, this.value.length); }); }; proto.calculate_shares_number = function () { var shares_number = ''; var capital = this.equity_capital.val(); capital = parseFloat(capital.replace(/[^.0-9]+/g, '')); var price = this.share_price.val(); price = parseFloat(price.replace(/[^.0-9]+/g, '')); if (!isNaN(capital) && !isNaN(price) && capital >= price) { shares_number = parseInt( Math.ceil( capital / price ) ); } var text_key = 'plural'; if (shares_number == 1) { text_key = 'singular'; } this.shares_number.val(this.format(shares_number, 0, "'") + ' ' + this.options.share_text[text_key]); }; proto.format = function(n, decPlaces, thouSeparator, decSeparator) { var decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces, decSeparator = decSeparator == undefined ? "." : decSeparator, thouSeparator = thouSeparator == undefined ? "," : thouSeparator, sign = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "", j = (j = i.length) > 3 ? j % 3 : 0; return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : ""); }; })(fasoon, jQuery);