﻿// JScript File

// ********************** UPDATE CALCULATOR ******************************
//Declare global variables

//Declare local variables

//Show all of the calculator controls that should be shown
function jsShowAll()
{
    //Update title and intro
    update_page_title();
    update_intro();
    
    var output = document.getElementById("txtOutput");
    
    //Hide error message, if necessary
    jsHideErrorMsg();
    
    //Process html for analysis section.  Note: The analysis section is always
    //  visible.  The only element that varies is chkDeduction.  The visibility
    //  of chkDeduction is also controlled by the js_ddlGoalChanged function. 
    js_ShowAnalysisSection()
    
    //Process html for loan description section
    js_ShowLoanDescription();
    
    //Process html for rate adjustment section
    js_ShowRateAdjustment();
    
    //Process html for closing cost section
    js_ShowClosingCst();
    
    //Process html for prepayment section
    js_ShowPrepayment();
    
    //Process html for tax and insurance section
    js_ShowTaxInsurance();
    
    //Update document title
    js_update_document_title();
    
    //Show context-appropriate sample problems
    js_ShowSampleProblems();

    //Position page 
    js_PositionPage();
    
    //Add mortgage info to frequently-asked questions
    js_add_mort_info();
    
    //Re-set txtOutput, so next page will display calculator
    output.value = "Calculator";
    output.style.display = "none";      //Hide output textbox    
    
    //alert("jsShowAll successful");
}

//This assigns the elements of a collection to the elements of an array.
//This can be useful, because collections do not support any 
//array-like functions (concat, splice, etc...).
//Source:  http://www.byteclub.net/wiki/index.php?title=Javascript_getElementsByTagName
function collectionToArray(col) {
	a = new Array();
	for (i = 0; i < col.length; i++)
		a[a.length] = col[i];
	return a;
}

function js_ShowSampleProblems()    
{
    //Identify current goal and mortgage
    var Goal = document.getElementById("ddlGoal");
    var Mort = document.getElementById("ddlMort");
    
    //Identify first sentence of the sample problem section
    var FirstSentence = document.getElementById("spFirstSentence");
    
    //Identify sample problems
    var dv001 = document.getElementById("div001");
    var dv002 = document.getElementById("div002");
    var dv101 = document.getElementById("div101");    
    var dv111 = document.getElementById("div111");    
    var dv121 = document.getElementById("div121");    
    var dv131 = document.getElementById("div131");    
    var dv141 = document.getElementById("div141");    
    var dv151 = document.getElementById("div151");    
    var dv161 = document.getElementById("div161");    
    var dv171 = document.getElementById("div171");    
    var dv181 = document.getElementById("div181");    
    var dv191 = document.getElementById("div191");    
    var dv201 = document.getElementById("div201");    
    var dv301 = document.getElementById("div301");     
    var dv441 = document.getElementById("div441");    
    
    //Hide all sample problems
    dv001.style.display="none";
    dv002.style.display="none";
    dv101.style.display="none";
    dv111.style.display="none";
    dv121.style.display="none";
    dv131.style.display="none";
    dv141.style.display="none";
    dv151.style.display="none";
    dv161.style.display="none";
    dv171.style.display="none";
    dv181.style.display="none";
    dv191.style.display="none";
    dv201.style.display="none";
    dv301.style.display="none";
    dv441.style.display="none";
    
    //Show sample problems, based on user input
    if (Goal.selectedIndex==0) {
        dv001.style.display="block";
        dv002.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Monthly Payment Mortgage Calculator to answer two common mortgage questions."
    }
    
    if (Goal.selectedIndex==1) {
        
        if (Mort.selectedIndex==0)  {
        dv101.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Total Cost Mortgage Calculator to compute the total cost of a fixed-rate mortgage."
        }
        
        if (Mort.selectedIndex==1)  {
        dv111.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Biweekly Mortgage Calculator to compute the total cost of a fixed-rate biweekly mortgage."
        }
        
        if (Mort.selectedIndex==2)  {
        dv121.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Fixed-Rate Balloon Mortgage Calculator to compute the total cost of a fixed-rate balloon loan."
        }
        
        if (Mort.selectedIndex==3)  {
        dv131.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Fixed-Rate Interest-Only Mortgage Calculator to compute the total cost of a fixed-rate interest-only loan."
        }
        
        if (Mort.selectedIndex==4)  {
        dv141.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Adjustable-Rate Mortgage Calculator to compute the total cost of an adjustable-rate mortgage (ARM)."
        }
        
        if (Mort.selectedIndex==5)  {
        dv151.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Total Cost Mortgage Calculator to compute the total cost of a convertible adjustable-rate mortgage (ARM)."
        }
        
        if (Mort.selectedIndex==6)  {
        dv161.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Total Cost Mortgage Calculator to compute the total cost of a two-step mortgage."
        }
        
        if (Mort.selectedIndex==7)  {
        dv171.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Total Cost Mortgage Calculator to compute the total cost of an adjustable-rate balloon loan (i.e., a balloon ARM)."
        }
        
        if (Mort.selectedIndex==8)  {
        dv181.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Total Cost Mortgage Calculator to compute the total cost of an adjustable-rate interest-only loan."
        }
        
        if (Mort.selectedIndex==9)  {
        dv191.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Total Cost Mortgage Calculator to compute the total cost of a graduated payment loan."
        }
    }
    
    if (Goal.selectedIndex==2) {
        dv201.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Prepayment Mortgage Calculator to assess the benefits of mortgage prepayment."
    }
    
    if (Goal.selectedIndex==3) {
        dv301.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Compare Two Mortgages Calculator to compare two mortgages objectively."
    }
    
    if (Goal.selectedIndex==4) {
        dv441.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Mortgage Refinance Calculator to assess the costs and benefits of refinancing."
    }
    
    if (Goal.selectedIndex==5) {
        dv001.style.display="block";
        dv002.style.display="block";
        FirstSentence.innerHTML = "This section shows how to use the Monthly Payment Mortgage Calculator to answer two common mortgage questions.  Each question also shows how to produce an amortization schedule."
    }
    
    
   
}
 

