function init()
{
	jQuery('input[@_attr]').bind('keyup', set_timer);
	jQuery('input[@attr2]').bind('change', check_radio_input);
	jQuery('#captcha').bind('keyup', set_timer);
}

function set_timer()
{
	var time = null;
	var last_text = '';
		
	var input_obj = this;
		
	if(time) clearTimeout(time);
	
	if(last_text == input_obj.value)
	return;
		
	time=setTimeout(function(ev)
	{	
		func=input_obj.id+'_check("'+input_obj.value+'")';
		show_error(input_obj.id, eval(func));
		
		last_text = input_obj.value;
	},500);
}

function check_radio_input()
{
	var id = jQuery('#'+this.id).attr('attr2');
	
	func = id+'_check("")';
	show_error(id, eval(func));
}

function show_error(id, notice)
{
	//скрываем все ошибки. {id}_incorrect есть у каждого инпута
	jQuery('#'+id+'_incorrect').parent().children().each(function() {jQuery(this).css('display', 'none');})
	
	if (notice != '')
	{	
		if (id == 'captcha')
		{
			id = 'profile_captcha';
		}
		
		jQuery('#'+id+notice).css('display', '');
		return false;
	}	
}

function profile_login_check(val)
{
	var notice = '';
	
	//если длинна логина меньше 4х, покажем ошибку "короткий логин"
	if(val.length<4)
	{
		notice = '_short';
	}
	//иначе если в поле логина есть недопустимые символы
	else if(!/^[а-яА-Яa-zA-Z0-9-\s_\.]+$/.test(val))
	{
		notice = '_incorrect';
	}
	else
	{
		//проверка логина на уникальность
		notice = send_request(jQuery('#profile_login'));
	}

	return notice;
}

function profile_pass1_check(val)
{
	var notice = '';
			
	if(val.length < 4) notice='_incorrect';
	
	return notice;
}

function profile_pass2_check(val)
{
	var notice = '';
	//не самамя красивая, надо сказать, проверка
	if(val != jQuery('#profile_pass1').attr('value')) notice='_incorrect';
	
	return notice;
}

function profile_mail_check(val)
{
	var notice = '';
			
	if(!/.+?@.+?\..+/.test(val)) 
	{
		notice = '_incorrect';
	}
	else
	{
		notice = send_request(jQuery('#profile_mail'));
	}
	
	return notice;
}

function profile_name_as_login_check(val)
{
	var notice = '';

	if (!(jQuery('#profile_name_as_login').attr('checked') || jQuery('#profile_name_as_fio').attr('checked')))
	{
		notice = '_required';
	}
	
	return notice;
}

function profile_show_data_check(val)
{
	var notice = '';
	
	if (!(jQuery('#profile_show_data').attr('checked') || jQuery('#profile_not_show_data').attr('checked')))
	{
		notice = '_required';
	}
	
	return notice;
}

function profile_city_check(val)
{
	var notice = '';
	return notice;
}

function profile_birth_year_check(val)
{
	var notice = '';
			
	if(!/^[0-9]+$/.test(val))
	notice = '_incorrect';
	
	return notice;
}

function captcha_check(val)
{
	var notice = '';
	return notice;
}

function send_request(input)
{
	var hash = {};
	hash[input.attr('name')] = input.attr('value');
	
	var text=AC.simple('?LRPC=p4cardesign.profile.register&args={"check":"1"}', 'POST', hash, false);
	
	jQuery('#'+input.attr('id')+'_check').css('display', 'none');
	
	//если первый ноль - все верно и уникально
	if(text == '01')
	{
		//показываем что все ок
		//jQuery('#'+input_obj.id+'_complete').css('display', '');
		 return '_complete';
	}
	
	//если первая единица - неверные символы, не соотвествует регвыр
	if (text == '11')
	return '_incorrect';
	//если вообще ничего не пришло, то введено все верно, но такое значение уже есть
	else
	return '_not_unique';
}

function check_required_field_and_submit()
{	
	error = false;
	
	jQuery('input[@_attr]').each(function()
	{	
		if (this.value == '')
		{				
			jQuery('#'+this.id+'_required').css('display', '');		
			if (!error) go_to_ankor(this.id);
			error = true;
		}
		else
		{
			jQuery('#'+this.id+'_required').css('display', 'none');			
			
			func=this.id+'_check("'+this.value+'")';
			error_text = eval(func);
			
			if (error_text != '' && error_text != '_complete')
			{
				jQuery('#'+this.id+error_text).css('display', '');
				if (!error) go_to_ankor(this.id);			
				error = true;
			}
		}
	});
	
	if (jQuery('#captcha').is('input'))
	{
		if (!jQuery('#captcha').attr('value'))
		{
			error = true;
			jQuery('#profile_captcha_required').css('display', '');
		}
	}
	
	//если все обязательные поля заполнены, сабмитим форму
	if (!error) jQuery('#profile_form').submit();
}

function go_to_ankor(ankor)
{
	if (location.href[location.href.length-1] == '/')
	{
		location.href = "#"+ankor;
	}
	else
	{
		location.href = location.href+"/#"+ankor;
	}
}

