﻿// JScript File

//*********************** HANDLE CONTROL EVENTS ******************************
//Declare global variables


//Analysis goal changed
function js_ddlGoalChanged()
{
    //Define local variables
    var temp = "";
    
    //Update the checkprepayments checkbox, if necessary
    var chkbx = document.getElementById("chkPrePay");
    var ddlb = document.getElementById("ddlGoal");
    if (ddlb.selectedIndex==2)
    {
        //Goal calls for prepayment analysis, so click prepay checkbox
        chkbx.checked=true;
    }
    
    //Update tax deduction option button
    if (ddlb.selectedIndex==0 || ddlb.selectedIndex==5 || ddlb.selectedIndex==2 || ddlb.selectedIndex==4)
    {
        //Goal calls for monthly payment analysis, prepayment analysis, or 
        //refinance, so hide tax deduction option
        temp = document.getElementById("chkDeduction");
        temp.style.visibility = "hidden";
        temp = document.getElementById("lblChkDeduction");        
        temp.style.visibility = "hidden"; 
        temp.checked=false;
    }
    else
    {
        //Show tax deductions option buttion
        temp = document.getElementById("chkDeduction");
        temp.style.visibility = "visible";
        temp = document.getElementById("lblChkDeduction");        
        temp.style.visibility = "visible"; 
    }
    
    //Identify the current goal and the amortization check box
    var goal = document.getElementById("ddlGoal");
    var opt =document.getElementById("chkAmort");
    
    //If the current goal calls for amortization, force the option button
    //to remain clicked
    if (goal.selectedIndex==5)
    {
        opt.checked = true;
    }
    
    //Update calculator
    jsShowAll();       
}

//Mortgage changed
function js_ddlMortChanged(s)
{
    //Define local variables
    var temp = "";
    
    //Identify the mortgage that was changed
    var ddlc = s;
    
    //Identify the dropdown list to be changed
    if (ddlc.name == "ddlMort")
    {
        var prepay = document.getElementById("ddlNumPrepay");
    }
    else
    {
        var prepay = document.getElementById("ddlNumPrepay1");
    }
    
    //Update the number of prepayments dropdown box, if necessary
    var currentSelection = prepay.selectedIndex;    //Current selection;
        
    //Add new option entries to dropdown list
    if (ddlc.selectedIndex==1)
    {
        //Mortgage calls for biweekly payments, so we make 0 to 26 prepayments per year
        if (prepay.length==13)
        {   prepay.length = 27;
        
            for (i=13; i<27; i++)
            {
                prepay.options[i].value = i;
                prepay.options[i].text = i;                    
            }
        }
    }
    else
    {
        //Mortgage calls for monthly payments, so we make 0 to 12 prepayments per year
        if (prepay.length==27)
        {   
            for (i=13; i<27; i++)
            {
                prepay.options[i].value = null;
                prepay.options[i].text = null;           
            }
            
            prepay.length = 13;           
        }
        
        //If necessary, adjust selected index
        if (currentSelection > 12) 
            {prepay.selectedIndex = 12; }
            else
            {prepay.selectedIndex = currentSelection;}
    }
    
    //Update calculator
    jsShowAll();       
}

//Prepayment option button changed
function js_chkPrePayChanged(s)
{
    //Identify the option button that was changed
    var opt = s;
    
    //Identify the current goal
    var goal = document.getElementById("ddlGoal");
    
    //If the current goal calls for prepayment analysis, force the option button
    //to remain clicked
    if (goal.selectedIndex==2)
    {
        opt.checked = true;
    }
    
    //Update calculator
    jsShowAll();    
}

//Fill dropdown lists for Mort and Mort1
function js_FillDropdownList()
{
    //Declare variable
    var ddld = document.getElementById("ddlMort");
     
    //Fill ddlMort
    ddld.length=10;
    ddld.options[0].text = "Fixed-rate mortgage";
    ddld.options[1].text = "Biweekly Fixed-Rate";
    ddld.options[2].text = "Fixed-Rate Balloon";
    ddld.options[3].text = "Fixed-Rate Interest-Only";
    ddld.options[4].text = "Adjustable-Rate Mortgage";
    ddld.options[5].text = "Convertible ARM";
    ddld.options[6].text = "Two-Step Mortgage";
    ddld.options[7].text = "Balloon ARM";
    ddld.options[8].text = "Interest-Only ARM";
    ddld.options[9].text = "Graduated Payment Loan";
    
    //Fill ddlMort1
    ddle = document.getElementById("ddlMort1");
    ddle.length=10;
    ddle.options[0].text = "Fixed-rate mortgage";
    ddle.options[1].text = "Biweekly Fixed-Rate";
    ddle.options[2].text = "Fixed-Rate Balloon";
    ddle.options[3].text = "Fixed-Rate Interest-Only";
    ddle.options[4].text = "Adjustable-Rate Mortgage";
    ddle.options[5].text = "Convertible ARM";
    ddle.options[6].text = "Two-Step Mortgage";
    ddle.options[7].text = "Balloon ARM";
    ddle.options[8].text = "Interest-Only ARM";
    ddle.options[9].text = "Graduated Payment Loan";    
}

//Number of prepayments changed
function js_ddlNumPrepayChanged(s)
{
    //Identify the prepayment dropdown list that was clicked
    var prepay = s;
    
    //Update the number of prepayments dropdown box, if necessary
    var currentSelection = prepay.selectedIndex;    //Current selection;
    
    //Identify mortgage type 
    var ddl = document.getElementById("ddlMort");
    var ddlMortName = "ddlMort";
    if (prepay.name == "ddlNumPrepay1")
    {
        var ddl = document.getElementById("ddlMort1");
        ddlMortName = "ddlMort1";
    }
     
    //Update dropdown list
    if (prepay.name == "ddlNumPrepay") 
    {    
        //Update dropdown list for mortgage 1
        if (ddlMortName.selectedIndex==1)
        {
            //Mortgage calls for biweekly payments, so we make 0 to 26 prepayments per year
            if (prepay.length==13)
            {   
                prepay.length = 27;
                for (i=13; i<27; i++)
                {
                    prepay.options[i].value = i;
                    prepay.options[i].text = i;                    
                }
            }
        }
        else    
        {
            //Mortgage calls for monthly payments, so we make 0 to 12 prepayments per year
            if (prepay.length==27)
            {   
                for (i=13; i<27; i++)
                {
                    prepay.options[i].value = null;
                    prepay.options[i].text = null;           
                }
                prepay.length = 13;           
            }
            
            //If necessary, adjust selected index
            if (currentSelection > 12) 
                {prepay.selectedIndex = 12; }
            else
                {prepay.selectedIndex = currentSelection;}
        }
    }    
       
    //Update calculator
    jsShowAll();
}
