﻿///<reference name="MicrosoftAjax.js"/>

var EstimatedPaymentAndLoanCostPage = function(tableLocationId,paymentOptionsScheduleLocation){
    EstimatedPaymentAndLoanCostPage.Instance = this;
    this.LoanTableLocation = $get(tableLocationId);
    this.PaymentScheduleLocation = $get(paymentOptionsScheduleLocation);
    this.PaymentSchedule = new PaymentOptionsSchedule("EPLC");
    this.LoanTableId;
    this.Step1Validator = new Validator('calcStep1Validation');
    this.SetPageWrapper("estimatedPaymentAndLoanCostPage");
    this.FinalStep = $get("calc-calculator-step2");
    
    this.SetActiveNavigationLink();
}
EstimatedPaymentAndLoanCostPage.prototype = new CalculatorPage;

EstimatedPaymentAndLoanCostPage.prototype.CalculateOnEnter = function(e){
    if(this.KeyEventIsValid(e))
        EstimatedPaymentAndLoanCostPage.Instance.Calculate();
}

EstimatedPaymentAndLoanCostPage.prototype.Calculate = function() {
    if (!this.IsServiceAvailable) return;   
                     
    if(EstimatedPaymentAndLoanCostPage.Instance.Step1Validator.Validate(document.getElementsByTagName('input'))){
        this.IsServiceAvailable = false;
        this.DisplayProcessingInContainer(this.PaymentScheduleLocation);
        EstimatedPaymentAndLoanCostPage.Instance.DisplayStep(2); 
        
        var checkedBox = document.getElementsByName("choice")[0].checked;
        
        if(checkedBox){
            var LoanPackage = LoanPackageBuilder.
                CreateLoanPackageFromObject({ Balance: njs.Converter.MoneyToNumber($get('loanBalance').value),
                                              InterestRate:'interestRate',
                                              Term:'term' });
        }
        else{
            var b = njs.Converter.MoneyToNumber($get('loanBalance').value);
            var i = njs.Converter.PercentToInt($get('interestRate').value)/100;
            var g = parseInt($get('gracePeriod').value);
            
            var newBalance = b+(b*((i/12)*g)); //Pull this out in Refactor
            
            var LoanPackage = LoanPackageBuilder.
                CreateLoanPackageFromObject({ Balance: newBalance,
                                              InterestRate:'interestRate',
                                              Term:'term' });
        }
        njs.services.Calculators.CalculateLoanPaymentOptions(   LoanPackage.Loans, 
                                                                    this.onComplete,
                                                                    this.onError    );
        
        this.SetCalculationIsValid(true);
    }  
}

EstimatedPaymentAndLoanCostPage.prototype.onComplete = function(data) {
    EstimatedPaymentAndLoanCostPage.Instance.IsServiceAvailable = true;
    EstimatedPaymentAndLoanCostPage.Instance.ClearContainer(
        EstimatedPaymentAndLoanCostPage.Instance.PaymentScheduleLocation);
        
    EstimatedPaymentAndLoanCostPage.Instance.PaymentScheduleLocation.appendChild(    
        EstimatedPaymentAndLoanCostPage.Instance.
            PaymentSchedule.GetPaymentOptions(data));
}

 EstimatedPaymentAndLoanCostPage.prototype.onError = function(){
    EstimatedPaymentAndLoanCostPage.Instance.ClearContainer(
        EstimatedPaymentAndLoanCostPage.Instance.PaymentScheduleLocation);
}   
