use ob_get_contents instead to get your $body variable set properly
http://us3.php.net/ob_get_contents
This way you can write your HTML/PHP/Javascript code more naturally and not in some big string that will cause tons of debugging headaches. Then you can echo or use the output in a single line of code at the end much more elegantly.
from the php page I linked above, here is an example of it's usage:
<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>