//Find the position of an element on a web page:  This was documented at
//  http://www.quirksmode.org/js/findpos.html# 
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//Position page after reloading
function js_PositionPage()
{
    //Identify required output
    var Output = document.getElementById("txtOutput");
    
    if (Output.value=="Report")
    {
        //Identify button
        var SummaryRept = document.getElementById("aReport");
    
        //Find the position of the Report section from the top of the browser window
        var fromTop = findPos(SummaryRept);
        
        //Scroll to just after the Calculate button
        window.scrollTo(fromTop[0],fromTop[1]);    
    }
}


//Delete error message. 
function jsHideErrorMsg()
{
    //Identify the error message Div and label
    var ErrMsgDiv = document.getElementById("divErrorMsg");
    var ErrMsgLbl = document.getElementById("lblErrMsg");
    
    //Identify required output
    var Output = document.getElementById("txtOutput");
    
    //If error message is not called for, hide divErrorMsg
    if (Output.value=="Error")
    {
        ErrMsgDiv.style.display = "block";  //Show error message.      
    }
    else
    {
        ErrMsgDiv.style.display = "none";   //Hide error message.
    }
}

//Analysis section: Process visibility and content of section elements
function js_ShowAnalysisSection()
{
    //Declare variables
    var Goal = document.getElementById("ddlGoal");
    var temp ="";
    
    //Update tax deduction option button
    if (Goal.selectedIndex==0 || Goal.selectedIndex==2 || Goal.selectedIndex==4 || Goal.selectedIndex==5)
    {
        //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"; 
    }
}

