//Sign Up
var SignUp = {
	Name: function() {
		var name = $('#name').val();
		if(name == 0){
			$('#name').css('border-color', '#dd4b39');
		} else if(isValidName(name)) {
			$('#name').css('border-color', '#5ab033');
		}  else {
			$('#name').css('border-color', '#dd4b39');
		}
	},
	Lastname: function() {
		var lastname = $('#lastname').val();
		if(lastname == 0){
			$('#lastname').css('border-color', '#dd4b39');
		} else if(isValidName(lastname)) {
			$('#lastname').css('border-color', '#5ab033');
		}  else {
			$('#lastname').css('border-color', '#dd4b39');
		}
	},
	Email: function() {
		var email = $('#email').val();
		if(email == 0){
			$('#email').css('border-color', '#dd4b39');
		} else if(isValidEmailAddress(email)) {
			$('#email').css('border-color', '#5ab033');
		} else {
			$('#email').css('border-color', '#dd4b39');
		}
	},
	Password1: function() {
		var password1 = $('#password1').val();
		if(password1 != 0 && password1.length >= 6){
			$('#password1').css('border-color', '#5ab033');
		} else {
			$('#password1').css('border-color', '#dd4b39');
		}
	},
	Password2: function() {
		var password1 = $('#password1').val();
		var password2 = $('#password2').val();
		if(password2 != password1 || password2 == 0){
			$('#password2').css('border-color', '#dd4b39');
		} else if(password2.length >= 6){
			$('#password2').css('border-color', '#5ab033');
		} else {
			$('#password2').css('border-color', '#5ab033');
		}
	},
	Gender: function() {
		var gender = $('#gender').val();
		if(gender == 0){
			$('#gender').css('border-color', '#dd4b39');
		} else {
			$('#gender').css('border-color', '#5ab033');
		}
	},
	Birthday: function() {
		var day = $('#day').val();
		var month = $('#month').val();
		var year = $('#year').val();
		if(day != 0){
			$('#day').css('border-color', '#5ab033');
		} else {
			$('#day').css('border-color', '#dd4b39');
		} 
		if(month != 0){
			$('#month').css('border-color', '#5ab033');
		} else {
			$('#month').css('border-color', '#dd4b39');
		} 
		if(year != 0){
			$('#year').css('border-color', '#5ab033');
		} else {
			$('#year').css('border-color', '#dd4b39');
		}
	},
	Country: function() {
		var country = $('#country').val();
		var show_city = $('#show_city').val();
		if(country == 0){
			$('#country').css('border-color', '#dd4b39');
		} else {
			$('#country').css('border-color', '#5ab033');
		}
		if(show_city == 0){
			$('#show_city').css('border-color', '#dd4b39');
		} else {
			$('#show_city').css('border-color', '#5ab033');
		}
	},
	LoadCity: function(id){
		$('#load_mini').show();
		if(id > 0){
			$('#city').slideDown();
			$('#show_city').load('/index.php?go=loadcity', {country: id});
		} else {
			$('#city').slideUp();
			$('#load_mini').hide();
		}
	},
	Finish: function(){
		var name = $('#name').val();
		var lastname = $('#lastname').val();
		var email = $('#email').val();
		var password1 = $('#password1').val();
		var password2 = $('#password2').val();
		var gender = $('#gender').val();
		var day = $('#day').val();
		var month = $('#month').val();
		var year = $('#year').val();
		var country = $('#country').val();
		var city = $('#show_city').val();
		var rndval = new Date().getTime(); 
		if(email != 0 && isValidEmailAddress(email) && name !=0 && isValidName(name) && lastname != 0 && isValidName(lastname) && password1 !=0 && password2 != 0 && password2 == password1 && day !=0 && month!=0 && year!=0 && country!=0 && show_city !=0){
			Box.Show('sec_code', 280, 'Enter the code', '<div style="padding:20px;text-align:center"><div class="cursor_pointer" onClick="updateCode(); return false"><div id="sec_code"><img src="/system/antibot/antibot.php?rndval=' + rndval + '" alt="" title="Show another code" width="120" height="50" /></div></div><div id="code_loading"><input type="text" id="val_sec_code" class="inpst" maxlength="6" style="margin-top:10px;width:110px" /></div></div>', lang_box_canсel, 'Send', 'checkCode(); return false;');
			$('#val_sec_code').focus();
		} else {
			Box.Info('boxerr', 'Error', 'All <b>fields</b> are required to fill', 300);
			Box.Close('sec_code');
		}
	},
	Send: function(sec_code){
		var name = $('#name').val();
		var lastname = $('#lastname').val();
		var email = $('#email').val();
		var password1 = $('#password1').val();
		var password2 = $('#password2').val();
		var gender = $('#gender').val();
		var day = $('#day').val();
		var month = $('#month').val();
		var year = $('#year').val();
		var country = $('#country').val();
		var city = $('#show_city').val();
		var val_sec_code = $("#val_sec_code").val();
		$.post('/index.php?go=signup_finish', {
				name: name,
				lastname: lastname,
				email: email,
				password1: password1,
				password2: password2,
				day: day,
				month: month,
				year: year,
				gender: gender,
				country: country,
				city: city,
				sec_code: sec_code
			}, function(d){
			var exp = d.split('|');
			if(exp[0] == 'ok'){
				window.location = '/u'+exp[1];
			} else if(exp[0] == 'err_mail'){
				$('#err2').show().html('User with this E-Mail address is already registered.');
				Box.Close('sec_code');
			} else {
				Box.Info('boxerr', 'Error', 'Unkown Error', 300);
				Box.Close('sec_code');
			}
		});
	}
}
//RESTORE
var Restore = {
	Next: function(){
		var email = $('#email').val();
		if(email != 0 && isValidEmailAddress(email)){
			butloading('send', '32', 'disabled', '');
			$.post('/index.php?go=restore&act=next', {email: email}, function(data){
				if(data == 'no_user'){
					Box.Info('boxerr', 'Error ', ' User with E-mail address: "<b>'+email+'</b>" not found. <br /> Please make sure that entered the correct E-mail.', 500, 3500);
					Box.Close('sec_code');
				} else {
					var exp = data.split('|');
					$('#step1').hide();
					$('#step2').show();
					$('#c_src').attr('src', exp[1]);
					$('#c_name').html('<b>'+exp[0]+'</b>');
				}
			});
			butloading('send', '32', 'enabled', 'Next step');
		} else
			Box.Info('boxerr', 'Error', 'Ensure that have entered your E-mail address', 300, 2500);
		    Box.Close('sec_code');
	},
	Send: function(){
		var email = $('#email').val();
		butloading('send2', '129', 'disabled', '');
		$.post('/index.php?go=restore&act=send', {email: email}, function(d){
			$('#step2').hide();
			$('#step3').show();
		});
	},
	Finish: function(){
		var new_pass = $('#new_pass').val();
		var new_pass2 = $('#new_pass2').val();
		var hash = $('#hash').val();
		if(new_pass != 0 && new_pass != 'Новый пароль'){
			if(new_pass2 != 0 && new_pass2 != 'Повторите еще раз новый пароль'){
				if(new_pass == new_pass2){
					if(new_pass.length >= 6){
						$('#err').hide();
						butloading('send', '43', 'disabled', '');
						$.post('/index.php?go=restore&act=finish', {new_pass: new_pass, new_pass2: new_pass2, hash: hash}, function(d){
							$('#step1').hide();
							$('#step2').show();
						});
					} else
						$('#err').show().html('Длина пароля должна быть не менее 6 символов.');
				} else
					$('#err').show().html('Оба введенных пароля должны быть идентичны.');
			} else
				setErrorInputMsg('new_pass2');
		} else
			setErrorInputMsg('new_pass');
	}
}
function isValidName(xname){
	var pattern = new RegExp(/^[a-zA-Zа-яА-Я]+$/);
 	return pattern.test(xname);
}
function isValidEmailAddress(emailAddress) {
 	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 	return pattern.test(emailAddress);
}
function updateCode(){
	var rndval = new Date().getTime(); 
	$('#sec_code').html('<img src="/system/antibot/antibot.php?rndval=' + rndval + '" alt="" title="Show another code" width="120" height="50" />');
}
function checkCode(){
	var val_sec_code = $("#val_sec_code").val();
	$('#code_loading').html('<img src="'+template_dir+'/images/loading_mini.gif" style="margin-top:21px" />');
	$.get('/system/antibot/sec_code.php?user_code='+val_sec_code, function(data){
		if(data == 'ok'){
			SignUp.Send(val_sec_code);
		} else {
			updateCode();
			$('#code_loading').html('<input type="text" id="val_sec_code" class="inpst" maxlength="6" style="margin-top:10px;width:110px" />');
			$('#val_sec_code').val('');
			$('#val_sec_code').focus();
		}
	});
}