function ResizeImage(imageDest, W, H)
{
  var image = new Image();
  image.src = imageDest.src;
  if(image.width>0 && image.height>0)
  {
    if(image.width/image.height >= W/H)
    {
      if(image.width > W)
      {
        imageDest.width = W;
        imageDest.height = (image.height*W)/image.width;
      }
      else
      {
        imageDest.width = image.width;
        imageDest.height = image.height;
      }
    }
    else
    {
      if(image.height > H)
      {
        imageDest.height = H;
        imageDest.width = (image.width*H)/image.height;
      }
      else
      {
        imageDest.width = image.width;
        imageDest.height = image.height;
      }
    }
  }
}

function DownImage(ImgD,MaxWidth,MaxHeight,alignX,alignY)
{
	alignX=alignX||1;
	alignY=alignY||1;
    var image=new Image();
    image.src=ImgD.src;
    if(image.width>0 && image.height>0)
	{
		var rate = (MaxWidth/image.width < MaxHeight/image.height)?MaxWidth/image.width:MaxHeight/image.height;
		var wid=0;
		var hid=0;
		if(rate <= 1)
		{
			wid=ImgD.width=image.width*rate;
			hid=ImgD.height=image.height*rate;
		}
		else
		{
			wid=ImgD.width=image.width;
			hid=ImgD.height=image.height;
		}
		if(alignX==1)
		{
			var ii=(MaxWidth-wid)/2;
			ImgD.style.marginLeft=ImgD.style.marginRight=ii+"px";
		}
		if(alignY==1)
		{
			var ii=(MaxHeight-hid)/2;
			ImgD.style.marginTop=ImgD.style.marginBottom=ii+"px";
		}
    }
}