//Loan Description: Process visibility and content of section elements
function js_ShowLoanDescription()
{
    //Initialize variables
    var Heading = document.getElementById("tdLoanDescriptionHead");
    var ColumnHeading = document.getElementById("trLoanHead");
    var Head1 = document.getElementById("lblLoanHead");
    var Head2 = document.getElementById("lblLoanHead1");
    var Goal = document.getElementById("ddlGoal");
    var Mort = document.getElementById("ddlMort");
    var Mort1 = document.getElementById("ddlMort1");
    var txtTerm1 = document.getElementById("txtTerm1");
    var txtAmt1 = document.getElementById("txtAmt1");
    var txtStartRate1 = document.getElementById("txtStartRate1");
    
    //Update section title
    Heading.innerHTML = "Describe the Loan";
    if (Goal.selectedIndex==3 || Goal.selectedIndex==4) {Heading.innerHTML = "Describe the Loans";} 
    
    //Define visibility and content of column elements
    Head1.style.visibility = "hidden";
    Head2.style.visibility = "hidden";
    Head1.innerHTML = "";
    Head2.innerHTML = "";
    Mort1.style.visibility = "hidden";
    txtTerm1.style.visibility = "hidden";
    txtAmt1.style.visibility = "hidden";
    txtStartRate1.style.visibility = "hidden";
    ColumnHeading.style.display = "none";
    
    if (Goal.selectedIndex==3 || Goal.selectedIndex==4)
    {
        Head1.style.visibility = "visible";
        Head2.style.visibility = "visible";
        ColumnHeading.style.display = "block";
        Mort1.style.visibility = "visible";
        txtTerm1.style.visibility = "visible";
        txtAmt1.style.visibility = "visible";
        txtStartRate1.style.visibility = "visible";
    }
        
    if (Goal.selectedIndex==3)
    {
        Head1.innerHTML = "Loan 1";
        Head2.innerHTML = "Loan 2";
    }    
    
    if (Goal.selectedIndex==4)
    {
        Head1.innerHTML = "Current Loan";
        Head2.innerHTML = "New Loan";
    }
    
    //Update visibility for loan term
    var trTrm = document.getElementById("trTerm"); 
    var txtTrm = document.getElementById("txtTerm"); 
    var txtTrm1 = document.getElementById("txtTerm1"); 
    
    if (Goal.selectedIndex==3 || Goal.selectedIndex==4)
    {
        //Update loan term visibility when there are two loans
        if ( (Mort.selectedIndex==3 || Mort.selectedIndex==8) && (Mort1.selectedIndex==3 || Mort1.selectedIndex==8) )
        {
            //Both loans are interest-only so hide loan term row
            trTrm.style.display="none";
            txtTrm.style.visibility = "hidden";
            txtTrm1.style.visibility = "hidden";            
        }
        else
        {
            //At least one loan is interest-only, so show loan term row
            trTrm.style.display="block";
            txtTrm.style.visibility = "visible";
            txtTrm1.style.visibility = "visible";
            
            //If Loan 1 is interest-only, hide loan term text box for Loan 1
            if (Mort.selectedIndex==3 || Mort.selectedIndex==8)  {
                txtTrm.style.visibility = "hidden"; 
            }
            
             //If Loan 2 is interest-only, hide loan term text box for Loan 2
            if (Mort1.selectedIndex==3 || Mort1.selectedIndex==8)  {
                txtTrm1.style.visibility = "hidden"; 
            }            
        }
    }
    
    if (Goal.selectedIndex<3 || Goal.selectedIndex==5)
    {
        //Update loan term visibility when there is one loan
        if ( (Mort.selectedIndex==3 || Mort.selectedIndex==8) )
        {
            //The loan is interest-only so hide loan term row
            trTrm.style.display="none";
            txtTrm.style.visibility = "hidden";
            txtTrm1.style.visibility = "hidden";            
        }
        else
        {
            //The loan is not interest-only, so show loan term row
            trTrm.style.display="block";
            txtTrm.style.visibility = "visible";
            txtTrm1.style.visibility = "hidden";
        }
    }
    
    //Update label for loan amount
    var Amt = document.getElementById("lblAmt"); 
    
    if (Goal.selectedIndex==4)
    {
        Amt.innerHTML = "Unpaid principal ($):";
    }
    else
    {
        Amt.innerHTML = "Loan amount ($):";
    }
    
    //Update label for starting interest rate
    var Rate = document.getElementById("lblStartRate"); 
    
    if (Mort.selectedIndex < 4 && Mort1.selectedIndex < 4)
    {
        Rate.innerHTML = "Interest rate (%):";
    }
    else
    {
        Rate.innerHTML = "Starting interest rate (%):";
    }
    
    //Update payoff row and payoff term input controls
    var PayoffRow = document.getElementById("trPayoffTerm");
    
    if ( (Mort.selectedIndex==2 || Mort.selectedIndex==3 || Mort.selectedIndex==7 || Mort.selectedIndex==8)  || (Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && (Mort1.selectedIndex==2 || Mort1.selectedIndex==3 || Mort1.selectedIndex==7 || Mort1.selectedIndex==8)  )   
    {
        var Payoff = document.getElementById("txtPayoffTerm");
        if (Mort.selectedIndex==2 || Mort.selectedIndex==3 || Mort.selectedIndex==7 || Mort.selectedIndex==8)
        {
            Payoff.style.visibility = "visible"; 
        }
        else
        {
            Payoff.style.visibility = "hidden";
        }
        
        var Payoff1 = document.getElementById("txtPayoffTerm1");
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && (Mort1.selectedIndex==2 || Mort1.selectedIndex==3 || Mort1.selectedIndex==7 || Mort1.selectedIndex==8 ) )
        {
            Payoff1.style.visibility = "visible"; 
        }
        else
        {
            Payoff1.style.visibility = "hidden";
        }
        
        PayoffRow.style.display = "block";
    }
    else
    {
        PayoffRow.style.display = "none";
    }
}

