local toc, privateData = ...
local AddonId = toc.identifier

EM.UI.Send = {}
EM.Send = {}
EM.Send.Event = {}

local mathFloor = math.floor
local tableInsert = table.insert
local tableSort = table.sort
local stringFind = string.find
local stringLower = string.lower

EM.Send.Time = Inspect.Time.Server()

EM.Event.Trigger.SendAttach, EM.Event.SendAttach = Utility.Event.Create(AddonId, "SendAttach")
EM.Event.Trigger.SendMessage, EM.Event.SendMessage = Utility.Event.Create(AddonId, "SendMessage")
EM.Event.Trigger.SendMultiRecipients, EM.Event.SendMultiRecipients = Utility.Event.Create(AddonId, "SendMultiRecipients")

function EM.UI.Setup.Send()

	for i=1,7,1 do
		local bag = "BagInventory"..i
		local bagNumber = "Bag" .. i
		if UI.Native[bag]:GetLoaded() then
			EM.UI.Inventory.Bags.Loaded[bagNumber]()
		end
	end

	local sendTab = EM.UI.Send
	local plat,gold,silver = EM.Utility.Coin(Inspect.Currency.List().coin)
	EM.UI.Send.Attachments = {}

	sendTab.Display = UI.CreateFrame("Texture", "SendDisplay", sendTab)
		sendTab.Display:SetTexture(AddonId, "gfx/sendTabSmall.png")
		sendTab.Display:SetLayer(0)
		sendTab.Display:SetPoint("TOPLEFT", sendTab, "TOPLEFT")
		sendTab.Display:SetPoint("BOTTOMRIGHT", sendTab, "BOTTOMRIGHT")

	sendDisplay = sendTab.Display

	sendTab.ToText = UI.CreateFrame("Text", "SendToText", sendDisplay)
		sendTab.ToText:SetPoint("TOPLEFT", sendTab, "TOPLEFT", 17, 8)
		sendTab.ToText:SetLayer(5)
		sendTab.ToText:SetText("TO")
		sendTab.ToText:SetFontSize(13)
		sendTab.ToText:SetFontColor(1,1,1,1)
		sendTab.ToText:SetWidth(30)

	sendTab.ToField = UI.CreateFrame("RiftTextfield", "SendToTextField", sendDisplay)
		sendTab.ToField:SetPoint("TOPLEFT", sendTab.ToText, "TOPRIGHT")
		sendTab.ToField:SetHeight(19)
		sendTab.ToField:SetWidth(332)
		sendTab.ToField:SetLayer(5)
		sendTab.ToField:EventAttach(Event.UI.Input.Key.Down,
			function(handle, hEvent, key)
				if key == "Tab" then
					sendTab.SubjectField:SetKeyFocus(true)
				else
					EM.Utility.SendBtnCheck()
				end
			end, "SendToTextField_KeyDown")
		sendTab.ToField:EventAttach(Event.UI.Input.Key.Focus.Loss,
			function()
				if sendTab:GetVisible() == false then
					sendTab.ToField:SetText("")
				else
					EM.Utility.SendBtnCheck()
				end
			end, "SendToTextField_KeyFocusLoss")
		sendTab.ToField:EventAttach(Event.UI.Textfield.Change,
			function()
				EM.Utility.SendBtnCheck()
			end, "SendToTextField_TextFieldChange")

	sendTab.btnContacts = UI.CreateFrame("Texture", "ContactsBtn", sendTab)
		sendTab.btnContacts:SetTexture("Rift", "btn_LFG_Quest_(normal).pn.png.dds")
		sendTab.btnContacts:SetPoint("TOPLEFT", sendTab.ToField, "TOPRIGHT", 0, -8)
		sendTab.btnContacts:SetHeight(32)
		sendTab.btnContacts:SetWidth(32)
		sendTab.btnContacts:SetLayer(5)
		sendTab.btnContacts:EventAttach(Event.UI.Input.Mouse.Cursor.In, function()
			sendTab.btnContacts:SetTexture("Rift", "btn_LFG_Quest_(over).pn.png.dds")
		end, "MouseIn")
		sendTab.btnContacts:EventAttach(Event.UI.Input.Mouse.Cursor.Out, function()
			sendTab.btnContacts:SetTexture("Rift", "btn_LFG_Quest_(normal).pn.png.dds")
		end, "MouseOut")
		sendTab.btnContacts:EventAttach(Event.UI.Input.Mouse.Left.Down, function()
			sendTab.btnContacts:SetTexture("Rift", "btn_LFG_Quest_(click).pn.png.dds")
		end, "LeftDown")
		sendTab.btnContacts:EventAttach(Event.UI.Input.Mouse.Left.Up, function()
			sendTab.btnContacts:SetTexture("Rift", "btn_LFG_Quest_(normal).pn.png.dds")
			if not EM.UI.Contacts.Window then
				EM.UI.Contacts.Create()
			elseif EM.UI.Contacts.Window and EM.UI.Contacts.Window:GetVisible() then
				EM.UI.Contacts.Window:SetVisible(false)
			else
				EM.UI.Contacts.Window:SetVisible(true)
				EM.UI.Contacts.Populate()
			end
		end, "LeftUp")

	sendTab.SubjectText = UI.CreateFrame("Text", "SendSubjectText", sendDisplay)
		sendTab.SubjectText:SetPoint("TOPLEFT", sendTab.ToText, "BOTTOMLEFT", 0, 10)
		sendTab.SubjectText:SetLayer(5)
		sendTab.SubjectText:SetText("SUBJECT")
		sendTab.SubjectText:SetFontSize(13)
		sendTab.SubjectText:SetFontColor(1,1,1,1)
		sendTab.SubjectText:SetWidth(64)

	sendTab.SubjectField = UI.CreateFrame("RiftTextfield", "SendSubjectTextField", sendDisplay)
		sendTab.SubjectField:SetPoint("TOPLEFT", sendTab.SubjectText, "TOPRIGHT")
		sendTab.SubjectField:SetHeight(19)
		sendTab.SubjectField:SetWidth(320)
		sendTab.SubjectField:SetLayer(5)
		sendTab.SubjectField:EventAttach(Event.UI.Input.Key.Down,
			function(handle, hEvent, key)
				if key == "Tab" then
					sendTab.SentMoneyPlat:SetKeyFocus(true)
				else
					EM.Utility.SendBtnCheck()
				end
			end, "SendSubjectTextField_KeyDown")
		sendTab.SubjectField:EventAttach(Event.UI.Input.Key.Focus.Loss,
			function()
				if sendTab:GetVisible() == false then
					sendTab.SubjectField:SetText("")
				else
					EM.Utility.SendBtnCheck()
				end
			end, "SendSubjectTextField_KeyFocusLoss")
		sendTab.SubjectField:EventAttach(Event.UI.Textfield.Change,
			function()
				EM.Utility.SendBtnCheck()
			end, "SendSubjectTextField_TextFieldChange")

	sendTab.MessageField = UI.CreateFrame("RiftTextfield", "SendMessage", sendDisplay)
		sendTab.MessageField:SetPoint("TOPLEFT", sendTab.SubjectField, "BOTTOMLEFT", -65, 14)
		sendTab.MessageField:SetWidth(387)
		sendTab.MessageField:SetHeight(179)
		sendTab.MessageField:SetLayer(5)
		sendTab.MessageField:SetText("")
		sendTab.MessageField:EventAttach(Event.UI.Input.Key.Down, EM.Send.MessageKeyDown, "KeyDown")
		sendTab.MessageField:EventAttach(Event.UI.Input.Key.Repeat, EM.Send.MessageKeyDown, "KeyRepeat")
		sendTab.SubjectField:EventAttach(Event.UI.Input.Key.Focus.Loss,
			function()
				if sendTab:GetVisible() == false then
					sendTab.MessageField:SetText("")
				end
			end, "SendMessage_KeyFocusLoss")

		sendTab.MessageField.ScrollBar = UI.CreateFrame("RiftScrollbar", "SendMessage_ScrollBar", sendTab.MessageField)
			sendTab.MessageField.ScrollBar:SetOrientation("vertical")
			sendTab.MessageField.ScrollBar:SetPoint("TOPLEFT", sendTab.MessageField, "TOPRIGHT", 7, 0)
			sendTab.MessageField.ScrollBar:SetPoint("BOTTOMLEFT", sendTab.MessageField, "BOTTOMRIGHT", 7, 0)
			sendTab.MessageField.ScrollBar:SetWidth(15)
			sendTab.MessageField.ScrollBar:SetLayer(5)
			sendTab.MessageField.ScrollBar:SetRange(1, 1)
			sendTab.MessageField.ScrollBar:SetEnabled(false)
			sendTab.MessageField.ScrollBar:SetVisible(false)
			sendTab.MessageField.ScrollBar:EventAttach(Event.UI.Scrollbar.Change, function()
				local position = sendTab.MessageField.ScrollBar:GetPosition()
				local minRange, maxRange = sendTab.MessageField.ScrollBar:GetRange()
			end, "ScrollbarChange")

	sendTab.Grid = UI.CreateFrame("Frame", AddonId .. "_Send_Grid", sendDisplay)
		sendTab.Grid:SetPoint("CENTER", sendTab, "CENTER", 0, 68)
		sendTab.Grid:SetWidth(344)
		sendTab.Grid:SetHeight(123)
		sendTab.Grid:SetLayer(10)
		sendTab.Grid:SetVisible(true)
		sendTab.Grid:SetBackgroundColor(0,0,0,0)

		sendTab.Grid.Header = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header", sendTab.Grid)
			sendTab.Grid.Header:SetPoint("TOPLEFT", sendTab.Grid, "TOPLEFT")
			sendTab.Grid.Header:SetPoint("BOTTOMRIGHT", sendTab.Grid, "TOPRIGHT", 0 , 26)
			sendTab.Grid.Header:SetLayer(15)
			sendTab.Grid.Header:SetBackgroundColor(1,1,1,0)

			sendTab.Grid.Header.icon = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header_icon", sendTab.Grid.Header)
				sendTab.Grid.Header.icon:SetPoint("TOPLEFT", sendTab.Grid.Header, "TOPLEFT")
				sendTab.Grid.Header.icon:SetPoint("BOTTOMRIGHT", sendTab.Grid.Header, "BOTTOMLEFT", 25, 0)
				sendTab.Grid.Header.icon:SetLayer(25)
				sendTab.Grid.Header.icon:SetBackgroundColor(0,0,0,0)

				sendTab.Grid.Header.icon.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Header_icon_text", sendTab.Grid.Header.icon)
					sendTab.Grid.Header.icon.text:SetPoint("CENTER", sendTab.Grid.Header.icon, "CENTER")
					sendTab.Grid.Header.icon.text:SetFontSize(15)
					sendTab.Grid.Header.icon.text:SetFontColor(0.925,0.894,0.741,1)
					sendTab.Grid.Header.icon.text:SetLayer(30)
					sendTab.Grid.Header.icon.text:SetText("Attachments:")

			sendTab.Grid.Header.item = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header_item", sendTab.Grid.Header)
				sendTab.Grid.Header.item:SetPoint("TOPLEFT", sendTab.Grid.Header.icon, "TOPRIGHT")
				sendTab.Grid.Header.item:SetPoint("BOTTOMRIGHT", sendTab.Grid.Header.icon, "BOTTOMRIGHT", 289, 0)
				sendTab.Grid.Header.item:SetLayer(20)
				sendTab.Grid.Header.item:SetBackgroundColor(0,0,0,0)

				sendTab.Grid.Header.item.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Header_item_text", sendTab.Grid.Header.item)
					sendTab.Grid.Header.item.text:SetPoint("CENTER", sendTab.Grid.Header.item, "CENTER")
					sendTab.Grid.Header.item.text:SetFontSize(15)
					sendTab.Grid.Header.item.text:SetFontColor(0.925,0.894,0.741,1)
					sendTab.Grid.Header.item.text:SetLayer(25)
					sendTab.Grid.Header.item.text:SetText("Item")

			sendTab.Grid.Header.count = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header_count", sendTab.Grid.Header)
				sendTab.Grid.Header.count:SetPoint("TOPLEFT", sendTab.Grid.Header.item, "TOPRIGHT")
				sendTab.Grid.Header.count:SetPoint("BOTTOMRIGHT", sendTab.Grid.Header.item, "BOTTOMRIGHT", 30, 0)
				sendTab.Grid.Header.count:SetLayer(25)
				sendTab.Grid.Header.count:SetBackgroundColor(0,0,0,0)

				sendTab.Grid.Header.count.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Header_count_text", sendTab.Grid.Header.count)
					sendTab.Grid.Header.count.text:SetPoint("CENTER", sendTab.Grid.Header.count, "CENTER")
					sendTab.Grid.Header.count.text:SetFontSize(15)
					sendTab.Grid.Header.count.text:SetFontColor(0.925,0.894,0.741,1)
					sendTab.Grid.Header.count.text:SetLayer(30)
					sendTab.Grid.Header.count.text:SetText("Amount")

		sendTab.Grid.Body = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body", sendTab.Grid)
			sendTab.Grid.Body:SetPoint("TOPLEFT", sendTab.Grid.Header, "BOTTOMLEFT")
			sendTab.Grid.Body:SetPoint("BOTTOMRIGHT", sendTab.Grid, "BOTTOMRIGHT")
			sendTab.Grid.Body:SetLayer(15)
			sendTab.Grid.Body:SetBackgroundColor(0,0,0,1)

			for i = 1, 4, 1 do

				sendTab.Grid.Body[i] = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i, sendTab.Grid.Body)
					if i == 1 then
						sendTab.Grid.Body[i]:SetPoint("TOPLEFT", sendTab.Grid.Body, "TOPLEFT", 1, 1)
					else
						sendTab.Grid.Body[i]:SetPoint("TOPLEFT", sendTab.Grid.Body[i-1], "BOTTOMLEFT", 0, 1)
					end
					sendTab.Grid.Body[i]:SetWidth(342)
					sendTab.Grid.Body[i]:SetHeight(23)
					sendTab.Grid.Body[i]:SetLayer(20)
					sendTab.Grid.Body[i]:SetBackgroundColor(0,0,0,0)
					sendTab.Grid.Body[i].slot = i
					sendTab.Grid.Body[i].displayed = i

					sendTab.Grid.Body[i].icon = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i .. "_icon", sendTab.Grid.Body[i])
						sendTab.Grid.Body[i].icon:SetPoint("TOPLEFT", sendTab.Grid.Body[i], "TOPLEFT")
						sendTab.Grid.Body[i].icon:SetPoint("BOTTOMRIGHT", sendTab.Grid.Body[i], "BOTTOMLEFT", 23, 0)
						sendTab.Grid.Body[i].icon:SetLayer(25)
						sendTab.Grid.Body[i].icon:SetBackgroundColor(0.133,0.133,0.133,1)

						sendTab.Grid.Body[i].icon.texture = UI.CreateFrame("Texture", AddonId .. "_Inbox_Grid_Body_" .. i .. "_icon_texture", sendTab.Grid.Body[i].icon)
							sendTab.Grid.Body[i].icon.texture:SetPoint("CENTER", sendTab.Grid.Body[i].icon, "CENTER")
							sendTab.Grid.Body[i].icon.texture:SetWidth(23)
							sendTab.Grid.Body[i].icon.texture:SetHeight(23)
							sendTab.Grid.Body[i].icon.texture:SetLayer(30)

					sendTab.Grid.Body[i].name = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i .. "_name", sendTab.Grid.Body[i])
						sendTab.Grid.Body[i].name:SetPoint("TOPLEFT", sendTab.Grid.Body[i].icon, "TOPRIGHT", 1, 0)
						sendTab.Grid.Body[i].name:SetPoint("BOTTOMRIGHT", sendTab.Grid.Body[i].icon, "BOTTOMRIGHT", 289, 0)
						sendTab.Grid.Body[i].name:SetLayer(25)
						sendTab.Grid.Body[i].name:SetBackgroundColor(0.133,0.133,0.133,1)

						sendTab.Grid.Body[i].name.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Body_" .. i .. "_name_text", sendTab.Grid.Body[i].name)
							sendTab.Grid.Body[i].name.text:SetPoint("TOPLEFT", sendTab.Grid.Body[i].name, "TOPLEFT", 2, -1)
							sendTab.Grid.Body[i].name.text:SetPoint("BOTTOMRIGHT", sendTab.Grid.Body[i].name, "BOTTOMRIGHT")
							sendTab.Grid.Body[i].name.text:SetFontSize(17)
							sendTab.Grid.Body[i].name.text:SetFontColor(1,1,1,1)
							sendTab.Grid.Body[i].name.text:SetLayer(30)

					sendTab.Grid.Body[i].count = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i .. "_count", sendTab.Grid.Body[i])
						sendTab.Grid.Body[i].count:SetPoint("TOPLEFT", sendTab.Grid.Body[i].name, "TOPRIGHT", 1, 0)
						sendTab.Grid.Body[i].count:SetPoint("BOTTOMRIGHT", sendTab.Grid.Body[i].name, "BOTTOMRIGHT", 30, 0)
						sendTab.Grid.Body[i].count:SetLayer(25)
						sendTab.Grid.Body[i].count:SetBackgroundColor(0.133,0.133,0.133,1)

						sendTab.Grid.Body[i].count.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Body_" .. i .. "_count_text", sendTab.Grid.Body[i].count)
							sendTab.Grid.Body[i].count.text:SetPoint("TOPLEFT", sendTab.Grid.Body[i].count, "TOPLEFT", 2, -1)
							sendTab.Grid.Body[i].count.text:SetPoint("BOTTOMRIGHT", sendTab.Grid.Body[i].count, "BOTTOMRIGHT")
							sendTab.Grid.Body[i].count.text:SetFontSize(17)
							sendTab.Grid.Body[i].count.text:SetFontColor(1,1,1,1)
							sendTab.Grid.Body[i].count.text:SetLayer(30)

				sendTab.Grid.Body[i]:EventAttach(Event.UI.Input.Mouse.Right.Click, EM.Send.RightClick,  AddonId .. "_Inbox_Grid_Body_" .. i .. "_RightClick")
			end

	sendTab.AttachScrollBar = UI.CreateFrame("RiftScrollbar", "SendAttach_ScrollBar", sendTab.Grid)
		sendTab.AttachScrollBar:SetOrientation("vertical")
		sendTab.AttachScrollBar:SetPoint("TOPLEFT", sendTab.Grid, "TOPRIGHT", 6, 26)
		sendTab.AttachScrollBar:SetLayer(5)
		sendTab.AttachScrollBar:SetHeight(97)
		sendTab.AttachScrollBar:SetWidth(15)
		sendTab.AttachScrollBar:SetRange(1, 1)
		sendTab.AttachScrollBar:SetEnabled(false)
		sendTab.AttachScrollBar:SetVisible(true)
		sendTab.AttachScrollBar:EventAttach(Event.UI.Scrollbar.Change, function()
			local position = sendTab.AttachScrollBar:GetPosition()
			local minRange, maxRange = sendTab.AttachScrollBar:GetRange()
			local newPosition = mathFloor(position) - 1
			EM.Send.Fill(newPosition)
		end, "ScrollbarChange")

	sendTab.Attach = UI.CreateFrame("Frame", "SendAttach", sendDisplay)
		sendTab.Attach:SetBackgroundColor(0,0,0,0)
		sendTab.Attach:SetLayer(10)
		sendTab.Attach:SetMouseMasking("limited")
		sendTab.Attach:SetPoint("TOPLEFT", sendTab.Grid, "TOPLEFT")
		sendTab.Attach:SetPoint("BOTTOMRIGHT", sendTab.Grid, "BOTTOMRIGHT")
		sendTab.Attach:EventAttach(Event.UI.Input.Mouse.Cursor.In, function()
			local held,id = Inspect.Cursor()
			if not held then return end
			if #EM.UI.Send.Attachments > 0 then
				for slot,data in pairs(EM.UI.Send.Attachments) do
					if data[1].key == id then
						held = ""
					end
				end
			end
			if held == "item" then
				local item = Inspect.Item.Detail(id)
				if item.bound == nil then
					table.insert(EM.UI.Send.Attachments, { { key = id, value = item.icon, typeID = item.type }, { value = item.name, color = 'FFFFFF' }, { value = (item.stack or 1), color = 'FFFFFF' } })
				elseif item.bind == "account" then
					table.insert(EM.UI.Send.Attachments, { { key = id, value = item.icon, typeID = item.type }, { value = item.name, color = 'FFFFFF' }, { value = (item.stack or 1), color = 'FFFFFF' } })
				end
			end
		end, "MouseIn")
		sendTab.Attach:EventAttach(Event.UI.Input.Mouse.Cursor.Out, function()
			local held,id = Inspect.Cursor()
			if not held then return end
			if held == "item" then
				table.remove(EM.UI.Send.Attachments, #EM.UI.Send.Attachments)
			end
		end, "MouseOut")
		sendTab.Attach:EventAttach(Event.UI.Input.Mouse.Left.Up, function()
			Command.Cursor(nil)
			if #EM.UI.Send.Attachments == 0 then return end

			EM.Send.Fill()

			if sendTab.SubjectField:GetText() == (nil or "") then
				sendTab.SubjectField:SetText(EM.UI.Send.Attachments[1][2].value .. " x" .. EM.UI.Send.Attachments[1][3].value)
				EM.Utility.SendBtnCheck()
			end
		end, "LeftUp")

	sendTab.CostText = UI.CreateFrame("Text", "SendCost", sendDisplay)
		sendTab.CostText:SetPoint("TOPLEFT", sendTab.Grid, "BOTTOMLEFT", -19, 5)
		sendTab.CostText:SetText("Postage Cost")
		sendTab.CostText:SetFontSize(12)
		sendTab.CostText:SetFontColor(1,1,1,1)
		sendTab.CostText:SetWidth(80)
		sendTab.CostText:SetHeight(20)

	sendTab.CostSilver = UI.CreateFrame("Text", "SendCostSilver", sendDisplay)
		sendTab.CostSilver:SetPoint("TOPRIGHT", sendTab.CostText, "BOTTOMRIGHT", 64, -1)
		sendTab.CostSilver:SetText("25")
		sendTab.CostSilver:SetFontSize(14)
		sendTab.CostSilver:SetFontColor(1,1,1,1)
		sendTab.CostSilver:SetWidth(20)
		sendTab.CostSilver:SetHeight(15)

	sendTab.CostGoldIcon = UI.CreateFrame("Texture", "SendCostGoldIcon", sendDisplay)
		sendTab.CostGoldIcon:SetPoint("TOPRIGHT", sendTab.CostSilver, "TOPLEFT", -2, 3)
		sendTab.CostGoldIcon:SetTexture(AddonId, "gfx/gold.png")
		sendTab.CostGoldIcon:SetWidth(16)
		sendTab.CostGoldIcon:SetHeight(16)
		sendTab.CostGoldIcon:SetVisible(false)

	sendTab.CostGold = UI.CreateFrame("Text", "SendCostGold", sendDisplay)
		sendTab.CostGold:SetPoint("TOPRIGHT", sendTab.CostGoldIcon, "TOPLEFT", -2, -3)
		sendTab.CostGold:SetText("")
		sendTab.CostGold:SetFontSize(14)
		sendTab.CostGold:SetFontColor(1,1,1,1)
		sendTab.CostGold:SetWidth(20)
		sendTab.CostGold:SetHeight(15)

	sendTab.CostPlatIcon = UI.CreateFrame("Texture", "SendCostPlatIcon", sendDisplay)
		sendTab.CostPlatIcon:SetPoint("TOPRIGHT", sendTab.CostGold, "TOPLEFT", -3, 3)
		sendTab.CostPlatIcon:SetTexture(AddonId, "gfx/platinum.png")
		sendTab.CostPlatIcon:SetWidth(16)
		sendTab.CostPlatIcon:SetHeight(16)
		sendTab.CostPlatIcon:SetVisible(false)

	sendTab.CostPlat = UI.CreateFrame("Text", "SendCostPlat", sendDisplay)
		sendTab.CostPlat:SetPoint("TOPRIGHT", sendTab.CostPlatIcon, "TOPLEFT", -3, -3)
		sendTab.CostPlat:SetText("")
		sendTab.CostPlat:SetFontSize(14)
		sendTab.CostPlat:SetFontColor(1,1,1,1)
		sendTab.CostPlat:SetWidth(20)
		sendTab.CostPlat:SetHeight(15)

	sendTab.YourMoneyText = UI.CreateFrame("Text", "SendYourMoney", sendDisplay)
		sendTab.YourMoneyText:SetPoint("BOTTOMLEFT", sendTab.CostText, "BOTTOMLEFT", 0, 40)
		sendTab.YourMoneyText:SetText("Your Money")
		sendTab.YourMoneyText:SetFontSize(12)
		sendTab.YourMoneyText:SetFontColor(1,1,1,1)
		sendTab.YourMoneyText:SetWidth(80)
		sendTab.YourMoneyText:SetHeight(20)

	sendTab.YourMoneySilver = UI.CreateFrame("Text", "SendYourMoneySilver", sendDisplay)
		sendTab.YourMoneySilver:SetPoint("TOPRIGHT", sendTab.CostSilver, "TOPRIGHT", 0, 40)
		sendTab.YourMoneySilver:SetText(silver)
		sendTab.YourMoneySilver:SetFontSize(14)
		sendTab.YourMoneySilver:SetFontColor(1,1,1,1)
		sendTab.YourMoneySilver:SetWidth(20)
		sendTab.YourMoneySilver:SetHeight(15)

	sendTab.YourMoneyGold = UI.CreateFrame("Text", "SendYourMoneyGold", sendDisplay)
		sendTab.YourMoneyGold:SetPoint("TOPRIGHT", sendTab.CostGold, "TOPRIGHT", 0, 40)
		sendTab.YourMoneyGold:SetText(gold)
		sendTab.YourMoneyGold:SetFontSize(14)
		sendTab.YourMoneyGold:SetFontColor(1,1,1,1)
		sendTab.YourMoneyGold:SetWidth(20)
		sendTab.YourMoneyGold:SetHeight(15)

	sendTab.YourMoneyPlat = UI.CreateFrame("Text", "SendYourMoneyPlat", sendDisplay)
		sendTab.YourMoneyPlat:SetPoint("TOPRIGHT", sendTab.CostPlat, "TOPRIGHT", 2, 40)
		sendTab.YourMoneyPlat:SetText(plat)
		sendTab.YourMoneyPlat:SetFontSize(14)
		sendTab.YourMoneyPlat:SetFontColor(1,1,1,1)

	sendTab.SentMoneyText = UI.CreateFrame("Text", "SendSentMoney", sendDisplay)
		sendTab.SentMoneyText:SetPoint("BOTTOMLEFT", sendTab.YourMoneyText, "BOTTOMLEFT", 208, 0)
		sendTab.SentMoneyText:SetText("Amount to Send")
		sendTab.SentMoneyText:SetFontSize(12)
		sendTab.SentMoneyText:SetFontColor(1,1,1,1)
		sendTab.SentMoneyText:SetWidth(100)
		sendTab.SentMoneyText:SetHeight(20)

	sendTab.SentMoneySilver = UI.CreateFrame("RiftTextfield", "SendSentMoneySilver", sendDisplay)
		sendTab.SentMoneySilver:SetPoint("TOPRIGHT", sendTab.YourMoneySilver, "TOPRIGHT", 208, 1)
		sendTab.SentMoneySilver:SetHeight(19)
		sendTab.SentMoneySilver:SetWidth(20)
		sendTab.SentMoneySilver:EventAttach(Event.UI.Input.Key.Down,
			function(handle, hEvent, key)
				if key == "Tab" then
					sendTab.SentMoneyPlat:SetKeyFocus(true)
				end
			end, "SendSentMoneySilver_KeyDown")
		sendTab.SentMoneySilver:EventAttach(Event.UI.Input.Key.Up,
			function(handle, hEvent, key)
				local subject = sendTab.SubjectField:GetText()
				if subject == (nil or "") or (string.find(subject, "platinum") and string.find(subject, "gold")) then
					sendTab.SubjectField:SetText((tonumber(sendTab.SentMoneyPlat:GetText()) or 0) .. " platinum " .. (tonumber(sendTab.SentMoneyGold:GetText()) or 0) .. " gold " .. (tonumber(sendTab.SentMoneySilver:GetText()) or 0) .. " silver")
				end
				EM.Utility.SendBtnCheck()
			end, "SendSentMoneySilver_KeyUp")
		sendTab.SentMoneySilver:EventAttach(Event.UI.Input.Key.Focus.Loss,
			function()
				if sendTab:GetVisible() == false then
					sendTab.SentMoneySilver:SetText("")
				else
					EM.Utility.SendBtnCheck()
				end
			end, "SendSentMoneySilver_KeyFocusLoss")

	sendTab.SentMoneyGold = UI.CreateFrame("RiftTextfield", "SendSentMoneyGold", sendDisplay)
		sendTab.SentMoneyGold:SetPoint("TOPRIGHT", sendTab.YourMoneyGold, "TOPRIGHT", 208, 1)
		sendTab.SentMoneyGold:SetHeight(19)
		sendTab.SentMoneyGold:SetWidth(20)
		sendTab.SentMoneyGold:EventAttach(Event.UI.Input.Key.Down,
			function(handle, hEvent, key)
				if key == "Tab" then
					sendTab.SentMoneySilver:SetKeyFocus(true)
				end
			end, "SendSentMoneyGold_KeyDown")
		sendTab.SentMoneyGold:EventAttach(Event.UI.Input.Key.Up,
			function(handle, hEvent, key)
				local subject = sendTab.SubjectField:GetText()
				if subject == (nil or "") or (string.find(subject, "platinum") and string.find(subject, "gold")) then
					sendTab.SubjectField:SetText((tonumber(sendTab.SentMoneyPlat:GetText()) or 0) .. " platinum " .. (tonumber(sendTab.SentMoneyGold:GetText()) or 0) .. " gold " .. (tonumber(sendTab.SentMoneySilver:GetText()) or 0) .. " silver")
				end
				EM.Utility.SendBtnCheck()
			end, "SendSentMoneyGold_KeyUp")
		sendTab.SentMoneyGold:EventAttach(Event.UI.Input.Key.Focus.Loss,
			function()
				if sendTab:GetVisible() == false then
					sendTab.SentMoneyGold:SetText("")
				else
					EM.Utility.SendBtnCheck()
				end
			end, "SendSentMoneyGold_KeyFocusLoss")

	sendTab.SentMoneyPlat = UI.CreateFrame("RiftTextfield", "SendSentMoneyPlat", sendDisplay)
		sendTab.SentMoneyPlat:SetPoint("TOPRIGHT", sendTab.YourMoneyPlat, "TOPRIGHT", 208, 1)
		sendTab.SentMoneyPlat:SetHeight(19)
		sendTab.SentMoneyPlat:SetWidth(65)
		sendTab.SentMoneyPlat:EventAttach(Event.UI.Input.Key.Down,
			function(handle, hEvent, key)
				if key == "Tab" then
					sendTab.SentMoneyGold:SetKeyFocus(true)
				end
			end, "SendSentMoneyPlat_KeyDown")
		sendTab.SentMoneyPlat:EventAttach(Event.UI.Input.Key.Up,
			function(handle, hEvent, key)
				local subject = sendTab.SubjectField:GetText()
				if subject == (nil or "") or (string.find(subject, "platinum") and string.find(subject, "gold")) then
					sendTab.SubjectField:SetText((tonumber(sendTab.SentMoneyPlat:GetText()) or 0) .. " platinum " .. (tonumber(sendTab.SentMoneyGold:GetText()) or 0) .. " gold " .. (tonumber(sendTab.SentMoneySilver:GetText()) or 0) .. " silver")
				end
				EM.Utility.SendBtnCheck()
			end, "SendSentMoneyPlat_KeyUp")
		sendTab.SentMoneyPlat:EventAttach(Event.UI.Input.Key.Focus.Loss,
			function()
				if sendTab:GetVisible() == false then
					sendTab.SentMoneyPlat:SetText("")
				else
					EM.Utility.SendBtnCheck()
				end
			end, "SendSentMoneyPlat_KeyFocusLoss")

	sendTab.radioSelected = "Money"

	sendTab.SendMoneyRadio2 = UI.CreateFrame("Frame", "SendMoneyRadio2", sendDisplay)
		sendTab.SendMoneyRadio2:SetPoint("BOTTOMLEFT", sendTab.SentMoneyText, "TOPLEFT", 0, -3)
		sendTab.SendMoneyRadio2:SetHeight(19)
		sendTab.SendMoneyRadio2:SetWidth(84)
		sendTab.SendMoneyRadio2.Name = "Money"
		sendTab.SendMoneyRadio2:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if sendTab.radioSelected == "CoD" then
				sendTab.SendMoneyRadio2.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				sendTab.CoDRadio2.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				sendTab.SentMoneyText:SetText("Amount to Send")
				sendTab.SentMoneySilver:SetText("")
				sendTab.SentMoneyGold:SetText("")
				sendTab.SentMoneyPlat:SetText("")
				sendTab.radioSelected = "Money"
			end
		end, "LeftClick")

		sendTab.SendMoneyRadio2.Texture = UI.CreateFrame("Texture", "SendMoneyRadio2_Texture", sendTab.SendMoneyRadio2)
			sendTab.SendMoneyRadio2.Texture:SetPoint("CENTERLEFT", sendTab.SendMoneyRadio2, "CENTERLEFT")
			sendTab.SendMoneyRadio2.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")

		sendTab.SendMoneyRadio2.Text = UI.CreateFrame("Text", "SendMoneyRadio2_Text", sendTab.SendMoneyRadio2)
			sendTab.SendMoneyRadio2.Text:SetPoint("CENTERLEFT", sendTab.SendMoneyRadio2.Texture, "CENTERRIGHT")
			sendTab.SendMoneyRadio2.Text:SetText("Send Money")

	sendTab.CoDRadio2 = UI.CreateFrame("Frame", "CoDRadio2", sendDisplay)
		sendTab.CoDRadio2:SetPoint("TOPLEFT", sendTab.SendMoneyRadio2, "TOPRIGHT", 15, 0)
		sendTab.CoDRadio2:SetHeight(19)
		sendTab.CoDRadio2:SetWidth(84)
		sendTab.CoDRadio2.Name = "CoD"
		sendTab.CoDRadio2:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if sendTab.radioSelected == "Money" then
				sendTab.SendMoneyRadio2.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				sendTab.CoDRadio2.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				sendTab.SentMoneyText:SetText("Cash on Delivery")
				sendTab.SentMoneySilver:SetText("")
				sendTab.SentMoneyGold:SetText("")
				sendTab.SentMoneyPlat:SetText("")
				EM.Utility.Popup(1, "Only 6 attachments are allowed for C.O.D. mail")
				if #EM.UI.Send.Attachments == 0 then
					EM.UI.Send.btnSend:SetEnabled(false)
				end
				if #EM.UI.Send.Attachments > 6 then
					local attachments = {}
					for idx = 1,#EM.UI.Send.Attachments,1 do
						if idx <= 6 then
							table.insert(attachments, EM.UI.Send.Attachments[idx])
						else
							if EM.UI.Send.Attachments[idx][1].slot then
								local bag = EM.UI.Send.Attachments[idx][1].slot[1]
								local slot = EM.UI.Send.Attachments[idx][1].slot[2]
								EM.UI.Inventory.Bags[bag][slot]:SetBackgroundColor(0,0,0,0)
								EM.UI.Inventory.Bags[bag][slot].attached = false
							end
						end
					end
					EM.UI.Send.Attachments = attachments
					EM.Send.Fill()
				end
				sendTab.radioSelected = "CoD"
			end
		end, "LeftClick")

		sendTab.CoDRadio2.Texture = UI.CreateFrame("Texture", "CoDRadio2_Texture", sendTab.CoDRadio2)
			sendTab.CoDRadio2.Texture:SetPoint("CENTERLEFT", sendTab.CoDRadio2, "CENTERLEFT")
			sendTab.CoDRadio2.Texture:SetTexture(AddonId, "gfx/radiobutton.png")

		sendTab.CoDRadio2.Text = UI.CreateFrame("Text", "CoDRadio2_Text", sendTab.CoDRadio2)
			sendTab.CoDRadio2.Text:SetPoint("CENTERLEFT", sendTab.CoDRadio2.Texture, "CENTERRIGHT")
			sendTab.CoDRadio2.Text:SetText("C.O.D.")

	sendTab.btnSend = UI.CreateFrame("RiftButton", "Sendbtn", sendTab)
		sendTab.btnSend:SetText("Send")
		sendTab.btnSend:SetPoint("BOTTOMCENTER", sendTab, "BOTTOMCENTER", 0, 2)
		sendTab.btnSend:SetEnabled(false)
		sendTab.btnSend:SetLayer(25)
		sendTab.btnSend:EventAttach(Event.UI.Button.Left.Press,
			function()
				EM.UI.Send.Recipients = {}
				if string.find(sendTab.ToField:GetText(), "; ") then
					for word in string.gmatch(sendTab.ToField:GetText(), "%a+") do
						table.insert(EM.UI.Send.Recipients, word)
					end
				else
					table.insert(EM.UI.Send.Recipients, sendTab.ToField:GetText())
				end
				EM.Event.Trigger.SendMessage()
			end, "Sendbtn_LeftPress")

	sendTab.SentTime = UI.CreateFrame("Frame", "SentTime", sendDisplay)
		sendTab.SentTime:SetPoint("TOPCENTER", EM.UI.MailWindow, "BOTTOMCENTER", 0, -5)
		sendTab.SentTime:SetHeight(25)
		sendTab.SentTime:SetWidth(300)
		sendTab.SentTime:SetBackgroundColor(0,0,0,1)

		sendTab.SentTime.Text = UI.CreateFrame("Text", "SentTime_Text", sendTab.SentTime)
			sendTab.SentTime.Text:SetPoint("CENTERLEFT", sendTab.SentTime, "CENTERLEFT", 5, 0)
			sendTab.SentTime.Text:SetText("Time since last sent message (10 minute max):")

		sendTab.SentTime.Time = UI.CreateFrame("Text", "SentTime_Time", sendTab.SentTime)
			sendTab.SentTime.Time:SetPoint("CENTERRIGHT", sendTab.SentTime, "CENTERRIGHT", -5, 0)
			sendTab.SentTime.Time:SetText("10:00")


	if EM.Settings and EM.Settings.Contacts and EM.Settings.Contacts.AutoOpen.status then
		if not EM.UI.Contacts.Window then
			EM.UI.Contacts.Create()
		else
			EM.UI.Contacts.Window:SetVisible(true)
			EM.UI.Contacts.Populate()
		end
	end

	EM.UI.Send.AttachGrid = sendTab.AttachGrid

	EM.Created.Send = true

