/*jslint browser:true*/
/*global document,appRoot,angular,$ */

$(document).off('.alert.data-api');
(function () {
    "use strict";
    appRoot.controller("GetErrMsg", ["$scope", "$window", "UtilService", function ($scope, $window, utilService) {
        var uiElement = angular.element(document.querySelector('#errMsgCnt'));
        var errmodal = angular.element(document.querySelector('#errMsg'));
        var PresumptiveModel = angular.element(document.querySelector('#PresumptivePopUp'));
        var presumptiveValue = angular.element(document.querySelector('#hdnPresumptive'));
        var userSavedApplicationsValue = angular.element(document.querySelector('#hideusersavedapplication'));

        //Pop Up for business message partial view in registration module Starts
        var RegistrationPopUp = angular.element(document.querySelector('#RegistrationPopUp'));
        var MCARegistrationMessageCount = angular.element(document.querySelector('#MCARegistrationHasMessageCount'));
        if (!utilService.isUndefinedOrNull(RegistrationPopUp) && !utilService.isUndefinedOrNull(RegistrationPopUp.val()) && !utilService.isUndefinedOrNull(MCARegistrationMessageCount.val())) {
            $scope.$watch(RegistrationPopUp.val(), function () {
                if (parseInt(MCARegistrationHasMessageCount.value) > 0) {
                    RegistrationPopUp.modal({ show: true, backdrop: 'static' });
                    $(".modal-open").removeClass("modal-open");
                    uiElement.val("");
                }
            });
        }
        //Pop Up for business message partial view in registration module Ends

        if (!utilService.isUndefinedOrNull(uiElement) && !utilService.isUndefinedOrNull(uiElement.val())) {
            $scope.$watch(uiElement.val(), function () {
                if (!utilService.isUndefinedOrNull(uiElement.val()) && uiElement.val() != "") {
                    errmodal.modal({ show: true, backdrop: 'static' });
                    $(".modal-open").removeClass("modal-open");
                }
            });
        }

        if (!utilService.isUndefinedOrNull(presumptiveValue)) {
            if (!utilService.isUndefinedOrNull(presumptiveValue) && !utilService.isUndefinedOrNull(presumptiveValue.val())) {

                $scope.$watch(presumptiveValue.val(), function () {
                    if (presumptiveValue.val() == "True") {
                        PresumptiveModel.modal({ show: true, backdrop: true });
                    }
                });
            }
        }

        if (!utilService.isUndefinedOrNull(userSavedApplicationsValue) && !utilService.isUndefinedOrNull(userSavedApplicationsValue.val())) {
            $scope.$watch(userSavedApplicationsValue.val(), function () {
                if (userSavedApplicationsValue.val() === "True") {
                    PresumptiveModel.modal({ show: true, backdrop: 'static', keyboard: false });
                }
            });
        }

        $scope.Ok = function (e, lang, userid) {
            if ($("#ReturnCode").val() == "8") {
                $("#SubmitButton_Submit").click();
            } else if ($("#ReturnValue").val() == "8") {
                $("#SubmitButton_Save").click();
            }
            else {

                var language = "ES";
                var errUrl = "";
                if (lang == "en") {
                    language = "EN";
                }

                if (e == "AccLock") {
                    errUrl = "../../OCOA/PGM/EN/CAMSG.aspx?error=80151&loginError=true&language=EN";
                }
                else if (e == "AccExp") {
                    errUrl = "../../OCOA/PGM/EN/CARED.aspx?action=forgotpass&language=EN";
                }
                else if (e == "Legacy") {
                    errUrl = "../../MenuItems/FlexSetHintQuestionPage.aspx";
                }
                else if (e == "Unauth") {
                    errUrl = "../../OCOA/pgm/EN/CAPRD.aspx?action=login&language=EN";
                }
                if (errUrl != "") {
                    $window.open(errUrl);
                }
                uiElement.val("");
            }
        };
    }]);
}());;
