userTabLoaded= function()
{
	var name = $("#name"),
		password = $("#password"),
		userrole = $("#userrole"),
		rowid = $("#userid"),
		allFields = $([]).add(name).add(password).add(rowid),
		tips = $("#validateTips");

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Довжина " + n + " повинна бути між "+min+" та "+max+".");
			return false;
		} else {
			return true;
		}
	}

	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}
	
	$("#userdialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 300,
		modal: true,
		buttons: {
			'Зберегти': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(name,"імені користувача",3,16);
				bValid = bValid && checkLength(password,"паролю",5,16);

				bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Ім'я користувача повинне складатися символів a-z, 0-9, _, та починатися з букви.");
				bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Дозволені символи для паролю: a-z 0-9");
				if (bValid)
				{
					$.ajax({
						type: "POST",
						url: "service.php",
						cache: false,
						dataType: "json",
						data: {name: name.val(), password: password.val(), userrole: userrole.val(), rowid: rowid.val(), tblId: "users", action: rowid.val()? "edit" : "create"},
						success: function(res)
						{
							if (res.result == "ok")
							{
								$('#' + res.container).append(res.text);
								$("#userdialog").dialog('close');
							}else
							{
								$('#' + res.container).empty().append(res.text);
							}
						},
						error: function(html)
						{
							alert("Error appeared!")
						}
					})
				}
			},
			'Скасувати': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	
	
	$('#create-user').click(function() {
		$('#userdialog').dialog('option', 'title', 'Створити користувача');
		$('#userdialog').dialog('open');
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	).mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
}
