dump = function (o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

dumpf = function (o)
	outfile=io.open("dump-file.txt", "w")

	outfile:write(dump(o))
	outfile:close()
end

-- Minimally effective quoting
quote = function (astring)
    local quoted1 = string.gsub(astring,'\"', '\\"')
    local quoted2 = '"' .. string.gsub(quoted1,"%'", "\\'") .. '"'
	local linefeed = string.gsub(quoted2,"\n", "\\n")
	return linefeed
end


write_saved = function (o)
--	local escaped
   if type(o) == 'table' then
      local s = '{\n'
      for k,v in pairs(o) do
         if type(k) ~= 'number' then
		 k = '"'..k..'"'
		 end
         s = s .. '['..k..'] = ' .. write_saved(v) .. ',\n'
      end
      return s .. '}\n'
   elseif
		type(o) == 'string' then
		return quote(o)
   else
		return tostring(o)
   end
end

_size = function (t)		-- return number of elements in table
	local i = 0
	for _,_ in pairs(t) do
		i = i +1
	end
	return i
end

reverse_id = function (Adata)	-- given a grp-id table like this
	--[[

	Grp_Dat["1N"].id = {
	[294]= {L=1, C=1},		-- "Fungal Grotto I Vanquisher",
	[78]= {L=2, C=1},		-- "Darkshade Caverns I Vanquisher"
	[272]= {L=3, C=1},		-- "Arx Corinium Vanquisher"
	[357]= {L=4, C=1},		-- "Direfrost Keep Vanquisher"
	[393]= {L=5, C=1},		-- "Blessed Crucible Vanquisher"

	return a table like this
	["11"] = 294
	["21"] = 78
	]]
	local rev = {}
	for ach, lc in pairs (Adata) do
		local key
		key = lc.L .. lc.C
		if rev[key] == nil then
			rev[key] = ach
		else
			print ("Reverse_id, error dup key: " .. key)
		end
	end
	return rev
end

blank_detail = function()
			detail_name.title = ""
			detail_desc.title = ""
end

box_mousemove_cb = 	function (self, l, c)	-- For Char Mode Only
-- we have previously added .dat to the box
		if self.dat == nil then
			detail_name.title = "box.dat is nil"
			return
		end

		if l == 0 or c == 0 then
			blank_detail()
			return
		end

		local key = l .. c

		local ach = self.dat.reverse_id[key]

		if ach == nil then
			blank_detail()
		--    detail_name.title = "No reverse lookup on " .. key	-- Normal on Group 2 and Pub panel with empty spaces
			return
		end

		if Ach_Detail[ach] == nil then
			detail_name.title = "No Achievement for " .. ach
			return
		end

		if (self.ach == nil) then
			print("box_mousemove_cb: self.ach is nil")
			return
		end

		if (self.ach[ach] ~= nil) then  -- completed (not on dungeon modes) This Mouseover shouldn't be on Dungeon Modes

			detail_name.title = "(ID: " .. ach .. ")  "  .. L.Completed .. os.date(dateformat,self.ach[ach].time) .. "   " .. Ach_Detail[ach].name
		else
			detail_name.title = "(ID: " .. ach .. ")  "  ..  Ach_Detail[ach].name
		end
		detail_desc.title = Ach_Detail[ach].description

	end


box_leavewindow_cb = function (self)
	blank_detail()
end

generate_id=function()
--write a combined list of achievement id we look for to add to the in-game part (cut and paste) for filtering
    local unique_id= {}   -- (id,true}

	outfile=io.open("data/ids.lua", "w")

	if outfile == nil then
		print ("Couldn't open id.lua file for writing.")
	end
	outfile:write("hist.IDs = {" .. "\n")

	for _,i in ipairs(Grp_Order) do
		outfile:write("-- GRP " .. i ..  "\n")
		for j,_ in pairs (Grp_Dat[i].id) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end

		end
	end

	for _,i in ipairs(Trials_Order) do
		outfile:write("-- Trial " .. i .. "\n")
		for j,_ in pairs (Trials_Dat[i].id) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
		end
	end

	outfile:write("-- DLC " .. "\n")
	for j,_ in pairs (DLC_Dat.id) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("-- SQ " .. "\n")
	for j,_ in pairs (SQ_dat) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("-- Pub " .. "\n")
	for j,_ in pairs (Pub_Dat_Char.id) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("-- WB " .. "\n")
	for j,_ in pairs (WB_dat) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("-- Specials " .. "\n")
	for j,_ in pairs (Special_dat) do
		    if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
	end

	outfile:write("-- DLC2 " .. "\n")

	for _,dlc in ipairs (DLC_Order) do
		outfile:write("-- DLC2 " .. dlc .."\n")
		for _,j in pairs (DLC_Dat2[dlc].line) do
			if unique_id[j] == nil then
				unique_id[j] = true
				outfile:write("[" .. j .. "] = true,\n")
			end
		end
	end

	outfile:write("}" .. "\n")
	outfile:close()
end


--Run through Location data to get the info needed to dimension the WB and SQ boxes
Location_Box = function (name)
	if type(name) ~= "string" then
		print ("Location_Box name is not string.")
		return
	end


	local cols = {}
	local lines = {}
	local Areas = {}
	--Summarise
	for i,entry in ipairs(Locations2Key) do
		if lines[entry.L] == nil then  lines[entry.L] = true end
		if cols[entry.C] == nil then  cols[entry.C] = entry.Area end	-- Note the Area for the Col
		if Areas[entry.Area] == nil then  Areas[entry.Area] = true end
	end
	--[[
	print("Cols:  " .. tostring(#cols))
	print("Lines: " .. #lines)
	print("Areas: " .. #Areas)
	--]]

	local return_t = iup.matrix{}
	return_t.numlin=#lines
	return_t.numcol=#cols
	return_t.widthdef=100

	iup.SetAttribute(return_t, "READONLY", "YES")
	iup.SetAttribute(return_t, "ALIGNMENT0", "ACENTER")
	-- Set Headings
	for col, Area in ipairs (cols) do
	--	print("Col:  " .. col .. ",  Area:  "  .. Area )
		return_t:setcell(0,col, Area_names[Area].long)
	--	print("Set Heading: " .. Area_names[Area].long)
	end

	--Set Lines
	return_t:setcell(0,0, L.Zone)
	for line,_ in ipairs(lines) do
		return_t:setcell(line,0, tostring(line))
		iup.SetAttribute(return_t,  "FGCOLOR*:".. tostring(line), FG_Colour_Not_Complete)
		iup.SetAttribute(return_t,  "BGCOLOR*:".. tostring(line), BG_Colour_Not_Complete)
	end


	return return_t
end