var $ = require('../js/bonzo_qwery');
var reset = module.exports;

var is_small_screen = require('../js/is_small_screen');
var collapse_onfocus = require('../js/collapse_onfocus');

reset.submit = function (widget, connectionName, username, password) {
  var container = $('.a0-reset form');

  widget._setLoginView({ mode: 'loading', title: 'reset' }, function () {
    widget._auth0.changePassword({
      connection: connectionName,
      username:   username,
      password:   password
    }, function (err) {

      if ( $()[0] !== widget._node ) {
        return console.log && console.log('this password reset was triggered from another node instance', arguments);
      }

      $('.a0-password input', container).val('');
      $('.a0-repeatPassword input', container).val('');

      if (err) {
        return widget._setLoginView({ mode: 'reset' }, function () {
          widget._showError(err.status === 400 ? 
            widget._dict.t('reset:userDoesNotExistErrorText') :
            widget._dict.t('reset:serverErrorText'));
        });
      }

      $('.a0-email input', container).val('');

      widget._setLoginView({}, function () {
        widget._showSuccess(widget._dict.t('reset:successText'));
      });
    });
  });
};

reset.bind = function (widget) {
  $('.a0-reset .a0-options').show(widget._openWith ? 'none' : 'block');

  var form = $('.a0-reset form')
    .a0_off('submit')
    .a0_on('submit', function (e) {
      e.preventDefault();
      var username = $('.a0-email input', form).val();
      var password = $('.a0-password input', form).val();
      var connection  = widget._getAuth0Connection();
      reset.submit(widget, connection.name, username, password);
    });

  if (is_small_screen()) {
    collapse_onfocus.hook($('.a0-reset form input'), $('.a0-collapse-reset'));
  }

  $('.a0-repeatPassword input', form)
    .a0_off('input')
    .a0_on('input', function() {
      if ($('.a0-password input', form).val() != this.value) {
        widget._setCustomValidity(this, widget._dict.t('reset:enterSamePasswordText'));
      } else {
        widget._setCustomValidity(this, '');
      }
    });
};
@@ -173,16 +173,116 @@
 focus');
+%0Avar regex = require('../js/regex');%0Avar email_parser = regex.email_parser;%0Avar empty = regex.empty;
 %0A%0Areset.
@@ -1016,17 +1016,16 @@
 == 400 ?
- 
 %0A       
@@ -1732,16 +1732,57 @@
 ction();
+%0A%0A      if (!valid(form, widget)) return;
 %0A      r
@@ -2274,14 +2274,983 @@
   %7D%0A    %7D);%0A
-
 %7D;
+%0A%0Afunction valid(form, widget) %7B%0A  var ok = true;%0A  var email_input = $('input%5Bname=email%5D', form);%0A  var email_empty = empty.test(email_input.val());%0A  var email_parsed = email_parser.exec(email_input.val().toLowerCase());%0A  var password_input = $('input%5Bname=password%5D', form);%0A  var password_empty = empty.test(password_input.val());%0A  var repeat_password_input = $('input%5Bname=repeat_password%5D', form);%0A%0A  // asume valid by default%0A  // and reset errors%0A  widget._showError();%0A  widget._focusError();%0A%0A  if (email_empty) %7B%0A    widget._focusError(email_input);%0A    ok = false;%0A  %7D%0A%0A  if (!email_parsed && !email_empty) %7B%0A    widget._focusError(email_input, widget._dict.t('invalid'));%0A    ok = false;%0A  %7D%0A%0A  if (password_empty) %7B%0A    widget._focusError(password_input);%0A    ok = false;%0A  %7D;%0A%0A  if (repeat_password_input.val() !== password_input.val()) %7B%0A    widget._focusError(repeat_password_input, widget._dict.t('mustMatch'));%0A    ok = false;%0A  %7D;%0A%0A  return ok;%0A%7D

Add basic reset form validations