local p = {}
local getArgs = require('Module:Arguments').getArgs
local util = require("Module:Road data/util")
local yesno = require('Module:Yesno')
local routeModule = require("Module:Infobox road/route")
local parserModule = require("Module:Road data/parser")
local parser = parserModule.parser
local format = mw.ustring.format
local frame = mw.getCurrentFrame()

local function country(args)
	local state = args.state or args.province
	local country
	local countryModule = mw.loadData("Module:Road data/countrymask")
	local country = args.country or countryModule[state]
	
	return country
end

local function the(args)
	if country(args) == "ALA" or country(args) == "BHS" or country(args) == "CAF" or country(args) == "COG" or country(args) == "COD" or country(args) == "COM" or country(args) == "CZE" or country(args) == "DOM" or country(args) == "FSM" or country(args) == "GMB" or country(args) == "IMN" or country(args) == "MDV" or country(args) == "MHL" or country(args) == "NLD" or country(args) == "PHL" or country(args) == "SLB" or country(args) == "ARE" or country(args) == "GBR" or country(args) == "USA" then
		return 'the'
	else
		return ''
	end
end

local function africa(args)
	return "*'''[[Trans-African Highway network]]'''"
end

local function asia(args)
	return "*'''[[Asian Highway Network]]'''"
end

local function europe(args)
	if args.type == "Pan" then
		return "*'''[[Pan-European corridors]]'''"
	else
		return "*'''[[International E-road network]]'''\n\n*[[List of A Class E-roads|A Class]]\n*[[List of B Class E-roads|B Class]]"
	end
end

