﻿var GLOBALregstep=0;

$(document).ready(function(){
	addInputEvents();
});



function checkRegForm(){

GLOBALregres=true;
GLOBALregstep=0;

	var res=true;
	$('input.submit-button').addClass('button-working');
	$('input.submit-button').attr('disabled',true);
	
	
	var name=$('#input-name').val();
	if(name==''){
		res=false;
		$('#input-name').addClass('input-error');
		showLarrHint($('#input-name'),'input-name-error-hint','Введите свое имя');
	}
	
	var day=parseInt($('select[name=day] option:selected').val());
	var month=parseInt($('select[name=month] option:selected').val());
	var year=parseInt($('select[name=year] option:selected').val());
	if(day==0 || month==0 || year==0){
		res=false;
		showLarrHint($('select[name=year]'),'input-birthday-error-hint','Выберите свою дату рождения');
	}
	
	var sex=$('form :radio[@name=sex]:checked').val();
	if(sex!='W' && sex!='M'){
		res=false;
		showLarrHint($('input:radio[name=sex]:last'),'input-sex-error-hint','Укажите свой пол',20);
	}
	
	var email=$('#input-email').val();
	if(email==''){
		res=false;
		$('#input-email').addClass('input-error');
		showLarrHint($('#input-email'),'input-email-error-hint','Введите свой E-mail');
	}
	
/*
	var city=$('#input-city').val();
	if(city==''){
		res=false;
		$('#input-city').addClass('input-error');
		showLarrHint($('#input-city'),'input-city-error-hint','Укажите свой город, если нет в списке, выберите ближайший');
	} else {
		var city_id=parseInt($('#input-city-id').val());
		if(!city_id>0){
			res=false;
			showLarrHint($('#input-city'),'input-city-error-hint','Начните печатать название города, и выберите его из списка, если нет в списке, выберите ближайший');
		}
	}
*/
	checkCity('input-city','left',0,'finalCheckRegForm','true');
	
	var pass=$('#input-pass').val();
	if(pass==''){
		res=false;
		$('#input-pass').addClass('input-error');
		showLarrHint($('#input-pass'),'input-pass-error-hint','Введите пароль');
	}
	
	var pass2=$('#input-pass2').val();
	if(pass2==''){
		res=false;
		$('#input-pass2').addClass('input-error');
		showLarrHint($('#input-pass2'),'input-pass2-error-hint','Введите пароль повторно');
	}
	
	if(pass!='' && pass2!='' && pass!=pass2){
		res=false;
		$('#input-pass2').addClass('input-error');
		showLarrHint($('#input-pass2'),'input-pass2-error-hint','Пароли не совпадают');
	}
	
	var code=$('#input-code').val();
	if(code==''){
		res=false;
		$('#input-code').addClass('input-error');
		showLarrHint($('#input-code'),'input-code-error-hint','Введите код с картинки');
	} else if(code.length!=5){
		res=false;
		$('#input-code').addClass('input-error');
		showLarrHint($('#input-code'),'input-code-error-hint','Неверный код, чтобы перезагрузить картинку, клините по ней');
	}
	
	if(!$('#agree-rules').attr('checked')){
		res=false;
		showRarrHint($('#agree-rules'),'agree-rules-error-hint','Регистрация невозможна, если Вы не согласны с правилами',-5);
	}
	
	if(!res){
		$('input.submit-button').removeClass('button-working');
		$('input.submit-button').attr('disabled',false);
	} else {
		$.getScript('/registration/code.html?code='+code+'&'+Math.random());
		$.getScript('/registration/email.html?email='+email+'&'+Math.random());
	}
	
	return false;
}


function addInputEvents(){
	$('#input-name').bind('focus',function(){
		$('#input-name').removeClass('input-error');
		$('#input-name-error-hint').remove();
	});
	$('#input-email').bind('focus',function(){
		$('#input-email').removeClass('input-error');
		$('#input-email-error-hint').remove();
	});
	$('#input-pass').bind('focus',function(){
		$('#input-pass').removeClass('input-error');
		$('#input-pass-error-hint').remove();
	});
	$('#input-pass2').bind('focus',function(){
		$('#input-pass2').removeClass('input-error');
		$('#input-pass2-error-hint').remove();
	});
	$('#input-city').bind('focus',function(){
		$('#input-city').removeClass('input-error');
		$('#input-city-error-hint').remove();
	});
	$('#input-code').bind('focus',function(){
		$('#input-code').removeClass('input-error');
		$('#input-code-error-hint').remove();
	});
	$('#agree-rules').bind('click',function(){$('#agree-rules-error-hint').remove();});
	$('form :radio[@name=sex]').bind('click',function(){$('#input-sex-error-hint').remove();});
	$('form select[name=day]').bind('change',function(){$('#input-birthday-error-hint').remove();});
	$('form select[name=month]').bind('change',function(){$('#input-birthday-error-hint').remove();});
	$('form select[name=year]').bind('change',function(){$('#input-birthday-error-hint').remove();});
}

function reCaptcha(){
	$('#captcha').attr('src','/i/captcha/?'+Math.random());
}

function finalCheckRegForm(){
	if(GLOBALregstep==3){
		document.getElementById('reg-form').submit();
	} else {
		$('input.submit-button').removeClass('button-working');
		$('input.submit-button').attr('disabled',false);
	}
}