//Show rate adjustment section.  This is the new function built for divs. 
function js_ShowRateAdjustment()
{
    //Initialize variables
    var Goal = document.getElementById("ddlGoal");
    var Mort = document.getElementById("ddlMort");
    var Mort1 = document.getElementById("ddlMort1");
    var Head = document.getElementById("trRateAdjustHead");    
    var rRateTerm = document.getElementById("trRateTerm");
    var rPerCap = document.getElementById("trPerCap");
    var rLifeCap = document.getElementById("trLifeCap");
    var rNumOfAdjustmentPeriods = document.getElementById("trNumOfAdjustmentPeriods");
    var rAdjustMos = document.getElementById("trAdjustMos");
    var rEndRate = document.getElementById("trEndRate");
    var rCostToConvert = document.getElementById("trCostToConvert");
    var rConvertMos = document.getElementById("trConvertMos");
    
    //Hide section
    Head.style.display = "none";
    rRateTerm.style.display = "none";
    rPerCap.style.display = "none";
    rLifeCap.style.display = "none";
    rNumOfAdjustmentPeriods.style.display = "none";
    rAdjustMos.style.display = "none";
    rEndRate.style.display = "none";
    rCostToConvert.style.display = "none";
    rConvertMos.style.display = "none";
    
    //Exit function if Rate Adjustment section is not displayed
    if ((Mort.selectedIndex>3) || (Mort1.selectedIndex>3 && (Goal.selectedIndex==3 || Goal.selectedIndex==4 )))
    {   
        //Show heading, since section will be displayed
        Head.style.display = "block";
        
        //Declare and initialize section controls
        var RateTerm = document.getElementById("txtRateTerm");
        var PerCap = document.getElementById("txtPerCap");
        var LifeCap = document.getElementById("txtLifeCap");
        var NumOfAdjustmentPeriods = document.getElementById("txtNumOfAdjustmentPeriods");
        var AdjustMos = document.getElementById("txtAdjustMos");
        var EndRate = document.getElementById("txtEndRate");
        var CostToConvert = document.getElementById("txtCostToConvert");
        var ConvertMos = document.getElementById("txtConvertMos");
        
        var RateTerm1 = document.getElementById("txtRateTerm1");
        var PerCap1 = document.getElementById("txtPerCap1");
        var LifeCap1 = document.getElementById("txtLifeCap1");
        var NumOfAdjustmentPeriods1 = document.getElementById("txtNumOfAdjustmentPeriods1");
        var AdjustMos1 = document.getElementById("txtAdjustMos1");
        var EndRate1 = document.getElementById("txtEndRate1");
        var CostToConvert1 = document.getElementById("txtCostToConvert1");
        var ConvertMos1 = document.getElementById("txtConvertMos1");
    
        //Hide controls for Loan 1
        RateTerm.style.visibility = "hidden";
        PerCap.style.visibility = "hidden";
        LifeCap.style.visibility = "hidden";
        NumOfAdjustmentPeriods.style.visibility = "hidden";
        AdjustMos.style.visibility = "hidden";
        EndRate.style.visibility = "hidden";
        CostToConvert.style.visibility = "hidden";
        ConvertMos.style.visibility = "hidden";
    
        //Hide controls for Loan 2
        RateTerm1.style.visibility = "hidden";
        PerCap1.style.visibility = "hidden";
        LifeCap1.style.visibility = "hidden";
        NumOfAdjustmentPeriods1.style.visibility = "hidden";
        AdjustMos1.style.visibility = "hidden";
        EndRate1.style.visibility = "hidden";
        CostToConvert1.style.visibility = "hidden";
        ConvertMos1.style.visibility = "hidden";
        
        //Show rate term controls, if necessary
        if (Mort.selectedIndex==4 || Mort.selectedIndex==6 || Mort.selectedIndex==7 || Mort.selectedIndex==8)
        {
            RateTerm.style.visibility = "visible";
            rRateTerm.style.display = "block";            
        }
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && (Mort1.selectedIndex==4 || Mort1.selectedIndex==6 || Mort1.selectedIndex==7 || Mort1.selectedIndex==8))
        {
            RateTerm1.style.visibility = "visible";
            rRateTerm.style.display = "block";            
        }
        
        //Show periodic cap controls, if necessary
        if (Mort.selectedIndex==4 || Mort.selectedIndex==5 || Mort.selectedIndex==7 || Mort.selectedIndex==8)
        {
            PerCap.style.visibility = "visible";
            rPerCap.style.display = "block";         
        }
        
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && (Mort1.selectedIndex==4 || Mort1.selectedIndex==5 || Mort1.selectedIndex==7 || Mort1.selectedIndex==8))
        {
            PerCap1.style.visibility = "visible";
            rPerCap.style.display = "block";          
        }
        
        //Show lifetime cap controls, if necessary
        if (Mort.selectedIndex==4 || Mort.selectedIndex==5 || Mort.selectedIndex==7 || Mort.selectedIndex==8)
        {
            LifeCap.style.visibility = "visible";
            rLifeCap.style.display = "block";
        }
        
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && (Mort1.selectedIndex==4 || Mort1.selectedIndex==5 || Mort1.selectedIndex==7 || Mort1.selectedIndex==8))
        {
            LifeCap1.style.visibility = "visible";
            rLifeCap.style.display = "block";
        }
        
        //Show number of adjustment period controls, if necessary
        if (Mort.selectedIndex==9)
        {
            NumOfAdjustmentPeriods.style.visibility = "visible";
            rNumOfAdjustmentPeriods.style.display = "block";
        }
        
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && Mort1.selectedIndex==9)
        {
            NumOfAdjustmentPeriods1.style.visibility = "visible";
            rNumOfAdjustmentPeriods.style.display = "block";
        }
        
        //Show adjustment months controls, if necessary
        if (Mort.selectedIndex==4 || Mort.selectedIndex==5 || Mort.selectedIndex==7 || Mort.selectedIndex==8 || Mort.selectedIndex==9)
        {
            AdjustMos.style.visibility = "visible";
            rAdjustMos.style.display = "block";  //problem line
        }
        
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && (Mort1.selectedIndex==4 || Mort1.selectedIndex==5 || Mort1.selectedIndex==7 || Mort1.selectedIndex==8 || Mort1.selectedIndex==9))
        {
            AdjustMos1.style.visibility = "visible";
            rAdjustMos.style.display = "block";
        }
        
        //Show end rate controls, if necessary
        if (Mort.selectedIndex==5 || Mort.selectedIndex==6 || Mort.selectedIndex==9)
        {
            EndRate.style.visibility = "visible";
            rEndRate.style.display = "block";
        }
        
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && (Mort1.selectedIndex==5 || Mort1.selectedIndex==6 || Mort1.selectedIndex==9))
        {
            EndRate1.style.visibility = "visible";
            rEndRate.style.display = "block";
        }
        
        //Show cost to convert controls, if necessary
        if (Mort.selectedIndex==5)
        {
            CostToConvert.style.visibility = "visible";
            rCostToConvert.style.display = "block";
        }
        
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && Mort1.selectedIndex==5)
        {
            CostToConvert1.style.visibility = "visible";
            rCostToConvert.style.display = "block";
        }
        
        //Show months before fixed rate begins controls, if necessary
        if (Mort.selectedIndex==5)
        {
            ConvertMos.style.visibility = "visible";
            rConvertMos.style.display = "block";
        }
        
        if ((Goal.selectedIndex==3 || Goal.selectedIndex==4 ) && Mort1.selectedIndex==5)
        {
            ConvertMos1.style.visibility = "visible";
            rConvertMos.style.display = "block";
        }
    }
    else
    {   //Return, leaving section hidden
        return " ";
    }
}

