'use strict';

(function (module) {

    module.service('FinanceService', [
        '$window', function ($window) {


            var articleOnClick = function (articleTypeID, articleURL) {
                var mainUrl = window.location.protocol + '//' + window.location.host + '/';
                var path = mainUrl + 'Article/Index/' + articleURL + '?type=' + articleTypeID;
                if (path != null && path.trim() != '') {
                    $window.open(path, '_self');
                }
            };


            return {
                ArticleOnClick: articleOnClick
            }
        }
    ]);
})(angular.module('AutoSMART.Web.Finance'));
"use strict";

(function (module) {

    module.component('financecomponent', {
        
        controller: ["$timeout",
            function ($timeout) {

                var $ctrl = this;
                document.title = "AutoSMART - Finance";
            }],
        templateUrl: UrlContent('/App/Finance/Views/Finance.html')
    });
})(angular.module('AutoSMART.Web.Finance'));

"use strict";

(function (module) {

    module.component('affordabilitycalculatorcomponent', {

        controller: ["CalculatorService", "$window", "GoogleAnalyticsService", "FieldValidationService",
            function (CalculatorService, $window, GoogleAnalyticsService, FieldValidationService) {
                
                var $ctrl = this;
                $ctrl.calculatorValues = {
                    DesiredMonthlyPayment: 0.0,
                    SelectedTerm: 0,
                    InterestRate: 0.0,
                    DownPayment: 0.0,
                    TradeInValue: 0.0,
                    CashRebate:0.0
            };
                $ctrl.$onInit = function () {
                    $ctrl.regularExpression = "/^\d+(\.\d{1,2})?$/";
                    CalculatorService.Init().then(function () {
                        $ctrl.loanTerms = CalculatorService.CalcContent.LoanTerms;
                        $ctrl.calculatorValues.SelectedTerm = CalculatorService.CalcContent.LoanDuration;
                        $ctrl.calculatorValues.InterestRate = CalculatorService.CalcContent.Rate;
                    });
                    $ctrl.validCalculator = true;
                    $ctrl.checking = false;
                    $ctrl.calculatorValues.DesiredMonthlyPayment = 200;
                    $ctrl.calculatorValues.DownPayment = 2000;
                    $ctrl.calculatorValues.TradeInValue = 0;
                    $ctrl.calculatorValues.CashRebate = 2000;
                    //amplitude.getInstance('app2').logEvent('log event on finance page - affordability calc init');

                };

                $ctrl.selectTerm = function (term) {
                    $ctrl.calculatorValues.SelectedTerm = term;
                };

                $ctrl.calculateAffordability = function () {
                   // amplitude.getInstance().logEvent('log event on finance page - calc affortability');

                    if ($ctrl.validCalculator) {
                        GoogleAnalyticsService.FinanceAffordCalculatorBtn;
                        $ctrl.calculatedPrice = CalculatorService.CalculateAffordability($ctrl.calculatorValues);
                    } else {
                        //Set focus to first invalid field
                        $('.affordability-calculator').find('input.required').first().focus();
                        return false;
                    }
                };
                $ctrl.goToSearchPage = function () {
                    GoogleAnalyticsService.FinanceAffordPaymentVehicleSearchBtn;
                    $window.location.href = CalculatorService.GetPriceRangeForSearch($ctrl.calculatedPrice);
                };

                $ctrl.validateMonthlyPayment = function () {
                    var monthlyPayment = $ctrl.calculatorValues.DesiredMonthlyPayment;
                    if (!FieldValidationService.IsPositive(monthlyPayment)) {
                        $ctrl.validCalculator = false;
                        $('.affordability-calculator').find('input.required').first().focus();
                        return false;
                    }
                    $ctrl.validCalculator = true;
                    return true;
                };

                $ctrl.isValidInterest = function () {
                    var value = $ctrl.calculatorValues.InterestRate;
                    if (!FieldValidationService.IsValidInterest(value)) {
                        $ctrl.validCalculator = false;
                        $('.affordability-calculator').find('input.required').first().focus();
                        return false;
                    }

                    $ctrl.validCalculator = true;
                    return true;
                };

                $ctrl.isValidNumber = function (value) {
                    if (FieldValidationService.CheckForValidNumbers(value)) {
                        $ctrl.validCalculator = true;
                        return true;
                    } else {
                        $ctrl.validCalculator = false;
                        $('.affordability-calculator').find('input.required').first().focus();
                        return false;
                    }
                };

              


            }],
        templateUrl: UrlContent('/App/Finance/Views/AffordabilityCalculator.html')
    });
})(angular.module('AutoSMART.Web.Finance'));

