﻿function MLX_MortgageCalculator() {
    this.price = 0;
    this.downPayment = null;
    this.downPercent = null;
    this.rate = 0;
    this.term = 0;
    this.type = '';
    this.monthlyPayment = 0;
    this.amount = 0;
    this.Load = mc_Load;
    this.CalculatePayment = _mc_CalculatePayment;
    this.SetResultVisibility = mc_SetResultVisibility;
    this.SaveCookie = mc_SaveCookie;
    this.GetMonthlyPayment = mc_GetMonthlyPayment;
    this.CalcWithDownPayment = mc_CalcWithDownPayment;
    this.CalcWithDownPercent = mc_CalcWithDownPercent;
    this.LoadFromCookie = mc_LoadFromCookie;
    this.GetFinOptionsFromCookie = mc_GetFinOptionsFromCookie
    this.LoadFromCookie = mc_LoadFromCookie;
    this.ShowDown = mc_ShowDown;
    this.GoToCalculator = mc_GoToCalculator;
    this.Clear = mc_Clear;
    this.DownPaymentChange = mc_DownPaymentChange;
    this.DownPercentChange = mc_DownPercentChange;
}
    
function mc_Load(listPrice){
    if (listPrice == null) 
        return;
    document.getElementById("hdnListPrice").value = listPrice;
    this.LoadFromCookie();
    this.listPrice = listPrice;
}

function _mc_CalculatePayment(isLoad) {

    this.price = document.getElementById("hdnListPrice").value;
    this.downPayment = document.getElementById("mc_txtDownPayment").value;
    this.downPercent = document.getElementById("mc_txtDownPercent").value;
    this.rate = document.getElementById("mc_txtInterestRate").value;
    this.term = document.getElementById("mc_cboTerm").value;

    if (isNaN(this.price) || this.price.length == 0) {
        if (!isLoad) 
            alert("The purchase price you entered is not numeric, please enter a numeric value.");
         return;
    }

    if (this.downPayment.length == 0 && this.downPercent.length == 0) {
        if (!isLoad)
            alert("Please enter a down payment dollar amount or percentage.");
         return;        
    }

    if (this.downPayment.length > 0) {
        if (isNaN(this.downPayment)) {
            if (!isLoad) 
                alert("The down payment you entered is not numeric, please enter a numeric value.");
            return;
        }
        else {
            this.amount = this.price - this.downPayment;
        }
    }
    else {
        if (this.downPercent.length == 0) {
            if (!isLoad) 
                alert("You must enter a down payment amount or percentage to complete the calculation.");
            return;
        }
        else {
            if (isNaN(this.downPercent)) {
                if (!isLoad) 
                    alert("The percentage you entered is not numeric, please enter a numeric value.");
                return;
            }
            else {
                this.amount = this.price - (this.price * (this.downPercent / 100))
            }
        }
    }
    this.monthlyPayment = this.GetMonthlyPayment(isLoad);
    
    if (this.monthlyPayment != "Error" && this.monthlyPayment > 0) {
        var payment = this.monthlyPayment.toString().format("C0")
        document.getElementById("mc_txtMonthlyPayment").innerHTML = payment;
        this.SaveCookie();
        
        $("#divMonthlyPayment").show();
        $("#pd_calculator_payment").html(payment);
        $(".pd_calculator_link_withvalue").show();
        $("#mc_clearEstimate").show();
        $(".pd_calculator_link_withoutvalue").hide();
    }
    else {
        $(".pd_calculator_link_withvalue").hide();
        $(".pd_calculator_link_withoutvalue").show();
        $("#mc_clearEstimate").hide();
    }
}

function mc_SetResultVisibility() {
    this.downPayment = document.getElementById("mc_txtDownPayment").value;
    this.downPercent = document.getElementById("mc_txtDownPercent").value;
    
    if ((this.downPercent.length == 0 || isNaN(this.downPercent))
            && (this.downPayment.length == 0 || isNaN(this.downPayment))) {
        $("#divMortgageAmount").hide();
        $("#divMonthlyPayment").hide();
        $(".pd_calculator_link_withvalue").hide();
        $(".pd_calculator_link_withoutvalue").show();
    }
    else {
        $(".pd_calculator_link_withvalue").show();
        $(".pd_calculator_link_withoutvalue").hide();
    }
}
function mc_DownPaymentChange() {
    document.getElementById("mc_txtDownPercent").value = "";
    document.getElementById("spnDownMessage").innerHTML = "";

    this.downPayment = document.getElementById("mc_txtDownPayment").value;

    this.SetResultVisibility(); 
    if (this.downPayment.length == 0)
        return;

    if (isNaN(this.downPayment)) {
        alert("The down payment is not a number, please enter a number.");
        return;
    }
   
    if (parseInt(this.downPayment) < parseInt(this.price))
        setTimeout("DownPayPctMsg(" + this.downPayment + ", " + this.price + ")", 500);
    else {
        $("#spnDownMessage").text("invalid down payment");
        $("#divMortgageAmount").hide();
        $("#divMonthlyPayment").hide();
    }
}

function DownPayPctMsg(x, y) {
    $("#spnDownMessage").text("(about " + Math.round(x / y * 100) + "%)");
}

