$(document).ready(function () {
  var validateUsername = $('#validateUsername');
  $('#username').keyup(function () {
    var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      validateUsername.removeClass('error').html('<img src="http://img.cwars.ru/images/ajax-loader.gif" height="16" width="16" /> Идёт проверка...');
      
      this.timer = setTimeout(function () {
        $.ajax({
          url: '/ajax/formvalidation.php',
          data: 'action=check_forgot&username=' + t.value,
          dataType: 'json',
          type: 'post',
          success: function (j) {
            validateUsername.html(j.msg);
          }
        });
      }, 500);
      
      this.lastValue = this.value;
    }
  });
  var validateEmail = $('#validateEmail');
  $('#email').keyup(function () {
    var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      validateEmail.removeClass('error').html('Идёт проверка...');
       this.timer = setTimeout(function () {
        $.ajax({
          url: '/ajax/formvalidation.php',
          data: 'action=check_email&email=' + t.value,
          dataType: 'json',
          type: 'post',
          success: function (j) {
            validateEmail.html(j.msg);
          }
        });
      }, 500);
      this.lastValue = this.value;
    }
  });
  var validateBirthDate = $('#validateBirthDate');
  $('#birth_day').change(function () {
    var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      validateBirthDate.removeClass('error').html('Идёт проверка...');
       this.timer = setTimeout(function () {
        $.ajax({
          url: '/ajax/formvalidation.php',
          data: 'action=check_birthdate&day=' + t.value + '&month=' + $('#birth_month').attr('value') + '&year=' + $('#birth_year').attr('value'),
          dataType: 'json',
          type: 'post',
          success: function (j) {
            validateBirthDate.html(j.msg);
          }
        });
      }, 500);
      this.lastValue = this.value;
    }
  });
  $('#birth_month').change(function () {
    var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      validateBirthDate.removeClass('error').html('Идёт проверка...');
       this.timer = setTimeout(function () {
        $.ajax({
          url: '/ajax/formvalidation.php',
          data: 'action=check_birthdate&day=' + $('#birth_day').attr('value') + '&month=' + t.value + '&year=' + $('#birth_year').attr('value'),
          dataType: 'json',
          type: 'post',
          success: function (j) {
            validateBirthDate.html(j.msg);
          }
        });
      }, 500);
      this.lastValue = this.value;
    }
  });
  $('#birth_year').change(function () {
    var t = this; 
    if (this.value != this.lastValue) {
      if (this.timer) clearTimeout(this.timer);
      validateBirthDate.removeClass('error').html('Идёт проверка...');
       this.timer = setTimeout(function () {
        $.ajax({
          url: '/ajax/formvalidation.php',
          data: 'action=check_birthdate&day=' + $('#birth_day').attr('value') + '&month=' + $('#birth_month').attr('value') + '&year=' + t.value,
          dataType: 'json',
          type: 'post',
          success: function (j) {
            validateBirthDate.html(j.msg);
          }
        });
      }, 500);
      this.lastValue = this.value;
    }
  });
});
