PK = function()
{
	var pk = "p" + (new Date() * 1).toString(32), pkn = 0;
	return function(aName)
	{
		if (aName)
			return aName;
		if (pkn > 99999)
		{
			pkn = 0;
			pk = "p" + (new Date() * 1).toString(32);
		};
		return pk + (++pkn).toString(32);
	}
}()
function checkState(aObj)
{
	//aObj.className = aObj.className?'':'checked'
	aObj.style.backgroundImage = aObj.style.backgroundImage?'':'url(images/chck1.gif)'
}
submitForm = function(aFormName, aObj, aEvent)
{
	aEvent.returnValue= false;
	var frm = document.forms[aFormName]
		frm.elements["selection"].value = aObj.id
		frm.submit()
}
btnClick = function(aFormName, aCmd, aBtnId, aEvent)
{
	aEvent.returnValue= false;
	var frm = document.forms[aFormName];
		frm.action = aCmd
		frm.elements["selection"].value = aBtnId;
		frm.submit()
}
clearFile =function(aObj)
{
	aObj.previousSibling.innerHTML=aObj.previousSibling.innerHTML.replace(/value="[^"]*"/, 'value=""')
}


tbl={
	colFunc: function(aObj)
	{
		var fnc = aObj.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("COL")[aObj.parentNode.cellIndex].getAttribute("colfunc")
		if (fnc)
			new Function(fnc).apply(aObj)
	},
	editRow: function(aObj)
	{
		$.ajax({
			type: "POST",
			url: "service.php",
			cache: false,
			dataType: "json",
			data:
			{
				rowId: aObj.parentNode.parentNode.id,
				action: "beforEditRow",
				tblId: aObj.parentNode.parentNode.parentNode.parentNode.id
			},
			success: function(res)
			{
				if (res.result == "ok")
				{
					for(var field in res.fields)
					{
						$('#' + field).val(res.fields[field]);
					}
					if (res.translations)
					{
						$("#translations").empty().append(res.translations);
						translButtons();
					}
				/*	{result: 'ok', dialog: 'transldialog' , fields:{translid: "22", translbookid: "49c21373dd457", translname: "Tutor", transllanguage: "2",
						translcomment: "\u0423 \u043e\u0440\u0438\u0433\u0456\u043d\u0430\u043b\u0456 \u0433\u043b\u0430\u0432\u0430 \u0425\u0406 \u0434\u0430\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u0430\u0434\u0443 \u0454 \u0447\u0430\u0441\u0442\u0438\u043d\u043e\u044e \u0433\u043b\u0430\u0432\u0438 \u0425.",
						translhavefile: "1", translfilesize: null, translfilename: null}}*/

					if (res.dialog == 'bookdialog')
					{
						loadKeyValues();
					}else if (res.dialog == 'transldialog')
					{
						if (res.fields.translhavefile=="1")
						{
							$("#fileupploading").hide();
							$("#uploadedfile").show();
							$("#ufname").text(res.fields.translfilename||"Назва файлу невідома")
							$("#ufsize").text((((Math.ceil(res.fields.translfilesize*1000/1024))/1000)||"0")+"Kb")
							$("#translfile").val(1);
							$('#hidefileupl').show()
						}else
						{
							$("#fileupploading").show();
							$("#uploadedfile").hide();
							$("#ufname").text('')
							$("#ufsize").text('')
							$("#translfile").val(0);
							$('#hidefileupl').hide() 
						}
					}else if(res.dialog=='themesdialog')
					{
						$.ajax({
							type: "POST", url: "service.php", cache: false, async: false,
							data: {action: 'keyvalsget', themeId: $("#themeid").val()},
							success: function(res)
							{
								document.getElementById("keyvals").innerHTML= res;
							},
							error: function(html)
							{
								alert("Loading problems...");
							}
						})
					}
					$('#'+res.dialog).dialog('option', 'title', 'Редагувати... ');
					$('#'+res.dialog).dialog('open');
				}else
				{
					alert(res.text);
				}
			},
			error: function(html)
			{
				alert("Error appeared!")
			}
		})
	},
	viewRow: function(aObj)
	{
		viewRow(aObj.parentNode.parentNode.parentNode.parentNode.id, aObj.parentNode.parentNode.id)
	},
	delRow: function(aObj)
	{
		window.delObj = aObj;
		$('#confirmdialog').dialog('open');
	}
}

viewRow = function(aTblId, aRowId)
{
	$.ajax({
		type: "POST",
		url: "service.php",
		cache: false,
		data:
		{
			rowId: aRowId,
			action: "viewRow",
			tblId: aTblId
		},
		success: function(res)
		{
			$('#viewdialog').empty().append(res)
			$('#viewdialog').dialog('open')
		},
		error: function(html)
		{
			alert("Error appeared!")
		}
	})
}