//Show closing cost section
function js_ShowClosingCst()
{    
    //Initialize variables
    var Goal = document.getElementById("ddlGoal");
    var Mort = document.getElementById("ddlMort");
    var Mort1 = document.getElementById("ddlMort1");
    
    var Head = document.getElementById("trCostsHead");
    var Body = document.getElementById("trCostsRow");
    
    var trDown = document.getElementById("trDown");
    var trPoints = document.getElementById("trPoints");
    var trOther = document.getElementById("trOtherCosts");
    
    var Down = document.getElementById("txtDown");
    var Points = document.getElementById("txtPoints");
    var Other = document.getElementById("txtOtherCosts");
    
    var Down1 = document.getElementById("txtDown1");
    var Points1 = document.getElementById("txtPoints1");
    var Other1 = document.getElementById("txtOtherCosts1");
    
    var chkDeduct = document.getElementById("chkDeduction");
    
    //Hide section
    Head.style.display = "none";
    Body.style.display = "none";
    
    //Hide section when goal is to find monthly payment or analyze prepayment plan 
    if (Goal.selectedIndex==0 || Goal.selectedIndex==2 || Goal.selectedIndex==5)
    {
        //Exit function, leaving closing section hidden
        return;
    }
    
    //Display discount points when visible tax deduction option has been checked
    if (chkDeduct.checked==true && chkDeduct.visibility=="visible")
    {
        //Show discount points for Loan 1
        Points.style.visibility="visible"
        Down.style.visibility="hidden"
        Other.style.visibility="hidden"
        
        //Show discount points for Loan 2 if goal is to compare mortgages
        Down1.style.visibility="hidden"
        Other1.style.visibility="hidden"
        
        if (Goal.selectedIndex==3)
        { 
            Points1.style.visibility="visible"
        }
        else
        { 
            Points1.style.visibility="hidden"
        }
        
        //Show discount points in closing cost section
        Head.style.display = "block";
        Body.style.display = "block";
    }
    
    //Display section when goal is to compute total cost
    if (Goal.selectedIndex==1)
    {
        //Show closing cost controls for Loan 1
        Down.style.visibility="visible"
        Points.style.visibility="visible"
        Other.style.visibility="visible"
        
        //Hide closing cost controls for Loan 2
        Down1.style.visibility="hidden"
        Points1.style.visibility="hidden"
        Other1.style.visibility="hidden"
        
        //Exit function, leaving section controls correctly displayed
        Head.style.display = "block";
        Body.style.display = "block";
        return;
    }
    
    //Display section when goal is to compute compare two mortgages
    if (Goal.selectedIndex==3)
    {
        //Show closing cost controls for Loan 1
        Down.style.visibility="visible"
        Points.style.visibility="visible"
        Other.style.visibility="visible"
        
        //Hide closing cost controls for Loan 2
        Down1.style.visibility="visible"
        Points1.style.visibility="visible"
        Other1.style.visibility="visible"
        
        //Exit function, leaving section controls correctly displayed
        Head.style.display = "block";
        Body.style.display = "block";
        return;
    }
    
    //Display section when goal is to analyze refinancing
    if (Goal.selectedIndex==4)
    {
        //Hide closing cost controls for Loan 1
        Down.style.visibility="hidden"
        Points.style.visibility="hidden"
        Other.style.visibility="hidden"
        
        //Show closing cost controls for Loan 2
        Down1.style.visibility="visible"
        Points1.style.visibility="visible"
        Other1.style.visibility="visible"
        
        //Exit function, leaving section controls correctly displayed
        Head.style.display = "block";
        Body.style.display = "block";
        return;
    }
}

