﻿// JScript File - Functions that control Calculator appearance 

//*********************** UPDATE HTML CONTROLS *********************************
//Set label text 
function set_label_text(ID, Text) 
{
    var label=document.getElementById(ID);
    label.innerHTML=Text;
}

//************************ QUERY STRING ****************************************
//Read and return query string. Returns "" if no value for query string is found. 
function ReadQueryString(QueryStringName) 
{
  //Declare variables
  var query = '';
  var url = ''+window.location;       //The quotes are needed to make this a string
  
  //Declare a regular expression, based on the query string name 
  var re = new RegExp(QueryStringName, "i");
  
  //Exit function if query string name does NOT appear in url  
  if (!re.test(url))
  {
    return query;  
  }
  
  //Split off portion of url that contains query strings 
  var split_url=url.split('?');
  
  if (split_url.length<2) 
  {
    //Url does not include a query string value
    return query;
  }
  else
  {
    //Redefine the url to include only the query string(s)
    for (var i=1;i<split_url.length;i++) 
    {
    query=''+query+split_url[i];
    }
  }
  
  //Split the url into individual query strings
  var split_query=query.split('&');
  
  //Check each query string against the query string name
  var teststr = '^( )*(%20)*( )*' + QueryStringName + '( )*(%20)*=( )*(%20)*( )*'
  var space1 = '( )*(%20)*( )*$'
  
  var re1 = new RegExp(teststr, "i");
  var re2 = new RegExp(space1, "i");
    
  for (var i=0;i<split_query.length;i++) 
  {
    query = split_query[i];         //Initialize query string to be tested
    if (re1.test(query))            //See if query string begins with string name
    {
    query = query.replace(re1,"")   //Remove spaces, string name, and equals from front
    query = query.replace(re2,"")   //Remove spaces from back, leaving only query value
    
    return query;                   //Return value of query string
    }
  }
  
  //Value for query string variable not found
  query='';
  return query;
}

//*********************** UPDATE CALCULATOR ************************************
//Define mortgage goal displayed in ddlGoal, based on query string.  
//This function should only run the first time the page loads; thereafter, the 
//goal is set by the User.
function set_goal() 
{
    //If this is not the first time the page is loaded, exit function
    var txtbox = document.getElementById("txtOutput");
    if (!(txtbox.value=="PageLoad")) {return;}
    
    //If the Url includes a query string, use it to define the goal           
    var goal = ReadQueryString("Goal");
    if (goal.length>0) 
    {document.form1.ddlGoal.selectedIndex=goal;}
    else
    {goal = document.form1.ddlGoal.selectedIndex}
    goal=goal    
}

//Define mortgage type displayed in ddlMort, based on query string
function set_mortgage() 
{
    //If Not page load, exit function
    var txtbox = document.getElementById("txtOutput");
    if (!(txtbox.value=="PageLoad")) {return;}
    
    //If the Url includes a query string, use it to define the mortgage           
    var mort = ReadQueryString("Mort"); 
    if (mort.length>0) 
    {document.form1.ddlMort.selectedIndex=mort;}
    else
    {mort = document.form1.ddlMort.selectedIndex}
    mort=mort       
    
    //If necessary, update ddlNumPrepay
    if (!(mort=="1"))
    {
        //Mortgage calls for monthly payments, so we make 0 to 12 prepayments per year
        var prepay = document.getElementById("ddlNumPrepay");
        prepay.length = 13;
        for (i=0; i<13; i++)
        {
            prepay.options[i].value = i;
            prepay.options[i].text = i;           
        } 
    }
}

//Update page title
function update_page_title() 
{
    //Find mortgage type
    var mort = "";
    var ddlMort = document.getElementById("ddlMort");
    if (ddlMort.selectedIndex==0) {mort="Fixed-Rate Loan";}
    if (ddlMort.selectedIndex==1) {mort="Biweekly Loan";}
    if (ddlMort.selectedIndex==2) {mort="Balloon Loan";}
    if (ddlMort.selectedIndex==3) {mort="Interest-Only Loan";}
    if (ddlMort.selectedIndex==4) {mort="Adjustable Rate Mortgage (ARM)";}
    if (ddlMort.selectedIndex==5) {mort="Convertible ARM";}
    if (ddlMort.selectedIndex==6) {mort="Two-Step Mortgage";}
    if (ddlMort.selectedIndex==7) {mort="Balloon ARM";}
    if (ddlMort.selectedIndex==8) {mort="Interest-Only ARM";}
    if (ddlMort.selectedIndex==9) {mort="Graduated Payment Loan";}
    
    //Update the calculator title, if necessary
    var pageTitle = document.getElementById("lblTitle");
    var ddlb = document.getElementById("ddlGoal");
    if (ddlb.selectedIndex==0) 
        {pageTitle.innerHTML="Monthly Mortgage Payment Calculator: " + mort;}
    if (ddlb.selectedIndex==1) 
        {pageTitle.innerHTML="Total Mortgage Cost Calculator: " + mort;}
    if (ddlb.selectedIndex==2) 
        {pageTitle.innerHTML="Extra Payment Mortgage Calculator: " + mort;}
    if (ddlb.selectedIndex==3) 
        {pageTitle.innerHTML="Compare Two Mortgages Calculator";}
    if (ddlb.selectedIndex==4) 
        {pageTitle.innerHTML="Mortgage Refinance Calculator";}
    if (ddlb.selectedIndex==5) 
        {pageTitle.innerHTML="Amortization Schedule Calculator: " + mort;}        
}  


