$(document).ready(function() {
	
	var last_state = '';
	var login = $("div#kabinet_block input[name=login]"),
		password = $("div#kabinet_block input[name=password]"),
		email = $("div#kabinet_block input[name=email]"),
		fio = $("div#kabinet_block input[name=fio]"),
		phone = $("div#kabinet_block input[name=phone]"),
		adres = $("div#kabinet_block textarea[name=adres]"),
		captcha = $("div#kabinet_block input[name=captcha]"),
		allFields = $([]).add(login).add(password).add(email).add(fio).add(phone).add(captcha),
		tips = $(".validateTips");
	
	$('a#vixod').live('click', function(){
		$.ajax({  
			type: "POST",  
			url: $('div#site_name').html() + "include/ajax/user_logout.php",  
			data: "act=logout",
			dataType: "html",
			success: function(html){
				if (html.match('0'))
				{
					window.location.reload();
                    //location.href = '/';
				}
			}
		});
		return false;
	});
    
	
	
	function updateTips(t, ti) {
		ti
			.text(t)
			.addClass('ui-state-highlight');
		setTimeout(function() {
			tips.removeClass('ui-state-highlight', 2000);
		}, 500);
	}

	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Длина " + n + " должна быть от " + min + " до " + max + ".", tips);
			return false;
		} else {
			return true;
		}
	}
	
	function checkRegexp(o,regexp,n) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n, tips);
			return false;
		} else {
			return true;
		}
	}
	
	$('div#kabinet_block').dialog({
		autoOpen: false,
		width: 500,
		modal: true,
		buttons: {
			'Сохранить': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(login,"логина",3,32);
				bValid = bValid && checkLength(password,"пароля",3,32);
				bValid = bValid && checkLength(fio,"ФИО",3,500);
				bValid = bValid && checkRegexp(phone,/^375(29|33|44|25)\d{7}$/,"Неверный номер телефона");
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Неверный email. Например user@some.com");

				if (bValid) {
					$.ajax({  
						type: "POST",  
						url: $('div#site_name').html() + "include/ajax/user_kabinet.php",  
						data: "login=" + login.val() + "&password=" + password.val() + "&email=" + email.val() + "&fio=" + fio.val() + "&phone=" + phone.val() + "&adres=" + adres.val() + "&captcha=" + captcha.val(),
						dataType: "html",
						beforeSend: function() {
							$('div#kabinet_block form fieldset').hide();
							$('div#kabinet_block form div#load').show();
						},
						success: function(html){
							$('div#kabinet_block form div#load').hide();
							if (html.match('0'))
							{
								updateTips('Вы успешно сохранили свои личные данные.', tips);
								$('div#login a#kabinet').html(login.val());
								setTimeout(function() {
									$('div#kabinet_block form fieldset').show();
									if ($("div#kabinet_block").dialog('isOpen'))
										$("div#kabinet_block").dialog('close');
								}, 2000);
							}
							else if (html.match('1'))
							{
								updateTips('Неверно введен код с картинки.', tips);
								$('div#kabinet_block form fieldset').show();
								$('img#captcha').attr('src','include/captcha/captcha.php?captcha='+Math.random());
								captcha.val('');
								captcha.addClass('ui-state-error');
							}
							else if (html.match('2'))
							{
								updateTips('Неверны данные пользователя.', tips);
								$('div#kabinet_block form fieldset').show();
								$('img#captcha').attr('src','include/captcha/captcha.php?captcha='+Math.random());
								captcha.val('');
								password.val('');
								login.addClass('ui-state-error');
							}
						}
					});
				}
			}	
		}
	});
	
	$('div#logout_menu a#kabinet').click(function(){
		$('img#captcha').attr('src','include/captcha/captcha.php?captcha='+Math.random());
		$('div#kabinet_block').dialog('open');
		return false;
	});
	
});
