function autorsTabLoaded()
{
	var authorname = $("#authorname"),
		livefrom = $("#livefrom"),
		liveto = $("#liveto"),
		comment = $("#comment"),
		rowid = $("#authorid"),
		allFields = $([]).add(authorname).add(livefrom).add(liveto).add(comment).add(rowid),
		tips = $("#autorsValidateTips");


	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;
		}
	}
	
	$("#autordialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 510,
		width: 400,
		modal: true,
		overlay: {
			backgroundColor: '#7D82AA',
			opacity: 0.5
		},
		buttons: {
			'Зберегти': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(authorname,"імені автора",3,100);
				bValid = bValid && checkLength(livefrom,"році народження",0,4);
				bValid = bValid && checkLength(liveto,"році смерті",0,4);
				
				bValid = bValid && checkRegexp(livefrom,/^\d{0,4}$/i,'В полі "Роки життя" дозволено вводити тільки цифри');
				bValid = bValid && checkRegexp(liveto,  /^\d{0,4}$/i,'В полі "Роки життя" дозволено вводити тільки цифри');
				
				if (bValid)
				{
					$.ajax({
						type: "POST",
						url: "service.php",
						cache: false,
						dataType: "json",
						data: {authorname: authorname.val(), livefrom: livefrom.val(), liveto: liveto.val(), comment: comment.val(), tblId: "authors", rowid: rowid.val(), action: rowid.val()? "edit" : "create"},
						success: function(res)
						{
							if (res.result == "ok")
							{
								$('#tabs').tabs('option', 'cache', false);
								if(res.action=="edit")
								{
									//$(res.authorid).cells[2].text(res.authorname)
								}else
								{
									//$('#' + res.container).append(res.text);
								}
								$("#autordialog").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-author').click(function() {
		$('#autordialog').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");
	});
}
