<script>
$.sub('setpassword', function () {
    // set password
    $('#setRouterPwd').on('submit', function (e) {
        e.preventDefault();
        var requestURL = '<%=luci.dispatcher.build_url("api", "xqsystem","set_name_password")%>',
            requestData = {},
            formObj = this,
            formName = formObj.name,
            oldPwd,
            _oldPwd,
            _newPwd,
            newPwd,
            nonce,
            validator = Valid.checkAll(formObj);
        if (validator) {
            _oldPwd = $('input[name=oldPwd]').val();
            _newPwd = $('input[name=newPwd]').val();
            if ( _oldPwd == _newPwd ) {
                Valid.fail(formObj.elements['newPwd'], '<%:新密码与原密码相同%>', true);
                return;
            }
            nonce = Encrypt.init();
            oldPwd = Encrypt.oldPwd(_oldPwd);
            newPwd = Encrypt.newPwd(_oldPwd, _newPwd);

            requestData = {
                nonce: nonce,
                newPwd: newPwd,
                oldPwd: oldPwd
            };

            if (_oldPwd === _newPwd) {
                $.alert('<%:你什么都没改变，提交什么啊%>');
                return;
            }
            $.pub('wait', {
                id: '#btnSetRouterPwd'
            });
            $.ajax({
                url: requestURL,
                dataType: 'json',
                timeout: 5000, //3 second timeout
                type: "POST",
                data: requestData,
                success: function (rsp) {
                    if (rsp.code == 0) {
                        $.alert('<%:设置成功%>').time(1.5 * 1000);
                        setTimeout('location.reload( true )', 2000);
                    } else {
                        if (rsp.code == 1552) {
                            Valid.fail(formObj.elements['oldPwd'], rsp.msg, true);
                        } else {
                            $.alert(rsp.msg);
                        }
                    }
                    $.pub('done', {
                        id: '#btnSetRouterPwd'
                    });
                }
            });
        }
    });
});

$(function(){
    $.formInit();
    $.pub('setpassword');
});
</script>