﻿///<reference name="MicrosoftAjax.js"/>

var EconomicHardshipPage = function(tableLocationId, testResultsLoacation){
    EconomicHardshipPage.Instance = this;
    this.LoanTableLocation = $get(tableLocationId);
    this.LoanTableBuilder = new LoanTableBuilder();
    this.LoanTableBuilder.SetCustomEventFunction(function(){EconomicHardshipPage.Instance.SetCalculationIsValid(false);});
    this.TestResultsLoacation = $get(testResultsLoacation);
    this.LoanTableId;
    this.Step1Validator = new Validator('calcStep1Validation');
    this.Step2Validator = new Validator('calcStep2Validation');
    this.SetPageWrapper("economicHardshipDefermentPage");
    this.FinalStep = $get("calc-calculator-step4");
    
    
    /************************************************************************/
    ///This function gets called by this.onComplete and is in charge of
    ///returning the Hardship Test Results to the page after calculation 
    /*======================================================================*/
    this.GetHardshipTestResults = function(data){
        var hardshipTestResultsSchedule = document.createElement('div');
        var tblContainer = document.createElement('div');
        var hardshipQualifyHeader = document.createElement('div');
        var hardshipQualifyFooter = document.createElement('div');
    
        function SetHeaderAndFooter(qualify){
            if(qualify){
                hardshipQualifyHeader.innerHTML = "You DO qualify for Economic Hardship Deferment";
                hardshipQualifyFooter.innerHTML = 
                    "If you want to apply for Economic Hardship Deferment click here for the "+
                    "<a href=\"/downloads/defer_hardship.pdf\" target=\"_blank\">EHD request</a>.";
            }
            else{
                hardshipQualifyHeader.innerHTML = "You DO NOT qualify for Economic Hardship Deferment"
                hardshipQualifyFooter.innerHTML = 
                    "If you do not qualify for Economic Hardship Deferment, understand that there are"+
                    "&nbsp;other options available to help you manage your student loan obligation."+
                    "&nbsp;Reveiw the <a href=\"/education/timeframes/graduation/?tab=0\" target=\"_blank\">Repayment Options</a> and"+
                    "&nbsp;<a href=\"/education/timeframes/graduation/?tab=3\" target=\"_blank\"> Postponing Repayment</a> information"+
                    "&nbsp;within the Graduation phase of Borrower Education.";
            }
            
        }
        SetHeaderAndFooter(data.DoYouQualifyForEconomicHardship);
        
        var hardshipTestResults = [ njs.Converter.NumberToMoney(data.TwentyPercentOfIncome),
                                    njs.Converter.NumberToMoney(data.EstimatedMonthlyPayment),
                                    data.IsMonthlyPaymentGreaterThanTwentyPercentOfIncome,
                                    njs.Converter.NumberToMoney(data.MonthlyPayment),
                                    njs.Converter.NumberToMoney(data.MonthlyIncomeMinusMonthlyPayment),
                                    njs.Converter.NumberToMoney(data.ThreeThirtyPercentOfStatePoverty),
                                    njs.Converter.NumberToMoney(data.TwoTwentyPercentOfMinimumWage),
                                    njs.Converter.NumberToMoney(data.GreaterOfLineThreeAndLineFour),
                                    data.IsLineTwoLessThanLineFive,
                                    data.DoYouQualifyForEconomicHardship    ];
            
        tblContainer.className = 'HardshipTestResultsSchedule';
        hardshipQualifyHeader.className = "hardshipQualifyHeader"
        hardshipQualifyFooter.className = "hardshipQualifyFooter"
        hardshipTestResultsSchedule.appendChild(hardshipQualifyHeader);
        tblContainer.appendChild(
            ScheduleBuilder.BuildHardshipTestResultsSchedule(hardshipTestResults));
        hardshipTestResultsSchedule.appendChild(tblContainer);
        hardshipTestResultsSchedule.appendChild(hardshipQualifyFooter);
        
        return hardshipTestResultsSchedule;
    }
    /*======================================================================*/
    /************************************************************************/
    
    this.SetActiveNavigationLink();
}
EconomicHardshipPage.prototype = new CalculatorPage;
    
EconomicHardshipPage.prototype.Continue = function(){
    var eleToValidate = [$get('monthlyIncome'),$get('familySize')];
    if(EconomicHardshipPage.Instance.Step1Validator.Validate(eleToValidate)){
        this.DisplayStep(2);
    }
}
    
EconomicHardshipPage.prototype.ContinueOnEnter = function(e){
        if(this.KeyEventIsValid(e))
            this.Continue();
}

EconomicHardshipPage.prototype.AddLoan = function(newTableId){
    var isValid = true;
    var validation1 = EconomicHardshipPage.Instance.
                        Step1Validator.Validate([$get('monthlyIncome'),$get('familySize')]);
    var validation2 =  EconomicHardshipPage.Instance.
                        Step2Validator.Validate([$get('loanBalance'),$get('interestRate')]);
    
    
    if(validation1 && validation2){
        this.LoanTableId = newTableId;
        this.LoanTableBuilder.BuildTable(   this.LoanTableLocation, 
                                            this.LoanTableId, 
                                            {'Balance':'loanBalance'}, 
                                            {'Interest Rate':'interestRate'}  );
        this.DisplayStep(3);
        this.SetCalculationIsValid(false);
    }
}
    
EconomicHardshipPage.prototype.AddLoanOnEnter = function(e, newTableId){
    if(this.KeyEventIsValid(e))
        EconomicHardshipPage.Instance.AddLoan(newTableId);
}

EconomicHardshipPage.prototype.Calculate = function() {
    if (!this.IsServiceAvailable) return;
    
    var isValid = true;
    if(!EconomicHardshipPage.Instance.
            Step1Validator.Validate([$get('monthlyIncome'),$get('familySize')])){
        isValid = false;
        this.Step1Validator.ShowInvalidInputs();
        $get('calcStep3Validation').innerHTML = "Invalid or missing values in Step 1";
    }

    if (isValid) {
        this.IsServiceAvailable = false;
        this.DisplayProcessingInContainer(this.TestResultsLoacation);
        EconomicHardshipPage.Instance.DisplayStep(4);
        
        $get('calcStep3Validation').innerHTML = "";
        var states = document.getElementsByName('state');
        var state;
        for (var i=0; i < states.length; i++){
            if(states[i].checked){
                state = states[i].value;
            }
        }
        var hardshipTestPackage = { State: state,
                                    MonthlyIncome: njs.Converter.MoneyToNumber($get('monthlyIncome').value),
                                    FamilySize: $get('familySize').value    }; 
                      
        var LoanPackage = 
            LoanPackageBuilder.CreateLoanPackageFromLoanTable(  this.LoanTableId, 
                                                                ['Balance','InterestRate']);
                                                                                      
        njs.services.Calculators.TestEconomicHardship(  hardshipTestPackage,
                                                        LoanPackage.Loans, 
                                                        this.onComplete,
                                                        this.onError    );
        this.SetCalculationIsValid(true);   
    }
}

EconomicHardshipPage.prototype.onComplete = function(data) {
    EconomicHardshipPage.Instance.IsServiceAvailable = true;
    EconomicHardshipPage.Instance.ClearContainer(
        EconomicHardshipPage.Instance.TestResultsLoacation);
        
    EconomicHardshipPage.Instance.TestResultsLoacation.appendChild(
        EconomicHardshipPage.Instance.GetHardshipTestResults(data));
}  
    
EconomicHardshipPage.prototype.onError = function(){
    EconomicHardshipPage.Instance.ClearContainer(
        EconomicHardshipPage.Instance.TestResultsLoacation);
} 