var ChangePass = {
    dir_cgi_cpan: '',
    init: function() {
        ChangePass.dir_cgi_cpan = Admin.dir_cgi_cpn;
        $('.button-back').off('click').on('click', function() {
            history.back();
            return false;
        });
        $('.button-send').off('click').on('click', function() {
            ChangePass.enviar();
            return false;
        });
        $('#_new_psw,#_new_psw_confirm').off('blur').on('blur', function () {
            ComunesPass.validaPwd(ChangePass);
        });

        if (typeof Captcha !== 'undefined') {
            var prop = {type:'cpan'};

            Captcha.generate(prop);

            $('#FrmRecordar').bind('submit', function() {
                    Captcha.setRefreshCaptcha();
            });
        }
    },

    // -------------------------------------------------------------------------
    enviar: function() {
        if (!ComunesPass.validaPwd(ChangePass)) {
            return false;
        }

        var configAjax = {
            formSelector: '#FrmChangePass',
            actionURL: '/' + Admin.dir_cgi_cpn + '/prontus_changepass.cgi'
        };
        var optsAjax = {
            success:   function(json, statusText) {
                if(json === null) {
                    return;
                }
                if (json.status == '1') {
                    if (typeof json.msg != 'undefined' && json.msg != '') {
                        ChangePass.mostrarAlerta(unescape(json.msg), true);
                    }
                    window.location.href = '/' + Admin.dir_cgi_cpn + '/prontus_index.cgi?_prontus_id=' + $("[name='_prontus_id']").val();
                } else  {
                    ChangePass.mostrarAlerta(unescape(json.msg));
                    $('.loading-action').hide();
                    $('.box-new-pwd').show();
                }
            }, // success
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                SubmitForm.handleError(configAjax.actionURL, XMLHttpRequest, textStatus, errorThrown);
            }
        };

        $('#box-alert').hide();
        $('.loading-action').show()
        $('.box-new-pwd').hide();
        SubmitForm.submitGenericAjax(configAjax, optsAjax);
    },
    mostrarAlerta: function(msg, success) {
        if (success) {
            $('#box-alert').removeClass('alert-danger').addClass('alert-success');
        }
        $('#box-alert span.msg').html(msg);
        $('#box-alert span.sr-only').html(msg);
        $('#box-alert').show();
    }
};

$(document).ready(function() {
    ChangePass.init();
});