function mc_DownPercentChange() {
    document.getElementById("mc_txtDownPayment").value = "";
    document.getElementById("spnDownMessage").innerHTML = "";
    
    this.price = document.getElementById("hdnListPrice").value;
    this.downPercent = document.getElementById("mc_txtDownPercent").value;

    this.SetResultVisibility(); 
    if (this.downPercent.length == 0)
        return;
    if (isNaN(this.downPercent)) {
        alert("The down percentage is not a number, please enter a number.");
        return;
    }
    
    this.downPayment = this.price * (this.downPercent / 100);

    if (this.downPayment < this.price)
        setTimeout("DownPayAmtMsg(" + this.downPayment + ")", 500);
    else {debugger;
        $("#spnDownMessage").text("invalid down payment pct");
        $("#divMortgageAmount").hide();
        $("#divMonthlyPayment").hide();
    }
}

function DownPayAmtMsg(x) {
    $("#spnDownMessage").text("(" + formatCurrency(x) + ")");
}

function mc_CalcWithDownPayment(isLoad) {
    this.price = document.getElementById("hdnListPrice").value;
    this.downPayment = document.getElementById("mc_txtDownPayment").value;

    this.SetResultVisibility();
    if (this.downPayment.length == 0) 
        return;
    
    if (isNaN(this.downPayment)) {
        alert("The down payment is not a number, please enter a number.");
        return;
    }
    document.getElementById("mc_txtDownPercent").value = "";
    document.getElementById("spnDownMessage").innerHTML = "(about " + Math.round(this.downPayment / this.price * 100) + "%)";
    this.CalculatePayment(isLoad);
    this.ShowDown(isLoad);   
}

function mc_ShowDown(isLoad) { 
    
    document.getElementById("hdnLoanAmount").value = this.amount;
    document.getElementById("txtMortgageAmount").innerHTML = formatCurrency(this.amount);
    if (!isLoad)
        document.getElementById("mc_txtInterestRate").focus();
   $("#divMortgageAmount").show();
}

function mc_CalcWithDownPercent(isLoad) {
    this.price = document.getElementById("hdnListPrice").value;
    this.downPercent = document.getElementById("mc_txtDownPercent").value;

    this.SetResultVisibility();
    if (this.downPercent.length == 0) 
        return;
    if (isNaN(this.downPercent)) {
        alert("The down percentage is not a number, please enter a number.");
        return;
    }
    this.downPayment = this.price * (this.downPercent / 100);

    document.getElementById("spnDownMessage").innerHTML = "(" + formatCurrency(this.downPayment) + ")";
    document.getElementById("mc_txtDownPayment").value = "";
    this.CalculatePayment(isLoad);
    this.ShowDown(isLoad);
}

function mc_LoadFromCookie() {
    var opts = this.GetFinOptionsFromCookie();
    if (opts != null) {
        document.getElementById("mc_txtDownPayment").value = !isNaN(opts.downPayment) ? opts.downPayment : "";
        document.getElementById("mc_txtDownPercent").value = !isNaN(opts.downPercent) ? opts.downPercent : "";
        document.getElementById("mc_txtInterestRate").value = opts.rate;
        SetDropDown(document.getElementById("mc_cboTerm"), opts.term);
        if (opts.downPayment.length > 0)
            this.CalcWithDownPayment(true);
        else
            this.CalcWithDownPercent(true);
    }
}

function mc_SaveCookie() {
    var val = this.downPayment + "," + this.downPercent + "," + this.rate + "," + this.term;
    SE_SetCookie("mc_FinOptions", val, AddDays(Date(), 180));
}

function mc_FinOptions(downPayment, downPercent, rate, term) {
    this.downPayment = downPayment;
    this.downPercent = downPercent;
    this.rate = rate;
    this.term = term;
}

function mc_GetFinOptionsFromCookie()
{
    var val = getCookie("mc_FinOptions");
	if(val!=null)
	{
		var a = val.split(",");
		return new finOptions(a[0],a[1],a[2],a[3]);
	}
	else
	{
		return null;
	}
}	

function mc_GetMonthlyPayment(isLoad) {
    var IsValid = true;

    if (isNaN(this.amount) || this.amount.length == 0) {
        if (!isLoad)
            alert("The mortgage amount that you entered is not numeric. Please enter a numeric value.");
        IsValid = false;
    }

    if (isNaN(this.rate)) {
        if (!isLoad)
            alert("The interest rate that you entered is not numeric. Please enter a numeric value.");
        IsValid = false;
    }

    if (isNaN(this.term)) {
        if (!isLoad)
            alert("The loan term that you entered is not numeric. Please enter a numeric value.");
        IsValid = false;
    }

    if (IsValid) {
        if (parseFloat(this.rate) >= 0.01 && parseFloat(this.rate) <= 99.99) {
            var totalPayments = parseInt(this.term) * 12;
            var monthlyInterest = this.rate / 1200;
            var principal = parseFloat(this.amount);
            if (this.term > 0) {
                var num = Math.pow(1 + monthlyInterest, totalPayments);
                var monthly = monthly = principal * monthlyInterest * num / (num - 1);
                return monthly;
            }
            else {
                return (monthlyInterest * principal);
            }
        }
        else {
            if (!isLoad)
                alert("The interest rate must be a number between 0 and 100.");
            return "Error";
        }
    }
}

function mc_Clear() {
    document.getElementById("mc_txtDownPayment").value = "";
    document.getElementById("mc_txtDownPercent").value = "";
    document.getElementById("spnDownMessage").innerHTML = "";
    SetDropDown(document.getElementById("mc_cboTerm"), "30");
    this.downPayment = null;
    this.downPercent = null;
    this.term = "";
    this.SaveCookie();
    this.SetResultVisibility();
}

function mc_GoToCalculator() {
    
    $(window).scrollTo(document.getElementById("mc_txtDownPayment"), 500,
        { onAfter: function() {
            $(".pd_calculator").effect("bounce", { times:3 }, 300);
            document.getElementById("mc_txtDownPayment").focus();
        }}
    );   
}

