var showLoadingImage = require('../../../../base/utility').showLoadingImage;
var RiskClassification = require('../../../../common_functions/risk_classification').RiskClassification;
var japanese_client = require('../../../../common_functions/country_base').japanese_client;

var FinancialAssessmentws = (function(){
   "use strict";

    var init = function(){
        if (checkIsVirtual()) return;
        LocalizeText();
        $("#assessment_form").on("submit",function(event) {
            event.preventDefault();
            submitForm();
            return false;
        });
        BinarySocket.send({get_financial_assessment : 1});
    };

    // For translating strings
    var LocalizeText = function(){
        $("#heading").text(page.text.localize($("#heading").text()));
        $('#heading_risk').text(page.text.localize($("#heading_risk").text()));
        $('#high_risk_classification').text(page.text.localize($('#high_risk_classification').text()));
        document.getElementsByTagName('legend')[0].innerHTML = page.text.localize(document.getElementsByTagName('legend')[0].innerHTML);
        if (document.getElementsByTagName('legend')[1]) document.getElementsByTagName('legend')[1].innerHTML = page.text.localize(document.getElementsByTagName('legend')[1].innerHTML);
        $("#assessment_form label").each(function(){
            var ele = $(this);
            ele.text(page.text.localize(ele.text()));
        });
        $("#assessment_form option").each(function(){
            var ele = $(this);
            ele.text(page.text.localize(ele.text()));
        });
        $("#warning").text(page.text.localize($("#warning").text()));
        $("#submit").text(page.text.localize($("#submit").text()));
    };

    var submitForm = function(){
        if(!validateForm()){
            return;
        }
        $('#submit').attr('disabled', 'disabled');
        var data = {'set_financial_assessment' : 1};
        showLoadingImage($('#form_message'));
        $('#assessment_form select').each(function(){
            data[$(this).attr("id")] = $(this).val();
        });
        $('html, body').animate({ scrollTop: 0 }, 'fast');
        BinarySocket.send(data);
        RiskClassification.cleanup();
    };

    var validateForm = function(){
        var isValid = true,
            errors = {};
        clearErrors();
        $('#assessment_form select').each(function(){
            if(!$(this).val()){
                isValid = false;
                errors[$(this).attr("id")] = page.text.localize('Please select a value');
            }
        });
        if(!isValid){
            displayErrors(errors);
        }

        return isValid;
    };

    var showLoadingImg = function(){
        showLoadingImage($('<div/>', {id: 'loading', class: 'center-text'}).insertAfter('#heading'));
        $("#assessment_form").addClass('invisible');
    };

    var hideLoadingImg = function(show_form){
        $("#loading").remove();
        if(typeof show_form === 'undefined'){
            show_form = true;
        }
        if(show_form)
            $("#assessment_form").removeClass('invisible');
    };

    var responseGetAssessment = function(response){
        hideLoadingImg();
        for(var key in response.get_financial_assessment){
            if(key){
                var val = response.get_financial_assessment[key];
                $("#"+key).val(val);
            }
        }
    };

    var clearErrors = function() {
        $('.errorfield').each(function() { $(this).text(''); });
    };

    var displayErrors = function(errors){
        var id;
        clearErrors();
        for(var key in errors){
            if(key){
                var error = errors[key];
                $("#error"+key).text(page.text.localize(error));
                if (!id) id = key;
            }
        }
        hideLoadingImg();
        $('html, body').animate({
            scrollTop: $("#"+id).offset().top
        }, 'fast');
    };

    var apiResponse = function(response){
        if (checkIsVirtual()) return;
        if (response.msg_type === 'get_financial_assessment') {
            responseGetAssessment(response);
        } else if (response.msg_type === 'set_financial_assessment') {
            $('#submit').removeAttr('disabled');
            if ('error' in response) {
                showFormMessage('Sorry, an error occurred while processing your request.', false);
                displayErrors(response.error.details);
            } else {
                showFormMessage('Your settings have been updated successfully.', true);
            }
        }
    };

    var checkIsVirtual = function(){
        if(page.client.is_virtual()) {
            $("#assessment_form").addClass('invisible');
            $('#response_on_success').addClass('notice-msg center-text').removeClass('invisible').text(page.text.localize('This feature is not relevant to virtual-money accounts.'));
            hideLoadingImg(false);
            return true;
        }
        return false;
    };

    var showFormMessage = function(msg, isSuccess) {
        $('#form_message')
            .attr('class', isSuccess ? 'success-msg' : 'errorfield')
            .html(isSuccess ? '<ul class="checked" style="display: inline-block;"><li>' + page.text.localize(msg) + '</li></ul>' : page.text.localize(msg))
            .css('display', 'block')
            .delay(5000)
            .fadeOut(1000);
    };

    var onLoad = function() {
        if (japanese_client()) {
            window.location.href = page.url.url_for('user/settingsws');
        }
        BinarySocket.init({
            onmessage: function(msg) {
                var response = JSON.parse(msg.data);
                if (response) {
                    FinancialAssessmentws.apiResponse(response);
                }
            }
        });
        showLoadingImage($('<div/>', {id: 'loading', class: 'center-text'}).insertAfter('#heading'));
        FinancialAssessmentws.init();
    };

    return {
        init : init,
        apiResponse : apiResponse,
        submitForm: submitForm,
        LocalizeText: LocalizeText,
        onLoad: onLoad,
    };
}());

module.exports = {
    FinancialAssessmentws: FinancialAssessmentws,
};

@@ -2098,67 +2098,8 @@
 %7D);%0A
-        $('html, body').animate(%7B scrollTop: 0 %7D, 'fast');%0A
     
@@ -3812,96 +3812,34 @@
    $
-('html, body').animate(%7B%0A            scrollTop: $(%22#%22+id).offset().top%0A        %7D, 'fast'
+.scrollTo($('#' + id), 500
 );%0A 

Fix scroll