function submit_form(fid) {
		var valid = true;
		
		$('#'+fid+" .required").each(function(){
			if ( !jQuery(this).val() || jQuery(this).val() == 0 ) {
				jQuery(this).addClass('important');
				valid = false;
			}
			else {
				jQuery(this).removeClass('important');
			}
		});
		if (!valid) {
			alert("Пожалуйста, заполните все поля!");
			return false;
		}
		
		var reg=/^\d+(\.\d+)?$/;
		$('#'+fid+" .number").each(function(){
			if (!reg.test(jQuery(this).val())) {
				jQuery(this).addClass('important');
				valid=false;
			}
		});
		if (!valid) {
			alert("Неправильный формат числа");
			return false;
		}
		
		$('#'+fid+" .password").each(function(){
			if ($('#'+jQuery(this).attr('id')+'_orig').val() && jQuery(this).val() != $('#'+jQuery(this).attr('id')+'_orig').val()) {
					jQuery(this).addClass('error');
					valid=false;
			}
		});
		if (!valid) {
			alert("Пароли не совпадают");
			return false;
		}

		if (valid) {
			$("#"+fid).ajaxSubmit({
				complete: function(msg) {
					if (msg.responseText.search(/Ошибка/i) != -1) {
						$("#"+fid+" .form_info").each(function() {
							jQuery(this).addClass('important');
							jQuery(this).removeClass('great');
						});
					}
					else {
						if ($("#"+fid+" #"+fid+"_redirect").val()) {
							window.location.href=escape($("#"+fid+" #"+fid+"_redirect").val());
						}
						$("#"+fid+" .form_info").each(function(){
							jQuery(this).addClass('great');
							jQuery(this).removeClass('important');
						});
					}
					$("#"+fid+" .form_info").each(function(){
						$(this).html(msg.responseText);
					});

					var el = $("#"+fid+" .close");
					if (el.length) {
						$("#"+fid)[0].reset();
						setTimeout(function() {
							hide_popup($("#"+fid+" .close").val());
						}, 3000);
					}
				}
			});
			return true;
		}
}

jQuery(function() {
	
	$(".submit").click(function() {
		var fid = this.form.id;
		submit_form(fid);
	});
	
	$(".required").change(function() {
		if (jQuery(this).val()) {
			jQuery(this).removeClass('important');
			$("#"+jQuery(this).id+"_msg").val("Ok");
		}
		else {
			jQuery(this).addClass('important');
		}
	}).change();

	$(".unique").change(function() {
		var fid = this.form.id;
		var id = "#"+jQuery(this).attr('id')+"_msg";
		var idspan = id+" span";
		$.ajax({
			url: '/db/unique.html',
			type: 'POST',
			data: { 
				form_id: $('#'+fid+'_form_id').val(),
				id: $('#'+fid+'_oid').val(),
				name : jQuery(this).attr('name'),
				value : jQuery(this).val()
			},
			success: function(data) {
				if (data) {
					$(id).addClass('important');
					$(id).removeClass('great');
					$(idspan).text(data);
				}
				else {
					$(id).addClass('great');
					$(id).removeClass('important');
					$(idspan).text('Ok');
				}
			}
		});
	}).change();

	$(".date").change(function() {
                if (!jQuery(this).val())
                        return;

                var re = /^\d{2}\.\d{2}\.\d{4}$/;
                try {
                        if (! re.test(jQuery(this).val())) {
                                throw("Неправильная дата. Правильный формат - дд.мм.гггг (например, 20.10.1976)");
                        }
                        var ad = jQuery(this).val().split(".").reverse();
                        var dt = new Date(ad[0],ad[1]-1,ad[2]);
                        if (/4|6|9|11/.test(ad[1]) && ad[2] == 31) {
                                throw('В этом месяце может быть всего 30 дней');
                        }
                        var y = ad[0];
                        if (ad[1] == 2 && (ad[2] > ((y = !(y % 4) && (y % 1e2) || !(y % 4e2)) ? 29 : 28))) {
                                throw('В этом месяце всего '+(y ? 29 : 28)+' дней');
                        }
                        if (ad[0] != dt.getFullYear() || ad[1]-1 != dt.getMonth() || ad[2] != dt.getDate()) {
                                throw('Неправильная дата');
                        }
                }
                catch (err) {
                        alert(err);
                        jQuery(this).addClass('important');
                        jQuery(this).val('');
                        this.focus();
                        return false;
                }
                jQuery(this).removeClass('important');
	}).change();

	$(".email").change(function() {
		if (!jQuery(this).val())
			return;

		try {
			var re = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
			if (! re.test(jQuery(this).val())) {
				throw("Неправильный адрес электронной почты");
			}
		}
                catch (err) {
                        alert(err);
                        jQuery(this).addClass('important');
                        jQuery(this).val('');
                        this.focus();
                        return false;
                }
                jQuery(this).removeClass('important');
	}).change();

	$(".phone").change(function() {
		if (!jQuery(this).val())
			return;
		try {
			var re = /^[0-9\-\(\)\+\ ]+$/;
			if (! re.test(jQuery(this).val())) {
				throw("Неправильный номер телефона. Допустимые символы - все цифры, -, +, () и пробел. Пример: +7 (495) 123-45-67");
			}		
		}
		catch (err) {
                        alert(err);
                        jQuery(this).addClass('important');
                        jQuery(this).val('');
                        this.focus();
                        return false;
		}
		jQuery(this).removeClass('important');	
	}).change();
});

var start_indicator = true;

jQuery(document).ajaxStart(function () {
	setTimeout(function(){
		if (start_indicator) 
			$.blockUI({
				message: $('#please_wait'),
				fadeIn: 700,
				showOverlay: false
			});
		else
			start_indicator = true;
	}, 500);
});

jQuery(document).ajaxStop(function(){ start_indicator = false; $.unblockUI() });

