Pakistan's Supreme Court, stacked with judges friendly to President Pervez Musharraf, on Thursday threw out a final challenge to his re-election and paved the way for him to quit as army chief.

The long-awaited ruling comes as Musharraf faces the prospect of Pakistan's second suspension from the Commonwealth since he took power in a bloodless 1999 coup, because he continues to resist calls to fully lift emergency rule imposed on November3.

Dismissed, Chief Justice Abdul Hamid Dogar said after hearing the petition, the sixth and final challenge to Musharraf's October 6 re-election to be thrown out by the court.

Attorney General Malik Qayyum said before the ruling he expected Musharraf to be sworn in for a second term by the weekend or immediately thereafter.

Musharraf's top legal adviser, Sharifuddin Pirzada, said there was now no legal obstacle to his re-election.

Now the court has to give us this in writing, Pirzada said.

Musharraf repeatedly promised to relinquish his army post and be sworn in as a civilian leader for a second five-year term in what he calls a transition to civilian-led democracy once his re-election had been endorsed by the court.

While critical of his actions, the United States has given leeway to General Musharraf, a crucial ally in the fight against al Qaeda and the Taliban, to put things right before a general election on January 8 that the opposition may boycott.

Investors in the Karachi stock market, already taking heart from U.S. President George W. Bush's endorsement of Musharraf, welcomed the favorable court ruling. Shares gained 0.25 percent on Thursday on top of 1.5 percent on Wednesday, leaving the market 2.7 percent below pre-emergency levels.

BUSH PART OF THE PROBLEM?

Amid fears the Supreme Court would rule against him on the re-election challenges, Musharraf declared emergency rule nearly three weeks ago, suspended the constitution, sacked the chief justice and purged the court, installing more amenable judges. The move drew widespread international condemnation.

He has assured me he will do his utmost to lift the state of emergency in time for free and fair elections to be held and to give up his military role and responsibilities as soon as possible, British Prime Minister Gordon Brown said before heading to a Commonwealth summit in Kampala.

The government has appealed to the Commonwealth of 53 nations not to follow through on a threat to suspend Pakistan for failing to meet a Thursday deadline to end emergency rule and reverse other authoritarian steps.

Western governments fear that stifling democracy could play into the hands of Islamist militants threatening nuclear-armed Pakistan.

Some Pakistanis say Bush's support of Musharraf -- describing him as a valuable ally, a man of his word and committed to democracy -- is part of the problem.

I am very angry with the attitude (Bush) is having towards the country, because he should realize that we as a nation have given him support, said human rights activist Shahnaz Bukhari, chief coordinator of the Progressive Women's Association.

He should not think that what he is doing and what he is saying is good for our country ... When American people come here they feel very insecure -- these are the reasons, she added. Bush is having this (kind of) democracy in the USA?

Musharraf has started to roll back the emergency, freeing around 5,000 lawyers, opposition and rights activists detained in a round-up of opponents. Those freed include Imran Khan, the cricketer-turned-politician, who called for a poll boycott.

It's a complete fraud. Participating in this election would mean giving legitimacy to Musharraf's violation of the constitution, Khan said, calling for the reinstatement of deposed judges.

Many of the judges and leading lawyers who represented the strongest challenge to Musharraf's authority are still in prison or under house arrest.

Musharraf remains in danger of political isolation.

Opposition parties led by Benazir Bhutto, the prime minister Musharraf allowed back to Pakistan, and Nawaz Sharif, the premier he deposed eight year ago and has kept exiled in Saudi Arabia, have yet to come up with a common strategy.

An opposition boycott could undermine the poll's credibility, but Bhutto may not want to gift Musharraf's allies a walkover.

(Additional reporting by Simon Gardner, Zeeshan Haider and Augustine Anthony; Editing by Simon Cameron-Moore and Sanjeev Miglani)