function validateArticleAction()
{
   startLoading();
   validateArticle(escapeEx(document.forms["articleForm"]["menutitle"].value),
                           escapeEx(document.forms["articleForm"]["key"].value),
                           escapeEx(document.forms["articleForm"]["id"].value),
                           validateArticleCallback);
}
function validateArticleCallback(result)
{
   stopLoading();
   if (result == "")
   {
	 	  clearError();
      document.forms["articleForm"].submit();
   }
   else
   {
      reportError(result);
   }
}
function validateCategoryAction()
{
   startLoading();
   validateCategory(escapeEx(document.forms["categoryForm"]["title"].value),
                           escapeEx(document.forms["categoryForm"]["key"].value),
                           escapeEx(document.forms["categoryForm"]["id"].value),
                           validateCategoryCallback);
}
function validateCategoryCallback(result)
{
   stopLoading();
   if (result == "")
   {
	    clearError();
      document.forms["categoryForm"].submit();
   }
   else
   {
      reportError(result);
   }
}

var curNode;
var curDir;
var curVisible;
function deleteArticleAction(articleid, actionNode)
{
	var tr = actionNode.parentNode.parentNode;
   if (confirm("Удалить работу " + getChildByTagName(tr.cells[2], "a").innerHTML + "?"))
   {
      curNode = tr;
      startLoading();
      deleteArticle(articleid, deleteArticleCallback);
   }
}
function deleteArticleCallback(result)
{
   stopLoading();
   if (result && curNode)
   {
      curNode.parentNode.removeChild(curNode);
   }
}
function deleteCategoryAction(categoryid, actionNode)
{
	var tr = actionNode.parentNode.parentNode;
   if (confirm("Удалить раздел " + getChildByTagName(tr.cells[2], "a").innerHTML + "?"))
   {
      curNode = tr;
      startLoading();
      deleteCategory(categoryid, deleteCategoryCallback);
   }
}
function deleteCategoryCallback(result)
{
   stopLoading();
   if (result && curNode)
   {
      curNode.parentNode.removeChild(curNode);
   }
}

var hideShowId;
function hideShowCategoryAction(id, visible, actionNode)
{
	 hideShowId = id;
   curNode = actionNode;
	 curVisible = visible == 1 ? 0 : 1; 
   startLoading();
   hideShowCategory(id, visible, hideShowCategoryCallback);
}
function hideShowCategoryCallback(result)
{
   stopLoading();
   if (result && curNode)
   {
	 		curNode.value = curVisible == 1 ? "Спрятать" : "Показать";
			curNode.setAttribute("vis", curVisible);
			var cat = document.getElementById("cat" + hideShowId);
			if (cat.className.indexOf("invis") != -1)
				cat.className = cat.className.substring(0, cat.className.indexOf("invis"));
			if (cat.className.indexOf("vis") != -1)
				cat.className = cat.className.substring(0, cat.className.indexOf("vis"));
			cat.className += curVisible == 1 ? "vis" : "invis";
   }
}
function hideShowArticleAction(id, visible, actionNode)
{
	 hideShowId = id;
   curNode = actionNode;
	 curVisible = visible == 1 ? 0 : 1; 
   startLoading();
   hideShowArticle(id, visible, hideShowArticleCallback);
}
function hideShowArticleCallback(result)
{
   stopLoading();
   if (result && curNode)
   {
	 		curNode.innerHTML = curVisible == 1 ? "Спрятать" : "Показать";
			curNode.setAttribute("vis", curVisible);
			var arc = document.getElementById("arc" + hideShowId);
			if (arc.className.indexOf("invis") != -1)
				arc.className = arc.className.substring(0, arc.className.indexOf("invis"));
			if (arc.className.indexOf("vis") != -1)
				arc.className = arc.className.substring(0, arc.className.indexOf("vis"));
			arc.className += curVisible == 1 ? "vis" : "invis";
   }
}
function moveArticleAction(id, dir, actionNode)
{
   curNode = actionNode.parentNode.parentNode;
	 curDir = dir; 
   startLoading();
   moveArticle(id, dir, moveArticleCallback);
}
function moveArticleCallback(result)
{
   stopLoading();
   if (result && curNode)
   {
	 		var tbody = curNode.parentNode;
			var table = tbody.parentNode;
	 		if (curDir == "up" && curNode.rowIndex > 1)
			{
				var prevNode = table.rows[curNode.rowIndex-1];
				tbody.removeChild(curNode);
				tbody.insertBefore(curNode, prevNode);
			}
			else if (curDir == "down" && curNode.rowIndex < table.rows.length-1)
			{
				var nextNode = table.rows[curNode.rowIndex+1];
				tbody.removeChild(curNode);
				if (nextNode.nextSibling)
					tbody.insertBefore(curNode, nextNode.nextSibling);
				else
					tbody.appendChild(curNode);
			}
   }
}
function moveCategoryAction(id, dir, actionNode)
{
   curNode = actionNode.parentNode.parentNode;
	 curDir = dir; 
   startLoading();
   moveCategory(id, dir, moveCategoryCallback);
}
function moveCategoryCallback(result)
{
   stopLoading();
   if (result && curNode)
   {
	 		var tbody = curNode.parentNode;
			var table = tbody.parentNode;
	 		if (curDir == "up" && curNode.rowIndex > 1)
			{
				var prevNode = table.rows[curNode.rowIndex-1];
				tbody.removeChild(curNode);
				tbody.insertBefore(curNode, prevNode);
			}
			else if (curDir == "down" && curNode.rowIndex < table.rows.length-1)
			{
				var nextNode = table.rows[curNode.rowIndex+1];
				tbody.removeChild(curNode);
				if (nextNode.nextSibling)
					tbody.insertBefore(curNode, nextNode.nextSibling);
				else
					tbody.appendChild(curNode);
			}
   }
}

function changeType(type)
{
	var index = 1;
	while (document.getElementById("rowtypeS" + index))
	{
		document.getElementById("rowtypeS" + index).style.display = type == "S" ? "" : "none";
		index++;
	}
	if (type == "L")
		document.getElementById("rowtypeS4").style.display = "";
}

var selNode;
function selectCategory(node)
{
	if (selNode != null)
		selNode.className = "";
	selNode = node;
	selNode.className = "selected";
}