end

function EM.Send.Event.SystemUpdate(hEvent)
	if EM.Created.Send and EM.UI.Send:GetVisible() then
		local serverTime = Inspect.Time.Server()
		if serverTime > EM.Send.Time then
			EM.Send.Time = serverTime
			if EM.NewDataTable.Sent and EM.NewDataTable.Sent[1] and EM.NewDataTable.Sent[1].timesentUnix then
				local lastSentTime = EM.NewDataTable.Sent[1].timesentUnix
				if serverTime < (lastSentTime + 600) then
					EM.UI.Send.SentTime:SetVisible(true)
					local diff = serverTime - lastSentTime
					EM.UI.Send.SentTime.Time:SetText(os.date("%M:%S", diff))
				else
					EM.UI.Send.SentTime:SetVisible(false)
				end
			else
				EM.UI.Send.SentTime:SetVisible(false)
			end
		end
	end
end

function EM.Send.MessageKeyDown(handle, hEvent, key)
	if key then
		local text = handle:GetText()
		local pos = handle:GetCursor()
		local start, last = handle:GetSelection()
		local prefix, suffix
		if start then
			prefix = string.sub(text, 1, start)
			suffix = string.sub(text, last+1)
			pos = start
		else
			prefix = string.sub(text, 1, pos)
			suffix = string.sub(text, pos+1)
		end
		if key == "Return" then
			local newText = prefix .."\n".. suffix
			handle:SetText(newText)
			handle:SetCursor(pos+1)
		elseif key == "Tab" then
			local newText = prefix .."\t".. suffix
			handle:SetText(newText)
			handle:SetCursor(pos+1)
		end
	end
