﻿function IsNumber(sValue){
    if(sValue == ''){
        return false;    
    }else{
        var y = sValue.replace(/,/g,'');    //now without comma
        if(isNaN(y)){
           return false;
        }else{
            return true;
        }
    }
}

function CalcNeeds(){

    var UserMessage = document.getElementById("UserMessage");
    var NumPoints;  // used to determine what type of Term Life they need, done at the end of function.
    var sPlanType;
    var bError = false;   
    
    // First error trap to make sure values (e.g. numbers) were entered in all cases
    var AmountOfLifeError = document.getElementById("AmountOfLifeError");
    if (!IsNumber(document.getElementById("AmountOfLife").value)){
        bError = true;        
        AmountOfLifeError.style.display = "";
    }else{
        AmountOfLifeError.style.display = "none";
    }
    var ValueOfAllInvestmentsError = document.getElementById("ValueOfAllInvestmentsError");
    if (!IsNumber(document.getElementById("ValueOfAllInvestments").value)){
        bError = true;
        ValueOfAllInvestmentsError.style.display = "";
    }else{
        ValueOfAllInvestmentsError.style.display = "none";
    }
    var OtherMonthlyIncomeError = document.getElementById("OtherMonthlyIncomeError");
    if (!IsNumber(document.getElementById("OtherMonthlyIncome").value)){
        bError = true;        
        OtherMonthlyIncomeError.style.display = "";
    }else{
        OtherMonthlyIncomeError.style.display = "none";
    }
    var AmountOwingOnMortgageError = document.getElementById("AmountOwingOnMortgageError");
    if (!IsNumber(document.getElementById("AmountOwingOnMortgage").value)){
        bError = true;        
        AmountOwingOnMortgageError.style.display = "";
    }else{
        AmountOwingOnMortgageError.style.display = "none";
    }
    var TotalOtherDebtError = document.getElementById("TotalOtherDebtError");
    if (!IsNumber(document.getElementById("TotalOtherDebt").value)){
        bError = true;        
        TotalOtherDebtError.style.display = "";
    }else{
        TotalOtherDebtError.style.display = "none";
    }
    var CostOfFuneralError = document.getElementById("CostOfFuneralError");
    if (!IsNumber(document.getElementById("CostOfFuneral").value)){
        bError = true;        
        CostOfFuneralError.style.display = "";
    }else{
        CostOfFuneralError.style.display = "none";
    }
    var EducationCostsError = document.getElementById("EducationCostsError");
    if (!IsNumber(document.getElementById("EducationCosts").value)){
        bError = true;        
        EducationCostsError.style.display = "";
    }else{
        EducationCostsError.style.display = "none";
    }
    var RequiredMonthlyIncomeError = document.getElementById("RequiredMonthlyIncomeError");
    if (!IsNumber(document.getElementById("RequiredMonthlyIncome").value)){
        bError = true;        
        RequiredMonthlyIncomeError.style.display = "";
    }else{
        RequiredMonthlyIncomeError.style.display = "none";
    }
    var NumYearsOfIncomeError = document.getElementById("NumYearsOfIncomeError");
    if (!IsNumber(document.getElementById("NumYearsOfIncome").value)){
        bError = true;        
        NumYearsOfIncomeError.style.display = "";
    }else{
        NumYearsOfIncomeError.style.display = "none";
    }    
    var MonthlyExpensesError = document.getElementById("MonthlyExpensesError");
    if (!IsNumber(document.getElementById("MonthlyExpenses").value)){
        bError = true;        
        MonthlyExpensesError.style.display = "";
    }else{
        MonthlyExpensesError.style.display = "none";
    }       
    
    if(!bError){
         
         // Now figure out type of insurance required!
         switch(document.getElementById("NumChildren").value)
        {
            case "0":
                NumPoints = 0;
                break;
            case "1":
                NumPoints = 2;
                break;
            case "2":
                NumPoints = 2;
                break;
            case "3":
                NumPoints = 4;
            break;
            case "4":
                NumPoints = 6;
                break;
            case "5+":
                NumPoints = 8;
                break;
        }    
        switch(document.getElementById("YoungestChildAge").value)
        {   
            case "na":
                NumPoints += 0;
                break;
            case "2OrLess":
                NumPoints += 1;
                break;
            case "2-5":
                NumPoints += 1;
                break;
            case "6-10":
                NumPoints += 3;
            break;
            case "11-15":
                NumPoints += 6;
                break;
            case "15-20":
                NumPoints += 6;
                break;
            case "21OrMore":
                NumPoints += 0;
                break;
        }
        switch(document.getElementById("MoreChildren").value)
        {   
            case "0":
                NumPoints += 0;
                break;
            case "1":
                NumPoints += 1;
                break;
        }
        switch(document.getElementById("EducationMoney").value)
        {   
            case "na":
                NumPoints += 0;
                break;
            case "No":
                NumPoints += 4;
                break;
            case "YesNotMuch":
                NumPoints += 4;
                break;
            case "Yes":
                NumPoints += 2;
                break;
            case "ChildrenNotInSchool":
                NumPoints += 0;
                break;          
        }
        switch(document.getElementById("PayOffMortgage").value)
        {   
            case "0":
                NumPoints += 0;
                break;
            case "1":
                NumPoints += 3;
                break;
        }
        switch(document.getElementById("WhenMortgagePaid").value)
        {   
            case "3OrLess":
                NumPoints += 0;
                break;
            case "4-10":
                NumPoints += 6;
                break;
            case "11-20":
                NumPoints += 2;
                break;
        }
        switch(document.getElementById("YearsOfIncome").value)
        {   
            case "LessThan2":
                NumPoints += 5;
                break;
            case "3-5":
                NumPoints += 4;
                break;
            case "6-10":
                NumPoints += 3;
                break;
            case "MoreThan10":
                NumPoints += 6;
                break;
        }
        switch(document.getElementById("AlreadyHaveLife").value)
        {   
            case "No":
                NumPoints += 0;
                break;
            case "YesButNotEnough":
                NumPoints += 6;
                break;
            case "YesButRunsOut":
                NumPoints += 4;
                break;
            case "YesButFromWork":
                NumPoints += 4;
                break;
            case "YesFromBank":
                NumPoints += 3;
                break;
        }
        switch(document.getElementById("ApplicantRetiring").value)
        {   
            case "10":
                NumPoints += 3;
                break;
            case "11-15":
                NumPoints += 5;
                break;
            case "16-20":
                NumPoints += 4;
                break;
            case "MoreThan20":
                NumPoints += 3;
                break;
        }
        switch(document.getElementById("SpouseRetiring").value)
        {   
            case "NotMarried":
                NumPoints += 0;
                break;
            case "10":
                NumPoints += 1;
                break;
            case "11-15":
                NumPoints += 4;
                break;
            case "16-20":
                NumPoints += 4;
                break;
            case "MoreThan20":
                NumPoints += 1;
        }
        switch(document.getElementById("RetirementFinancing").value)
        {   
            case "WorkPensionNoRRSP":
                NumPoints += 8;
                break;
            case "WorkPensionSomeRRSP":
                NumPoints += 6;
                break;
            case "WorkPensionLotsRRSP":
                NumPoints += 1;
                break;
            case "NoPensionSomeRRSP":
                NumPoints += 10;
                break;            
            case "GovernmentBenefits":
                NumPoints += 8;
                break;            
            case "NoPensionLotsRRSP":
                NumPoints += 0;
                break;
        }    
        // switch(document.Form2.TaxFreeAccount[1].checked)
        // the following evaluates backwards, but just works. Note that if they have a tax-free account it is zero points!
        switch(document.getElementById("TaxFreeAccount").checked)
        {   
            case true:
                NumPoints += 3;
                break;
            case false:
                NumPoints += 0;
                break;
        }  
        
        // Now determine how much insurance is needed!
        var iNumYears = document.getElementById("NumYearsOfIncome").value;          
        var iMonthlyDebt;
        
        // before using parseFloat method we must first strip out commas!
        var dTotalOtherDebt = document.getElementById("TotalOtherDebt").value.replace(/,/g,'');
        var dEducationCosts = document.getElementById("EducationCosts").value.replace(/,/g,'');
        var dCostOfFuneral = document.getElementById("CostOfFuneral").value.replace(/,/g,'');        
        var dAmountOwingOnMortgage = document.getElementById("AmountOwingOnMortgage").value.replace(/,/g,'');        
        var iImmediateDebt = parseFloat(dTotalOtherDebt) + parseFloat(dEducationCosts) + parseFloat(dCostOfFuneral) + parseFloat(dAmountOwingOnMortgage);
                
        var dAmountOfLife = document.getElementById("AmountOfLife").value.replace(/,/g,'');
        var dValueOfAllInvestments = document.getElementById("ValueOfAllInvestments").value.replace(/,/g,'');
        var iAssets = parseFloat(dAmountOfLife) + parseFloat(dValueOfAllInvestments);
        var iMonthlyCost;
        var iMonthlyIncome;
        var iInsuranceRequired;

        dMonthlyExpenses = document.getElementById("MonthlyExpenses").value.replace(/,/g,'');
        iMonthlyCost = parseFloat(dMonthlyExpenses);
        dOtherMonthlyIncome = document.getElementById("OtherMonthlyIncome").value.replace(/,/g,'');        
        iMonthlyIncome = parseFloat(dOtherMonthlyIncome);
       iMonthlyDebt = iMonthlyCost - iMonthlyIncome
       // if monthly income is greater than monthly debt then assume monthly debt of zero
       if(iMonthlyDebt < 0)
            iMonthlyDebt = 0

       // unadjusted total required is: ((time in years) * 12mnths/year * monthly debt) + immediate debt - total assets
       iInsuranceRequired = (iNumYears * 12 * iMonthlyDebt) + iImmediateDebt - iAssets

       //  total amount of insurance required is total minus immediate expenses plus assets (assumed to be liquidated), 
       //  we multiplied 1.5% return on investment.
       for (i=1;i<=iNumYears;i++)
       {
            iInsuranceRequired = iInsuranceRequired - iInsuranceRequired * 0.015;
       }

       //Check to see if negative, if so make zero
       if(iInsuranceRequired < 0)
            iInsuranceRequired = 0    
            
       // now round to nearest integer
       iInsuranceRequired = iInsuranceRequired.toFixed(0);
        
    }
    
    // If no end-user error display results, otherwise prompt to fix error!    
    if(!bError){
        
        // get handle to bottom message box
        var BottomMessage = document.getElementById("BottomMessage")
        
        // Look at amount of points, and display appropriate message to end-user.
        if(NumPoints < 15){
            UserMessage.innerHTML = "<p style='border: 1px solid Green; padding: 7px;'><strong>Term 100</strong> is the plan we recommend. It offers the peace of mind of a permanent policy that will never have a premium increase. It is designed without cash vales, so it represents the lowest cost permanent insurance available. This policy can be set up on a joint &quot;last to die&quot; basis so an estate can have the cash when it is needed most (at the second death of a couple as this is when the estate is often most heavily taxed).<br />&nbsp;<br />The amount of life insurance that is required is <strong>$" + iInsuranceRequired + "</strong>. This amount is an estimate only, and is based on a conservative investment return (1.5%) above the rate of inflation (we assume that the immediate debts will be paid off and that the rest of the benefits will be invested).<br />&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- To continue this process please click <a href='https://www.life-insurance-quotes.ca/default.aspx?Section=Common&amp;Page=Quote'>here</a> to get your quotes -</p>";
            sPlanType = "Term100";
        }
        else if(NumPoints > 14 && NumPoints < 35){
         UserMessage.innerHTML = "<p style='border: 1px solid Green; padding: 7px;'><strong>Term 20</strong> is the plan we recommend. It offers affordable term insurance, and is guaranteed not to increase in the initial 20 year period.<br />&nbsp;<br />The amount of life insurance that is required is <strong>$" + iInsuranceRequired + "</strong>. This amount is an estimate only, and is based on a conservative investment return (1.5%) above the rate of inflation (we assume that the immediate debts will be paid off and that the rest of the benefits will be invested).<br />&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- To continue this process please click <a href='https://www.life-insurance-quotes.ca/default.aspx?Section=Common&amp;Page=Quote'>here</a> to get your quotes -</p>";
         sPlanType = "Term20";
        }
        else{
         UserMessage.innerHTML = "<p style='border: 1px solid Green; padding: 7px;'><strong>Term 10</strong> is the plan we recommend, since it offers the most coverage for the fewest premium dollars.<br />&nbsp;<br />This plan is designed for young families, and those who have debt or a mortgage obligation.<br />&nbsp;<br />The amount of life insurance that is required is <strong>$" + iInsuranceRequired + "</strong>. This amount is an estimate only, and is based on a conservative investment return (1.5%) above the rate of inflation (we assume that the immediate debts will be paid off and that the rest of the benefits will be invested).<br />&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- To continue this process please click <a href='https://www.life-insurance-quotes.ca/default.aspx?Section=Common&amp;Page=Quote'>here</a> to get your quotes -</p>";
         sPlanType = "Term10";
        }
        
        // let's set a cookie that will record the recommended Plan, which
        //    will be use by the Quote page (if set the Plan will initially be set to Plan determiend by this Tool)
        Set_Cookie("LQRequiredPlan",sPlanType,"360","/","","");
        Set_Cookie("LQRequiredAmount",iInsuranceRequired,"360","/","","");
        
        // finally, display top of page, where we display result information!
        window.scrollTo(0,0);        
        
    }else{
        UserMessage.innerHTML = "<p style='text-align: center; color: red;'>Please fix the errors indicated in red below.</p>"    
    }    

}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              