function resizeImages(width, maxHeight, maxAspectRatio) {
  var i = 0;
  for(i = 0; i < document.images.length; i ++) {
    resizeImage(document.images[i], width, maxHeight, maxAspectRatio);
  }                
}

function resizeImage(image, width, maxHeight, maxAspectRatio) { 
  if(width == null) {
    if(maxHeight != null) {
      if(image.height != maxHeight) { 
        if((maxHeight/image.height) <= maxAspectRatio) {               
          image.height = maxHeight;
        }
      }
    }
    image.style.visibility = "visible";
    return;
  }
  
  if(maxHeight == null) {
    if((width / image.width) <= maxAspectRatio) { 
      image.width = width;
    }
    image.style.visibility = "visible";
    return;
  }

  var factor = width / image.width;
  var factorMax = maxHeight / image.height;
  
  var newHeight = image.height*factor;
  
  if(newHeight > maxHeight) {
    if(factorMax <= maxAspectRatio) {  
      image.height = image.height*factorMax;
    }
    image.style.visibility = "visible";  
  } else {
    if(factor <= maxAspectRatio) { 
      image.width = image.width*factor;
    } else {
      image.width = image.width*maxAspectRatio;
    }
    image.style.visibility = "visible";
  }
}
