////////////////////////////////////////////////
// Obfuscated by Javascript Obfuscator v.2.53 //
//        http://javascript-source.com        //
////////////////////////////////////////////////
var totalSecurity = 0;
var balloonTerm = 0;
var monthlyDebtServiced = 0;
var principlebalance = 0;
var endingPeriodBalance = 0;
var schedPrincipal = 0;
var interestAndPrincipal = 0;
var formattedOrigBalance;
var S = 15000;
var T = 20000;
var V = 4000;
var P = 9000;
var R = 9000;
var ADVISORY_FEE = 15000;

function validateFields() {
    formattedOrigBalance = new NumberFormat(document.calculator.origBalance.value);
    if (parseFloat(formattedOrigBalance.toUnformatted()) <= 0) {
        alert("Please enter a valid amount for Original Loan Balance");
        return false;
    }
    if (isNaN(parseFloat(document.calculator.interest.value))) {
        alert("Please enter a valid Interest Rate");
        return false;
    }
    if (isNaN(parseFloat(document.calculator.amortTerm.value))) {
        alert("Please enter a valid number for Amortization Term");
        return false;
    }
    var firstPmtDate = document.calculator.firstPmtDate.value;
    if (!isDate(firstPmtDate, "First Payment Date")) return false;
    var maturityDate = document.calculator.maturityDate.value;
    if (!isDate(maturityDate, "Maturity Date")) return false;
    var defeasanceDate = document.calculator.defeasanceDate.value;
    if (!isDate(defeasanceDate, "Defeasance Date")) return false;
    if (parseFloat(document.calculator.interest.value) <= 0 || parseFloat(document.calculator.interest.value) >= 25) {
        alert("Interest Rate needs to be between 0 and 25");
        return false;
    }
    return true;
}
function calculate() {
    roundAndFormatOriginalLoan();
    roundAndFormatInterest();
    if (!validateFields()) return false;
    totalSecurities = 0;
    var defeasanceDate = new Date(document.calculator.defeasanceDate.value);
    var firstPmtDate = new Date(document.calculator.firstPmtDate.value);
    var maturityDate = new Date(document.calculator.maturityDate.value);
    if (maturityDate < firstPmtDate) {
        alert("Maturity Date needs to be greater than First Payment Date");
        return false;
    }
    if (defeasanceDate < new Date()) {
        alert("Defeasance Date needs to be greater than today's date");
        return false;
    }
    if (defeasanceDate < firstPmtDate || defeasanceDate > maturityDate) {
        alert("Defeasance Date needs to be between First Payment Date and Maturity Date");
        return false;
    }
    var firstPmtToMaturity = ((maturityDate.getFullYear() - firstPmtDate.getFullYear()) * 12) + firstPmtDate.getMonth() - maturityDate.getMonth();
    if (firstPmtToMaturity > document.calculator.amortTerm.value) {
        alert("Amortization Term must be greater than or equal to the number of months between First Payment Date and Maturity Date");
        return false;
    }
    var month = firstPmtDate.getMonth();
    var year = firstPmtDate.getFullYear();
    if (firstPmtDate.getMonth() == 1) {
        month = 12;
        year--;
    } else {
        month--;
    };
    var Z = new Date(year, month, 1);
    var H = ((defeasanceDate.getFullYear() - year) * 12) + (defeasanceDate.getMonth() - (month));
    balloonTerm = ((maturityDate.getFullYear() - year) * 12) - month + maturityDate.getMonth();
    var G = document.calculator.interest.value / 12 / 100;
    formattedOrigBalance = new NumberFormat(document.calculator.origBalance.value);
    endingPeriodBalance = formattedOrigBalance.toUnformatted();
    var term = 0;
    var interest = 0;
    var D = 0;
    var F = 0;
    var Q = 0;
    F = parseFloat(endingPeriodBalance) * Math.pow(1 + G, document.calculator.amortTerm.value);
    D = (Math.pow(1 + G, document.calculator.amortTerm.value) - 1) / G;
    monthlyDebtServiced = Math.round((F / D) * 100) / 100;
    var A = new Array();
    populateTreasuryRates(A);
    var Y;
    var X = 0;
    var C = 0;
    var B = 0;
    var treasuryRate = 0;
    if (firstPmtDate.getMonth() == 12) {
        month = 0;
        year++;
    }
    for (var x = 1; x <= balloonTerm; x++) {
        month++;
        Y = new Date(year, month, 1);
        X = Math.round((Y - Z) / 86400000);
        if (document.calculator.accType.value == "Act/360") {
            interest = Math.round(endingPeriodBalance * G * X / 30 * 100) / 100;
        } else {
            interest = Math.round(endingPeriodBalance * G * 100) / 100;
        };
        if (x < balloonTerm) {
            schedPrincipal = Math.round((monthlyDebtServiced - interest) * 100) / 100;
        } else {
            schedPrincipal = endingPeriodBalance;
        };
        endingPeriodBalance -= schedPrincipal;
        interestAndPrincipal = interest + schedPrincipal;
        if (x == H) {
            principleBalance = endingPeriodBalance;
        }
        if (x > H) {
            C = x - H;
            B = C / 12;
        } else {
            C = 0;
            B = 0;
        };
        D = A[Math.floor(B)];
        F = A[Math.ceil(B)];
        treasuryRate = 0;
        if (B > 0) {
            if (C == 1) {
                treasuryRate = TREASURY_1MO / 100;
            } else if (C <= 3) {
                treasuryRate = Math.round(((((C - 1) / 2) * (TREASURY_3MO - TREASURY_1MO) + TREASURY_1MO) / 100) * 1000000) / 1000000;
            } else if (C <= 6) {
                treasuryRate = Math.round(((((C - 3) / 3) * (TREASURY_6MO - TREASURY_3MO) + TREASURY_3MO) / 100) * 1000000) / 1000000;
            } else if (C < 12) {
                treasuryRate = Math.round(((((C - 6) / 6) * (TREASURY_1YR - TREASURY_6MO) + TREASURY_6MO) / 100) * 1000000) / 1000000;
            } else if (B <= 30) treasuryRate = Math.round((((B - Math.floor(B)) * (F - D) + D) / 100) * 1000000) / 1000000;
            else treasuryRate = Math.round(A[30] * 1000000) / 1000000;
        } else if (B == 0) {
            treasuryRate = Math.round(A[1]) * 1000000 / 1000000;
        }
        Q = 0;
        if (x > H) {
            Q = Math.round(Math.pow((1 / (1 + treasuryRate / 2)), (C / 6)) * interestAndPrincipal * 100) / 100;
        }
        totalSecurities = Math.round((totalSecurities + Q) * 100) / 100;
        Z = new Date(year, month, 1);
        if (month == 12) {
            month = 0;
            year++;
        }
    }
    outputResults();
    return true;
}
function outputResults() {
    document.getElementById('monthly').innerHTML = formatNumber(Math.floor(monthlyDebtServiced));
    //document.calculator.monthlyResult.value = formatNumber(Math.floor(monthlyDebtServiced));
    document.getElementById('balloon').innerHTML = formatNumber(Math.round(interestAndPrincipal));
    //document.calculator.balloonResult.value = formatNumber(Math.round(interestAndPrincipal));
    document.getElementById('defbalance').innerHTML = formatNumber(Math.round(principleBalance));
    //document.calculator.defbalanceResult.value = formatNumber(Math.round(principleBalance));
    document.getElementById('securities').innerHTML = formatNumber(Math.round(totalSecurities));
    //document.calculator.securitiesResult.value = formatNumber(Math.round(totalSecurities));
    document.getElementById('premium').innerHTML = formatNumber(Math.round(totalSecurities - principleBalance));
    //document.calculator.premiumResult.value = formatNumber(Math.round(totalSecurities - principleBalance));
    document.getElementById('svcproc').innerHTML = formatNumber(S);
    //document.calculator.svcprocResult.value = formatNumber(S);
    document.getElementById('svclegal').innerHTML = formatNumber(T);
    //document.calculator.svclegalResult.value = formatNumber(T);
    document.getElementById('accountant').innerHTML = formatNumber(V);
    //document.calculator.accountantResult.value = formatNumber(V);
    document.getElementById('custodian').innerHTML = formatNumber(P);
    //document.calculator.custodianResult.value = formatNumber(P);
    document.getElementById('succborr').innerHTML = formatNumber(R);
    //document.calculator.succborrResult.value = formatNumber(R);
    var ratingAgencies = 0;
    if (parseFloat(formattedOrigBalance.toUnformatted()) > 20000000) {
        ratingAgencies = 15000;
        document.getElementById("ratingagencies").innerHTML = formatNumber(ratingAgencies);
        document.calculator.ratingagenciesResult.value = formatNumber(ratingAgencies);
    } else {
        document.getElementById("ratingagencies").innerHTML = "N/A" + "&nbsp;&nbsp;&nbsp;&nbsp;";
        //document.calculator.ratingagenciesResult.value = "N/A" + "&nbsp;&nbsp;&nbsp;&nbsp;"
    };
    var thirdPartyTotals = S + T + V + P + R + ratingAgencies;
    document.getElementById("thirdPartyTotals").innerHTML = formatNumber(thirdPartyTotals);
    //document.calculator.thirdPartyTotalsResult.value = formatNumber(thirdPartyTotals);
    var advisoryFee = ADVISORY_FEE;
    document.getElementById("advisoryFee").innerHTML = formatNumber(advisoryFee);
    //document.calculator.advisoryFeeResult.value = formatNumber(advisoryFee);
    var totalDefeasance = Math.round(totalSecurities) + thirdPartyTotals + advisoryFee;
    document.getElementById("totalDefeasance").innerHTML = formatNumber(totalDefeasance);
    //document.calculator.totalDefeasanceResult.value = formatNumber(totalDefeasance);
}
function formatNumber(number) {
    var formattedNumber = new NumberFormat(number);
    formattedNumber.setCurrency(true);
    formattedNumber.setCommas(true);
    formattedNumber.setPlaces(0);
    formattedNumber.setCurrencyPrefix("$");
    return formattedNumber.toFormatted() + "&nbsp;&nbsp;&nbsp;&nbsp;";
}
function populateTreasuryRates(A) {
    A[0] = TREASURY_1YR;
    A[1] = TREASURY_1YR;
    A[2] = TREASURY_2YR;
    A[3] = TREASURY_3YR;
    A[5] = TREASURY_5YR;
    A[10] = TREASURY_10YR;
    A[30] = TREASURY_30YR;
    A[4] = (A[3] + A[5]) / 2;
    var W = (A[10] - A[5]) / 5;
    for (var x = 6; x < 10; x++) A[x] = W + A[x - 1];
    W = (A[30] - A[10]) / 20;
    for (var x = 11; x < 30; x++) A[x] = W + A[x - 1];
}
function roundAndFormatOriginalLoan() {
    var formattedNumber = new NumberFormat(document.calculator.origBalance.value);
    formattedNumber.setCurrency(true);
    formattedNumber.setCommas(true);
    formattedNumber.setPlaces(0);
    document.calculator.origBalance.value = formattedNumber.toFormatted();
    return true;
}
function roundAndFormatInterest() {
    var formattedNumber = new NumberFormat(document.calculator.interest.value);
    formattedNumber.setCurrency(false);
    formattedNumber.setCommas(true);
    formattedNumber.setPlaces(3);
    document.calculator.interest.value = formattedNumber.toFormatted();
    return true;
}
function resetAll() {
    document.calculator.reset();
    document.getElementById('monthly').innerHTML = "";
    document.getElementById('balloon').innerHTML = "";
    document.getElementById('defbalance').innerHTML = "";
    document.getElementById('securities').innerHTML = "";
    document.getElementById('svcproc').innerHTML = "";
    document.getElementById('svclegal').innerHTML = "";
    document.getElementById('accountant').innerHTML = "";
    document.getElementById('custodian').innerHTML = "";
    document.getElementById('succborr').innerHTML = "";
    document.getElementById("ratingagencies").innerHTML = "";
    document.getElementById("thirdPartyTotals").innerHTML = "";
    document.getElementById("advisoryFee").innerHTML = "";
    document.getElementById("totalDefeasance").innerHTML = "";
}
