function getBestFitSize(imageId, maxWidth, maxHeight)
{
    var img = document.getElementById(imageId);
    getBestFitSize_Ref(img, maxWidth, maxHeight);
}

function getBestFitSize_Ref(img, maxWidth, maxHeight)
{
    try
    {
        var width = img.clientWidth;
        var height = img.clientHeight;
        var ratio = width / height;
     
        if (width <= maxWidth && height <= maxHeight)
        {
            return;
        }
        
        if (ratio >= 1)
        {
            img.width = maxWidth - 2;
        }
        else
        {
            img.width = Math.floor(maxHeight * ratio) - 2;
        }
    }
    catch(e)
    {}
}