local function browselinks(args)
	local ISO = require('Module:ISO 3166')
	local name = ISO.name({country(args)})
	
	if country(args) == '' or country(args) == 'YUG' or country(args) == nil then
		return nil
		
	elseif country(args) == 'ASIA' then
		return asia(args)
		
	elseif country(args) == 'EUR' then
		return europe(args)
		
	elseif country(args) == 'TAH' then
		return africa(args)
		
	elseif country(args) == 'ARE' then
		return string.format("*'''[[Transport in %s %s]]'''\n\n*[[List of roads in Dubai|Roads in Dubai]]", the(args), name)
		
	elseif country(args) == 'AUS' then
		local state = args.state or ''
		local stateName = ISO.name({"AUS-" .. state})
		local links = "*'''[[Highways in Australia]]'''\n\n*[[National Highway (Australia)|National Highway]]\n*[[Freeways in Australia]]"
			if state == 'ACT' then
				return links .. "\n\n'''[[Road infrastructure in Canberra]]'''"
			elseif state == 'NSW' then
				return links .. "\n\n'''[[Highways in New South Wales]]'''"
			elseif state ~= '' then
				return string.format("%s \n\n'''[[List of highways in %s|Highways in %s]]'''", links, stateName, stateName)
			else
				return links
			end
	
	elseif country(args) == 'AUT' then
		return "*'''Highways of Austria'''\n\n*[[Autobahns of Austria|Autobahns]]\n*[[Autobahns of Austria#Schnellstraßen|Expressways]]\n*[[Landesstraße|State Roads]]"
		
	elseif country(args) == 'BEL' then
		return "*'''Highways of Belgium'''\n\n*[[List of motorways in Belgium|Motorways]]\n*[[List of National Roads in Belgium|National Roads]]"
		
	elseif country(args) == 'BOL' then
		return "*'''Highways of Bolivia'''\n\n*[[List of National Roads in Bolivia|National Roads]]"
		
	elseif country(args) == 'BRA' then
		local state = args.state or ''
		local stateName = ISO.name({"BRA-" .. state})
		local links = "*'''[[List of highways in Brazil|Highways in Brazil]]'''\n\n*[[List of federal highways in Brazil|Federal]]"
			if state == 'RS' or state == 'SP' or state == 'MG' then
				return string.format("%s \n\n*[[List of state highways in %s|%s State Highways]]", links, stateName, stateName)
			else
				return links
			end
		
	elseif country(args) == 'BRN' then
		return "*'''[[Brunei National Roads System]]'''"
		
	elseif country(args) == 'CAN' then	
		local province = args.province or ''
		local stateName = ISO.name({"CAN-" .. province})
		
		local TCH
			if args.type == 'TCH' and province == '' then
				TCH = "*'''[[Trans-Canada Highway]]'''\n\n*[[National Highway System (Canada)|National Highway System]]"
			elseif args.type == 'TCH' and province ~= '' then
				TCH = "*'''[[Trans-Canada Highway]]'''"
			else
				TCH = ''
			end
			
		local provinces
			if province == '' then
				provinces = ''
			elseif province == 'AB' then
				provinces = "'''[[Alberta Provincial Highway Network]]'''\n\n*[[List of Alberta provincial highways|List]]\n*[[List of former Alberta provincial highways|Former]]"
			elseif province == 'NT' then
				provinces = "'''[[List of Northwest Territories highways|Northwest Territories highways]]'''"
			elseif province == 'YT' or province == 'YK' then
				provinces = "'''[[List of Yukon territorial highways|Territorial highways in Yukon]]'''"
			elseif province == 'NU' then
				provinces = "'''[[Highways in Nunavut]]'''"
			elseif province == 'BC' then
				provinces = "'''[[List of British Columbia provincial highways|British Columbia provincial highways]]'''"
			elseif province == 'NL' then
				provinces = "'''[[List of Newfoundland and Labrador highways|Highways in Newfoundland and Labrador]]'''"
			elseif province == 'QC' then
				provinces = "'''[[Highways in Quebec|Quebec provincial highways]]'''"
			elseif province == 'ON' then
				if args.type == 'Fwy' or args.type == 'Hwy' or args.type == 'ON' or args.type == 'ON 1960' or args.type == 'Secondary' or args.type == 'Sec' or args.type == 'TCH' then
					provinces = "'''[[Ontario Provincial Highway Network|Ontario provincial highways]]'''"
				elseif args.type == 'CR' or args.type == 'DR' or args.type == 'MR' or args.type == 'RR' then
					provinces = "'''[[List of county roads in Ontario|County roads in Ontario]]'''"
				elseif args.type == 'Parkway' then
					provinces = "'''Parkways of the Great Lakes'''"
				elseif args.type == 'Expressway' then
					provinces = "'''[[List of controlled-access highways in Ontario#Municipal_expressways|Ontario municipal expressways]]'''"
				elseif args.type == 'Toronto' then
					provinces = "'''[[Lists of roads in Toronto|Toronto municipal expressways]]'''"
				elseif args.type == 'NCC' then
					provinces = "'''[[National Capital Commission#Ottawa_parkways|NCC parkways in Ottawa]]'''"
				else
					provinces = "'''[[Roads in Ontario]]'''"
				end
			else
				provinces = string.format("'''[[List of %s provincial highways|Provincial highways in %s]]'''", stateName, stateName)
			end
				
		local second
			if province == 'YT' or province == 'YK' then
				second = "[[Miscellaneous Yukon roads|Miscellaneous]]"
			elseif province == 'MB' then
				second = "[[List of Winnipeg City Routes|Winnipeg City Routes]]"	
			elseif province == 'NB' then
				second = "[[List of former New Brunswick provincial highways|Former routes]]"
			elseif province == 'NS' then
				second = "[[100-series highways (Nova Scotia)|100-series]]"
			elseif province == 'QC' then
				second = "[[Autoroutes of Quebec|Autoroutes]]\n*[[List of Quebec provincial highways|List]]\n*[[List of former Quebec provincial highways|Former]]"
			elseif province == 'ON' then
				if args.type == 'Fwy' or args.type == 'Hwy' or args.type == 'ON' or args.type == 'ON 1960' or args.type == 'Secondary' or args.type == 'Sec' or args.type == 'TCH' then
					second = "[[List of Ontario provincial highways|Current]]\n*[[List of former Ontario provincial highways|Former]]\n*[[400-series highways|400-series]]"
				elseif args.type == 'Parkway' then
					second = "[[Long Sault Parkway|Long Sault]]\n*[[Niagara Parkway|Niagara]]\n*[[St. Clair Parkway|St. Clair]]\n*[[Thousand Islands Parkway|1000 Islands]]\n\n*'''[[Roads in Ontario]]'''"
				else
					second = ''
				end
			else
				second = ''
			end
		
		return string.format("%s\n\n*%s\n\n*%s", TCH, provinces, second)		

	elseif country(args) == 'CHE' then
		return "*'''[[Transport in Switzerland]]'''\n\n*[[Motorways of Switzerland|Motorways]]"
		
	elseif country(args) == 'CHN' then
		local province = args.province or ''
		local stateName = ISO.name({"CHN-" .. province})
		local links
			if args.type == "Ex" or args.type == "Expwy" then
				links = "*'''[[Expressways of China|National Trunk Highway System]]'''\n\n*[[List of primary NTHS Expressways|Primary]]\n*[[List of auxiliary NTHS Expressways|Auxiliary]]\n\n*[[China National Highways|National Highways]]\n*[[Transport in China]]"
			elseif args.type == "G" then
				links = "*'''[[Expressways of China|National Trunk Highway System]]'''\n\n*[[List of primary NTHS Expressways|Primary]]\n*[[List of auxiliary NTHS Expressways|Auxiliary]]"
			else
				links = "*'''[[Transport in China]]'''"
			end
			
			if province == 'BJ' or province == 'SH' or province == 'JS' or province == 'ZJ' or province == 'HA' or province == 'GD' then
				return string.format("%s \n\n*'''[[Expressways of %s]]'''", links, stateName)
			else
				return links
			end
		
	elseif country(args) == 'CRI' then
		return "*'''[[National Road Network of Costa Rica]]'''"
		
	elseif country(args) == 'CYP' then
		return "*'''[[Motorways and roads in Cyprus]]'''"
		
	elseif country(args) == 'DEU' then
		if args.type == "E" then
			return europe(args)
		else
			return "*'''Roads in Germany'''\n\n*[[Autobahn]]s\n**[[List of autobahns in Germany|List]]\n*[[Bundesstraße|Federal]]\n**[[List of federal highways in Germany|List]]\n\n*[[Landesstraße|State]]\n*[[List of E-roads in Germany|E-roads]]"
		end
		
	elseif country(args) == 'ESP' then
		local province = args.province or ''
		local stateName = ISO.name({"ESP-" .. province})
		local links
			if args.type == "E" then
				links = europe(args)
			else
				links = "*'''[[Highways in Spain]]'''\n\n*[[List of autopistas and autovías in Spain|''Autopistas'' and ''autovías'']]\n*[[List of national roads in Spain|National Roads]]"
			end
		
		local community
			if province ~= '' then
				if province == 'B' or province == 'GI' or province == 'L' or province == 'T' then
					community = "'''[[List of primary highways in Catalonia|Primary Highways in Catalonia]]'''"
				elseif province == 'GC' or province == 'TF' then
					community = "'''[[Transport in the Canary Islands]]'''"
				elseif province == 'ML' then
					community = "'''[[Transport in Melilla]]'''"
				elseif province == 'M' or province == 'MD' then
					community = "'''[[Transport_in_Madrid#Road|Transport in the Community of Madrid]]'''"
				else
					community = ''
				end
			else
				community = ''
			end
		
		return string.format("%s\n\n*%s", links, community)
		
	elseif country(args) == 'FIN' then
		if args.type == "E" then
			return europe(args)
		else
			return "*'''[[Highways in Finland]]'''"
		end
		
	elseif country(args) == 'FRA' then
		if args.type == "E" then
			return europe(args)
		else
			return "*'''Roads in France'''\n\n*[[Autoroutes of France|''Autoroutes'']]\n*[[Route nationale|''Routes nationales'']]"
		end
		
	elseif country(args) == 'GBR' or country(args) == 'ENG' or country(args) == 'SCT' or country(args) == 'WLS' then
		if args.type == "Roman" then
			return "*'''[[Roman roads in Britannia]]'''"
		else
			return "*'''[[Roads in the United Kingdom]]'''\n\n*[[List_of_motorways_in_the_United_Kingdom|Motorways]]\n*[[List of roads zones in Great Britain|A and B road zones]]"
		end
		
	elseif country(args) == 'GEO' then
		return "*'''Roads in Georgia'''\n\n*[[List of roads in Georgia (country)#Roads of international importance|International Routes]]\n*[[List of roads in Georgia (country)#Roads of domestic importance|National Routes]]"
		
	elseif country(args) == 'GHA' then
		return "*'''[[Ghana Road Network]]'''"
		
	elseif country(args) == 'GIB' then
		return "*'''[[Streets in Gibraltar]]'''"
		
	elseif country(args) == 'GRC' then
		return "*'''Highways in Greece'''\n\n*[[Motorways in Greece|Motorways]]\n*[[National Roads in Greece|National Roads]]"
		
	elseif country(args) == 'HKG' then
		return "*'''[[Transport in Hong Kong]]'''\n\n*[[Hong Kong Strategic Route and Exit Number System|Routes]]\n*[[List of streets and roads in Hong Kong|Roads and Streets]]"
		
	elseif country(args) == 'HUN' then
		return "*'''Roads in Hungary'''\n\n*[[Highways in Hungary|Highways]]\n*[[Roads in Hungary#Main roads|Main roads]]\n*[[Roads in Hungary#Local roads|Local roads]]"
		
	elseif country(args) == 'IND' then
		local state = args.state or ''
		local stateName = ISO.name({"IND-" .. state})
		local links = "*'''[[Roads in India]]'''\n\n*[[Expressways of India|Expressways]]\n*[[National highways of India|National]]\n*[[State highways in India|State]]\n*[[Asian Highway Network|Asian]]"
			if state ~= '' then
				return string.format("%s \n\n'''[[List of state highways in %s|State Highways in %s]]'''", links, stateName, stateName)
			else
				return links
			end
		
	elseif country(args) == 'IRL' then
		return "*'''[[Roads in Ireland]]'''\n\n*[[Motorways in the Republic of Ireland|Motorways]]\n*[[National primary road|Primary]]\n*[[National secondary road|Secondary]]\n*[[Regional road (Ireland)|Regional]]"
		
	elseif country(args) == 'IRN' then
		return "*'''[[List of highways in Iran|Highways in Iran]]'''\n\n*[[Freeways in Iran|Freeways]]"
		
	elseif country(args) == 'ISR' then
		return "*'''[[Roads in Israel]]'''\n\n*[[List of highways in Israel|Highways]]"
		
	elseif country(args) == 'ITA' then
		return "*'''[[Roads in Italy]]'''\n\n*[[Autostrade of Italy|''Autostrade'']]\n*[[State highway (Italy)|State]]\n*[[Regional road (Italy)|Regional]]\n*[[Provincial road (Italy)|Provincial]]\n*[[Municipal road (Italy)|Municipal]]"
		
	elseif country(args) == 'JPN' then
		return "*'''[[National highways of Japan]]'''\n\n*[[Expressways of Japan]]"
		
	elseif country(args) == 'KOR' then
		return "*'''[[Highway systems of South Korea]]'''\n\n*[[Expressways in South Korea|Expressways]]\n*[[National highways of South Korea|National]]\n*[[Local highways of South Korea|Local]]"
		
	elseif country(args) == 'KOS' then -- this is not an official ISO code
		return "*'''[[Roads in Kosovo]]'''"
		
	elseif country(args) == 'LKA' then
		return "*'''Roads in Sri Lanka'''\n\n*[[Expressways of Sri Lanka|Expressways]]\n*[[List of A-Grade highways in Sri Lanka|A-Grade]]\n*[[List of B-Grade highways in Sri Lanka|B-Grade]]"
		
	elseif country(args) == 'LUX' then
		return "*'''[[List of motorways in Luxembourg|Motorways in Luxembourg]]'''"
		
	elseif country(args) == 'LVA' then
		return "*'''[[List of National Roads in Latvia|National Roads in Latvia]]'''"
		
	elseif country(args) == 'MEX' then
		local state = args.state or ''
		local stateName = ISO.name({"MEX-" .. state})
		local links = "*'''[[Mexican Federal Highway]]s'''\n\n*[[List of Mexican Federal Highways|List]]\n*[[List of Mexican autopistas|''Autopistas'']]"
			if state == 'SON' then
				return string.format("%s \n\n'''[[List of highways in %s|State Highways in %s]]'''", links, stateName, stateName)
			elseif state == 'DUR' then
				return string.format("%s \n\n'''[[%s state highways|State Highways in %s]]'''", links, stateName, stateName)
			else
				return links
			end
		
	elseif country(args) == 'MWI' then
		return "*'''[[Transport in Malawi]]'''\n\n*[[List of roads in Malawi|Roads]]"
		
	elseif country(args) == 'MYS' then
		return "*'''[[List of expressways and highways in Malaysia|Highways in Malaysia]]'''\n\n*[[Malaysian Expressway System|Expressways]]\n*[[Malaysian Federal Roads System|Federal]]\n*[[Malaysian State Roads system|State]]"
		
	elseif country(args) == 'NIR' then
		return "*'''[[Roads in Northern Ireland]]'''\n\n*[[List_of_motorways_in_the_United_Kingdom#Northern_Ireland|Motorways]]\n*[[List of A roads in Northern Ireland|A roads in Northern Ireland]]"
		
	elseif country(args) == 'NLD' then
		if args.type == "E" then
			return europe(args)
		else
			return "*'''[[Roads in the Netherlands]]'''\n\n*[[List of motorways in the Netherlands|Motorways]]\n*[[List of E-roads in the Netherlands|E-roads]]\n*[[List of provincial roads in the Netherlands|Provincial]]\n*[[List of city routes in the Netherlands|City routes]]"
		end
		
	elseif country(args) == 'NOR' then
		if args.type == "E" then
			return europe(args)
		else
			return "*'''Roads in Norway'''\n\n*[[Norwegian national road|National Roads]]\n*[[Norwegian county road|County Roads]]"
		end
		
	elseif country(args) == 'NZL' then
		return "*'''[[New Zealand state highway network|New Zealand state highways]]'''\n\n*[[List of motorways and expressways in New Zealand|Motorways and expressways]]\n*[[List of New Zealand state highways|List]]"
		
		
	elseif country(args) == 'PHL' then
		if args.type == "AH" then
			return asia(args)
		else
			return "*'''Roads in the Philippines'''\n\n*[[Philippine highway network|Highways]]\n*[[Philippine expressway network|Expressways]]\n**[[List of expressways in the Philippines|List]]"
		end
		
	elseif country(args) == 'POL' then
		return "*'''[[National roads in Poland]]'''\n\n*[[Voivodeship road]]s"
		
	elseif country(args) == 'ROU' then
		return "*'''[[Roads in Romania]]'''\n\n*[[Highways in Romania|Highways]]"
		
	elseif country(args) == 'RUS' then
		return "*'''[[Russian federal highways|Russian Federal Highways]]'''"
		
	elseif country(args) == 'SGP' then
		return "*'''[[Expressways of Singapore]]'''"
		
	elseif country(args) == 'SRB' then
		return "*'''[[Roads in Serbia]]'''\n\n*[[Motorways in Serbia|Motorways]]"
		
	elseif country(args) == 'SWE' then
		if args.type == "E" then
			return europe(args)
		else
			return "*'''Roads in Sweden'''\n\n*[[Swedish national road|National Roads]]\n*[[Swedish county road|County Roads]]"
		end
		
	elseif country(args) == 'THA' then
		return "*'''[[Thai highway network|Highways in Thailand]]'''\n\n*[[Thai motorway network|Motorways]]\n*[[Asian Highway Network|Asian Highways]]"
		
	elseif country(args) == 'TUR' then
		return "*'''[[List of highways in Turkey|Highways in Turkey]]'''\n\n*[[Otoyol|Motorways]]\n**[[List of otoyol routes in Turkey|List]]\n*[[Turkish State Highway System|State Highways]]\n**[[List of highways in Turkey#State roads|List]]"
		
	elseif country(args) == 'TWN' then
		return "*'''[[Highway system in Taiwan]]'''"
		
	elseif country(args) == 'UKR' then
		return "*'''[[Roads in Ukraine]]'''\n\n*[[State Highways (Ukraine)|State Highways]]"
		
	elseif country(args) == 'URY' then
		return "*'''[[National Routes of Uruguay]]'''"
		
	elseif country(args) == 'USA' then
		local state = args.state or ''
		local stateName = ISO.name({"USA-" .. state})
		
		local national
			if args.type == nil then
				national = ''
			elseif args.type == 'ADHS' then
				national = "*'''[[Appalachian Development Highway System]]'''"
			elseif args.type == 'FFH' or args.type == 'FH' or args.type == 'FR' or args.type == 'FH-named' then
				national = "*'''[[Forest Highway|Forest Highway System]]'''"
			elseif args.type == 'ISTEA' then
				national = "*'''[[Intermodal Surface Transportation Efficiency Act|High-Priority Corridors]]'''"
			elseif args.type == 'NER' then
				national = "*'''[[New England road marking system]]'''"
			elseif args.type == 'NHS' then
				national = "*'''[[National Highway System (United States)|National Highway System]]'''\n\n*[[Interstate Highway System|Interstate]]\n*[[Strategic Highway Network|STRAHNET]]"
			elseif args.type == 'NSB' or args.type == 'NFSB' or args.type == 'FSB' or args.type == 'Byway' or args.type == 'Npkwy' or args.type == 'GRR' or args.type == 'NTP' or args.type == 'BRP' or args.type == 'BCB' or args.type == 'Back Country' then
				national = "*'''[[Scenic byways in the United States|Scenic Byways]]'''\n\n*[[National Scenic Byway|National]]\n*[[National Forest Scenic Byway|National Forest]]\n*[[Bureau of Land Management Back Country Byway|BLM]]\n*[[National Parkway|NPS]]"
			elseif args.type == 'Post' or args.type == 'Postal' then
				national = "*'''[[Post_road#United_States|Post Roads in the United States]]'''"
			elseif args.type == 'Trail' or args.type == 'Albert Pike' or args.type == 'Arrowhead Trail' or args.type == 'Dixie' or args.type == 'Jackson' or args.type == 'Jefferson' or args.type == 'Jefferson Davis' or args.type == 'LCT' or args.type == 'Lincoln' or args.type == 'National' or args.type == 'Roosevelt' or args.type == 'Spanish' or args.type == 'National Old Trails Road' then
				national = "*'''[[Auto trail]]s'''"
			elseif args.type == 'Tour' then
				national = "*'''[[Great Lakes Circle Tour]]'''"
			elseif args.type == 'USBR' then
				national = "*'''[[United States Bicycle Route System]]'''\n\n*[[United States Bicycle Route System#List of routes and planned corridors|List]]"
			elseif string.match( args.type, "^US" ) == 'US' then
				national = "*'''[[United States Numbered Highway System]]'''\n\n*[[List of United States Numbered Highways|List]]\n*[[List of special routes of the United States Numbered Highway System|Special]]\n*[[List of divided U.S. Routes|Divided]]"
			elseif args.type == 'I' or args.type == 'Interstate' or args.type == 'I-named' or args.type == 'I' .. state or args.type == 'BL' or string.match( args.type, "^BL" ) == 'BL' or string.match( args.type, "^Fut" ) == 'Fut' or string.match( args.type, "^IS" ) == 'IS' or string.match( args.type, "^I %d" ) == 'I 1' or (state == 'OH' and args.type == 'Turnpike') then
				national = "*'''[[Interstate Highway System]]'''\n\n*[[List of Interstate Highways|Main]]\n*[[List of auxiliary Interstate Highways|Auxiliary]]\n*[[List of suffixed Interstate Highways|Suffixed]]\n*[[List of business routes of the Interstate Highway System|Business]]\n*[[List of future Interstate Highways|Future]]"
			else
				national = ''
			end
			
		local function routes(args)
			local routes
			if state == 'AL' or state == 'AZ' or state == 'CT' or state == 'IL' or state == 'ME' or state == 'NV' or state == 'NH' or state == 'NY' or state == 'OH' or state == 'PA' or state == 'TN' or state == 'WA' or state == 'WV' then
				routes = "state routes"
			elseif state == 'FL' or state == 'IN' or state == 'NM' then
				routes =  "state roads"
			elseif state == 'KY' or state == 'VA' then
				routes =  "primary state highways"
			elseif state == 'MI' then
				routes =  "state trunkline highways"
			elseif state == 'WI' then
				routes =  "state trunk highways"
			else
				routes =  "state highways"
			end
			
			return routes
		end
		
		local function whoCallsThemRoutes(args)
			local routes
			if state == 'CA' or state == 'MS' or state == 'MO' or state == 'NV' or state == 'NH' or state == 'NJ' or state == 'NM' or state == 'NY' or state == 'OH' or state == 'PA' or state == 'WA' then
				routes = "Routes"
			else
				routes =  "Highways"
			end
			
			return routes
		end
			
		local function three(args)
			return string.format("[[List of Interstate Highways in %s|Interstate]]\n*[[List of U.S. %s in %s|US]]\n*[[List of %s in %s|State]]", stateName, whoCallsThemRoutes(args), stateName, routes(args), stateName ) 
		end
		
		local function shs(args)
			return string.format("*'''%s State Highway System'''\n\n*%s", stateName, three(args))
		end
		
		local function shsLinked(args)
			return string.format("*'''[[%s State Highway System]]'''\n\n*%s", stateName, three(args))
		end
		
		local states
			if state == '' then
				states = ''
			elseif state == 'AL' or state == 'ID' or state == 'ND' or state == 'WY' then
				states = shs(args)
			elseif state == 'AK' then
				states = "*'''[[List_of_Alaska Routes|Alaska Routes]]'''\n\n*[[List of Interstate Highways in Alaska|Interstate]]\n*[[List of Alaska Scenic Byways|Scenic Byways]]"
			elseif state == 'AS' then
				states = "*'''[[List of highways in American Samoa|Highways in American Samoa]]'''"
			elseif state == 'AZ' then
				states = string.format("%s\n*[[Unconstructed state routes in Arizona|Proposed]]\n*[[Former state routes in Arizona|Former]]", shs(args))
			elseif state == 'AR' then
				states = string.format("*'''[[Arkansas Highway System]]'''\n\n*%s\n*[[List of Arkansas state highway business_routes|Business]]\n*[[List of Arkansas state highway spurs|Spurs]]\n*[[List of suffixed Arkansas state highways|Suffixed]]\n*[[Arkansas Scenic Byways|Scenic]]\n*[[Arkansas Heritage Trail|Heritage]]", three(args))
			elseif state == 'CA' then
				if args.type == 'CR' then
					states = "*'''[[County routes in California]]'''"
				else
					states = string.format("*'''[[State highways in California]]'''\n\n*%s\n*[[State Scenic Highway System (California)|Scenic]]\n*[[History of California's state highway system|History]]\n*[[List of state highways in California (pre-1964)|Pre‑1964]]\n*[[Unconstructed state highways in California|Unconstructed]]\n*[[Deleted state highways in California|Deleted]]\n*[[California Freeway and Expressway System|Freeways]]", three(args))
				end
			elseif state == 'CO' then
				states = string.format("%s\n*[[List of Colorado Scenic and Historic Byways|Scenic]]", shs(args))
			elseif state == 'CT' then
				states = string.format("%s\n**[[List of Special Service Roads in Connecticut|SSR]]\n**[[List of State Roads in Connecticut|SR]]\n*[[Scenic Byways in Connecticut|Scenic]]", shs(args))
			elseif state == 'DE' then
				states = "*'''[[Delaware State Route System]]'''\n\n*[[List of numbered routes in Delaware|List]]\n*[[List of Delaware Byways|Byways]]"
			elseif state == 'DC' then
				states = "*'''[[Streets and highways of Washington, D.C.|Streets and Highways of Washington, DC]]'''\n\n*[[List of Interstate Highways in Washington, D.C.|Interstate]]\n*[[List of U.S. Highways in Washington, D.C.|US]]\n*[[List of numbered highways in Washington, D.C.|DC]]\n*[[List of state-named roadways in Washington, D.C.|State-Named Streets]]"
			elseif state == 'FL' then
				local county = args.county or ''
				if args.type == 'CR' and county ~= '' then
					states = string.format("*'''[[County roads in Florida]]'''\n\n*[[List of county roads in %s County, Florida|County roads in %s County]]", county, county)
				elseif args.type == 'CR' and county == '' then
					states = "*'''[[County roads in Florida]]'''"
				else
					states = string.format("%s\n**[[List of former state roads in Florida|Former]]\n**[[State roads in Florida before 1945|Pre‑1945]]\n*[[List of toll roads in Florida|Toll]]\n*[[List of Florida Scenic Highways|Scenic]]", shsLinked(args))
									end
			elseif state == 'GA' then
				states = "*'''Georgia State Highway System'''\n\n*[[List of Interstate Highways in Georgia|Interstate]]\n*[[List of U.S. Highways in Georgia|US]]\n*[[List of state routes in Georgia|State]]\n*\n*[[List of special state routes in Georgia|Special]]"
			elseif state == 'GU' then
				states = "*'''[[List of highways in Guam|Guam Highways]]'''"
			elseif state == 'HI' then
				states = "*'''[[List of state highways in Hawaii|Routes in Hawaii]]'''"
			elseif state == 'IL' then
				local county = args.county or ''
				if args.type == 'CR' and county ~= '' then
					states = string.format("*'''%s County Roads'''", county)
				elseif args.type == 'CR' and county == '' then
					states = "*'''County roads in Illinois'''"
				else
					states = string.format("%s\n*[[Illinois Tollway|Tollways]]\n*[[Scenic highways in Illinois|Scenic]]", shsLinked(args))
				end
			elseif state == 'IN' then
				local county = args.county or ''
				if args.type == 'CR' and county ~= '' then
					states = string.format("*'''%s County Roads'''", county)
				elseif args.type == 'CR' and county == '' then
					states = "*'''County roads in Indiana'''"
				else
					states = string.format("%s\n*[[List of Indiana Scenic Byways|Scenic]]", shs(args))
				end
			elseif state == 'IA' then
				states = string.format("*'''[[Iowa Primary Highway System]]'''\n\n*%s\n*[[County roads in Iowa|Secondary]]\n*[[List of Iowa Scenic Byways|Scenic]]", three(args))
			elseif state == 'KS' then
				states = string.format("%s\n*[[List of Kansas state highway spurs|Spurs]]", shs(args))
			elseif state == 'KY' then
				states = string.format("%s\n*[[List of parkways and named highways in Kentucky|Parkways]]", shs(args))
			elseif state == 'LA' then
				local county = args.county or args.parish or ''
				if args.type == 'PR' or args.type == 'CR' then
					if county ~= '' then
						states = string.format("*'''%s Parish Roads'''", county)
					else
						states = "*'''Parish roads in Louisiana'''"
					end
				else
					states = string.format("%s\n*[[List of Louisiana Scenic Byways|Scenic]]", shs(args))
				end
			elseif state == 'ME' then
				states = string.format("%s\n*[[List of auto trails in Maine|Auto trails]]\n*[[List of lettered highways in Maine|Lettered highways]]", shsLinked(args))
			elseif state == 'MD' then
				local county = args.county or ''
				if args.type == 'CR' and county ~= '' then
					states = string.format("*''' County Roads in %s County'''", county)
				elseif args.type == 'CR' and county == '' then
					states = "*'''County roads in Maryland'''"
				else
					states = string.format("*'''[[Maryland highway system]]'''\n\n*%s\n*[[List of Maryland Scenic Byways|Scenic Byways]]", three(args))
				end
			elseif state == 'MA' or state == 'MS' then
				states = string.format("%s", shsLinked(args))
			elseif state == 'MI' then
				local county = args.county or ''
				if args.type == 'CR' and county ~= '' or args.type == 'NFSB' and county ~= '' then
					states = string.format("*'''%s County Roads'''", county)
				elseif args.type == 'CR' and county == '' or args.type == 'NFSB' and county == '' then
					states = "*'''County roads in Michigan'''"
				elseif args.type == 'CDH' then
					states = "*'''[[List of county-designated highways in Michigan|County-Designated Highways]]'''"
				else
					states = string.format("*'''[[Michigan State Trunkline Highway System]]'''\n\n*%s\n*[[Pure Michigan Byway|Byways]]", three(args))
				end
			elseif state == 'MN' then
				local county = args.county or ''
				if args.type == 'CR' or args.type == 'CSAH' then
					if county ~= '' then
						states = string.format("*'''[[County roads in Minnesota|County roads of Minnesota]]'''\n\n*[[List of county roads in %s County, Minnesota|%s County]]", county, county)
					else
						states = "*'''[[County roads in Minnesota|County roads of Minnesota]]'''"
					end
				else
					states = string.format("*'''Minnesota Trunk Highway System'''\n\n*%s\n*[[Legislative route (Minnesota)|Legislative]]\n*[[Minnesota Scenic Byways|Scenic]]", three(args))
				end
			elseif state == 'MO' then
				states = string.format("%s\n*[[Missouri supplemental route|Supplemental]]", shsLinked(args))
			elseif state == 'MT' then
				states = string.format("*'''[[Montana Highway System]]'''\n\n*%s\n*[[List of secondary highways in Montana|Secondary]]", three(args))
			elseif state == 'NE' then
				states = string.format("%s\n*[[List of Nebraska Connecting Link, Spur, and Recreation Highways|Link]]\n*[[List of Nebraska Connecting Link, Spur, and Recreation Highways|Spur]]\n**[[List of state spur highways in Nebraska|State Spurs]]\n*[[List of Nebraska Connecting Link, Spur, and Recreation Highways|Recreation]]", shsLinked(args))
			elseif state == 'NV' then
				states = string.format("%s\n*\n*[[List of state routes in Nevada prior to 1976|Pre‑1976]]\n*[[List of Nevada Scenic Byways|Scenic]]", shs(args))
			elseif state == 'NH' then
				states = string.format("*'''[[New Hampshire Highway System]]'''\n\n*%s\n*[[List of turnpikes in New Hampshire|Turnpikes]]", three(args))
			elseif state == 'NJ' then
				local county = args.county or ''
				if args.type == 'CR' then
					if county ~= '' then
						states = string.format("*'''[[List of county routes in %s County, New Jersey|County Routes in %s County]]'''\n\n*[[County routes in New Jersey|System]]\n*[[List of 500-series county routes in New Jersey|500-series routes]]", county, county)
					else
						states = "*'''[[County routes in New Jersey]]'''\n\n*[[List of 500-series county routes in New Jersey|500-series routes]]"
					end
				else
					states = string.format("*'''[[State highways in New Jersey|New Jersey State Highway Routes]]'''\n\n*%s\n*[[List of New Jersey Scenic Byways|Scenic Byways]]", three(args))
				end
			elseif state == 'NM' then
				states = string.format("%s\n*[[List of New Mexico Scenic and Historic Byways|Scenic]]", shs(args))
			elseif state == 'NY' then
				local county = args.county or ''
				if args.type == 'CR' then
					if county ~= '' then
						states = string.format("*'''[[County routes in New York]]'''\n\n*[[List of county routes in %s County, New York|County Routes in %s County]]", county, county)
					else
						states = "*'''[[County routes in New York]]'''"
					end
				else
					states = string.format("*'''[[Numbered highways in New York|New York Highways]]'''\n\n*%s\n*[[List of reference routes in New York|Reference]]\n*[[Parkways in New York|Parkways]]", three(args))
				end
			elseif state == 'NC' then
				states = string.format("*'''[[North Carolina Highway System]]'''\n\n*%s\n*[[List of North Carolina Scenic Byways|Scenic]]", three(args))
			elseif state == 'MP' then
				states = "*'''[[List of numbered highways in the Northern Mariana Islands|Northern Mariana Islands Highways]]'''"
			elseif state == 'OH' then
				if args.type == 'CR' then
					states = "*'''[[County roads in Ohio]]'''"
				else
					states = string.format("%s\n*[[Ohio Scenic Byway|Scenic]]", shs(args))
				end
			elseif state == 'OR' then
				states = string.format("*'''[[State highways in Oregon|Oregon Highways]]'''\n\n*%s\n*[[List of named state highways in Oregon|Named]]\n*[[Oregon State Scenic Byways|Scenic]]", three(args))
			elseif state == 'OK' then
				states = string.format("%s\n*[[Turnpikes of Oklahoma|Turnpikes]]", shs(args))
			elseif state == 'PA' then
				states = string.format("*'''[[Pennsylvania State Route System]]'''\n\n*%s\n*[[List of Pennsylvania Scenic Byways|Scenic]]\n*[[List of Legislative Routes in Pennsylvania|Legislative]]", three(args))
			elseif state == 'PR' then
				states = "*'''[[Roads in Puerto Rico]]'''\n\n*[[List of highways in Puerto Rico|List]]"
			elseif state == 'RI' then
				states = "*'''[[Numbered routes in Rhode Island|Rhode Island Routes]]'''"
			elseif state == 'SC' then
				states = string.format("%s\n*[[List of South Carolina Scenic Byways|Scenic]]", shsLinked(args))
			elseif state == 'SD' then
				states = string.format("*'''South Dakota State Trunk Highway System'''\n\n*%s", three(args))
			elseif state == 'TN' then
				states = string.format("*'''[[Tennessee State Route System|Tennessee State Routes]]'''\n\n*%s", three(args))
			elseif state == 'TX' then
				states = string.format("*'''[[Texas state highway system|Highways in Texas]]'''\n\n*%s\n*[[Toll roads in Texas|Toll]]\n*[[List of state highway loops in Texas|Loops]]\n*[[List of state highway spurs in Texas|Spurs]]\n*[[List of Farm to Market Roads in Texas|FM/RM]]\n*[[List of Park Roads in Texas|Park]]\n*[[List of Recreational Roads in Texas|Rec]]", three(args))
			elseif state == 'UT' then
				states = string.format("%s\n*\n*[[List of state highways serving Utah state parks and institutions|Minor]]\n*[[List of Utah Scenic Byways|Scenic]]", shs(args))
			elseif state == 'VI' then
				states = "*'''[[List of United States Virgin Islands highways|USVI Highways]]'''"
			elseif state == 'VT' then
				states = "*'''[[List of state highways in Vermont|State highways in Vermont]]'''"
			elseif state == 'VA' then
				states = "*'''[[State highways in Virginia|Virginia Routes]]'''\n\n*[[List of Interstate Highways in Virginia|Interstate]]\n*[[List of U.S. Highways in Virginia|US]]\n*[[List of primary state highways in Virginia|Primary]]\n*[[List of secondary state highways in Virginia|Secondary]]\n*[[List of Virginia Byways|Byways]]\n*[[History of state highways in Virginia|History]]\n*[[Virginia HOT lanes|HOT lanes]]"
			elseif state == 'WA' then
				states = string.format("*'''[[State highways in Washington]]'''\n\n*%s\n*[[List of Washington State Scenic and Recreational Highways|Scenic]]\n*[[List of Primary State Highways in Washington|Pre-1964]]\n*[[1964 state highway renumbering (Washington)|1964 renumbering]]\n*[[List of former state routes in Washington|Former]]", three(args))
			elseif state == 'WV' then
				local county = args.county or ''
				if args.type == 'CR' then
					if county ~= '' then
						states = string.format("*'''[[List of county routes in West Virginia|County Routes in West Virginia]]'''\n\n*[[List of county routes in %s County, West Virginia|%s County Routes]]", county, county)
					else
						states = "*'''[[List of county routes in West Virginia|County Routes in West Virginia]]'''"
					end
				else
					states = string.format("%s", shsLinked(args))
				end
			elseif state == 'WI' then
				if args.type == 'CR' or args.type == 'CTH' then
					states = "*'''[[County Trunk Highways (Wisconsin)|County Trunk Highways]]'''"
				else
					states = string.format("*'''[[Wisconsin State Trunk Highway System]]'''\n\n*%s\n*[[List of Wisconsin Scenic Byways|Scenic]]\n*[[Rustic Road (Wisconsin)|Rustic]]", three(args))
				end
			end
			
			return string.format("%s\n\n%s", national, states) 
		
	elseif country(args) == 'ZAF' then
		return "*'''[[Numbered routes of South Africa]]'''"

	elseif country(args) == 'ARG' or country(args) == 'BRB' or country(args) == 'IRQ' or country(args) == 'CHL' or country(args) == 'PRY' then
		return string.format("*'''[[List of highways in %s %s|Highways in %s %s]]'''", the(args), name, the(args), name)

	elseif country(args) == 'BGD' or country(args) == 'IMN' or country(args) == 'ISL' or country(args) == 'MDG' or country(args) == 'NPL' or country(args) == 'UGA' then
		return string.format("*'''[[List of roads in %s %s|Roads in %s %s]]'''", the(args), name, the(args), name)
		
	elseif country(args) == 'DNK' or country(args) == 'MNE' or country(args) == 'PRK' or country(args) == 'TUN' then
		return string.format("*'''[[Transport in %s %s]]'''\n\n*[[Motorways in %s %s|Motorways]]", the(args), name, the(args), name)
		
	elseif country(args) == 'AZE' or country(args) == 'BLR' or country(args) == 'BLZ' or country(args) == 'CPV' or country(args) == 'CUB' or country(args) == 'HND' or country(args) == 'JAM' or country(args) == 'KWT' or country(args) == 'MDA' or country(args) == 'PAK' or country(args) == 'PRT' then
		return string.format("*'''[[Roads in %s %s]]'''", the(args), name)
		
	elseif country(args) == 'ALB' or country(args) == 'BGR' or country(args) == 'COL' or country(args) == 'CZE' or country(args) == 'DOM' or country(args) == 'ECU' or country(args) == 'GUF' or country(args) == 'HRV' or country(args) == 'PER' or country(args) == 'SVK' or country(args) == 'SVN' or country(args) == 'VEN' then
		return string.format("*'''[[Highways in %s %s]]'''", the(args), name)
		
	else
		return string.format("*'''[[Transport in %s %s]]'''", the(args), name)
	end
end

function p.browselinks(frame)
	local args = getArgs(frame)
	
	if args.nobrowse ~= nil then
		return nil
	else
		return browselinks(args)
	end
	
end

return p