//Show or hide the prepayment section, based on user input
function js_ShowPrepayment()
{
    //Initialize variables
    var Goal = document.getElementById("ddlGoal");
    var Mort = document.getElementById("ddlMort");
    var Mort1 = document.getElementById("ddlMort1");
    var chkbx = document.getElementById("chkPrePay");
    
    var Head = document.getElementById("trPrepayHead");
    var Body = document.getElementById("trPrepayBody");
    
    var ddla = document.getElementById("ddlNumPrepay");
    var amt = document.getElementById("txtPrepayAmt");
    var ddl1a = document.getElementById("ddlNumPrepay1");
    var amt1 = document.getElementById("txtPrepayAmt1");
    
    //Display prepay section, as required by user analysis
    if (chkbx.checked==true || Goal.selectedIndex==2)
    {
        //Make Loan 1 controls visible
        ddla.style.visibility = "visible";
        amt.style.visibility = "visible";
        
        //Set visibility for Loan 2 controls
        if (Goal.selectedIndex==3 || Goal.selectedIndex==4)
        {
            ddl1a.style.visibility = "visible";
            amt1.style.visibility = "visible";
        }
        else
        {
            ddl1a.style.visibility = "hidden";
            amt1.style.visibility = "hidden";
        }
        
        //Show section
        Head.style.display = "block";
        Body.style.display = "block";
        
    }
    else
    {
        //Hide section
        Head.style.display = "none";
        Body.style.display = "none";
    } 
}

