'use strict';


/**
 *  client
 **/

/**
 *  client.common
 **/

/**
 *  client.common.auth
 **/

/**
 *  client.common.auth.register
 **/


/*global nodeca*/

var DELAY = 500;

var start_point;

/**
 *  client.common.auth.register.check_nick($elem, event)
 *
 *  send nick value on server
 *  and show error if nick exists
 **/
module.exports = function ($elem, event) {
  // update start point each time
  start_point = new Date();

  // delay request
  setTimeout(function() {

    // time is not come
    // new event(s) update start point
    if (DELAY > new Date() - start_point) {
      return;
    }

    // reset time
    start_point = null;

    var nick = $elem.val();
    nodeca.server.users.auth.register.check_nick({ nick: nick }, function(err){
      var $control_group = $elem.parents('.control-group:first');

      if (err) {
        // Problems with nick
        if (err.statusCode === nodeca.io.BAD_REQUEST) {
          $control_group.addClass('error');

          $control_group.find('.help-block').text(
            err.message['nick']
            );
          return;
        }

        // something fatal
        nodeca.client.common.notify('error', err.message);
        return;
      }

      // no errors -> restore defaults

      $control_group.removeClass('error');
      $control_group.find('.help-block').text(
        nodeca.runtime.t('users.auth.reg_form.nick_help')
      );

    });
  }, DELAY);
  return false;
};

@@ -175,26 +175,22 @@
 0;%0A%0Avar 
-start_poin
+timeou
 t;%0A%0A/**%0A
@@ -370,282 +370,127 @@
  // 
-update start point each time%0A  start_point = new Date();%0A%0A  // delay request%0A  setTimeout(function() %7B%0A%0A    // time is not come%0A    // new event(s) update start point%0A    if (DELAY %3E new Date() - start_point) %7B%0A      return;%0A    %7D%0A%0A    // reset time%0A    start_point = null;%0A
+make sure previous timeout was cleared%0A  clearTimeout(timeout);%0A%0A  // delay request%0A  timeout = setTimeout(function() %7B
 %0A   

Simplify nick checking