  //------------------------------------- Подсчет введенных символов ---------------------

    $(document).ready(function()
      {
        $(".char_counter").each(function() {
          $(this).next().html($(this).attr("maxlength")-$(this).val().length);
        });
        
        $(".char_counter").keypress(function(e)
          { 
            k=1;       
            if (e.which == 8) k = -1;
            if (e.which == 0) k = 0;
            if ($(this).val().length = "") k = 0;
            
            maxlength = $(this).attr("maxlength");
            length = $(this).val().length +k;

            char_count = $("#"+$(this).attr("name")+"_char_count"); 
            
            
            count = maxlength-length;
            
            if (maxlength-length > maxlength) count = maxlength;
            if (maxlength-length < 0) count = 0;
            
            char_count.html(String(count));
          });        
      });

  $(document).ready(function()
    {      
      // Аппендим каждому инпуту эррор-див и ок-див
      $('.formInput').after('<div class="input_error"><\/div>');
      $('.formInput').append('<div class="input_ok"><\/div>');

      // Переписываем цсс для радиобутонов
      $('.radio').css({width: "15px", float: "left", margin: "2px", clear: "left", border: "0px"});

      // Делаем года (12-100) :)
      d = new Date();
      year = d.getFullYear();
      
      for (i = 12; i < 100; i++)
        {
          birth = year - i;
	      $('#year').append('<option value=' + birth + '>' + birth + '<\/option>');
	    }
    });
    
    if ($("#empty_fields").val()=='true')
    {
      // если ничего не ввели, возвращаем дефолтное значение инпута
	  $(":text, :password").blur(function()
        {
	      if (this.value=='')
		    {
		      this.value=this.defaultValue;
	        }
	    });
	  
	  // если в инпуте дефолтное значение - убираем
	  $(":text, :password").focus(function()
        {
	      if (this.value==this.defaultValue)
            {
		      this.value=''; 
              this.focus();
            }
	    });
	}
	  
    $(".validate_required").blur(function()
	  {
	    if ($("#empty_fields").val()=='true')
          {
	        if (this.value == this.defaultValue)
		      {
		        $(this).css({backgroundColor:"#ffffff"});
			    $(this).next(".input_ok").hide();
		        $(this).parent("div.formInput").next(".input_error").show();
 		        $(this).parent("div.formInput").next(".input_error").text('Данное поле обязательно для заполнения.');
		      }
		    else
		      {
		        $(this).css({backgroundColor:"#b6e1ae"});
                $(this).next(".input_ok").show();
		        $(this).parent("div.formInput").next(".input_error").hide();
		      }
		  }
		else
		  {
		    
		    if (this.value == '')
		      {		        
		        $(this).css({backgroundColor:"#ffffff"});
			    $(this).next(".input_ok").hide();
		        $(this).parent("div.formInput").next(".input_error").show();
 		        $(this).parent("div.formInput").next(".input_error").text('Данное поле обязательно для заполнения.');
		      }
		    else
		      {		    
		        $(this).css({backgroundColor:"#b6e1ae"});
                $(this).next(".input_ok").show();
		        $(this).parent("div.formInput").next(".input_error").hide();
		      }
		  }
	  });

	// проверяем nikname
	$(".validate_nikname").blur(function()
	  { 
	    if (this.value!='' & this.value.match(/^[А-яA-z0-9\_\-\ ]{3,12}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только русские/латинские буквы, цифры, пробел и символы: - и _ <br /> Длина не должна привышать 12 символов');
		  }	  
	  
        if(this.value != '' && $(this).attr("class").lastIndexOf('ajax_validate') != -1)
          { 
            $("#end_textbox_nikname").before('<img class="ajaxPreloader" src="/img/ajax-loader.gif">');
            $.post("/registration/validnikname/", {nikname:this.value}, function(data) 
              {      
	            if (data != '')
                  {  
                    $(".validate_nikname").css({backgroundColor:"#ffffff"});
                    $(".validate_nikname").next(".input_ok").hide();
       	            $(".validate_nikname").parent("div.formInput").next(".input_error").show();
 		            $(".validate_nikname").parent("div.formInput").next(".input_error").text(data);
                   }
                 $(".ajaxPreloader").remove();              
              }); 	    
          }
          
        if(this.value != '' && $(this).attr("class").lastIndexOf('ajax_exist') != -1)
          { 
            $("#end_textbox_nikname").before('<img class="ajaxPreloader" src="/img/ajax-loader.gif">');
            $.post("/registration/validnikname/", {nikname:this.value}, function(data) 
              {      
	            if (data == '')
                  {  
                    $(".validate_nikname").css({backgroundColor:"#ffffff"});
                    $(".validate_nikname").next(".input_ok").hide();
       	            $(".validate_nikname").parent("div.formInput").next(".input_error").show();
 		            $(".validate_nikname").parent("div.formInput").next(".input_error").text('Пользователь с таким именем не найден');
                   }
                 $(".ajaxPreloader").remove();              
              }); 	    
          }	          
          	   
	  });
	  
	// проверяем мейл
	$(".validate_email").blur(function()
	  {
       if (this.value.match(/^[a-z0-9!#$%*\/?|^{}`~&'+=\-\_]+(\.[a-z0-9!#$%*\/?|^{}`~&'+=-_]+)*@([a-z]([a-z0-9-]*[a-z0-9])*\.)+[a-z]([a-z0-9-]*[a-z0-9])*$/i)==null)
         {	
           $(this).css({backgroundColor:"#fbd9d9"});
           $(this).next(".input_ok").hide();
           $(this).parent("div.formInput").next(".input_error").show();
           $(this).parent("div.formInput").next(".input_error").html('Введен некорректный адрес электронной почты.');
         }	 
          
        if(this.value != '' && $(this).attr("class").lastIndexOf('ajax_validate') != -1)
          {
            $("#end_textbox_email").before('<img class="ajaxPreloader" src="/img/ajax-loader.gif">');
            $.post("/registration/validemail/", {email:this.value}, function(data) 
              {      
	            if (data != '')
                  {  
                    $(".validate_email").css({backgroundColor:"#ffffff"});
                    $(".validate_email").next(".input_ok").hide();
       	            $(".validate_email").parent("div.formInput").next(".input_error").show();
 		            $(".validate_email").parent("div.formInput").next(".input_error").text(data);
                   }
                 $(".ajaxPreloader").remove();              
              }); 	    
          }	 	  
	  });
	
	// проверяем пароль
	$(".validate_password").blur(function()
	  {
	    if (this.value == this.defaultValue)
		  {
		    $(this).css({backgroundColor:"#ffffff"});
			$(this).next(".input_ok").hide();
		    $(this).parent("div.formInput").next(".input_error").show();
 		    $(this).parent("div.formInput").next(".input_error").text('Вы не ввели пароль.');
		  }
		else
		  {
            if (this.value.match(/^[^А-я]{6,}$/i)==null)
	          {	
			    $(this).css({backgroundColor:"#fbd9d9"});
                $(this).next(".input_ok").hide();
			    $(this).parent("div.formInput").next(".input_error").show();
				$(this).parent("div.formInput").next(".input_error").html('Пароль может состоять минимум из 6 символов.');
		      }
		    else
			  {
			    $(this).css({backgroundColor:"#b6e1ae"});
                $(this).next(".input_ok").show();
			    $(this).parent("div.formInput").next(".input_error").hide();
			  }
		  }
	  });	
	  
	// проверяем повторный ввод пароля
	$(".password_same").blur(function()
	  {	    
	    if (this.value == this.defaultValue)
		  {
		    $(this).css({backgroundColor:"#ffffff"});
			$(this).next(".input_ok").hide();
		    $(this).parent("div.formInput").next(".input_error").show();
 		    $(this).parent("div.formInput").next(".input_error").text('Вы не ввели пароль повторно.');
		  }
		else
		  {
            if (this.value != $("#rdpass").val())
	          {				  
			    $(this).css({backgroundColor:"#fbd9d9"});
                $(this).next(".input_ok").hide();
			    $(this).parent("div.formInput").next(".input_error").show();
				$(this).parent("div.formInput").next(".input_error").html('Введенные пароли не совпадают.');
		      }
		    else
			  {
			    $(this).css({backgroundColor:"#b6e1ae"});
                $(this).next(".input_ok").show();
			    $(this).parent("div.formInput").next(".input_error").hide();
			  }
		  }
	  });
	  
	// проверяем повторный ввод пароля
	$(".validate_only_ru").blur(function()
	  {	    
        if (this.value!='' & this.value.match(/^[А-я]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только русские буквы.');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	  });
	  
    $(".validate_only_ru_words").blur(function()
	  {
        if (this.value!='' & this.value.match(/^[А-я\\ ]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только русские буквы и пробел.');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	  });
	  
	  
    $(".validate_telephone").blur(function()
	  {
        if (this.value!='' & this.value.match(/^[0-9\+\-\ \(\)\#]{10,30}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только цифры и символы: + ( ) - # , длинна от 10 до 30 символов');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	  });	  
	  
	  
    $(".validate_only_eng").blur(function()
	  {
        if (this.value!='' & this.value.match(/^[A-z]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только латинские буквы');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	  });
	  
    $(".validate_only_eng_words").blur(function()
	  {
        if (this.value!='' & this.value.match(/^[A-z\\ ]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только латинские буквы и пробел.');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	  });
	  
	$(".validate_numeric").blur(function()
	  {
	    if (this.value!='' & this.value.match(/^[0-9]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только цифры');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	    else if (this.value == '')
	      {
	        $(this).css({backgroundColor:"#fff"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();	    
	      }
	      
	  });


	$(".validate_url").blur(function()
	  {                                      
	    if (this.value!='' & this.value.match(/^[A-z0-9/\:\.\-\=\$\%\&\?]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только латинские буквы, цифры и символы Url адреса');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	    else if (this.value == '')
	      {
	        $(this).css({backgroundColor:"#fff"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();	    
	      }
	      
	  });
	  
	$(".validate_range").blur(function()
	  {
	    alert('we need to write this validator');
	  });

	$(".validate_num_eng").blur(function()
	  {
        if (this.value!='' & this.value.match(/^[A-z0-9]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только латинские буквы и цифры.');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	  });
	  
	$(".validate_ru_eng_words").blur(function()
	  {
        if (this.value!='' & this.value.match(/^[A-zА-я\\ ]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только латинские, русские буквы и пробел.');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	  });

	$(".validate_ru_eng_words_spec").blur(function()
	  { 
        if (this.value!='' & this.value.match(/^[А-яA-z0-9\\ \-\_\\,\\.\\"\\!\\?\\*\\(\\)\\:]{1,}$/i)==null)
	      {				  
			$(this).css({backgroundColor:"#fbd9d9"});
            $(this).next(".input_ok").hide();
			$(this).parent("div.formInput").next(".input_error").show();
			$(this).parent("div.formInput").next(".input_error").html('Допускаются только латинские, русские буквы, цифры, спец символы и пробел.');
		  }
		else if (this.value!='')
		  {
			$(this).css({backgroundColor:"#b6e1ae"});
            $(this).next(".input_ok").show();
			$(this).parent("div.formInput").next(".input_error").hide();
	      }
	  });
	  
	$(".validate_ddmmyyyy").blur(function()
	  {
	    alert('we need to write this validator');
	  });
	  
    $(".validate_time_hi").blur(function()
	  {
	    alert('we need to write this validator');
	  });
	  
	$(".validate_time_his").blur(function()
	  {
	    alert('we need to write this validator');
	  });