﻿var global = this;
global.sHost = root + "mortgagecalculatorajax.ashx?";
var bIsIE = navigator.userAgent.indexOf("compatible") > -1 && navigator.userAgent.indexOf("MSIE") > -1 && !(navigator.userAgent.indexOf("Opera") > -1);
var sParams = "";
var loaded = false;
var time;
var whichUpdate = 0;

if (typeof XMLHttpRequest == "undefined" && window.ActiveXObject) {
    function XMLHttpRequest() {
        var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
        for (var i = 0; i < arrSignatures.length; i++) {
            try {
                var oRequest = new ActiveXObject(arrSignatures[i]);
                return oRequest;
            } catch (oError) {
                //ignore
            }
        }
        throw new Error("MSXML is not installed on your system.");
    }
}

function getDOM(sId) {
    return document.getElementById(sId);
}

function io(sUrl) {
    var temp = document.createElement("script");
    temp.type = "text/javascript";
    temp.src = sUrl;
    if (loaded) {
        if (global.oScript == null)
            document.body.appendChild(temp);
        else
            document.body.replaceChild(temp, global.oScript);

        global.oScript = temp;
    } else {
        getDOM('ctl00_Head1').appendChild(temp);
    }
}

function setParam(name, value) {
    if (global.sParams.length > 0) global.sParams += "&";
    global.sParams += encodeURIComponent(name) + "=" + encodeURIComponent(value);
}

function resetParams() {
    global.sParams = "";
}

function update(wu) {
    whichUpdate = wu;
    clearTimeout(time);
    time = setTimeout(Calculate, 500);

}
function Calculate() {
    if (whichUpdate == 1) {
        document.getElementById("ctl00_BodyText_ctl00_DownPaymentPercent").value = parseInt(document.getElementById("ctl00_BodyText_ctl00_DownPayment").value / document.getElementById("ctl00_BodyText_ctl00_PropertyValue").value * 100);
    }
    if (whichUpdate == 2) {
        document.getElementById("ctl00_BodyText_ctl00_DownPayment").value = parseInt(document.getElementById("ctl00_BodyText_ctl00_PropertyValue").value * document.getElementById("ctl00_BodyText_ctl00_DownPaymentPercent").value) / 100;
    }
    if (new Number(document.getElementById("ctl00_BodyText_ctl00_DownPayment").value) < new Number(document.getElementById("ctl00_BodyText_ctl00_PropertyValue").value)) {
        setParam('Price', document.getElementById("ctl00_BodyText_ctl00_PropertyValue").value);
        setParam('DownPayment', document.getElementById("ctl00_BodyText_ctl00_DownPayment").value);
        setParam('InterestRate', document.getElementById("ctl00_BodyText_ctl00_InterestRate").value);
        if (document.getElementById("ctl00_BodyText_ctl00_AnnualTaxes").value != '')
            setParam('AnnualTaxes', document.getElementById("ctl00_BodyText_ctl00_AnnualTaxes").value);
        else
            setParam('AnnualTaxes', '0');
        if (document.getElementById("ctl00_BodyText_ctl00_AnnualInsurance").value != '')
            setParam('AnnualInsurance', document.getElementById("ctl00_BodyText_ctl00_AnnualInsurance").value);
        else
            setParam('AnnualInsurance', '0');
        if (document.getElementById("ctl00_BodyText_ctl00_OtherMonthlyDebt").value != '')
            setParam('OtherMonthlyDebt', document.getElementById("ctl00_BodyText_ctl00_OtherMonthlyDebt").value);
        else
            setParam('OtherMonthlyDebt', '0');
        setParam('show40YearMortgage', show40YearMortgage);
        io(global.sHost + global.sParams);
        resetParams();
    } else {
        alert("The down payment cannot be more than the property value.");
    }
}

function ShowDiv(whichOne) {
    document.getElementById("div_PaymentValues").className = "hide";
    document.getElementById("div_LoanQualifyingValues").className = "hide";
    document.getElementById("div_" + whichOne).className = "show";

    document.getElementById("img_PaymentValues").src = "images/expand.gif";
    document.getElementById("img_LoanQualifyingValues").src = "images/expand.gif";
    document.getElementById("img_" + whichOne).src = "images/contract.gif";
}