//Update intro.  Note: This update conforms to the VB intro defined in VB code behind.
function update_intro() 
{
    //If goal query string has not been defined, exit function 
    //var goal = ReadQueryString("Goal");
    //if (goal == "") {return;};
    
    //Declare variables 
    var mortValue = document.form1.ddlMort.selectedValue;
    var mort = document.form1.ddlMort.selectedIndex;
    var goal = document.form1.ddlGoal.selectedIndex;
    var intro = "This mortgage calculator is set up to ";
    var help=""
    //var help = "<br /><br />For help in using the calculator, click a <span onclick='jsHelpIcon();'><a href='#calculator'>Help Icon</a></span> <span class='HelpIcon' onclick='jsHelpIcon();'>&nbsp;&nbsp;?&nbsp;&nbsp;</span>, read the <A href='#faq'>Frequently-Asked Questions</A>, or review the <A href='#examples'>Sample Problems</A>.";
            
    //If necessary, initialize goal
    if ( !( (goal>=0) && (goal<=5) ) ) 
    {
        goal=1;
    }
    
    //Define intro, based on goal
    switch (goal)
        {
        case 0 :
            intro += "find the monthly mortgage payment for ";            
            break;
        case 1 :
            intro += "find the total mortgage cost for ";            
            break;
        case 2 :
            intro += "compute the savings you will realize by making extra mortgage payments (i.e., prepayments) on ";            
            break;
        case 3 :
            intro += "compare mortgage options objectively. ";            
            intro += "The calculator shows how monthly payments and lifetime costs ";            
            intro += "are affected by points, down payment, prepayments, ";            
            intro += "different types of mortgages, etc. ";            
            intro += "";            
            break;
        case 4 :
            intro += "assess mortgage refinance costs and benefits. ";            
            intro += "The calculator shows how refinancing affects total mortgage cost ";                        
            intro += "and loan duration. It even reports the breakeven point, where ";                        
            intro += "refinancing costs are paid for by refinancing savings. ";
            break;
        case 5 :
            intro += "generate an amortization schedule (aka, amortization table, amortization chart) for ";
            intro += "";            
            break;
        default :
            Purpose = "analyze the relative merits of different mortgage options.";
        }
    
    //Define intro, based on mort, by adding mortgage-specific info
    if (goal==0 || goal==1 || goal==2 || goal==5)    
    {
        switch (mort)
        {
        case 0 :
            intro += "a conventional fixed-rate loan. ";            
            break;
        case 1 :
            intro += "a biweekly fixed-rate home loan. "; 
            break;
        case 2 :
            intro += "a fixed-rate balloon loan. "; 
            break;
        case 3 :
            intro += "a fixed-rate interest-only loan. "; 
            break;
        case 4 :
            intro += "an adjustable-rate mortgage (ARM). " ; 
            break;
        case 5 :
            intro += "a convertible adjustable-rate mortgage (ARM). "; 
            break;
        case 6 :
            intro += "a two-step mortgage. "; 
            break;
        case 7 :
            intro += "a balloon adjustable-rate mortgage (ARM). "; 
            break;
        case 8 :
            intro += "an interest-only adjustable-rate mortgage (ARM). "; 
            break;
        case 9 :
            intro += "a graduated payment loan. "; 
            break;
        default :
            intro += "a particular type of mortgage. ";
        }
    }
    
    if (goal != 3 && goal != 4) 
    {
    intro += "To work with a different type of loan, simply choose a different loan option from the 'Mortgage Type' box below. You can choose from 10 different kinds of mortgage."    
    }      

    //Set Intro
    intro = intro + help;
    set_label_text("lblIntro", intro);   
    
    //Redefine txtOutput to prevent Mort and Goal from being changed
    var txtbox = document.getElementById("txtOutput");
    txtbox.style.visibility="hidden";
    if (txtbox.value=="PageLoad")
    {
        txtbox.value = "Calculator";
    }     
} 

//Update calculator to reflect User input
function update_calc() 
{
    //Update content
    update_page_title();        //Set title
    update_intro();
}

//Update document title
function js_update_document_title() 
{
    //Find mortgage type
    var mort = "";
    var ddlMort = document.getElementById("ddlMort");
    if (ddlMort.selectedIndex==0) {mort="Fixed-Rate Loan";}
    if (ddlMort.selectedIndex==1) {mort="Biweekly Loan";}
    if (ddlMort.selectedIndex==2) {mort="Balloon Loan";}
    if (ddlMort.selectedIndex==3) {mort="Interest-Only Loan";}
    if (ddlMort.selectedIndex==4) {mort="Adjustable Rate Mortgage (ARM)";}
    if (ddlMort.selectedIndex==5) {mort="Convertible ARM";}
    if (ddlMort.selectedIndex==6) {mort="Two-Step Mortgage";}
    if (ddlMort.selectedIndex==7) {mort="Balloon ARM";}
    if (ddlMort.selectedIndex==8) {mort="Interest-Only ARM";}
    if (ddlMort.selectedIndex==9) {mort="Graduated Payment Loan";}
    
    //Update the calculator title, if necessary
    var docTitle = "";
    var ddlb = document.getElementById("ddlGoal");
    if (ddlb.selectedIndex==0) 
        {docTitle="Monthly Mortgage Payment Calculator: " + mort;}
    if (ddlb.selectedIndex==1) 
        {docTitle="Total Mortgage Cost Calculator: " + mort;}
    if (ddlb.selectedIndex==2) 
        {docTitle="Extra Payment Mortgage Calculator: " + mort;}
    if (ddlb.selectedIndex==3) 
        {docTitle="Compare Two Mortgages Calculator";}
    if (ddlb.selectedIndex==4) 
        {docTitle="Mortgage Refinance Calculator";}
    if (ddlb.selectedIndex==5) 
        {docTitle="Amortization Schedule Calculator: " + mort;}    
        
    document.title = docTitle;
}
