﻿// JScript File

var radioCombination;
var radioInterestOnly;
var radioRepayment;
var liAmountOnRepaymentBasis;
var liAmountOnInterestOnly;
var amountOnRepaymentBasis;
var amountOnInterestOnly;
var hiddenTypeOfMortgage;
var divPaymentMethodWithRadios;
var divPaymentMethodWithButtons;
var amountToBorrow;

/// <summary>
/// Initializes the global variables for the monthly repayment page.
/// </summary>
function InitVariables()
{
    fieldsetMonthlyRepayment = document.getElementById("fieldsetMonthlyRepayment");
       
    // radio buttons
    radioCombination = findElementLikeRecursively(fieldsetMonthlyRepayment, "radioCombination");
    radioInterestOnly = findElementLikeRecursively(fieldsetMonthlyRepayment, "radioInterestOnly");
    radioRepayment = findElementLikeRecursively(fieldsetMonthlyRepayment, "radioRepayment");    

    // divs for radios/button group.
    divPaymentMethodWithRadios = findElementLikeRecursively(fieldsetMonthlyRepayment, "divPaymentMethodWithRadios");
    divPaymentMethodWithButtons = findElementLikeRecursively(fieldsetMonthlyRepayment, "divPaymentMethodWithButtons");

    // amount inputs.
    amountOnRepaymentBasis = findElementLikeRecursively(fieldsetMonthlyRepayment, "amountOnRepaymentBasis");   
    amountOnInterestOnly = findElementLikeRecursively(fieldsetMonthlyRepayment, "amountOnInterestOnly");   
    amountToBorrow = findElementLikeRecursively(fieldsetMonthlyRepayment, "howMuch");

    // list items.    
    liAmountOnRepaymentBasis = findElementLikeRecursively(fieldsetMonthlyRepayment, "liAmountOnRepaymentBasis");   
    liAmountOnInterestOnly = findElementLikeRecursively(fieldsetMonthlyRepayment, "liAmountOnInterestOnly");    
}

/// <summary>
/// Handles the onchange event for the listTypeOfMortgage control.
/// Hides the amountOnRepaymentBasis and amountOnInterestOnly controls if 
/// the selected value is not "Combination".
/// </summary>
function RadioCombinationOnChange()
{
    //debugger;
    var isCombination = radioCombination.checked;
    var isInterestOnly = radioInterestOnly.checked;
    var isRepayment = radioRepayment.checked;
       
    // combination related.
    liAmountOnRepaymentBasis.style.display = isCombination ? "" : "none";
    liAmountOnInterestOnly.style.display = isCombination ? "" : "none";  
    //enableValidatorsForControl(isCombination, amountOnRepaymentBasis);
    for (var i = 0; i < Page_Validators.length; i++)
    {
        if (Page_Validators[i].controltovalidate == amountOnRepaymentBasis.id || Page_Validators[i].controltovalidate == amountOnInterestOnly.id )
        {
            ValidatorEnable(Page_Validators[i],isCombination);
        }
    }   
    //enableValidatorsForControl(isCombination, amountOnInterestOnly);

    // set the type of mortgage.
    var hiddenTypeOfMortgage = findElementLikeRecursively(fieldsetMonthlyRepayment, "hiddenTypeOfMortgage");
    
    if (isCombination) { hiddenTypeOfMortgage.value = "Combination"; }
    if (isInterestOnly) { hiddenTypeOfMortgage.value = "InterestOnly"; }
    if (isRepayment) { hiddenTypeOfMortgage.value = "Repayment"; }
}

/// <summary>
/// Handles the onload event for the MonthlyRepaymentCalculator page.
/// Subscribes to the onchange event of the listTypeOfMortgage select element.
/// Hides the go button for the listTypeOfMortgage select element.
/// </summary>
function MonthlyRepaymentCalculatorOnLoad()
{
   addLoadEvent(function () {       
       // debugger
        InitVariables();
        radioRepayment.onclick = RadioCombinationOnChange;
        radioInterestOnly.onclick = RadioCombinationOnChange;
        radioCombination.onclick = RadioCombinationOnChange;

        RadioCombinationOnChange();
    });
}

/// <summary>
/// Sets the visibility for the divs that contains the radios/buttons for the types of mortgage.
/// If this function is called it means that the javascript is enabled. Thus hide the
/// div that contains the buttons, and show the div that contains the radios.
/// </summary>
function SetCombinationControlsVisibility()
{    
    divPaymentMethodWithButtons.style.display = "none";
    divPaymentMethodWithRadios.style.display = "";
}

/// <summary>
/// Validates that a type of mortgage was selected using the radio buttons.
/// <summary>
function ValidatePaymentMethod(source, arguments)
{
    arguments.IsValid = radioCombination.checked 
        || radioInterestOnly.checked 
        || radioRepayment.checked;        
}

/// <summary>
/// Validates that repayment amount + interest amount = amount to borrow.
/// Performs this validation only if the radioCombination is checked.
/// </summary>
function ValidateCombinationSum(source, arguments)
{
    if (radioCombination.checked)
    {
        var amount = amountToBorrow.value * 1;
        var interestOnlyAmount = amountOnInterestOnly.value * 1;
        var repaymentAmount = amountOnRepaymentBasis.value * 1;
        
        var precondition = !isNaN(amount)
            && !isNaN(interestOnlyAmount)
            && !isNaN(repaymentAmount);
            
        arguments.IsValid = false;
        if (precondition && amount == interestOnlyAmount + repaymentAmount)
        {
            arguments.IsValid = true;
        }
    }
}

/// <summary>
/// Validates that repayment amount is legal.
/// Performs this validation only if the radioCombination is checked.
/// </summary>
function ValidateRepaymentRange(source , arguments)
{
    arguments.IsValid=true;
    if (radioCombination.checked && (amountOnRepaymentBasis.value<0 || amountOnRepaymentBasis.value>99999999 || isNaN(amountOnRepaymentBasis.value) || amountOnRepaymentBasis.value.indexOf('.')>=0))
    {
        arguments.IsValid=false;
    }
}

/// <summary>
/// Validates that interest only amount is legal.
/// Performs this validation only if the radioCombination is checked.
/// </summary>
function ValidateInterestRange(source , arguments)
{
    arguments.IsValid=true;
    if (radioCombination.checked && (amountOnInterestOnly.value<0 || amountOnInterestOnly.value>99999999 || isNaN(amountOnInterestOnly.value) || amountOnInterestOnly.value.indexOf('.')>=0))
    {
        arguments.IsValid=false;
    }
}

/// <summary>
/// Validates that Repayment amount is legal.
/// Performs this validation only if the radioCombination is checked.
/// </summary>
function ValidateRepaymentValue(source , arguments)
{
    arguments.IsValid=true;
    if (radioCombination.checked && amountOnRepaymentBasis.value == "")
    {
        arguments.IsValid=false;
    }
}

/// <summary>
/// Validates that Interest amount is legal.
/// Performs this validation only if the radioCombination is checked.
/// </summary>
function ValidateInterestValue(source , arguments)
{
    arguments.IsValid=true;
    if (radioCombination.checked && amountOnInterestOnly.value == "")
    {
        arguments.IsValid=false;
    }
}

/// <summary>
/// Enables or disables the associated validators for the given control id.
/// </summary>
function enableValidatorsForControl(enable, control)
{
    var validatorsForControl=new Array();
    var k=0;
    for (var i = 0; i < Page_Validators.length; i++)
    {
        if (Page_Validators[i].controltovalidate == control.id)
        {

            ValidatorEnable(Page_Validators[i],enable);
        }
    }
}
