﻿//-------------------------------------------------------------------------------

var _rblCalcType;
var _txtLoanAmountForMP;
var _txtTermForMP;
var _txtLoanAmountForTM;
var _txtMonthlyPaymentForTM;
var _txtTermForLA;
var _txtMonthlyPaymentForLA;
var _txtInterestRateForSI;

function Initialise(rblCalcType, txtLoanAmountForMP, txtTermForMP, txtLoanAmountForTM, txtMonthlyPaymentForTM, txtTermForLA, txtMonthlyPaymentForLA, txtInterestRateForSI)
{
    _rblCalcType = document.getElementsByName(rblCalcType);
    _txtLoanAmountForMP = document.getElementById(txtLoanAmountForMP);
    _txtTermForMP = document.getElementById(txtTermForMP);
    _txtLoanAmountForTM = document.getElementById(txtLoanAmountForTM);
    _txtMonthlyPaymentForTM = document.getElementById(txtMonthlyPaymentForTM);
    _txtTermForLA = document.getElementById(txtTermForLA);
    _txtMonthlyPaymentForLA = document.getElementById(txtMonthlyPaymentForLA);
    _txtInterestRateForSI = document.getElementById(txtInterestRateForSI);
}

function SwitchCalculationType()
{
    var mp01 = document.getElementById('dphMP01');
    var mp02 = document.getElementById('dphMP02');
    var la01 = document.getElementById('dphLA01');
    var la02 = document.getElementById('dphLA02');
    var lt01 = document.getElementById('dphLT01');
    var lt02 = document.getElementById('dphLT02');
    var mortgageType = document.getElementById('dphMortgageType');
    
    if (_rblCalcType[0].checked)
    {
        mp01.style.display = 'block';
        mp02.style.display = 'block';
        la01.style.display = 'none';
        la02.style.display = 'none';
        lt01.style.display = 'none';
        lt02.style.display = 'none';
        mortgageType.style.display = 'block';
    }
    else if (_rblCalcType[1].checked)
    {
        mp01.style.display = 'none';
        mp02.style.display = 'none';
        la01.style.display = 'block';
        la02.style.display = 'block';
        lt01.style.display = 'none';
        lt02.style.display = 'none';
        mortgageType.style.display = 'block';
    }
    else
    {
        mp01.style.display = 'none';
        mp02.style.display = 'none';
        la01.style.display = 'none';
        la02.style.display = 'none';
        lt01.style.display = 'block';
        lt02.style.display = 'block';
        mortgageType.style.display = 'none';
    }
}

function CheckForm()
{
    if (_rblCalcType[0].checked == true) 
    {
        if (isInteger(_txtLoanAmountForMP.value) == false)
        {
            alert("Please enter a numeric value for the loan amount.");
            _txtLoanAmountForMP.focus();
            return false;
        }

        if (isInteger(_txtTermForMP.value) == false)
        {
            alert("Please enter a numeric value for the term.");
            _txtTermForMP.focus();
            return false;
        }
    }

    if (_rblCalcType[1].checked == true) 
    {
        if (isInteger(_txtMonthlyPaymentForLA.value) == false)
        {
            alert("Please enter a numeric value for the monthly payment amount.");
            _txtMonthlyPaymentForLA.focus();
            return false;
        }

        if (isInteger(_txtTermForLA.value) == false)
        {
            alert("Please enter a numeric value for the term.");
            _txtTermForLA.focus();
            return false;
        }
    }

    if (_rblCalcType[2].checked == true) 
    {
        if (isInteger(_txtLoanAmountForTM.value) == false)
        {
            alert("Please enter a numeric value for the term.");
            _txtLoanAmountForTM.focus();
            return false;
        }

        if (isInteger(_txtMonthlyPaymentForTM.value) == false)
        {
            alert("Please enter a numeric value for the monthly payment amount.");
            _txtMonthlyPaymentForTM.focus();
            return false;
        }
    }

    if (isFloatingPt(_txtInterestRateForSI.value) == false)
    {
        alert("Please enter a valid interest rate.");
        _txtInterestRateForSI.focus();
        return false;
    }

    return true;
}
