* Oil prices climb, energy stocks follow
* RIM, Nike shares sharply lower, KB Homes jumps
* Dow up 1.7 pct, S&P up 1.8 pct, Nasdaq up 2.4 pct
By Edward Krudy
NEW YORK, June 29 (Reuters) - U.S. stocks surged on Friday, the last day of a weak quarter, as E uropean leaders agreed to directly shore up troubled European banks, of fering some relief to investors worried abo ut a meltdown of the euro zone.
The broad rally of 2 percent gave the S&P 500 its best day in three weeks and helped the benchmark index trim its quarterly loss to about 4 percent. It is the first down quarter for the S&P 500 since the third quarter of last year after inconclusive Greek elections and fears about the solvency of Spanish banks created turbulence in 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, and without preferential seniority status.
Such a move avoids the trap that markets fell into when policy makers bailed out Spanish banks to the tune of $125 billion, but ended up further extending the indebted Spanish state and shunting existing bondholders down the food chain.
"The market is loving it," said Ken Polcari, a floor trader at ICAP Equities in New York. "I think part of that is relief that the leaders seem to be coming together, but I still think there is a lot of conditionality and a lot of 'devil's in the details.'"
Polcari said that part of the bounce was being driven by traders reversing bets that the market would fall.
Policymakers also agreed on 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.
Investors also 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 most beaten down recently led gains. U.S. bank stocks were among the market leaders as the KBW bank index jumped 2 percent. Shares of Citigroup rose 2.7 percent to $27.09.
The Dow Jones industrial average gained 208.85 points, or 1.66 percent, to 12,811.11. The Standard & Poor's 500 Index rose 24.51 points, or 1.84 percent, to 1,353.55. The Nasdaq Composite Index added 67.86 points, or 2.38 percent, to 2,917.35.
Exact details of the euro agreement remain to be worked out.
But 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.
"We've gotten used to being underwhelmed by the outcomes, so with little to no expectations for success, the fact that it appears we are going to get something substantial is a real important positive for the market in the near term," said Art Hogan, managing director of Lazard Capital Markets in New York.
"It's inching closer to a banking union and the closer we get to a banking union would put (the EU) well on the road to a fiscal union."
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. Th e S&P energy sector index added 1.9 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.
Last week Goldman Sachs recommended taking a short position in the S&P 500, with a target of 1,285 after the Philadelphia Federal Reserve Bank reported that factory activity in the U.S. mid-Atlantic region contracted for a second straight month in June. A slowing global economy remains a headwind for stocks.
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.
Hospitals and insurers providing Medicaid plans for the poor t urned out to be the m ain corporate winners from the U.S. Supreme Court's decision on Thursday to uphold President Barack Obama's Affordable Care Act.
U.S.-traded shares of Research in Motion tumbled 18.5 percent to $7.44 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 7.3 percent to $89.86 a day after the world's largest sportswear maker missed quarterly profit estimates for the first time in at least two years.
Shares of KB Homes rose 11.6 percent to $9.71 after the fifth-largest U.S. homebuilder reported a narrower second-quarter loss, helped by higher sale prices and net orders.