end

function EM.Send.Fill(start)

	local sendTab = EM.UI.Send
	local settingsTab = EM.UI.Settings

	if not start or start == nil then
		start = EM.UI.Send.AttachScrollBar:GetPosition()-1
	end

	for i = 1,4,1 do
		local value = start+i
		if EM.UI.Send.Attachments[value] then
			sendTab.Grid.Body[i].icon.texture:SetVisible(true)
			sendTab.Grid.Body[i].icon.texture:SetTexture("Rift", EM.UI.Send.Attachments[value][1].value)
			sendTab.Grid.Body[i].name.text:SetText(EM.UI.Send.Attachments[value][2].value)
			sendTab.Grid.Body[i].count.text:SetText(tostring(EM.UI.Send.Attachments[value][3].value))
			sendTab.Grid.Body[i].displayed = value
		else
			sendTab.Grid.Body[i].icon.texture:SetVisible(false)
			sendTab.Grid.Body[i].name.text:SetText("")
			sendTab.Grid.Body[i].count.text:SetText("")
		end
	end
	if (#EM.UI.Send.Attachments) > 4 then
		EM.UI.Send.AttachScrollBar:SetRange(1, (#EM.UI.Send.Attachments-3))
		EM.UI.Send.AttachScrollBar:SetEnabled(true)
	else
		EM.UI.Send.AttachScrollBar:SetEnabled(false)
	end
	if EM.UI.Send.AttachScrollBar:GetPosition() ~= (start + 1) then
		local min, max = EM.UI.Send.AttachScrollBar:GetRange()
		if (start + 1) > max then
			EM.UI.Send.AttachScrollBar:SetPosition(max)
		else
			EM.UI.Send.AttachScrollBar:SetPosition(start + 1)
		end
	end

	local coin = 0
	coin = (#EM.UI.Send.Attachments * 50) + (math.ceil(#EM.UI.Send.Attachments/6) * 25)
	local plat,gold,silver = EM.Utility.Coin(coin)
	if tonumber(silver) == 0 then
		sendTab.CostSilver:SetText("25")
	else
		sendTab.CostSilver:SetText(silver)
	end
	if tonumber(gold) > 0 then
		sendTab.CostGold:SetText(gold)
		sendTab.CostGold:SetVisible(true)
		sendTab.CostGoldIcon:SetVisible(true)
	else
		sendTab.CostGold:SetVisible(false)
		sendTab.CostGoldIcon:SetVisible(false)
	end
	if tonumber(plat) > 0 then
		sendTab.CostPlat:SetText(plat)
		sendTab.CostPlat:SetVisible(true)
		sendTab.CostPlatIcon:SetVisible(true)
	else
		sendTab.CostPlat:SetVisible(false)
		sendTab.CostPlatIcon:SetVisible(false)
	end
end

function EM.UI.SendAttach(hEvent)

	if EM.Temp.PopupTest then
		EM.UI.SendMessage()
		EM.Temp.PopupTest = false
	elseif EM.Temp.PopupTest == nil then
		EM.Temp.PopupTest = false
	else
		EM.Utility.Popup(2, "Send next set of attachments?", EM.UI.SendAttach)
	end

end

function EM.UI.SendMultiRecipients(hEvent)

	if EM.Temp.PopupTest then
		--EM.UI.SendMessage()
		EM.Event.Trigger.SendMessage()
		EM.Temp.PopupTest = false
	elseif EM.Temp.PopupTest == nil then
		EM.Temp.PopupTest = false
	else
		EM.Utility.Popup(2, "Send message to " .. EM.UI.Send.Recipients[1] .. "?", EM.UI.SendMultiRecipients)
	end

end

function EM.UI.Send.UpdateCurrency(hEvent, currencies)
	if currencies.coin and (EM.UI.MailWindow and EM.UI.MailWindow:GetVisible() and EM.UI.Send:GetVisible()) then
		local plat,gold,silver = EM.Utility.Coin(currencies.coin)
		EM.UI.Send.YourMoneyPlat:SetText(plat)
		EM.UI.Send.YourMoneyGold:SetText(gold)
		EM.UI.Send.YourMoneySilver:SetText(silver)
	end
end

function EM.Send.RightClick(handle)

	local sendTab = EM.UI.Send

	local realRow = handle.displayed

	if not EM.UI.Send.Attachments[realRow] then return end

	if EM.UI.Send.Attachments[realRow][1].slot then
		local bag = EM.UI.Send.Attachments[realRow][1].slot[1]
		local slot = EM.UI.Send.Attachments[realRow][1].slot[2]
		EM.UI.Inventory.Bags[bag][slot]:SetBackgroundColor(0,0,0,0)
		EM.UI.Inventory.Bags[bag][slot].attached = false
	end

	table.remove(EM.UI.Send.Attachments, realRow)

	if sendTab.radioSelected == "CoD" and #EM.UI.Send.Attachments == 0 then
		sendTab.btnSend:SetEnabled(false)
	end

	EM.Send.Fill()
end

EM.Temp.mail = {}

function EM.UI.SendMessage(hEvent)
	dump(hEvent)
	local codValue = nil
	local coinValue = nil

	for child, st in pairs(EM.Context.Inventory:GetChildren()) do
		for child2, st2 in pairs(child:GetChildren()) do
			child2:SetBackgroundColor(0,0,0,0)
			child2.attached = false
		end
	end

	local attachment = EM.Utility.AttachSetup()

	local sendTab = EM.UI.Send

	local tofield,multipleRecipients = EM.Utility.RecipientSetup()

	if multipleRecipients then
			EM.Temp.mail = { 	to = tofield, subject = sendTab.SubjectField:GetText(),
								body = sendTab.MessageField:GetText() }
			if attachment then
				EM.Utility.ClearAttachments()
				print("Cannot send Attachments to Multiple Recipients.")
			end
	else
		if sendTab.radioSelected == "Money" then
			coinValue = ((tonumber(sendTab.SentMoneyPlat:GetText()) or 0) * 10000) + ((tonumber(sendTab.SentMoneyGold:GetText()) or 0) * 100) + (tonumber(sendTab.SentMoneySilver:GetText()) or 0)
			EM.Temp.mail = { 	to = tofield, subject = sendTab.SubjectField:GetText(),
								body = sendTab.MessageField:GetText(), coin = coinValue, attachments = attachment }
			EM.UI.Send.SentMoneySilver:SetText("")
			EM.UI.Send.SentMoneyGold:SetText("")
			EM.UI.Send.SentMoneyPlat:SetText("")
		elseif sendTab.radioSelected == "CoD" then
			codValue = ((tonumber(sendTab.SentMoneyPlat:GetText()) or 0) * 10000) + ((tonumber(sendTab.SentMoneyGold:GetText()) or 0) * 100) + (tonumber(sendTab.SentMoneySilver:GetText()) or 0)
			EM.Temp.mail = { 	to = tofield, subject = sendTab.SubjectField:GetText(),
								body = sendTab.MessageField:GetText(), cod = codValue, attachments = attachment }
		end
	end
	Command.Mail.Send(EM.Temp.mail, EM.UI.Callback.Send)
end

function EM.UI.Callback.Send(status, message)
	if EasyMailTable[EM.PlayerName].SentCount then
		EasyMailTable[EM.PlayerName].SentCount = EasyMailTable[EM.PlayerName].SentCount + 1
		repeat
			EasyMailTable[EM.PlayerName].SentCount = "0" .. EasyMailTable[EM.PlayerName].SentCount
		until string.len(EasyMailTable[EM.PlayerName].SentCount) == 12
	else
		EasyMailTable[EM.PlayerName].SentCount = "000000000001"
	end
	if not status then
		local sentMail = EM.Temp.mail
		local id = "m454d" .. EasyMailTable[EM.PlayerName].SentCount
		local sendtime = os.date("%x %X", Inspect.Time.Server())
		sentMail["timesent"] = sendtime
		sentMail["timesentUnix"] = Inspect.Time.Server()
		sentMail["id"] = id
		if sentMail["attachments"] then
			sentMail["attachmentCount"] = #sentMail["attachments"]
			sentMail["attachmentsType"] = EM.Temp.attachmentsType
		end
		EasyMailTable[EM.PlayerName].Sent[id] = sentMail
		EM.Utility.CreateNewDataTable()
		if EM.UI.Archive.Grid then
			EM.Archive.Populate(0)
		end
	else
		if message then
			print(message)
		end
	end
	if #EM.UI.Send.Attachments > 0 then
		EM.Event.Trigger.SendAttach()
	elseif #EM.UI.Send.Recipients > 0 then
		EM.Event.Trigger.SendMultiRecipients()
	else
		EM.Utility.ClearMessage()
	end
end

Command.Event.Attach(Event.Currency,				EM.UI.Send.UpdateCurrency, AddonId .. "_UpdateCurrency" )
Command.Event.Attach(EM.Event.SendMessage,			EM.UI.SendMessage, AddonId .. "_SendMessage" )
Command.Event.Attach(EM.Event.SendAttach,			EM.UI.SendAttach, AddonId .. "_SendAttach" )
Command.Event.Attach(EM.Event.SendMultiRecipients,	EM.UI.SendMultiRecipients, AddonId .. "_SendMultiRecipients" )
Command.Event.Attach(Event.System.Update.Begin,		EM.Send.Event.SystemUpdate, AddonId .. "_SystemUpdateBegin" )