function themesTabLoaded()
{
	var themename = $("#themename"),
		rowid = $("#themeid"),
		allFields = $([]).add(themename).add(rowid),
		tips = $("#themesValidateTips");


	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;
		}
	}
	
	$("#themesdialog").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(themename,"назви теми",0,1000);
				var data = {themename: themename.val(), tblId: "themes", rowid: rowid.val(), action: rowid.val() ? "edit" : "create"}
				var keyvaltab=$("#keyvaltab")[0], rowscol = keyvaltab.rows;
				if (keyvaltab.deletedRows)
					data.deletedRows = keyvaltab.deletedRows.join(",")
				var re=/^newkw|^editedkw/
				for (var i=0; i<rowscol.length; ++i)
					if(re.test(rowscol[i].id))
						data[rowscol[i].id] = $(rowscol[i].cells[1]).text()
				if (bValid)
				{
					$.ajax({
						type: "POST",
						url: "service.php",
						cache: false,
						dataType: "json",
						data: data,
						success: function(res)
						{
							if (res.result == "ok")
							{
								if(res.action=="edit")
								{
									$($("#"+res.themeid + " A")[2]).text(res.themename)
								}else
								{
									$('#' + res.container).append(res.text);
								}
								$("#autordialog").dialog('close');
							}else
							{
								$('#' + res.container).empty().append(res.text);
							}
						},
						error: function(html)
						{
							alert("Error appeared!")
						}
					})
				}

				$(this).dialog('close');
			},
			'Скасувати': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	$('#create-themes').click(function() {
		$('#keyvaltab TBODY').empty()
		$('#themesdialog').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");
	});
/*
	$('#create-keyval').click(function() {
		var newVal = prompt("Введіть нове значення:")
		rowId = "newkw"+PK();
		if (newVal)
		{
			$('#keyvaltab TBODY').append('<tr id="' + rowId + '"><td class="RC RCEdit"><a href="javascript:void(0)" onclick="tbl.colFunc(this)">&nbsp;</a></td>'+
				'<td>' + newVal + '</td>'+
				'<td class="RC RCDel"><a href="javascript:void(0)" onclick="tbl.colFunc(this)">&nbsp;</a></td></tr>');
		}
	})
	.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");
	});*/
}

newKeyWord = function(aObj)
{
	var newVal = prompt("Введіть нове значення:")
	rowId = "newkw"+PK();
	if (newVal)
	{
		$('#keyvaltab TBODY').append('<tr id="' + rowId + '"><td class="RC RCEdit"><a href="javascript:void(0)" onclick="tbl.colFunc(this)">&nbsp;</a></td>'+
			'<td>' + newVal + '</td>'+
			'<td class="RC RCDel"><a href="javascript:void(0)" onclick="tbl.colFunc(this)">&nbsp;</a></td></tr>');
	}
}

editKeyWord = function(aObj)
{
	var word = aObj.parentNode.parentNode.cells[1];
	newVal = prompt("Введіть нове значення:", word.innerHTML)
	if (newVal)
	{
		word.innerHTML = newVal;
		var row = aObj.parentNode.parentNode, re=/^newkw|^editedkw/;
		if(!re.test(row.id))
			row.id="editedkw"+row.id
	}
}

delKeyWord = function(aObj)
{
	var row = aObj.parentNode.parentNode, re=/^newkw/;
	if(!re.test(row.id))
	{
		if (!row.parentNode.parentNode.deletedRows)
			row.parentNode.parentNode.deletedRows=[];
		row.parentNode.parentNode.deletedRows.push("'"+row.id+"'");
	}
	row.parentNode.removeChild(row);
}
