﻿var debugging = false;

function calculate() {
    try
    {
        if(debugging)
        {
            alert("[calculate] Running.");
        }
        
        // Get the user's input from the form. Assume it is all valid.
        // Convert interest from a percentage to a decimal, and convert from
        // an annual rate to a monthly rate. Convert payment period in years
        // to the number of monthly payments.
    	
	    if(document.getElementById("ctl00_cphStage_MortgageCalculator_downpayment").value == "" || document.getElementById("ctl00_cphStage_MortgageCalculator_downpayment").value < 0){
		    document.getElementById("ctl00_cphStage_MortgageCalculator_downpayment").value = 0;
	    }
    	
        var principal = document.getElementById("ctl00_cphStage_MortgageCalculator_principal").value - document.getElementById("ctl00_cphStage_MortgageCalculator_downpayment").value;
        var interest = document.getElementById("ctl00_cphStage_MortgageCalculator_interest").value / 100 / 12;
        var payments = document.getElementById("ctl00_cphStage_MortgageCalculator_years").value * 12;
        var fee = round(document.getElementById("ctl00_cphStage_MortgageCalculator_fee").value);
        var taxes = (principal * (document.getElementById("ctl00_cphStage_MortgageCalculator_tax").value/100)) / 12;

        // Now compute the monthly payment figure, using esoteric math.
        var x = Math.pow(1 + interest, payments);
        var monthly = (principal*x*interest)/(x-1);

        // Check that the result is a finite number. If so, display the results
        if (!isNaN(monthly) && 
            (monthly != Number.POSITIVE_INFINITY) &&
            (monthly != Number.NEGATIVE_INFINITY)) {
            if(debugging)
            {
                alert("[calculate] Setting Payment.");
            }
            document.getElementById("payment").value = round(monthly);
            if(debugging)
            {
                alert("[calculate] Payment: "+document.getElementById("payment").value);
            }
        } else {
        // Otherwise, the user's input was probably invalid, so don't
        // display anything.
            if(debugging)
            {
                alert("[calculate] monthly isNaN");
            }
            document.getElementById("payment").value = "";
            document.getElementById("monthly").value = "";
        }
        
        if (!isNaN(taxes) && 
            (taxes != Number.POSITIVE_INFINITY) &&
            (taxes != Number.NEGATIVE_INFINITY)) {
            if(debugging)
            {
                alert("[calculate] Setting Monthly.");
            }
                document.getElementById("ctl00_cphStage_MortgageCalculator_monthly").value = round(monthly + taxes + fee);
            
            if(debugging)
            {
                alert("[calculate] Monthly: "+document.getElementById("ctl00_cphStage_MortgageCalculator_monthly").value);
            }                
                
        } else {
            if(debugging)
            {
                alert("[calculate] taxes isNaN");
            }
            document.getElementById("ctl00_cphStage_MortgageCalculator_tax").value = "";
            document.getElementById("ctl00_cphStage_MortgageCalculator_fee").value = "";
            document.getElementById("ctl00_cphStage_MortgageCalculator_monthly").value = "";
        }
    }
    catch(err)
    {
        if(debugging)
        {
            alert(err);
        }
    }
}

function calculate_additional() {
    try
    {
        if(debugging)
        {
            alert("[calculate_additional] Running.");
        }
        // Get the user's input from the form. Assume it is all valid.
        // Convert interest from a percentage to a decimal, and convert from
        // an annual rate to a monthly rate. Convert payment period in years
        // to the number of monthly payments.
    	
	    if(document.getElementById("ctl00_cphStage_MortgageCalculator_downpayment").value == "" || document.getElementById("ctl00_cphStage_MortgageCalculator_downpayment").value < 0){
		    document.getElementById("ctl00_cphStage_MortgageCalculator_downpayment").value = 0;
	    }
    	
        var principal = document.getElementById("ctl00_cphStage_MortgageCalculator_principal").value - document.getElementById("ctl00_cphStage_MortgageCalculator_downpayment").value;
        var interest = document.getElementById("ctl00_cphStage_MortgageCalculator_interest").value / 100 / 12;
        var payments = document.getElementById("ctl00_cphStage_MortgageCalculator_years").value * 12;
        var fee = round(document.getElementById("ctl00_cphStage_MortgageCalculator_fee").value);
        var taxes = (principal * (document.getElementById("ctl00_cphStage_MortgageCalculator_tax").value/100)) / 12;

	    if(principal == ""){
		    alert("\"Price of The Home\" cannot be zero.");
		    document.getElementById("ctl00_cphStage_MortgageCalculator_principal").focus();
		    return false;
	    }
	    if(interest == ""){
		    alert("\"Annual Interest Rate\" cannot be zero.");
		    document.getElementById("ctl00_cphStage_MortgageCalculator_interest").focus();
		    return false;
	    }
	    if(payments == ""){
		    alert("\"Term of Loan in Years\" cannot be zero.");
		    document.getElementById("ctl00_cphStage_MortgageCalculator_years").focus();
		    return false;
	    }

        // Now compute the monthly payment figure, using esoteric math.
        var x = Math.pow(1 + interest, payments);
        var monthly = (principal*x*interest)/(x-1);

        // Check that the result is a finite number. If so, display the results
        if (!isNaN(monthly) && 
            (monthly != Number.POSITIVE_INFINITY) &&
            (monthly != Number.NEGATIVE_INFINITY)) {

            document.getElementById("payment").value = round(monthly);            
        }
        else {
        // Otherwise, the user's input was probably invalid, so don't
        // display anything.
            if(debugging)
            {
                alert("[calculate_additional] taxes isNaN");
            }
            document.getElementById("payment").value = "";
            document.getElementById("monthly").value = "";
        }
        if (!isNaN(taxes) && 
            (taxes != Number.POSITIVE_INFINITY) &&
            (taxes != Number.NEGATIVE_INFINITY)) {
            document.getElementById("ctl00_cphStage_MortgageCalculator_monthly").value = round(monthly + taxes + fee);            
        }
        else {
            if(debugging)
            {
                alert("[calculate_additional] taxes isNaN");
            }
            document.getElementById("ctl00_cphStage_MortgageCalculator_tax").value = "";
            document.getElementById("ctl00_cphStage_MortgageCalculator_fee").value = "";
            document.getElementById("ctl00_cphStage_MortgageCalculator_monthly").value = "";
        }
    }
    catch(err)
    {
        if(debugging)
        {
            alert(err);
        }
    }
}

// This simple method rounds a number to two decimal places.
function round(x) {
    var returnValue;
    try
    {
        returnValue = Math.round(x*100)/100;
        if(debugging)
        {
            alert("[round] " + x + " rounded is " + returnValue);
        }
    }
    catch(err)
    {
        if(debugging)
        {
            alert(err);
        }
        returnValue = null;
    }
    
    return returnValue;
}