"use strict";

(function (module) {

    module.component('calculatorselectorcomponent', {

        controller: [
            function () {

                var $ctrl = this;
                $ctrl.$onInit = function () {
                    $ctrl.affordText = "WHAT CAN I AFFORD?";
                    $ctrl.monthlyText = "WHAT'S MY MONTHLY PAYMENT?";
                    $ctrl.affordabilitySelected = true;
                    $ctrl.selectDropDownVisible = false;
                    $ctrl.selectedView = $ctrl.affordText;
                };
                $ctrl.chooseAffordCalculator = function () {
                    $ctrl.selectedView = $ctrl.affordText;
                    $ctrl.selectDropDownVisible = false;
                    $ctrl.affordabilitySelected = true;
                };
               
                $ctrl.chooseMonthlyCalculator = function () {
                    $ctrl.affordabilitySelected = false;
                    $ctrl.selectDropDownVisible = false;
                    $ctrl.selectedView = $ctrl.monthlyText;
                };
            }],
        templateUrl: UrlContent('/App/Finance/Views/CalculatorSelector.html')
    });
})(angular.module('AutoSMART.Web.Finance'));

"use strict";

(function (module) {

    module.component('financeinsurancecomponent', {

        controller: ["Config", "$window", "FinanceService","GoogleAnalyticsService",
            function (Config, $window, FinanceService,GoogleAnalyticsService) {

                var ctrl = this;
                var mainUrl = window.location.protocol + '//' + window.location.host + '/';
                ctrl.$onInit = function () {
                    ctrl.CarInsuranceArticles = JSON.parse(Config.CarInsuranceArticles.replace(/&quot;/g, '"'));
                    ctrl.ArticleOnClick = FinanceService.ArticleOnClick;
                    ctrl.FinanceInformationArticleClick = GoogleAnalyticsService.FinanceInformationArticleClick;
                };
            }],
        templateUrl: UrlContent('/App/Finance/Views/FinanceInsurance.html')
    });
})(angular.module('AutoSMART.Web.Finance'));
"use strict";

(function (module) {

    module.component('financeloanappcomponent', {
        
        controller: ["Config","LoggingService", "GoogleAnalyticsService", "$window",
            function (Config,LoggingService,GoogleAnalyticsService, $window) {

                var ctrl = this;

                var userActionType = 22; //ApplyForLoan
                var sourceSectionID = 8; //Finance

                ctrl.$onInit = function () {
                    ctrl.PreApprovalUrl = Config.PreApprovalUrl.replace(/&amp;/g, '&');
                    ctrl.IsLoanApplicationEnabled = Config.IsLoanApplicationEnabled;
                    ctrl.Phone = Config.Phone;
                    ctrl.PhoneExt = Config.PhoneExt; 
                    ctrl.IsPhoneDisabled = Config.IsPhoneDisabled;
                    ctrl.CreditUnionLogo = Config.ClientLogo;
                    ctrl.CreditUnionName = Config.ClientName;
                    ctrl.FinanceApplyLoanBtn = GoogleAnalyticsService.FinanceApplyLoanBtn;
                    ctrl.ApplyForLoanLogging = function () {
                        LoggingService.LoggingUserAction(userActionType, null, sourceSectionID);
                    };
                };

                ctrl.OpenLoanWindow = function () {
                    $window.open(ctrl.PreApprovalUrl, "_blank");
                };
            }],
        templateUrl: UrlContent('/App/Finance/Views/FinanceLoanApp.html')
    });
})(angular.module('AutoSMART.Web.Finance'));
"use strict";

(function (module) {

    module.component('financeratescomponent', {
        
        controller: ["Config", "FinanceService","GoogleAnalyticsService",
            function (Config, FinanceService,GoogleAnalyticsService) {

                var ctrl = this;
                ctrl.$onInit = function () {
                    ctrl.RatesAndFinanceArticles = JSON.parse(Config.RatesAndFinanceArticles.replace(/&quot;/g, '"'));
                    ctrl.ArticleOnClick = FinanceService.ArticleOnClick;
                    ctrl.FinanceInformationArticleClick = GoogleAnalyticsService.FinanceInformationArticleClick;
                };
            }],
        templateUrl: UrlContent('/App/Finance/Views/FinanceRates.html')
    });
})(angular.module('AutoSMART.Web.Finance'));
"use strict";

