var selectedSection = 0;

function replaceLinkWithPanel(linkelem, panelid) {
	linkelem.style.display = "none";
	document.getElementById(panelid).style.display = "block";
}

/* SITE ADMIN PANEL */

Ajax.Responders.register({
	onComplete:function(request, transport, json) {
			if (transport.status == 403) {
				window.location.href = 'login.php?go=' + encodeURIComponent(window.location.href) + "&error=timeout";
			}
		}
});


function drawTiers() {
	new Ajax.Updater('tiersControl', 'drawTiers.ajax.php');
}

function drawTree(sectionID) {
	if (!sectionID) {
		if (selectedSection) {
			new Ajax.Updater('treeControl', 'drawTree.ajax.php',
				{parameters:	{id:	selectedSection}}
			);
		} else {
			new Ajax.Updater('treeControl', 'drawTree.ajax.php');
		}
	} else {
		selectedSection = sectionID;
		new Ajax.Updater('treeControl', 'drawTree.ajax.php',
			{parameters:	{id:	sectionID}}
		);
	}
}

/* TIER ADMIN PANEL */

function promptRename(tierSpanID, tierID) {
	if ($(tierSpanID).firstChild.nodeValue) {
		var tierName = prompt("Rename this tier to:", $(tierSpanID).firstChild.nodeValue);
		if (tierName) {
			renameTier(tierID, tierName);
		}
	}
	return false;
}

function promptAddTier() {
	var tierName = prompt("Name of the new tier:");
	if (tierName) {
		addTier(tierName);
	}
	return false;
}

function moveTierUp(tierID) {
	new Ajax.Request('moveTierUp.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{id:	tierID}
		});
	return false;
}

function moveTierDown(tierID) {
	new Ajax.Request('moveTierDown.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{id:	tierID}
		});
	return false;
}

function addTier(tierName) {
	new Ajax.Request('addTier.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{name:	tierName}
		});
	return false;
}

function renameTier(tierID, tierName) {
	new Ajax.Request('renameTier.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{id:	tierID, name:	tierName}
		});
	return false;
}

function deleteTier(tierID) {
	new Ajax.Request('deleteTier.ajax.php', {
		onSuccess:	function(transport) {
					drawTiers();
				},
		parameters:	{id:	tierID}
		});
	return false;
}

/* TREE ADMIN PANEL */

function promptRenameSection(sectionElemID, sectionID) {
	if ($(sectionElemID).firstChild.nodeValue) {
		var sectionName = prompt("Rename this section to:", $(sectionElemID).firstChild.nodeValue);
		if (sectionName) {
			renameSection(sectionID, sectionName);
		}
	}
	return false;
}

function promptAddSection(parentID) {
	var sectionName = prompt("Name of the new section:");
	if (sectionName) {
		addSection(parentID, sectionName);
	}
	return false;
}

function moveSectionUp(sectionID) {
	new Ajax.Request('moveSectionUp.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function moveSectionDown(sectionID) {
	new Ajax.Request('moveSectionDown.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function addSection(parentID, sectionName) {
	new Ajax.Request('addSection.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{pid:	parentID, name:	sectionName}
		});
	return false;
}

function confirmSectionDelete() {
	return confirm('Are you sure you want to delete this section?  All sub-sections will be deleted as well, and the user enrollment for the deleted sections will be cleared.');
}

function deleteSection(sectionID) {
	new Ajax.Request('deleteSection.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function renameSection(sectionID, sectionName) {
	new Ajax.Request('renameSection.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID, name:	sectionName}
		});
	return false;
}

function attachPage(sectionID) {
	new Ajax.Request('attachPage.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function detachPage(sectionID) {
	new Ajax.Request('detachPage.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function setSectionPublic(sectionID) {
	new Ajax.Request('setSectionPublic.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function removeSectionPublic(sectionID) {
	new Ajax.Request('removeSectionPublic.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function setSectionOptional(sectionID) {
	new Ajax.Request('setSectionOptional.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function removeSectionOptional(sectionID) {
	new Ajax.Request('removeSectionOptional.ajax.php', {
		onSuccess:	function(transport) {
					drawTree();
				},
		parameters:	{id:	sectionID}
		});
	return false;
}

function popup2(url, winname, winw, winh){
	var margx = (screen.width - winw) / 2;
	var margy = (screen.height - winh) / 2;
	var win = window.open(url, winname, 'width=' + winw + ',height=' + winh + ',status=no,scrollbars=yes,resizable=yes,top=' + margy + ',left=' + margx);
	win.focus();
}
