The ballots have been cast and counted. But Felipe Calderon isn't Mexico's president-elect until the nation's highest electoral court says so.

The independent agency that ran Sunday's election added up more than 41 million votes and declared that Calderon won the most: 240,000 more than rival Andres Manuel Lopez Obrador.

But this agency has no legal authority to declare a winner.

Under Mexico's complex election laws, Calderon won't have won until the Federal Electoral Tribunal certifies the count. And that's not a sure thing: the widely respected tribunal has overturned two gubernatorial races in recent years, both for meddling by the ruling party.

Disgruntled candidates have gone to court to dispute election results in many countries _ the U.S. presidential race won by George W. Bush in 2000 is a famous example.

But Mexico, where electoral disputes are almost a tradition, makes the courts part of the process right from the start. It's part of an elaborate system designed to eliminate the fraud that was once nearly universal. Once the votes are officially counted, each party has four days to file challenges with the Electoral Tribunal.

The tribunal's seven magistrates then consider the evidence in weeks of hearings, deciding whether individual ballot boxes were stuffed, whether particular voters were intimidated, whether candidates violated spending limits or bought votes.

Mexico's election law says the court must decide all of that by Aug. 31. The magistrates then add up the votes that have survived challenges and declare a winner by Sept. 6, a decision that can't be appealed. The new president is inaugurated on Dec. 1.

Even in the last presidential election, when Vicente Fox had a commanding lead and his opponents quickly conceded defeat, the tribunal needed more than a month to declare him president-elect.

This year's race is a squeaker and Lopez Obrador's Democratic Revolution Party, or PRD, has alleged that Calderon's National Action Party benefited from irregularities at about 50,000 polling places, setting the stage for a complex, emotional battle before the court.

At least one key Lopez Obrador aide says he's ready to trust the tribunal, known by its Spanish initials as the TRIFE.

"It is the final arbiter, and it would be a tragedy if it wasn't impartial,"said Manuel Camacho, a top campaign aide to Lopez Obrador."The PRD has confidence in the TRIFE."

The court's magistrates, nominated by Mexico's Supreme Court and confirmed by a two-thirds vote of the Senate, have been working together for 10 years. This will be their last and probably biggest decision before they leave the court in October.

"The magistrates are serious people. They come from academic and legal backgrounds,"said historian Lorenzo Meyer, who endorsed Lopez Obrador before the election."I can't imagine them distorting their decision because of pressure from a party."

The judges, most of them little-known outside legal circles, emerged from a justice system long tied to the Institutional Revolutionary Party, or PRI, that dominated Mexico from 1929 until 2000. But the PRI has suffered from some of their most notable rulings _ as has National Action:

_ The tribunal threw out the PRI's victory in a tight Tabasco state governor's race in 2000 because of official interference and ordered a new election, which eventually was won by the PRI.

_ It prompted riots by ruling party supporters in Yucatan state in 2000 by removing a PRI-stacked state electoral board. National Action won the next gubernatorial election.

_ In 2003, it annulled a PRI victory in Colima state, saying the outgoing governor had illegally intervened, partly by installing police roadblocks to discourage opposition voters from reaching the polls. The PRI won that new election as well.

_ In 2003, it upheld a US$91 million fine against the PRI for using government funds in its unsuccessful 2000 presidential campaign. The following year, it approved a US$34 million fine against Fox's party for violating campaign finance laws in 2000.

_ In 2005, the tribunal ordered Fox's party to repeat a primary election in the State of Mexico because the first had violated the party's own rules.

Associated Press writer John Rice contributed to this report.

Copyright 2006 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.