var App_SimpleBlock_Calculator = qs.createObject(); App_SimpleBlock_Calculator.prototype = { types: { liteWindow: 'lite', singleWindow: 'single', doubleWindow: 'double' }, prices: { liteWindow: 150.20, singleWindow: 170.50, doubleWindow: 190.80, economy: 0.9, premium: 1.1, premiumPlus: 1.2 }, preSelector: '#window-', initialize: function (prices) { this.prices = prices; this.enableDigits(); var that = this; $.each(this.types, function (k, type) { that.calculatePrice(k, type); $(that.preSelector + type + '-height').change(function(){that.calculatePrice(k, type)}); $(that.preSelector + type + '-width').change(function(){that.calculatePrice(k, type)}); }); }, calculatePrice: function (k, type) { var height = $(this.preSelector + type + '-height').val() / 1000; var width = $(this.preSelector + type + '-width').val() / 1000; var x = (height*width)*this.prices[k]; var xe = x * this.prices['economy']; var xp = x * this.prices['premium']; var xpp = x * this.prices['premiumPlus']; $(this.preSelector + type + '-price').val(x.toFixed(2) + ' €'); $(this.preSelector + type + '-price-economy').val(xe.toFixed(2) + ' €'); $(this.preSelector + type + '-price-premium').val(xp.toFixed(2) + ' €'); $(this.preSelector + type + '-price-premium-plus').val(xpp.toFixed(2) + ' €'); }, enableDigits: function () { $(".digits-only").keydown(function (e) { // Allow: backspace, delete, tab, escape, enter and . if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 || // Allow: Ctrl+A (e.keyCode == 65 && e.ctrlKey === true) || // Allow: Ctrl+C (e.keyCode == 67 && e.ctrlKey === true) || // Allow: Ctrl+X (e.keyCode == 88 && e.ctrlKey === true) || // Allow: home, end, left, right (e.keyCode >= 35 && e.keyCode <= 39)) { // let it happen, don't do anything return; } // Ensure that it is a number and stop the keypress if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { e.preventDefault(); } }); } };