function resizeImageId(imgId, maxw, maxh) {
  var img = document.getElementById(imgId);
  resizeImage(img, maxw, maxh);
} 

function resizeImage (img, maxw, maxh) {
  var ratio = maxh/maxw;
  if (typeof img == "undefined") {
    return;
  }
  img.style.visibility = "hidden";
  img.removeAttribute('height');
  img.removeAttribute('width');
  if (img.height/img.width > ratio){
     // height is the problem
    if (img.height > maxh){
      img.width = Math.round(img.width*(maxh/img.height));
      img.height = maxh;
    }
  } else {
    // width is the problem
    if (img.width > maxw){
      img.height = Math.round(img.height*(maxw/img.width));
      img.width = maxw;
    }
  }
  img.style.visibility = "visible";
} 
