/* Minification failed. Returning unminified contents.
(1,10): run-time error CSS1031: Expected selector, found 'AdviserUserGroupVM('
(1,10): run-time error CSS1025: Expected comma or open brace, found 'AdviserUserGroupVM('
(34,10): run-time error CSS1031: Expected selector, found 'BenefitOptionsVM('
(34,10): run-time error CSS1025: Expected comma or open brace, found 'BenefitOptionsVM('
(71,10): run-time error CSS1031: Expected selector, found 'CommissionBasisDetailsVM('
(71,10): run-time error CSS1025: Expected comma or open brace, found 'CommissionBasisDetailsVM('
(99,10): run-time error CSS1031: Expected selector, found 'ContactDetailsVM('
(99,10): run-time error CSS1025: Expected comma or open brace, found 'ContactDetailsVM('
(115,10): run-time error CSS1031: Expected selector, found 'EAppDefaultsVM('
(115,10): run-time error CSS1025: Expected comma or open brace, found 'EAppDefaultsVM('
(144,10): run-time error CSS1031: Expected selector, found 'GeneralPreferencesVM('
(144,10): run-time error CSS1025: Expected comma or open brace, found 'GeneralPreferencesVM('
(171,10): run-time error CSS1031: Expected selector, found 'MyDetailsVM('
(171,10): run-time error CSS1025: Expected comma or open brace, found 'MyDetailsVM('
(263,10): run-time error CSS1031: Expected selector, found 'ReportOptionsVM('
(263,10): run-time error CSS1025: Expected comma or open brace, found 'ReportOptionsVM('
(307,23): run-time error CSS1031: Expected selector, found '='
(307,23): run-time error CSS1025: Expected comma or open brace, found '='
(399,3): run-time error CSS1019: Unexpected token, found '('
(399,4): run-time error CSS1019: Unexpected token, found ')'
 */
