ISTANBUL, Jan 3 (Reuters) - Turkish President Abdullah Gul urged the judiciary to remain impartial as it pursues a corruption investigation shaking the government, warning on Friday of grave economic consequences if confidence in the country's institutions is eroded.

In his most exhaustive comments on the graft scandal so far, Gul said the existence of a "state within the state" would not be tolerated, an apparent reference to the movement of U.S.-based Turkish cleric Fethullah Gulen, whose followers are influential in Turkey's police and judiciary.

He also said there should be no tolerance for corruption.

The corruption investigation, which has led to the resignation of three ministers, poses the biggest challenge to Prime Minister Tayyip Erdogan in his 11 years as leader. He has cast the probe as a foreign-backed plot to undermine his government and sap his influence in the Middle East and beyond.

Gul, whose role as president is largely ceremonial but who must approve laws passed by parliament and makes key appointments in the judiciary, has not been implicated in the corruption allegations.

"There can be no state within the state," the president said in a live interview on Turkish television, echoing Erdogan's words after police raided offices and homes and detained businessmen close to the government two weeks ago.

"Anybody can work at state institutions - the army, the judiciary, or other state actors - but they have to abide by the law, the constitution and the rules of that institution ... taking orders from somewhere else is not acceptable," he said.

"f there are such claims, these will be investigated and this cannot be allowed. If this is happening within the judiciary, among the judges, this cannot be tolerated."

The corruption probe has pitched Erdogan against Gulen, whose Hizmet ("Service") movement controls a vast global network of schools and businesses and whose sympathisers among Turkey's religious elite say they number in the millions.

Many of Gulen's followers see him as a more progressive and pro-Western influence than Erdogan, whose opinions on issues from abortion to alcohol consumption, and the concentration of power around him they view with increasing alarm.

Erdogan's backers see Gulen's connivance in the inquiry, a charge the cleric has denied through his lawyers.

The prime minister has responded by purging some 70 police officers connected with the investigation and blocking a second probe into big infrastructure projects he has championed.

ECONOMIC CONSEQUENCES

Gul, seen as a more unifying figure than Erdogan and who has largely stayed out of the furore, emphasised that the judiciary should also be free from government interference.

"We are in parliamentary system and as the president I am trying to do ... everything within my authority to ensure that state institutions are working in harmony," he said.

"The legislative and executive powers are in a way accountable through elections but the judicial system is in a different position. For them, independence and impartiality is much more important."

Erdogan's Islamist-rooted AK Party has relied on its economic record to maintain the support of many Turks.

But the corruption scandal is shaking investor confidence at a time when the lira currency is weakening, inflation rising and growth slowing, risking tipping the nation into its greatest period of political instability in a decade - just before local and national elections this year and next.

"Economic stability comes first," said Gul, who co-founded the AK Party with Erdogan more than a decade ago.

"If there is a worsening in the economy we would be shooting ourselves in the foot. If there is any deterioration in confidence, that will be the biggest damage to the country."

Erdogan, who has won three elections, is barred by party rules from standing for a fourth term as prime minister and is widely expected to run instead for the presidency in August.

That has generated speculation that Gul, rather than running against Erdogan, could become prime minister in general elections currently scheduled for 2015.

Gul declined to be drawn.

"It is too early for me to say anything on this, but I would like to add that nobody should put a risk premium for Turkey on this issue," he said.

"To be frank nothing has been discussed until now, I want everyone to know this. We will have three elections in the next two years and I believe Turkey will complete these as a mature democracy."

We welcome comments that advance the story through relevant opinion, anecdotes, links and data. If you see a comment that you believe is irrelevant or inappropriate, you can flag it to our editors by using the report abuse links. Views expressed in the comments do not represent those of the Thomson Reuters Foundation. For more information see our Acceptable Use Policy.