(function( window, document, $, undefined ){ var interestCalculation = { init : function(){ var defaults = {}, options = options ? options : {}; $( '#calculate, #btn-calculate, #penalty-results' ).hide(); $( '#deadline-year' ).on('change', function( evt ){ var year = $(this).val() - 1, $calculate = $( '#calculate' ); $calculate .find('#tax-year') .remove() .end() .find('strong') .after('<span id="tax-year"> for tax year ' + year + '</span>' ) .end() .show(); $( '#amount-owed' ).val(''); }); interestCalculation.getInputData(); }, acceptKeyCodes : function( key ) { /* * Backspace : 8 * End : 35 * Home : 36 * Arrow Left : 37 * Arrow Up : 38 * Arrow Right : 39 * Arrow Down : 40 * 0 : 48 * ... * 9 : 57 */ var acceptKeyCodes = [8, 35, 36, 37, 39, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]; return ( 0 > $.inArray( parseInt(evt.which, 10), acceptKeyCodes ) ); }, getInputData : function(){ $( '#amount-owed' ).on( 'keyup', function( evt ){ var $inputAmount = $( this ); $( '#too-many-digits' ).remove(); if( !acceptKeyCodes( event.which ) || 6 < $inputAmount.val().length ){ var inputValue = $inputAmount.val(); if( 6 < $inputAmount.val().length ){ $( '#amount-owed' ).after( '<span id="too-many-digits" class="error">Please no more than 6 digits.</span>' ); setTimeout( function(){ $( '#too-many-digits' ) .fadeOut( 'slow', function(){ $( this ).remove(); } ); }, 1000 ); } var inputValue = $inputAmount.val(); $inputAmount.val(inputValue.slice(0, 6)); } amount = $( '#amount-owed' ).val(); year = $( '#deadline-year' ).val(); intRate = $( '#interest-rate' ).val(); deadlineDate = new Date( year, 4, 15, 0, 0, 0, 0 ); filedDate = new Date(); penalty_data = { 'amount-owed' : amount, 'deadline-year' : year, 'interest-rate' : intRate }; interestCalculation.calculate( penalty_data); });/* END on keyup */ }, calculate : function( penalty_data){ $.ajax({ url : 'utilities/penalty_calculation.php', dataType : 'json', type : 'post', data : penalty_data, success : function( data, textStatus, jqXHR ){ interestCalculation.display( data.amountOwed, data.failureToFilePenalty, data.failureToPayPenalty, data.interestPenalty ); $( '#penalty-results' ).show(); }, error : function( jqXHR, textStatus, errorThrown ){ //console.log(jqXHR); // console.log(textStatus); // console.log(errorThrown); } } ); }, display : function( amountOwed, failureToFilePenalty, failureToPayPenalty, interestPenalty ){ var $resultsFragment = $(document.createDocumentFragment()), $penaltyResults = $( '#penalty-results' ); $resultsFragment.append( '<p><span class="penalty-type">Failure to File Penalty: </span>$<span id="file-penalty" class="penalty">' + failureToFilePenalty.toFixed(2) + '</span></p>' ); $resultsFragment.append( '<p><span class="penalty-type">Failure to Pay Penalty: </span>$<span id="pay-penalty" class="penalty">' + failureToPayPenalty.toFixed(2) +'</span></p>' ); $resultsFragment.append( '<p><span class="penalty-type">Interest: </span>$<span id="interest-penalty" class="penalty">' + interestPenalty.toFixed(2) + '</span></p>' ); $resultsFragment.append( '<p id="penalties"><span class="penalty-type">Total Penalties and Interest: </span>$<span class="penalty">' + (failureToFilePenalty + failureToPayPenalty + interestPenalty).toFixed(2) + '</span></p>' ); $resultsFragment.append( '<p><span class="penalty-type">Principle amount: </span>$<span id="principle-amount" class="penalty">' + amountOwed.toFixed(2) + '</span></p>' + '<p id="total-results"><span class="penalty-type">Total Bill: </span>$<span class="penalty">' + (+amountOwed + +failureToFilePenalty + +failureToPayPenalty + +interestPenalty).toFixed(2) + '</span> (Principal amount owed plus interest and penalties)</p>' ); $penaltyResults.empty(); $penaltyResults.append($resultsFragment); } };/* END object */ $.penaltyCalculation = function( method ){ method.init(); }; $.penaltyCalculation( interestCalculation ); })(window, this.document, jQuery);