program["Browser"] = function ()
	local address = ""
	local w, h = window.getSize()
	local colorPick
	local colorCurrent
	local displayList
	local tab = 1
	local tabs
	local websites
	local buttons = {}
	
	local websiteURL = "website"
	local serverURL = "server"
	local cookieosURL = "cookieos"
	
	local color = {
		colors.white,
		colors.orange,
		colors.magenta,
		colors.lightBlue,
		colors.yellow,
		colors.lime,
		colors.pink,
		colors.gray,
		colors.lightGray,
		colors.cyan,
		colors.purple,
		colors.blue,
		colors.brown,
		colors.green,
		colors.red,
		colors.black,
		text = colors.black,
		textBack = colors.white
	}
	
	local customWeb = {
		back = colors.white
	}
	
	local function isOpen()
		for n,m in ipairs(rs.getSides()) do 
			if rednet.isOpen(m) then
				return true
			end 
		end
	end
	
	local function free(page)
		local timer = os.clock() + 1
		local event
		
		rednet.broadcast("BrowserAddress:" .. page)
			
		while timer >= os.clock() and event ~= "rednet_message" do
			event = os.pullEvent()
		end
		
		for k,v in pairs(fs.list("programs/Browser/websites")) do
			if v == page then
				return false
			end
		end
		
		if event ~= "rednet_message" and page ~= "CookieOS" and page ~= "Server" and page ~= "Website" and #page > 0 and #page < 30 then
			return true
		end
	end
	
	local function display()
		window.setTextColor(userColor.text)
		window.setBackgroundColor(userColor.window)
		window.setCursorPos(1, 1)
		window.write("  ")
		window.setBackgroundColor(userColor.back)
		window.write(">" .. address .. string.rep(" ", w - 5 - #address))
		window.setBackgroundColor(userColor.window)
		window.write("  ")
		window.setCursorPos(1, 2)
		window.write(string.rep(" ", w))
		
		if address == websiteURL then
			window.setBackgroundColor(userColor.windowDark)
			window.setTextColor(userColor.windowText)
			window.setCursorPos(1,2)
			window.write("text:")
			window.setBackgroundColor(color.text)
			window.write("  ")
			window.setBackgroundColor(userColor.window)
			window.write(" ")
			window.setBackgroundColor(userColor.windowDark)
			window.write("back:")
			window.setBackgroundColor(color.textBack)
			window.write("  ")
			window.setBackgroundColor(userColor.window)
			window.write(" ")
			window.setBackgroundColor(userColor.windowDark)
			window.write("background:")
			window.setBackgroundColor(customWeb.back)
			window.write("  ")
			window.setBackgroundColor(userColor.window)
			window.write(" ")
			
			if colorPick then
				for i = 1,#color do
					window.setBackgroundColor(color[i])
					window.write(" ")
				end
			end
			
			window.setCursorPos(w - 3, 2)
			window.setBackgroundColor(userColor.windowDark)
			window.write("Save")
		end
	end
	
	local function displayWebpage()
		window.setBackgroundColor(userColor.back)
		window.setTextColor(userColor.text)
		window.clear()
		
		if address ~= websiteURL then
			customWeb = {
				back = colors.white
			}
			
			color.text = colors.black
			color.textBack = colors.white
			colorPick = nil
			colorCurrent = nil
		end
		
		if address ~= serverURL then
			displayList = false
		end
		
		if address == cookieosURL then
			window.setCursorPos(5, 5)
			window.write("Welcome to CookieOS")
		elseif address == serverURL then
			if not displayList then
				window.setCursorPos(1, 4)
				window.write("If you want to set up a server with websites you")
				window.setCursorPos(1, 5)
				window.write("have to set up a server computer (not your main")
				window.setCursorPos(1, 6)
				window.write("computer).")
				window.setCursorPos(1, 7)
				window.write("Install CookieOS on that computer and type Website")
				window.setCursorPos(1, 8)
				window.write("in the browser.")
				window.setCursorPos(1, 9)
				window.write("When you are done with your websites")
				window.setCursorPos(1, 10)
				window.write("press Start.")
				
				window.setCursorPos(3, 15)
				window.setBackgroundColor(userColor.windowDark)
				window.setTextColor(userColor.windowText)
				window.write(" Start ")
				
				window.setCursorPos(20, 15)
				window.write(" Delete Website ")
			else
				window.setBackgroundColor(userColor.windowDark)
				window.setTextColor(userColor.windowText)
				
				websites = fs.list("programs/Browser/websites")
				tabs = math.ceil(#websites / 6)
				local y = 4
				
				for i = (tab - 1) * 6 + 1,math.min(#websites, tab * 6) do
					window.setCursorPos(2, y)
					window.write(websites[i])
					y = y + 2
				end
				
				if tab > 1 then
					window.setCursorPos(2, 16)
					window.write(" < ")
				end
				
				if tab < tabs then
					window.setCursorPos(6, 16)
					window.write(" > ")
				end
				
				window.setCursorPos(12, 16)
				window.write("back")
			end
			
		elseif address == websiteURL then
			window.setBackgroundColor(customWeb.back)
			window.clear()
			
			for i = 1,#customWeb do
				window.setBackgroundColor(customWeb[i][5])
				window.setTextColor(customWeb[i][4])
				window.setCursorPos(customWeb[i][2], customWeb[i][3])
				window.write(customWeb[i][1])
			end
		else
			local timer = os.clock() + 1
			local event, id, message
			buttons = {}
			
			display()
			
			rednet.broadcast("BrowserAddress:" .. address)
			
			while timer >= os.clock() and event ~= "rednet_message" do
				event, id, message = os.pullEvent()
			end
			
			if event == "rednet_message" then
				window.setBackgroundColor(message.back)
				window.clear()
				display()
				
				for i = 1,#message do
					window.setBackgroundColor(message[i][5])
					window.setTextColor(message[i][4])
					window.setCursorPos(message[i][2], message[i][3])
					
					local text1, mode, id, msg, text2 = string.match(message[i][1], "(.*)%<(%w+)%s+(%d+)%s+(%w+)%>(.*)")
					
					if mode == "send" then
						buttons[#buttons + 1] = {x = message[i][2], y = message[i][3], id = tonumber(id), msg = msg, text = text1 .. text2}
						window.write(text1 .. text2)
					elseif mode == "get" then
						rednet.send(tonumber(id), msg)
						local t = os.clock() + 1
						local text = ""
						
						while os.clock() < t do
							local event, id, msg = os.pullEvent()
							
							if event == "rednet_message" then
								text = tostring(msg)
								break
							end
						end
						
						window.write(text1 .. text .. text2)
					else
						window.write(message[i][1])
					end
				end
			else
				window.setBackgroundColor(colors.yellow)
				window.clear()
				display()
			
				window.setCursorPos(2, 4)
				window.setBackgroundColor(colors.yellow)
				window.setTextColor(colors.black)
				window.write("Yellow Screen of Death")
				
				window.setCursorPos(2, 6)
				window.write("Can't connect to: " .. address)
				
				if isOpen() then
					window.setCursorPos(2, 8)
					window.write("The website doesn't exist or")
					window.setCursorPos(2, 9)
					window.write("the server is inactive")
				else
					window.setCursorPos(2, 8)
					window.write("Your modem is turned off or")
					window.setCursorPos(2, 9)
					window.write("you don't have a modem")
				end
			end
		end
	end
	
	while true do
		display()
		
		local event, p1, p2, p3 = os.pullEvent()
		
		if event == "mouse_click" then
			p3 = p3 - 1
			
			if p2 >= 3 and p2 <= w - 2 and p3 == 1 then
				address = ""
				window.setBackgroundColor(userColor.window)
				window.setTextColor(userColor.text)
				window.setCursorPos(1, 1)
				window.write("  ")
				window.setBackgroundColor(userColor.back)
				window.write(">" .. string.rep(" ", w - 5))
				window.setCursorPos(4, 1)
				window.setBackgroundColor(userColor.back)
				window.setTextColor(userColor.text)
				address = string.lower(io.read())
				
				if address ~= "" then
					displayWebpage()
				end
			elseif address == serverURL then
				if not displayList then
					if p2 >= 3 and p2 <= 9 and p3 == 15 then
						fs.copy("version", "programs/Browser/host")
						os.reboot()
					elseif p2 >= 20 and p2 <= 35 and p3 == 15 then
						displayList = true
						displayWebpage()
					end
				else
					local y = 4
				
					for i = (tab - 1) * 6 + 1,math.min(#websites, tab * 6) do
						if p2 >= 2 and p2 < 2 + #websites[i] and p3 == y then
							fs.delete("programs/Browser/websites/" .. websites[i])
						end
						
						y = y + 2
					end
					
					if tab > 1 and p2 >= 2 and p2 <= 4 and p3 == 16 then
						tab = tab - 1
					end
					
					if tab < tabs and p2 >= 6 and p2 <= 8 and p3 == 16 then
						tab = tab + 1
					end
					
					if p2 >= 12 and p2 < 16 and p3 == 16 then
						displayList = false
					end
					
					displayWebpage()
				end
			elseif address == websiteURL then
				if colorPick then
					for i = 1,#color do
						if p3 == 2 and p2 == 30 + i then
							if colorCurrent == "back" then
								customWeb.back = color[i]
							else
								color[colorCurrent] = color[i]
							end
						end
					end
					
					colorPick = false
				else
					if p1 == 2 then
						for i = #customWeb,1,-1 do
							if p2 >= customWeb[i][2] and p2 <= customWeb[i][2] + #customWeb[i][1] - 1 and p3 == customWeb[i][3] then
								table.remove(customWeb, i)
								break
							end
						end
					elseif p2 >= 6 and p2 <= 7 and p3 == 2 then
						colorPick = true
						colorCurrent = "text"
					elseif p2 >= 14 and p2 <= 15 and p3 == 2 then
						colorPick = true
						colorCurrent = "textBack"
					elseif p2 >= 28 and p2 <= 29 and p3 == 2 then
						colorPick = true
						colorCurrent = "back"
					elseif p3 > 2 then
						window.setBackgroundColor(color.textBack)
						window.setTextColor(color.text)
						window.setCursorPos(p2, p3)
						local text = io.read()
						
						if #text > 0 then
							customWeb[#customWeb + 1] = {text, p2, p3, color.text, color.textBack}
						end
					elseif p2 >= w - 3 and p2 <= w and p3 == 2 then
						window.setBackgroundColor(userColor.window)
						window.setCursorPos(1,3)
						window.write(string.rep(" ", w))
						window.setBackgroundColor(userColor.windowDark)
						window.setTextColor(userColor.windowText)
						window.setCursorPos(1,4)
						window.write("Address:")
						window.setBackgroundColor(userColor.back)
						window.write(string.rep(" ", w - 10))
						window.setBackgroundColor(userColor.window)
						window.write("  ")
						window.setCursorPos(1,5)
						window.write(string.rep(" ", w))
						
						local page
						
						repeat
							if page ~= nil then
								window.setBackgroundColor(userColor.window)
								window.setCursorPos(1,5)
								window.write(string.rep(" ", w))
								window.setCursorPos(9, 5)
								window.setBackgroundColor(colors.red)
								window.setTextColor(colors.white)
								window.write("Invalid Address")
							end
							
							window.setCursorPos(9,4)
							window.setBackgroundColor(userColor.back)
							window.write(string.rep(" ", w - 10))
							window.setBackgroundColor(userColor.back)
							window.setTextColor(userColor.text)
							window.setCursorPos(9, 4)
							page = string.lower(io.read())
						until free(page)
						
						if fs.exists("programs/Browser/websites/" .. page) then
							fs.delete("programs/Browser/websites/" .. page)
						end
						
						local file = io.open("programs/Browser/websites/" .. page, "w")
						file:write("WebInfo = " .. textutils.serialize(customWeb))
						file:close()
					end
				end
				
				displayWebpage()
			else
				for i = 1,#buttons do
					if buttons[i].x <= p2 and buttons[i].x + #buttons[i].text > p2 and p3 == buttons[i].y then
						rednet.send(buttons[i].id, buttons[i].msg)
					end
				end
			end
		end
	end
end