PHOENIX - State lawmakers did nothing wrong when they seized $50 million out of the settlement Arizona got with mortgage lenders, a judge ruled Wednesday.

Maricopa County Superior Court Judge Mark Brain acknowledged that Attorney General Tom Horne signed an agreement specifying how the funds were to be used. These permitted uses include "to avoid preventable foreclosures" and to help prevent and prosecute financial fraud. The agreement required the money be put into a "court-ordered trust fund."

But the judge said that did not place the funds off-limits to lawmakers.

"The Legislature had the right to do what it in fact did - direct $50 million of the settlement to be put into the general fund so that it could be spent as directed by the Legislature," Brain wrote.

Wednesday's ruling is a setback for advocacy groups that contended the money is needed to help homeowners who were the victims of mortgage fraud.

Attorney Tim Hogan of the Arizona Center for Law in the Public Interest, who is representing the advocacy groups, said an appeal is likely.

Hogan also said if that fails, the state could be sued for breach of contract - the agreement Horne signed - if lawmakers do not use the funds for the specific purposes outlined in the agreement.

The fight is a direct outgrowth of a nationwide $26 billion deal with five major lenders. While they did not concede they were guilty of mortgage fraud, they agreed to the settlement to limit their liability.

Arizona's share is about $1.6 billion.

The biggest chunk of Arizona's share of the settlement - $1.3 billion - is earmarked to directly help those who are "underwater" with their mortgages, owing more than the property is worth.

The deal also includes another $110 million for those who lost their homes in foreclosure between 2008 and the end of 2011 due to lender misconduct. And there is $85 million available for interest rate reductions for some.

This fight is over $97 million for the state itself.

Lawmakers, in their bid to balance the budget earlier this year, took $50 million of that.

They conceded the settlement Horne signed requires the cash to be used for issues related to mortgage fraud.

But they argued the state itself is a victim because of the whole mortgage foreclosure mess that sent property values into a tailspin and reduced state tax collections. And that, they said, entitled the state as a whole to a share of the cash.

Hogan countered the settlement itself contains specific examples of permissible uses, things like paying for housing counselors, legal assistance and foreclosure assistance hotlines. And he conceded that the state might be able to take some cash for its out-of-pocket legal expenses.

But he said nothing in the agreement covers the kind of "losses" the Legislature is claiming.

Brain sidestepped that question, focusing instead on Hogan's argument the deal required Horne to put the money into "a separate court-ordered trust fund," which he said puts the cash off-limits to legislative raids.

Brain, however, said while the deal, which was approved by a federal court, does mention a "trust fund," that is not enough to create the kind of charitable trust recognized under state law and the restrictions that go with that.

Hogan said Brain's ruling ignores the fact Horne agreed to the specific requirement for a trust.

"When the attorney general establishes a court-ordered trust fund, you usually think he knows what he's doing," Hogan said.

Horne initially fought the move by the Republican-controlled Legislature to divert the cash, calling it "bad public policy."

But he eventually said the move is not illegal because the agreement has other permitted uses for the cash. Anyway, Horne said, if he tried to fight lawmakers for control of that $50 million, the Legislature would be free to reduce his budget by an identical amount.

Officially, Hogan's clients are two people who would benefit by the state having more money to help homeowners avoid foreclosure. Both, Hogan said, are "at risk" of losing their homes.

But the case was brought to him by groups like the Arizona Housing Alliance. Valerie Iverson, its executive director, said the money could be better used for counseling, legal help and education of homeowners in trouble with mortgages.