jQuery(function() { var hkh = hkh || {}; hkh.mortgageLightBox = hkh.mortgageLightBox || null; var loan_term = getCookie('mortgage_term'); var loan_rate = getCookie('mortgage_rate'); var loan_perc = getCookie('mortgage_perc'); var min_loan_term = 1; if (navigator.userAgent.indexOf('MSIE 6') != -1) { jQuery('
Mortgage Calculator
') .css('top', -199) .appendTo(jQuery('body')); } else { jQuery('
Mortgage Calculator
') .css('top', -199) .appendTo(jQuery('body')); } //jQuery('#mortgage_lightbox').pngFix(); jQuery('.price_ask .rent_price_amt').each(function(n) { //alert(jQuery(this).html()); var mortgage_box = jQuery('
') .appendTo(jQuery(this).parent()); jQuery('Monthly Repayment:').appendTo(mortgage_box); // mortgage amt var loan_amount = jQuery(this).html().replace(/,/g, ''); if (isNaN(loan_term) || loan_term == '') loan_term = 20; if (loan_term < min_loan_term) loan_term = min_loan_term; if (isNaN(loan_rate) || loan_rate == '') loan_rate = 1.5; if (isNaN(loan_perc) || loan_perc == '') loan_perc = 70; var mort_calc = new MortgageCalc(loan_amount * loan_perc / 100, loan_term, loan_rate); jQuery('' + formatPrice2(mort_calc.monthly_payment(), true) +'') .click ( function(e) { if (hkh.mortgageLightBox == null) hkh.mortgageLightBox = new MortgageLightBox(); hkh.mortgageLightBox.setCaller(this); hkh.mortgageLightBox.setAmt(jQuery(this).parent().parent().children('.rent_price_amt').html().replace(/,/g,'')); hkh.mortgageLightBox.show(); }) .appendTo(mortgage_box); }); // copy from templates/info_mortgage.php function formatPrice2(val, is_with_currency_sym) { if (is_with_currency_sym == undefined || is_with_currency_sym == true) return CURRENCY_SYM + ' ' + $.formatNumber(val, { format:"#,###" }); else return $.formatNumber(val, { format:"#,###" }); } // copy from templates/info_mortgage.php function parseNumber2(num, is_allow_dot, max_num) { if (is_allow_dot) { var non_decimal = /[^0-9.]/g; if (non_decimal.test(num)) return 0; else if (max_num != undefined && parseFloat(num) > parseFloat(max_num)) return max_num; else return num; } else { // object from jquery.numberformatter var num = Number(num); if (isNaN(num)) return 0; else { if (num.toString().indexOf('.') != -1) return 0; else if (max_num != undefined && parseFloat(num) > parseFloat(max_num)) return max_num; else return num; } } } function unformatNumber2(num) { return num.toString().replace(/[^0-9]/g, ''); } var MortgageLightBox = function() { var prev_caller = null; var caller = null; var amt = 0; var shown = false; var lightbox = null; this.setCaller = function (caller) { this.prev_caller = this.caller; this.caller = caller; } this.setAmt = function (amt) { this.amt = amt; if (jQuery('#mortgage_org_amt')) jQuery('#mortgage_org_amt').val(this.amt); } this.show = function () { if (this.lightbox == null) this._init(); if (this.prev_caller == null) { this._show(); } else if (this.prev_caller != this.caller && this.shown) { this._moveshow(); } else if (this.prev_caller != this.caller && !this.shown) { this._show(); } else if (this.prev_caller == this.caller && !this.shown) { this._show(); } } this.hide = function () { this._hide(); } this._init = function () { if (this.lightbox != null) return; this.lightbox = jQuery('#mortgage_lightbox'); this.lightbox.show(); // close button jQuery('#mortgage_lightbox_close').click( function(e) { hkh.mortgageLightBox.hide(); }); // content jQuery('
Mortgage Ratio (%)
Loan Amount (' + CURRENCY + ')
Loan Period (years)
Interest Rate (%)
') .appendTo(jQuery('#mortgage_lightbox_mid')); // keyup effect of calculator jQuery('#mortgage_amt_perc').keyup( function () { if (isNaN(jQuery(this).val())) jQuery(this).val(loan_perc); else if (jQuery(this).val() < 0 || jQuery(this).val() == '') jQuery(this).val(0); else if (jQuery(this).val() > 100) jQuery(this).val(100); jQuery('#mortgage_amt').val(formatPrice2(jQuery(this).val() * jQuery('#mortgage_org_amt').val() / 100, false)); loan_perc = parseFloat(jQuery(this).val()); hkh.mortgageLightBox._updateMortgage(); setCookie('mortgage_perc', loan_perc, false); }); jQuery('#mortgage_amt').keyup( function () { amt = parseNumber2(unformatNumber2(this.value), false, jQuery('#mortgage_org_amt').val()); jQuery(this).val( formatPrice2(amt, false) ); jQuery('#mortgage_amt_perc').val( ((amt / jQuery('#mortgage_org_amt').val() * 100) + '').substr(0, 4) ); loan_perc = parseFloat(jQuery('#mortgage_amt_perc').val()); hkh.mortgageLightBox._updateMortgage(); setCookie('mortgage_perc', loan_perc, false); }); jQuery('#mortgage_term_year').keyup( function () { if (isNaN(jQuery(this).val())) jQuery(this).val(loan_perc); else if (jQuery(this).val() < 0 || jQuery(this).val() == '') jQuery(this).val(0); else if (jQuery(this).val() > 30) { jQuery(this).val(30); jQuery('#mortgage_term_month').val(0); alert('Maximum mortgage term is: 30'); } else if (jQuery(this).val() == 30) jQuery('#mortgage_term_month').val(0); else jQuery(this).val(parseInt(jQuery(this).val())); loan_term = jQuery('#mortgage_term_month').val() / 12 + jQuery(this).val() / 1; hkh.mortgageLightBox._updateMortgage(); setCookie('mortgage_term', loan_term, false); }); jQuery('#mortgage_rate').keyup( function () { if (isNaN(jQuery(this).val())) jQuery(this).val(loan_perc); else if (jQuery(this).val() < 0 || jQuery(this).val() == '') jQuery(this).val(0); else if (jQuery(this).val() > 100) jQuery(this).val(100); // else // jQuery(this).val(parseFloat(jQuery(this).val())); loan_rate = parseFloat(jQuery(this).val()); hkh.mortgageLightBox._updateMortgage(); setCookie('mortgage_rate', loan_rate, false); }); jQuery(window).resize( function () { if (hkh.mortgageLightBox != null) hkh.mortgageLightBox._afterResize(); }); this._pngFix(); } this._show = function() { this.lightbox.stop(); var offset = jQuery(this.caller).offset(); this.lightbox.css('top', offset.top + 12); var offset = jQuery(this.caller).parent().offset(); this.lightbox.css('left', offset.left - 15 ); //this.lightbox.animate({ opacity: 1}, 500); //this.lightbox.fadeIn('500'); this.lightbox.show(); this.shown = true; this._updateCurrent(); // this.container.main.animate({ height: 'show' }, 1000, 'easeOutBounce'); } this._moveshow = function() { var offset = jQuery(this.caller).offset(); this.lightbox.animate({ top: offset.top + 12 }, 800); this.shown = true; this._updateCurrent(); } this._afterResize = function () { if (this.shown) { var offset = jQuery(this.caller).offset(); this.lightbox.css('top', offset.top + 12); offset = jQuery(this.caller).parent().offset(); this.lightbox.css('left', offset.left - 15 ); } } this._hide = function() { //this.lightbox.animate({ opacity: 0}, 500); //this.lightbox.fadeOut('500'); this.lightbox.hide(); this.shown = false; } this._setCookie = function(key, val) { setCookie(key, val, false); } this._pngFix = function() { if (navigator.userAgent.indexOf('MSIE 6') != -1) { // fix css background pngs $('#mortgage_lightbox').css('background-image', 'none'); $('#mortgage_lightbox').find("*").each( function() { var bg_img = $(this).css('background-image'); if(bg_img.indexOf(".png")!=-1){ bg_img = bg_img.substring(5, bg_img.length - 2); this.style.background = 'none'; this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+bg_img+"', sizingMethod='scale')"; } }); } } this._updateCurrent = function() { jQuery('#mortgage_amt').val(formatPrice2(this.amt * loan_perc / 100, false)); } this._updateMortgage = function () { jQuery('.price_ask .rent_price_amt').each(function(n) { var mort_calc = new MortgageCalc(jQuery(this).html().replace(/,/g, '') * loan_perc / 100, loan_term, loan_rate); if (mort_calc.monthly_payment() == Infinity) jQuery(this).parent().find('.mortgage_box .mortgage_amt').html(CURRENCY_SYM + '  ' + '-'); else jQuery(this).parent().find('.mortgage_box .mortgage_amt').html(formatPrice2(mort_calc.monthly_payment(), true)); }); } this._init(); } });