var newClass = function() {
	return function() {
		return this.init.apply(this, arguments);
	}
}
var Validate = newClass();

Validate.prototype = {
  init: function() {
    var t = this;
    t.name = $("#name");
    t.last_name = $("#last_name");
    t.email = $("#email");
    t.city = $("#city");
    t.country = $("#country");

    t.login = $("#login");
    t.password = $("#password");
    t.confirm_password = $("#confirm_password");

    t.captcha = $("#captcha_word");

    //t.captcha.keyup(function(){t.checkField(this);});
     $("#registr_page").find("input").keyup(function(){t.checkField(this)});
     $("#registr_page").find("input").blur(function(){t.checkField(this)});
     //$("#registr_page").find("input").focus(function(){t.removeError(this)});

     t.name_err = true;
     t.last_name_err = true;
     t.email_err = true;
     t.login_err = true;
     t.password_err = true;
     t.confirm_password_err = true;
     t.captcha_err = true;


  },

  removeError: function(obj) {
    $(obj).parents("tr").next("tr").find("td.Error").parents("tr").remove();
    switch($(obj).attr("id"))
    {
        case "name":
            t.name_err = false;
            break;
        case "last_name":
            t.last_name_err = false;
            break;
        case "email":
            t.email_err = false;
            break;
        case "login":
            t.login_err = false;
            break;
        case "password":
            t.password_err = false;
            break;
        case "confirm_password":
            t.confirm_password_err = false;
            break;
        case "captcha_word":
            t.captcha_err = false;
            break;
    }
  },

  showError: function(obj,text) {

    t.beforErr = "<tr><td></td><td class='Error' colspan='2'>";
    t.afterErr = "</td></tr>"



    if($(obj).parents("tr").next("tr").find("td.Error").length == 0)
     $(obj).parents("tr").after(t.beforErr+text+t.afterErr);

    switch($(obj).attr("id"))
    {
        case "name":
            t.name_err = true;
            break;
        case "last_name":
            t.last_name_err = true;
            break;
        case "email":
            t.email_err = true;
            break;
        case "login":
            t.login_err = true;
            break;
        case "password":
            t.password_err = true;
            break;
        case "confirm_password":
            t.confirm_password_err = true;
            break;
        case "captcha_word":
            t.captcha_err = true;
            break;
    }
  },

  checkField: function(obj) {
    t = this;
    switch($(obj).attr("id"))
    {
      case "name":
       if($(obj).val().length == 0)
       {
        t.showError(obj, "Ошибка, поле не должно быть пустым");
       }
       else
       {
        t.removeError(obj);
       }
       break;
      case "last_name":
       if($(obj).val().length == 0)
        t.showError(obj,"Ошибка, поле не должно быть пустым");
       else
       {
        t.removeError(obj);
       }
       break;
       /* --- ПРОВЕРКА E-MAIL --->>> */
      case "email":
        if($(obj).val().length == 0)
        {
          t.removeError(obj);
          t.showError(obj,"Ошибка, поле не должно быть пустым");
        }
        else
        {
          if(!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($(obj).val()))
          {
            t.removeError(obj);
            t.showError(obj,"Ошибка, введен неверный e-mail");
          }
          else
          {
            t.removeError(obj);
            var parametr = $(obj).val();
            jQuery.get("/forum/profile/registration_ajax.php",{
            param: "email",
            value: parametr
          },
            function name(text) {
              if(text == "false")
               t.showError(obj,"Такой e-mail уже зарегистрирован");
              else
              {
                t.removeError(obj);
                t.checkBtn();
              }
           });
          }
        }
        break;
      /* <<<--- ПРОВЕРКА E-MAIL --- */

      /* --- ПРОВЕРКА ЛОГИНА --->>> */
      case "login":
        if($(obj).val().length == 0)
        {
          t.removeError(obj);
          t.showError(obj,"Ошибка, поле не должно быть пустым");
        }
        else if(/[!\[\]#\$%&'\*\+\/=\?\^`{\|\}~а-яА-Я\\\(\)\.,]/i.test($(obj).val()))
        {
            t.removeError(obj);
            t.showError(obj,"Ошибка, введены недопустимые символы");
        }
        else
        {
          jQuery.get('/forum/profile/registration_ajax.php',{
          param: "login",
          value: $(obj).val()
           }, function name(text) {
              if(text == "false")
              {
                 t.removeError(obj);
                 t.showError(obj,"Такой пользователь уже существует");
              }
              else
              {
                t.removeError(obj);
                t.checkBtn();
              }

          });
        }
        break;
      /* <<<--- ПРОВЕРКА ЛОГИНА --- */

      /* --- ПРОВЕРКА ПАРОЛЕЙ --->>> */
      case "password":
        if($(obj).val().length == 0)
        {
          t.showError(obj,"Ошибка, поле не должно быть пустым");
        }
         else if ($(obj).val().length < 6)
         {
           t.removeError(obj);
           var count_simbols;
           switch($(obj).val().length)
           {
             case 1:
                count_simbols = "1 символ";
                break;

             case 2:
             case 3:
             case 4:
                count_simbols = $(obj).val().length + " символа";
                break;

             default :
                count_simbols = $(obj).val().length + " символов";
                break;
           }
           t.showError(obj,"Ошибка, короткий пароль (" + count_simbols + ")");
         }
         else
         {
            t.removeError(obj);
         }
        break;

      case "confirm_password":
         if($(obj).val() != t.password.val())
          t.showError(obj,"Ошибка, введенные пароли не совпадают");
         else
         {
          t.removeError(obj);
         }
         break;
      /* <<<--- ПРОВЕРКА ПАРОЛЕЙ --- */

      /* --- ПРОВЕРКА КАПТЧИ --->>> */
      case "captcha_word":
        $.get('/forum/profile/registration_ajax.php',{
          param: "captcha",
          value: $(obj).val(),
          captcha_sid: $('#sid').val()
         },
         function(text)
         {
           if(text == "false")
           {
               t.showError(obj,"Не верно введено слово с картинки");
           }
              else
              {
               t.removeError(obj);
               t.checkBtn();
              }
         }
         );
        break;
      /* <<<--- ПРОВЕРКА КАПТЧИ --- */
    }
    t.checkBtn();
  },

  checkBtn: function() {
    t = this;
    if(!(t.name_err || t.last_name_err || t.email_err || t.login_err || t.password_err || t.confirm_password_err || t.captcha_err))
    {
      $("#send").removeAttr("disabled");
      $("#send").css("color","#000000");
    }
    else
    {
      $("#send").attr("disabled","disabled");
      $("#send").css("color","#999999");
    }
  },

  refresh: function() {
     t = this;
     $.get('/forum/profile/registration_ajax.php',{
        param: "capchaRefresh",
        captcha_sid: $("#sid").val()
       },
       function(text)
       {
         t.captcha_err = true;
         t.checkBtn();
         $("#captcha_word").val("");
         $("#sid").val(text);
         $("#captcha").attr("src","/bitrix/tools/captcha.php?captcha_sid=" + text);
       });
   }
}