//Show or hide the tax and insurance section, based on user input
function js_ShowTaxInsurance()
{
    //Initialize variables
    var Goal = document.getElementById("ddlGoal");
    var Mort = document.getElementById("ddlMort");
    var Mort1 = document.getElementById("ddlMort1");
    var cPmi = document.getElementById("chkPMI");
    var cHazard = document.getElementById("chkHazard");
    var cPropTax = document.getElementById("chkPropTax");
    var cDeduction = document.getElementById("chkDeduction");
    
    var Head = document.getElementById("trTaxInsHead");
    var Body = document.getElementById("trTaxInsBody");
    
    var PropTax = document.getElementById("trTax");
    var Hazard = document.getElementById("trHazard");
    var Pmi = document.getElementById("trPMI");
    var Fmv = document.getElementById("trFMV"); 
    var IncTaxRate = document.getElementById("trIncomeTaxRate"); 
    
    var PropTax1 = document.getElementById("txtTax1");
    var Hazard1 = document.getElementById("txtHazard1");
    var Pmi1 = document.getElementById("txtPMI1");
    var Fmv1 = document.getElementById("txtFMV1"); 
        
    //Hide section when tax and insurance inputs are not required
    if (cPmi.checked==false && cHazard.checked==false && cPropTax.checked==false && cDeduction.checked==false)
    {
        //Hide section
        Head.style.display = "none";
        Body.style.display = "none";
        
        //Exit function, leaving tax and insurance section hidden
        return;
    }
    
    //Show or hide tax deduction info
    if ((cDeduction.checked==true) && (Goal.selectedIndex==1 || Goal.selectedIndex==3))
    {
        
        IncTaxRate.style.display = "block";
    }
    else
    {
        IncTaxRate.style.display = "none";
    }
    
    //Show or hide hazard insurance info
    if (cHazard.checked==true)
    {
       if (Goal.selectedIndex==3 || Goal.selectedIndex==4 )
       {
            Hazard1.style.visibility = "visible";
       }
       else
       {
            Hazard1.style.visibility = "hidden";
       }
       Hazard.style.display = "block";
    }
    else
    {
        Hazard.style.display = "none";
    }    
    
    //Show or hide property tax info
    if (cPropTax.checked==true)
    {
       if (Goal.selectedIndex==3 || Goal.selectedIndex==4 )
       {
            PropTax1.style.visibility = "visible";
       }
       else
       {
            PropTax1.style.visibility = "hidden";
       }
       PropTax.style.display = "block";
    }
    else
    {
        PropTax.style.display = "none";
    }
    
    //Show or hide PMI
    if (cPmi.checked==true)
    {        
        Pmi.style.display = "block";
        if (Goal.selectedIndex==3 || Goal.selectedIndex==4)
        {
            Fmv1.style.visibility = "visible";
            Pmi1.style.visibility = "visible";
        }
        else
        {
            Fmv1.style.visibility = "hidden";
            Pmi1.style.visibility = "hidden";
        }
        Fmv.style.display = "block";
    }
    else
    {
        Pmi.style.display = "none";
        Fmv.style.display = "none";
    }  

    //Update tax/insurance heading, based on checkbox options
    Head.innerHTML = "Enter Tax and Insurance Info";
    if (cPmi.checked == false && cHazard.checked == false)
    {
        Head.innerHTML = "Enter Tax Info";
    }
    if (cPropTax.checked == false && cDeduction.checked == false)
    {
        Head.innerHTML = "Enter Insurance Info";
    }
    //Show tax and insurance section
    Head.style.display = "block";
    Body.style.display = "block";
}