BRASILIA (Reuters) - Eight Brazilians arrested before the Rio Olympics for belonging to a loosely organized group that supported Islamic State and discussed attacking the games were charged on Friday under Brazil's anti-terrorism law.

The men, who were loosely connected by the internet, were arrested in July two weeks before the Olympics opened in Rio de Janeiro. Police working on an intelligence tip from the FBI traced them through email exchanges about the purchase of an automatic weapon and discussion of plans for a possible attack on the Games.

Prosecutors charged the men with promoting a terrorist organization and criminal association. Five of them were charged with inciting children and adolescents to commit crimes, and one was charged for recruiting members for a terrorist group, the federal prosecutors said in a statement.

"It wasn't a professional terrorist organization. They were amateurs, but they could have endangered the Olympics and the nation," Justice Minister Alexandre de Moraes, who is responsible for law enforcement, told reporters in Rio.

Another six members of the group are under investigation, the statement said. The prosecutors requested that all 14 continued to be held in police custody.

Lawyers for the men denied they were supporters of Islamic State and demanded their release. Only two of the men knew each other personally and had studied Arabic in Egypt, police said.

The charges were the first to be made under an anti-terrorism law that Brazil's Congress finally approved earlier this year after years of encouragement from the United States.

The group was a disorganized cell with no weapons or explosives training and were being monitored because they had accessed websites linked to IS, Brazilian officials said. The group had no contact with the militant Islamist group but some members had made "pro forma" declarations of allegiance to IS leader Abu Bakr al-Baghdadi via social media.

They were arrested after they started planning actions including martial-arts training and the purchase of an AK-47 rifle from a gunshop in Paraguay, officials said.

(Reporting by Anthony Boadle in Brasilia and Rodrigo Viga in Rio de Janeiro; Editing by Andrew Hay)