﻿//========================================================
// START: Mortgage Calc
//========================================================

var MCalc = function(){};
//========================================================
//   formatCurrency
//   by passing numeric value it will return in $000,000.00
//========================================================
MCalc.prototype.formatCurrency = function(num) 
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' +  num );//+ '.' + cents
};
//========================================================
//   formatCurrency
//   by passing numeric value it will return in $000,000.00
//========================================================
MCalc.prototype.IsNumeric = function(theValue) 
{
//    theValue = theValue.replace(RegExNumberReplace,'');
//	if (!RegExNumberFormat.test(theValue)) 
//	{
//        return false;
//    }
//    return true;
///// Updated - Madhavi
    theValue = theValue.replace(/\d*(\.\d*)?/,'');
	if (theValue != '') 
	{
        return false;
    }
    return true;
};

//========================================================
//  calculateMortgage
//  takes values loan term,interest rate,down payment and returns 
//  estimated monthly payment
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  Built for JQuery
//========================================================
MCalc.prototype.calculateMortgage = function()
{  
    var askPrice="";
    
        askPrice = $("#MC-AskPrice").val().replace(/[^0-9\.]+/g,"");
//        if (!MC.IsNumeric(askPrice))
//        {
//            alert("Please enter numeric value in Price Field");
//            $("#MC-AskPrice").focus();
//            return;   
//        }
         
    askPrice = Math.round(parseFloat(askPrice));
    
    var percentDown = $("#MC-PercentDown").val();
    if (!MC.IsNumeric(percentDown) || $("#MC-PercentDown").val() == "")
    {
        window.alert("Please enter a numeric value for Percent Down");
        $("#MC-PercentDown").focus();
        return;   
    }
    
    var loanTerm = $("#MC-LoanTerm").val();
    if (!MC.IsNumeric(loanTerm) || $("#MC-LoanTerm").val() == "")
    {
        window.alert("Please enter a numeric value for Loan Term");
        $("#MC-LoanTerm").focus();
        return;
    }
    loanTerm = parseFloat($("#MC-LoanTerm").val())*12;
   
    var interestRate = $("#MC-InterestRate").val();
    if (!MC.IsNumeric(interestRate) || $("#MC-InterestRate").val() == "")
    {
        window.alert("Please enter a numeric value for Interest Rate");
        $("#MC-InterestRate").focus();
        return;
    }
    interestRate = (parseFloat($("#MC-InterestRate").val())/100)/12;   
   
    var principalBalance = (askPrice)-((askPrice*percentDown)/100)
   
    var monthlyMortgagePayment = 0;
        monthlyMortgagePayment = (interestRate+(interestRate/(Math.pow(1+interestRate,loanTerm)-1)))*principalBalance;
        
    var downPayment = 0;
        downPayment = askPrice-principalBalance;
    
    $("#MC-MonthlyMortgagePayment").html(MC.formatCurrency(Math.round(monthlyMortgagePayment)));
    $("#MC-PrincipalBalance").html(MC.formatCurrency(Math.round(principalBalance)));
    $("#MC-DownPayment").html(MC.formatCurrency(Math.round(downPayment)));
   
};


MCalc.prototype.resetMortgageValue = function() {
    var askPrice = Math.round(parseInt($("#MC-AskPrice").val().replace(/[^0-9\.]+/g,""), 10)); 
    
    $("#MC-PercentDown").val("20");
    $("#MC-LoanTerm").val("30");
    //$("#MC-InterestRate").html(MC.formatCurrency(CacheObject.APR)); 
    //alert(MC.formatCurrency(CacheObject.APR)); 
    //alert((parseInt($("#MC-InterestRate").val(),10)/100)/12);   
    var interestRate = (parseInt($("#MC-InterestRate").val(),10)/100)/12;
    var principalBalance= (askPrice)-((askPrice*parseInt($("#MC-PercentDown").val()))/100)
    var monthlyMortgagePayment = 0;    
        monthlyMortgagePayment = (interestRate+(interestRate/(Math.pow(1+interestRate,(parseInt($("#MC-LoanTerm").val())*12))- 1)))*principalBalance;
    var downPayment = 0;
        downPayment = askPrice-principalBalance;
    $("#MC-MonthlyMortgagePayment").html("$--");
    $("#MC-PrincipalBalance").html("$--");
    $("#MC-DownPayment").html("$--");
};

var MC = new MCalc();
//========================================================
// END: Mortgage Calc
//========================================================
