$(document).ready(function(){
	$('#doReg').attr('disabled', 'disabled');
	$('.licence-agree').click(function(){
		if($('.licence-agree').hasClass('licence-agree')){
			$('#doReg').removeAttr('disabled', 'disabled');
			$('.licence-agree').addClass('licence-no-agree').removeClass('licence-agree');
		} else {
			$('#doReg').attr('disabled', 'disabled');
			$('.licence-no-agree').addClass('licence-agree').removeClass('licence-no-agree');
		}
	});

	$('#login').bind("keyup",function(e){
		if ($('#login').val().length > 3) {
			check_user('login',$('#login'));
		}
	});
	$('#email').bind("blur",function(e){
		
		check_user('email',$('#email'));
	});
	$('#password').bind("blur",function(e){
		if ($('#password').val().length > 0) {
			check_password_leight($('#password'));
		}
	});
	$('#password2').bind("blur",function(e){
		if ($('#password2').val().length > 0) {
			check_password($('#password'),$('#password2'));
		}
	});
});
function check_password_leight(pass1)
{
	if(pass1.val().length < 5) {
		$('#password').val('');
		$('input#password').removeClass('ok');
		$('input#password').addClass('error');
		$('#password_error').html(AlertText.check_user.error.password_leight);
	} else {
		$('input#password').removeClass('error');
		$('input#password').addClass('ok');
		$('#password_error').html('');
		$('#password2').removeAttr('readonly');
	}
}
function check_password(pass1,pass2)
{
	if(pass1.val() != pass2.val()) 
	{
		$('#password2').val('');
		$('input#password2').removeClass('ok');
		$('input#password2').addClass('error');
		$('#password2_error').html(AlertText.check_user.error.password_repeat);
	} else {
		$('input#password2').removeClass('error');
		$('input#password2').addClass('ok');
		$('#password2_error').html('');
	}
}
function check_user(type,obj)
{
	if(obj.val() == '') return false;
	var params = new Object();
	params.value =	obj.val();
	params.type = type;
	params.action = 'CheckUser';
	jQuery.get('/registration/checkuser/', params, post_check_user, 'json');
}

function post_check_user(obj)
{
	if(obj.error) 
	{
		$('input#' + obj.type).removeClass('ok');
		$('input#' + obj.type).addClass('error');
		$('#' + obj.type + '_error').html(AlertText.check_user.error[obj.error]);
	} else {
		$('#' + obj.type + '_error').html('');
		$('input#' + obj.type).removeClass('error');
		$('input#' + obj.type).addClass('ok');
	}
}