* Oil prices climb, energy stocks follow
* RIM, Nike shares sharply lower, KB Homes jumps
* Dow up 1.8 pct, S&P up 2 pct, Nasdaq up 2.6 pct
By Chuck Mikolajczak
NEW YORK, June 29 (Reuters) - U.S. stocks surged on Friday as investors cheered an agreement by European leaders to stabilize the region's banks, a pact that helped remove some of the uncertainty that has been plaguing markets.
The broad rally put the S&P 500 on track for its best day in about three weeks and helped the benchmark index trim its quarterly loss to less than 4 percent.
The decline would mark the first down quarter for the benchmark index in the last three after inconclusive Greek elections and concerns about the solvency of Spanish banks roiled financial markets around the world.
Crucially, euro-zone leaders agreed that countries would be able to recapitalize banks directly without increasing a country's budget deficit.
Such a move serves to end the cycle markets fell into when policymakers bailed out Spanish banks to the tune of $125 billion, but ended up further extending the indebtedness of the Spanish state and shunting existing bondholders down the food chain.
"The key is in separating the banking system from the sovereign system and they had to have seen that," said Paul Mendelsohn, chief investment strategist at Windham Financial Services in Charlotte, Vermont.
"The answer was obvious and (German Chancellor Angela) Merkel made a lot of noise before the meeting and I guess she saw the handwriting on the wall."
Policymakers also agreed to discuss a proposal for a pan-bloc banking union and said they wouldn't force countries that comply with EU budget rules to adopt extra austerity measures or economic reforms.
In addition to the boost the EU agreement gave to the Wall Street rally, investors cited end-of-quarter portfolio adjustments, including reallocation to bonds from stocks after bonds outperformed stocks by a wide margin this month.
Sectors that have been among the most sensitive to developments in the euro zone led gains. U.S. bank stocks were among the market leaders as the KBW bank index jumped 2.1 percent. Shares of Bank of America Corp rose 3.6 percent to $8.02. The PHLX Europe sector index climbed 3.8 percent.
The Dow Jones industrial average gained 226.14 points, or 1.79 percent, to 12,828.40. The Standard & Poor's 500 Index gained 26.63 points, or 2.00 percent, to 1,355.67. The Nasdaq Composite Index gained 74.52 points, or 2.62 percent, to 2,924.01.
Exact details of the euro agreement remain to be worked out.
Italian and Spanish borrowing costs fell, though they remained not far from recent highs. Market expectations for any action during a two-day European Union summit had all but vanished, giving markets room to bounce on good news.
Brent and U.S. crude oil prices soared on the back of the EU agreement. Energy futures prices also got a boost from the euro's jump of almost 2 percent against the U.S. dollar. The S&P energy sector index added 2.3 percent.
The EU summit news overshadowed a batch of mixed U.S. data. U.S. consumer spending stalled in May as auto purchases flagged while consumer sentiment hit a six-month low in June in the latest signs of trouble for the economy.
Although another report on Friday showed manufacturing activity in the Midwest picked up this month, factories saw a modest decline in new orders.
Attention in Europe now turns to next week's European Central Bank meeting. The consensus is that the bank will cut its main refinancing rate by 25 basis points to 0.75 percent and may trim the deposit rate - the rate it pays banks for parking money with it - by 25 basis points to 0 percent.
"You have more fireworks coming next week when the ECB meets on the fifth because I have to believe they are going to cut their interest rates by at least a half to stimulate growth," said Mendelsohn.
Among the few Wall Street decliners, U.S.-traded shares of Research in Motion tumbled 18.2 percent to $7.47 in the wake of the company's decision on Thursday to delay the make-or-break launch of its next-generation BlackBerry phones until next year.
Nike shares dropped 8.2 percent to $89.92 a day after the world's largest sportswear maker missed quarterly profit estimates for the first time in at least two years.
Ford Motor Co fell 5.5 percent to $9.54 after the automaker became the latest large multinational to warn on weakness stemming from Europe, joining the likes of Procter & Gamble Co.
But shares of KB Homes jumped 11.5 percent to $9.69 after the fifth-largest U.S. homebuilder reported a narrower second-quarter loss, helped by higher sale prices and net orders.