﻿(function($) {

    eligibilityRequestConfirmation = function(selector, options) {
        this.content = null;
        this.popup = null;
        this.options = $.extend({}, this.defaults, options);
        this.init();
    };

    eligibilityRequestConfirmation.prototype = {
        init: function() {
            this.showRequestConfirmationPopup();
        },

        showRequestConfirmationPopup: function() {
            if (!this.content) {
                this.createContent();
            }
            this.initializeComponent();
            this.popup = this.createRequestConfirmatioPopup();
        },

        initializeComponent: function() {
            $('#NoBtn', this.content).click(Function.createDelegate(this, this.noStepHandler));
            $('#yesBtn', this.content).click(Function.createDelegate(this, this.yesBtnHandler));
        },

        noStepHandler: function() {
            this.popup.hide();
        },


        yesBtnHandler: function() {
            __doPostBack(this.options.SubmitControlID, '');
            this.popup.hide();

        },

        createContent: function() {
            this.content = $.create('div', {});
            this.content.setTemplateURL(this.options.templateUrl, null, { filter_data: false });
            this.content.processTemplate(null);
        },

        createRequestConfirmatioPopup: function() {
            return $('form').messageBox(null, {
                messageType: '',
                contentClassName: '',
                title: 'Warning',
                width: 500,
                top: 150,
                modal: true,
                autoClose: false,
                innerRound: 0
            }, this.content);
        },

        defaults: {
            serviceUrl: '',
            clearDataRequest: 'ClearData'
        }
    };

    $.fn.eligibilityRequestConfirmation = function(options) {
        $(this).each(function() {
            new eligibilityRequestConfirmation(this, options);
        });
    };
})(jQuery);

(function($) {

    survey = function(selector, options) {
        this.content = null;
        this.popup = null;
        this.options = $.extend({}, this.defaults, options);
        this.init();
    };

    survey.prototype = {
        init: function() {
            this.showRequestConfirmationPopup();
        },

        showRequestConfirmationPopup: function() {
            if (!this.content) {
                this.createContent();
            }
            this.initializeComponent();
            this.popup = this.createRequestConfirmatioPopup();
        },

        initializeComponent: function() {
            $('#saveBtn', this.content).click(Function.createDelegate(this, this.saveSurvey));
            $('#cancelBtn', this.content).click(Function.createDelegate(this, this.cancelSurvey));
        },

        saveSurvey: function() {
            var rate = $('#rate', this.content).val();
            if (rate == 0) {
                alert('Please rate from 1 to 10');
                return;
            }
            var comment = $('#comments', this.content).val();
            params = {
                mark: rate,
                comment: comment
            };
            jQuery.ajax({
                type: 'POST',
                url: this.options.serviceUrl,
                data: params,
                dataFilter: $.msDataFilter,
                success: Function.createDelegate(this, this.updateSurveyRequestCompleteHandler),
                error: Function.createDelegate(this, this.updateSurveyRequestErrorHandler)
            });
            this.popup.hide();
        },


        updateSurveyRequestCompleteHandler: function(data, result) {
            if (data.IsSuccess) {
                //add action
            } else {
                alert('Some errors was occured. Please try again later.');
            }
        },

        updateSurveyRequestErrorHandler: function(response, result) {
            $.log('WARN', response.responseText);
            alert('Some errors was occured. Please try again later.');
        },

        cancelSurvey: function() {
            this.popup.hide();
        },



        createContent: function() {
            this.content = $.create('div', {});
            this.content.setTemplateURL(this.options.templateUrl, null, { filter_data: false });
            this.content.processTemplate(null);
        },

        createRequestConfirmatioPopup: function() {
            return $('form').messageBox(null, {
                messageType: '',
                contentClassName: '',
                title: 'Survey',
                width: 500,
                top: 150,
                modal: true,
                autoClose: false,
                innerRound: 0
            }, this.content);
        },

        defaults: {
            serviceUrl: '',
            clearDataRequest: 'ClearData'
        }
    };

    $.fn.survey = function(options) {
        $(this).each(function() {
            new survey(this, options);
        });
    };
})(jQuery);