$(function(){
	$('#confirmdialog').dialog({
		bgiframe: true,
		autoOpen: false,
		height: 100,
		width: 300,
		modal: true,
		overlay: {
			backgroundColor: '#7D82AA',
			opacity: 0.5
		},
		buttons: {
			'ОК': function() {
				var aObj = window.delObj;
				$.ajax({
					type: "POST",
					url: "service.php",
					cache: false,
					dataType: "json",
					data: {
						rowId: aObj.parentNode.parentNode.id, 
						action: "delRow", 
						tblId: aObj.parentNode.parentNode.parentNode.parentNode.id},
					success: function(res)
					{
						if (res.result == "ok")
						{
							$('#' + res.container).remove();
							
						}else
						{
							alert(res.text);
						}
						$("#confirmdialog").dialog('close');
					},
					error: function(html)
					{
						alert("Error appeared!")
					}
				})
			},
			'Скасувати': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			;
		}
	});
});
LoadSubLevel= function(aObj, aQuerry)
{
	var div
	if (aObj.getAttribute("loaded"))
	{
		div = $(aObj).next()
		if(div.css("display")=="block")
			div.hide("slow");
		else
			div.show("slow");
		return
	}
	if (aObj.getAttribute("loading"))
		return
	div = $('<div "style=display: none">&nbsp;</div>')
	$(aObj).after(div)
	with ($(div))
	{
		css({'padding': '0px 0px 0px 20px', 'background': 'url(images/loading.gif) no-repeat 0 0'});
		ajaxSuccess(function(evt, request, settings){
			$(this).css({'background-image': 'none'});
			$(this).show("slow");
			$(this).prev().attr("loaded", "true")
		});
		ajaxStart(function(){
			$(this).prev().attr("loading", "true")
		 });
		ajaxError(function(event, request, settings){
			$(this).prev().removeAttr("loading")
			$(this).css({'background-image': 'none', "background-color":"#ff0000", color: '#ffffff'})
			$(this).text("Помилка завантаження!");
		 });
		load("service.php", {querryId: aQuerry, rowId: aObj.getAttribute("rowid")});
	}
}

startSearch = function(aObj)
{
	$("#searchResult").load("service.php",  $(aObj.form).serialize())
}

TabLoaded= function(aObj, a, a2)
{
	selected = $('#tabs').tabs('option', 'selected')
	if (selected==2)
		autorsTabLoaded()
	else if (selected==3)
		themesTabLoaded()
	else if (selected==4)
		userTabLoaded()
		

}
function ajaxFileUpload()
{
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});

	$.ajaxFileUpload
	(
		{
			url:'doajaxfileupload.php',
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (res, status)
			{
				if(typeof(res.error) != 'undefined')
				{
					if(res.error != '')
					{
						alert(res.error);
					}else
					{
						
						if (res.fields.translhavefile)
						{
							$("#fileupploading").hide();
							$("#uploadedfile").show();
							$("#ufname").text(res.fields.translfilename||"Назва файлу невідома")
							$("#ufsize").text((((Math.ceil(res.fields.translfilesize*1000/1024))/1000)||"0")+"Kb")
							$("#translfile").val(1);
							$('#hidefileupl').show()
						}else
						{
							$("#translValidateTips").empty().text(res.msg).effect("highlight",{},1500);
							$("#translfile").val(0);
							/*$("#fileupploading").show();
							$("#uploadedfile").hide();
							$("#ufname").text('')
							$("#ufsize").text('')*/
						}
					}
				}
			},
			error: function (data, status, e)
			{
				$("#translValidateTips").empty().text(e).effect("highlight",{},1500);
			}
		}
	)
	
	return false;

}

$(function(){
	$('#viewdialog').dialog({
		bgiframe: true,
		autoOpen: false,
		height: 400,
		width: 500,
		modal: true,
		overlay: {
			backgroundColor: '#7D82AA',
			opacity: 0.5
		},
		buttons: {
			'Скасувати': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			;
		}
	});
});
comparetext = function(aObj)
{
	
	/*
	var l, t1 = $("#txt1"), v1="", v2="";
	if (t1 && (l = t1.length))
	{
		frm = $("#txt1")[0].form;
		t1 = frm["txt1"];
		t2 = frm["txt2"];
		for (var i = 0; i < l; i++) {
			if (t1[i].checked)
				v1 = t1[i].value
			if (t2[i].checked)
				v2 = t2[i].value
		}
		aObj.href = "gettext.php?txt1=" + v1 + "&txt2=" + v2;
	}*/
	if ($("#txt1").length)
		$($("#txt1")[0].form).submit();
}

loadKeyValues = function()
{
	themeId: $("#bookthemes").val()
	$("#keyValues").empty().ajaxStart(function()
	{
		$("#keyValues")[0].style.background="url(loading.gif) no-repeat 0px 0px";
	}).ajaxComplete(function(){
		$("#keyValues")[0].style.background="none"
	}).load("service.php", {action: "keywordsget", themeId: $("#bookthemes").val(), bookId: $("#bookid").val()});
}