function AdviserUserGroupVM(myDetails, data) {
    var self = this;

    ko.mapping.fromJS(data, {}, self);

    self.myDetails = myDetails;

    self.isBaseCommssionFactorSelectable = function () {
     return self.CommissionRules.BaseCommissionFactorIsSelectable();
    }

    self.isAdviserRiskCommissionSelectable = function () {
        return self.CommissionRules.AdviserRiskCommissionFactorIsSelectable();
    }

    self.isAdviserFixedSALoadingCommissionSelectable = function () {
        return self.CommissionRules.AdviserFixedSALoadingCommissionFactorIsSelectable();
    }

    self.isAdviserRenewalCommissionSelectable = function () {
        return self.CommissionRules.AdviserRenewalCommissionFactorIsSelectable();
    }

    self.isAdviserUpFrontRiskYRTCommissionSelectable = function () {
        return self.CommissionRules.AdviserUpFrontRiskYRTIsSelectable();
    }

    self.isAdviserUpFrontRiskLevelCommissionSelectable = function () {
        return self.CommissionRules.AdviserUpFrontRiskLevelIsSelectable();
    }


}
function BenefitOptionsVM(myDetails, data) {
    var self = this;


    ko.mapping.fromJS(data, {}, self);

    self.myDetails = myDetails;

    self.ID = ko.computed({
        read: function () {
            return self.myDetails.ID();
        },
        deferEvaluation: true
    });

    self.dirtyFlag = new ko.dirtyFlag(self, 'BenefitOptionsVM');

    self.clearDirtyFlag = function () {
        self.dirtyFlag.clear();
    };

    self.tabID = 4;

    self.isSelected = function () {
        return self.myDetails.selectedTabID() == self.tabID;

    }

    self.selectedWaitingPeriod = ko.computed({
        read: function () {
            return self.DefaultWaitPeriodWeeks() + ' weeks';
        },
        write: function (value) {
            self.DefaultWaitPeriodWeeks(parseInt(value.replace(' weeks', '')));
        }
    });
}
function CommissionBasisDetailsVM(myDetails, data) {
   
    var self = this;
    ko.mapping.fromJS(data, {}, self);

    self.myDetails = myDetails;

    self.ID = ko.computed({
        read: function () {
            return self.myDetails.ID();
        },
        deferEvaluation: true
    });

    self.dirtyFlag = new ko.dirtyFlag(self, 'CommissionBasisDetailsVM');

    self.clearDirtyFlag = function () {
        self.dirtyFlag.clear();
    };

    self.tabID = 2;

    self.isSelected = function () {
        return self.myDetails.selectedTabID() == self.tabID;

    }
  
}
function ContactDetailsVM(myDetails, data) {
    var self = this;

   
    ko.mapping.fromJS(data, {}, self);

    self.myDetails = myDetails;

    self.tabID = 1;


    self.isSelected = function () {
        return self.myDetails.selectedTabID() == self.tabID;

    }
}
function EAppDefaultsVM(myDetails, data) {
    var self = this;


    ko.mapping.fromJS(data, {}, self);

    self.myDetails = myDetails;

    self.ID = ko.computed({
        read: function () {
            return self.myDetails.ID();
        },
        deferEvaluation: true
    });

    self.dirtyFlag = new ko.dirtyFlag(self, 'EAppDefaultsVM');

    self.clearDirtyFlag = function () {
        self.dirtyFlag.clear();
    };

    self.tabID = 6;

    self.isSelected = function () {
        return self.myDetails.selectedTabID() == self.tabID;

    }

}
function GeneralPreferencesVM(myDetails, data) {
    var self = this;

    ko.mapping.fromJS(data, {}, self);
    self.myDetails = myDetails;

    self.ID = ko.computed({
        read: function () {
            return self.myDetails.ID();
        },
        deferEvaluation: true
    });

    self.dirtyFlag = new ko.dirtyFlag(self, 'GeneralPreferencesVM');

    self.clearDirtyFlag = function () {
        self.dirtyFlag.clear();
    };

    self.tabID = 5;

    self.isSelected = function () {
        return self.myDetails.selectedTabID() == self.tabID;

    }
   
}
function MyDetailsVM(data) {
    var self = this;

    var mapping = {
        'ContactDetails': {
            create: function (options) {
                return new ContactDetailsVM(self, options.data);
            }
        },
        'CommissionBasisDetails': {
            create: function (options) {
                return new CommissionBasisDetailsVM(self, options.data);
            }
        },
        'ReportOptions': {
            create: function (options) {
                return new ReportOptionsVM(self, options.data);
            }
        },
        'BenefitOptions': {
            create: function (options) {
                return new BenefitOptionsVM(self, options.data);
            }
        },
        'GeneralPreferences': {
            create: function (options) {
                return new GeneralPreferencesVM(self, options.data);
            }
        },
        'EAppDefaults': {
            create: function (options) {
                return new EAppDefaultsVM(self, options.data);
            }
        },
        'UserGroup': {
            create: function (options) {
                return new AdviserUserGroupVM(self, options.data);
            }
        }



    }
    ko.mapping.fromJS(data, mapping, self);

    self.dirtyFlag = new ko.dirtyFlag(self, 'MyDetailsVM');

    self.clearDirtyFlag = function () {
        self.dirtyFlag.clear();
        self.CommissionBasisDetails.dirtyFlag.clear();
        self.ReportOptions.dirtyFlag.clear();
        self.BenefitOptions.dirtyFlag.clear();
        self.GeneralPreferences.dirtyFlag.clear();
        self.EAppDefaults.dirtyFlag.clear();
    };
		

    //tabs 
    var tabData = [
               new tabItem("Contact Details", 1, true, true),
               new tabItem("Commission", 2, false, self.UserGroup.CanSetCommissionPreferences()),
               new tabItem("Report Options", 3, false, self.UserGroup.CanSetReportOptions()),
               new tabItem("Benefit Options", 4, false, self.UserGroup.CanSetBenefitOptions()),
               new tabItem("Preferences", 5, false, self.UserGroup.CanSetPreferences()),
               new tabItem("e-App Defaults",6,false, self.UserGroup.CanSetEappDefaults()) 
            ];


    function tabItem(displayName, ID, selected, visible) {
        return {
            DisplayName: ko.observable(displayName),
            ID: ko.observable(ID),
            Selected: ko.observable(selected),
            IsVisible: visible  
        };
    }

    self.tabs = ko.observableArray(tabData);
    self.selectedTabID = ko.observable(1);


    self.selectTab = function () {
        ko.utils.arrayForEach(self.tabs(), function (item) {
            item.Selected(false);
        });
        self.selectedTabID(this.ID());
        this.Selected(true);

    }

   
}
function ReportOptionsVM(myDetails, data) {
    var self = this;


    ko.mapping.fromJS(data, {}, self);

    self.myDetails = myDetails;

    self.ID = ko.computed({
        read: function () {
            return self.myDetails.ID();
        },
        deferEvaluation: true
    });

    self.dirtyFlag = new ko.dirtyFlag(self, 'ReportOptionsVM');

    self.clearDirtyFlag = function () {
        self.dirtyFlag.clear();
    };

    self.tabID = 3;

    self.isSelected = function () {
        return self.myDetails.selectedTabID() == self.tabID;
    }

    self.canSetCommissionPreferences = function () {
        return self.myDetails.UserGroup.CanSetCommissionPreferences();
    }

    self.setGraphsOption = function () {
        if (!self.PrintIllustrationProjections()) {
            self.PrintIllustrationGraphs(false);
        }
    }

    self.setPermanentNotesOption = function () {
        if (!self.PrintIllustrationCoverPage()) {
            self.HasStandardIllustrationNotes(false);
        }
    }
}

