-- This file is provided under the Open Game License version 1.0a
-- For more information on OGL and related issues, see 
--   http://www.wizards.com/d20
--
-- For information on the Fantasy Grounds d20 Ruleset licensing and
-- the OGL license text, see the d20 ruleset license in the program
-- options.
--
-- All producers of work derived from this definition are adviced to
-- familiarize themselves with the above licenses, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- Copyright 2007 SmiteWorks Ltd.


iscustom = true;
sets = {};

-- Functions to manage and store class skill set membership
function parseSets()
	sets = {};

	-- Set strings are number entries separated by commas and optional whitespace
	for w in string.gmatch(setnode.getValue(), "(%d),?%s*") do
		if tonumber(w) then
			sets[tonumber(w)] = true;
		end
	end
end

function storeSets()
	local setstring = "";

	for k, v in pairs(sets) do
		setstring = setstring .. k .. ",";
	end
	
	setnode.setValue(setstring);
end

function setInSet(set, state)
    sets[tonumber(set)] = true;
	storeSets();
end

function isInSet(set)
	return sets[tonumber(set)];
end

function setUpdated()
	parseSets();
end

function setRadialDeleteOption()
	resetMenuItems();
    registerMenuItem("Delete", "delete", 6);

	if not iscustom then
		-- Disallow deletion of non-custom skills
		local labelval = label.getValue();
	else
		registerMenuItem("Delete", "delete", 6);
	end
end

function onMenuSelection(item)
	if item == 6 then
		getDatabaseNode().delete();
	end
end

function onInit()
	setnode = getDatabaseNode().createChild("sets", "string");
	setnode.onUpdate = setUpdated;
	
	parseSets();
	setRadialDeleteOption();
end

function onClose()
	storeSets();
end