(function (module) {

    module.component('financesearchcomponent', {
        
        controller: ['$scope', '$window','Config','VehicleMakesService','VehicleModelService', '$filter','GoogleAnalyticsService',
            function ($scope, $window, Config, VehicleMakesService, VehicleModelService, $filter,GoogleAnalyticsService) {

                var ctrl = this;

                var specialtyVehicles = JSON.parse(Config.SpecialtyVehicleTypes.replace(/&quot;/g, '"'));
                ctrl.FindADealerMakeSelect = [{ 'name': 'Select a Make', 'value': '', 'IsSpecialtyType': false }];
                ctrl.FindAVehicleMakeSelect = [{ 'name': 'Select a Make', 'value': '', 'IsSpecialtyType': false }];
                ctrl.FindAVehicleModelSelect = [{ 'Id':'','Model': 'Select a Model' }];

                var populateMakes = function (data) {

                    angular.forEach(data, function (make) {
                        var makeObj = {
                            'name': make.Make,
                            'value': make.Make,
                            'IsSpecialtyType': false
                        };
                        ctrl.FindADealerMakeSelect.push(makeObj);
                        ctrl.FindAVehicleMakeSelect.push(makeObj);
                    });

                    angular.forEach(specialtyVehicles, function (specialtyVehicle) {
                        var makeObj = {
                            'name': specialtyVehicle.Key,
                            'value': specialtyVehicle.SearchValue,
                            'IsSpecialtyType': true
                        };
                        ctrl.FindADealerMakeSelect.push(makeObj);
                        ctrl.FindAVehicleMakeSelect.push(makeObj);
                    });

                    //add Independent Dealer Option
                    var independentDealerMakeObj = {
                        'name': 'Independent Dealers Only',
                        'value': 'Independent Dealers Only',
                        'IsSpecialtyType': false
                    };
                    ctrl.FindADealerMakeSelect.push(independentDealerMakeObj);

                    ctrl.selectedMakeInventoryObject = ctrl.FindAVehicleMakeSelect[0];
                    ctrl.selectedModelInventoryObject = ctrl.FindAVehicleModelSelect[0];
                    ctrl.selectedMakeDealerObject = ctrl.FindADealerMakeSelect[0];
                };

                ctrl.DealerSearchClicked = function () {
                    if (ctrl.selectedMakeDealerObject.IsSpecialtyType) {
                        return $window.location.href = "/Dealer?ItemType=" + encodeURIComponent(ctrl.selectedMakeDealerObject.value);
                    } 

                    return $window.location.href = (ctrl.selectedMakeDealerObject.value != '')? "/Dealer?Makes=" + encodeURIComponent(ctrl.selectedMakeDealerObject.value):"/Dealer";
                    
                }

                ctrl.InventorySearchClicked = function () {
                    if (ctrl.selectedModelInventoryObject.Id != '') {
                        return $window.location.href = "/Search?Makes=" + encodeURIComponent(ctrl.selectedMakeInventoryObject.value) + "&MakeModel=" + encodeURIComponent(ctrl.selectedMakeInventoryObject.value + "|" + ctrl.selectedModelInventoryObject.Model);
                    }
                    return $window.location.href = (ctrl.selectedMakeInventoryObject.value != '') ? "/Search?Makes=" + encodeURIComponent(ctrl.selectedMakeInventoryObject.value) : "/Search";
                }

                ctrl.$onInit = function () {
                    ctrl.FinanceVehicleSearchBtn = GoogleAnalyticsService.FinanceVehicleSearchBtn;
                    ctrl.FinanceDealerSearchBtn = GoogleAnalyticsService.FinanceDealerSearchBtn;
                    ctrl.ResearchOnClick = function () {
                        $window.open("/Research", "_self");
                    };
                    VehicleMakesService.GetInventoryMakes().then(function (data) {
                        if (data.data) {
                            data = data.data;
                        }
                        populateMakes(data);
                    });


                    ctrl.GetInventoryModels = function(make) {
                        return VehicleModelService.GetInventoryModel(make).then(function (data) {
                            if (data.data) {
                                data = data.data;
                            }
                            ctrl.selectedModelInventoryObject = { 'Id': '', 'Model': 'Select a Model' };
                            ctrl.FindAVehicleModelSelect = data;
                       });
                    };

                };

            }],
        templateUrl: UrlContent('/App/Finance/Views/FinanceSearch.html')
    });
})(angular.module('AutoSMART.Web.Finance'));
"use strict";