var myDetailsUIModule = function () {


    // PRIVATE MEMBERS

    var myDetails = null,
    urls,
    loaded = ko.observable(false),
    updatingBindings = false;

    // Adds a subscription to a published dirty view model. Defines an observable which will
    // hold a JSON representation of the dirty view model at the relevant level of the mydetails
    // graph, and subscribes to the observable to save the JSON to the server
    var createDirtySubscription = function (topic, updateBaseUrl) {
        var url = updateBaseUrl;
        ko.observable().subscribeTo(topic).subscribe(function (json) {
            // Do not post to server again as a result of an update to the mydetails returned by a previous post
            if (!updatingBindings) {
                postAndUpdate(url, json);
            }
        });
    };

    var init = function (ajaxUrls, myDetailsData) {
        urls = ajaxUrls;
        myDetails = new MyDetailsVM(myDetailsData);

        ko.applyBindings(myDetails, $('#content').get(0));

        loaded(true);

        // Subscribe to dirty view models to update to server
        createDirtySubscription('DirtyCommissionBasisDetailsVM', urls.updateCommissionBasisDetails);
        createDirtySubscription('DirtyReportOptionsVM', urls.updateReportOptions);
        createDirtySubscription('DirtyBenefitOptionsVM', urls.updateBenefitOptions);
        createDirtySubscription('DirtyGeneralPreferencesVM', urls.updateGeneralPreferences);
        createDirtySubscription('DirtyEAppDefaultsVM', urls.updateEAppDefaults);

        // Set view model graph's dirty state as clean initially
        myDetails.clearDirtyFlag();


    };

 
    var postAndUpdate = function (url, json, successCallback) {
        ajaxModule.postJson(url, json, function (result) {
            // Check if an entire updated policy graph is returned
            if (result.Data) {
                // Map the returned result onto the illustration view model
                updateMyDetailsViewModel(result.Data);

                // If any domain validation messages are returned, populate them into the AjaxModule's
                // validation list which will cause the validation dialog to be opened

                if (result.Data.ValidationMessages.length > 0) {
                    ajaxModule.validationMessages(result.Data.ValidationMessages);
                    ajaxModule.showValidationMessages(true);
                }
            }

            if (successCallback) {
                successCallback(result);
            }
            myDetails.clearDirtyFlag();
        });
    };

    //
    var updateMyDetailsViewModel = function (data) {
        try {
            updatingBindings = true;
            ko.mapping.fromJS(data, myDetails);
            updatingBindings = false;
        }
        catch (ex) {
            ajaxModule.processException(ex);
        }
    };




    // PUBLIC MEMBERS

    return {
        init: init,
        loaded: loaded
    };



} ();

  

