This is what I did...
I changed your code to read:
<div style="
<?php if ($this->countModules("left") && !$this->countModules("right")) { ?>margin-left:<?php echo $left_margin; ?>px;"><?php } ?>
<?php if (!$this->countModules("left") && !$this->countModules("right")) { ?>margin-left:0px;"><?php } ?>
<?php if (!$this->countModules("left") && $this->countModules("right")) { ?>margin-right:<?php echo $right_margin; ?>px;"><?php } ?>
<?php if ($this->countModules("left") && $this->countModules("right")) { ?>margin-right:<?php echo $right_margin; ?>px;margin-left:<?php echo $left_margin; ?>px;"><?php } ?>
Then, just before the <div id="s5_mainbottomback"></div> I added this code to close off the DIV tag that was left open:
<?php if (!$this->countModules("left") && !$this->countModules("right")) { ?></div><?php } ?>
It appears to be working...