(function (module) {

    module.component('financetoolscomponent', {
        
        controller: ["Config","GoogleAnalyticsService",
            function (Config,GoogleAnalyticsService) {

                var ctrl = this;
                ctrl.$onInit = function() {
                    ctrl.CreditBureaus = JSON.parse(Config.CreditBureaus.replace(/&quot;/g, '"'));
                    ctrl.HasCreditBureaus = (ctrl.CreditBureaus.DisplayEquifax || ctrl.CreditBureaus.DisplayExperian || ctrl.CreditBureaus.DisplayTransUnion);
                    ctrl.ExperianUrl = Config.ExperianUrl;
                    ctrl.TransUnionUrl = Config.TransUnionUrl;
                    ctrl.EquifaxUrl = Config.EquifaxUrl;
                    ctrl.FinanceResearchToolsClick = GoogleAnalyticsService.FinanceResearchToolsClick;
                };

            }],
        templateUrl: UrlContent('/App/Finance/Views/FinanceTools.html')
    });
})(angular.module('AutoSMART.Web.Finance'));
"use strict";

(function (module) {

    module.component('monthlycalculatorcomponent', {

        controller: ["CalculatorService", "$window", "GoogleAnalyticsService", "FieldValidationService",
            function (CalculatorService, $window, GoogleAnalyticsService, FieldValidationService) {

                var $ctrl = this;
                $ctrl.calculatorValues = {
                    PurchasePrice: 0.0,
                    SelectedTerm: 0,
                    InterestRate: 0.0,
                    DownPayment: 0.0,
                    TradeInValue: 0.0,
                    AmountOwed:0.0,
                    CashRebate: 0.0
                };
                $ctrl.$onInit = function () {
                    CalculatorService.Init().then(function () {
                        $ctrl.loanTerms = CalculatorService.CalcContent.LoanTerms;
                        $ctrl.calculatorValues.SelectedTerm = CalculatorService.CalcContent.LoanDuration;
                        $ctrl.calculatorValues.InterestRate = CalculatorService.CalcContent.Rate;
                    });
                    $ctrl.calculatorValues.PurchasePrice = 20000;
                    $ctrl.calculatorValues.DownPayment = 2000;
                    $ctrl.calculatorValues.CashRebate = 2000;
                    $ctrl.calculatorValues.TradeInValue = 0;
                    $ctrl.calculatorValues.AmountOwed = 0;
                    $ctrl.validCalculator = true;
                };
                $ctrl.selectTerm = function (term) {
                    $ctrl.calculatorValues.SelectedTerm = term;
                };

                $ctrl.calculateMonthlyPayment = function () {
                    GoogleAnalyticsService.FinanceMonthlyPaymentCalculatorBtn;
                        $ctrl.calculatedPrice = CalculatorService.CalculateMonthly($ctrl.calculatorValues);
                };
                $ctrl.goToSearchPage = function () {
                    GoogleAnalyticsService.FinanceMonthlyPaymentVehicleSearchBtn;
                    $window.location.href = CalculatorService.GetPriceRangeForSearch($ctrl.calculatorValues.PurchasePrice);
                };

                $ctrl.validatePurchasePrice = function () {
                    var purchasePrice = $ctrl.calculatorValues.PurchasePrice;
                    if (!FieldValidationService.IsPositive(purchasePrice)) {
                        $ctrl.validCalculator = false;
                        $('.monthly-calculator').find('input.required').first().focus();
                        return false;
                    }
                    $ctrl.validCalculator = true;
                    return true;
                };


                $ctrl.isValidInterest = function () {
                    var value = $ctrl.calculatorValues.InterestRate;
                    if (!FieldValidationService.IsValidInterest(value)) {
                        $ctrl.validCalculator = false;
                        $('.monthly-calculator').find('input.required').first().focus();
                        return false;
                    }
                   
                    $ctrl.validCalculator = true;
                    return true;
                };


                $ctrl.isValidNumber = function (value) {
                    if (FieldValidationService.CheckForValidNumbers(value)) {
                        $ctrl.validCalculator = true;
                        return true;
                    } else {
                        $ctrl.validCalculator = false;
                        $('.monthly-calculator').find('input.required').first().focus();
                        return false;
                    }
                };

            }],
        templateUrl: UrlContent('/App/Finance/Views/MonthlyCalculator.html')
    });
})(angular.module('AutoSMART.Web.Finance'));

