window.onload = set_footer;
window.onresize = set_footer;

function set_footer () {
  if (!document.getElementById || !document.body.offsetHeight) return;
  var footer_height = 40;
  var footer = document.getElementById('footer');
  var magin_top = footer.style.marginTop ? parseInt(footer.style.marginTop) : footer_height;
  var window_height = typeof(window.innerHeight) == 'number' ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : (document.body && document.body.clientHeight) ? document.body.clientHeight : 0;
  var content_height = document.getElementById('body').offsetHeight;
  var footer_margin_top = content_height <= window_height ? window_height - content_height : footer_height;
  if (footer_margin_top > footer_height) {
    footer_margin_top -= footer_height;
    footer.style.marginTop = footer_margin_top + 'px';
  }
}