﻿
//*//////////////////////////////////////////////////////////

//自动调整图片大小，超过限制则自动按比例缩小。
//例子：
// <img src="demo.jpg" width="50" height="50" onerror="this.src='nopic.gif'" onload="imgAutoSize(this)" />  限定显示 宽度 和 高度。
// <img src="demo.jpg" width="0" height="0" onload="imgAutoSize(this,50,50)" />  限定显示 宽度 和 高度。
// <img src="demo.jpg" width="0" height="0" onload="imgAutoSize(this,50,-1)" />  限定显示 宽度，不限 高度。
// <img src="demo.jpg" width="0" height="0" onload="imgAutoSize(this,50,50,1)" />  限制 宽度 和 高度，当图片尺寸大于限定显示尺寸时，自动添加超链接。
function imgAutoSize(elemImg, MaxWidth, MaxHeight,display)
{
    var img = new Image();
	img.src = elemImg.src;
	var w = MaxWidth;
	var h = MaxHeight;
	var w2 = img.width;
	var h2 =  img.height;
	var scale = w2 / h2;
	if(w2 < w && h2 < h){
		elemImg.style.display = 'inline';
		return;
	}
	if(w2 >= w){
		w2 = w;
		h2 = w2 / scale;
	}
	if(h2 >= h){
		h2 = h;
		w2 = h2 * scale;
	}
	
	elemImg.style.display = display ? display : 'inline';
	elemImg.width = Math.round(w2);
	elemImg.height = Math.round(h2);
}

//////////////////////////////////////////////////////////*/



//在指定ImgObject显示图片，并且自动调整图片大小
// ImgObject： 显示对象 或者 对象ID
//例子：
//  <a id="lnk01" href="#"><img id="img01" alt="" src="" align="right" /></a>
//  <input type="file" id="UploadFile1" runat="server"
//         onpropertychange="if(event.propertyName=='value'){imgShow(img01,100,100,this.value);}" />
//  <input type="file" id="UploadFile2" runat="server"
//         onpropertychange="if(event.propertyName=='value'){imgShow('img01',100,100,this.value);}" />
//  <input type="file" id="UploadFile3" runat="server"
//         onpropertychange="if(event.propertyName=='value'){imgShow('lnk01.childNodes[0]',100,100,this.value);}" />
function imgShow(ImgObject,MaxWidth,MaxHeight,ImgSrc)
{
	//alert(ImgObject);
	//alert(ImgSrc);
	var o;
	if(typeof(ImgObject)=="string")
	{
		//alert("string "+ImgObject.indexOf("."));
		if(ImgObject.indexOf(".")<0) o=document.getElementById(ImgObject);
		else o=eval(ImgObject); //alert(eval(ImgObject)); //
	}
	else if(typeof(ImgObject)=="object")
	{
		//alert("object");
		o=ImgObject;
	}
	else return;
	//var o=document.getElementById(ImgObject);
	o.src=ImgSrc;

	var img=new Image();
	img.src=ImgSrc;
	var rate;
    if (img.width>MaxWidth)
    {
        rate=MaxWidth/img.width;
        o.height=img.height*rate;
        o.width=MaxWidth;
    }
    if (img.height>MaxHeight)
    {
        rate=MaxHeight/img.height;
        o.width=img.width*rate;
        o.height=MaxHeight;
    }
	
	//setTimeout("imgAutoSize('"+ImgObject+"',"+MaxWidth+","+MaxHeight+");alert('[string] "+ImgObject+","+MaxWidth+","+MaxHeight+"');",500);
}


//img对象OnError处理函数
//例子1：
//<img onerror="imgError(this,'nopic.gif')" src="" />
//例子2：
//<input type="text" id="txtPicture" value="sample.gif" />
//<img onerror="imgError(this,'txtPicture')" src="" />
//<img onerror="imgError(this,'txtPicture','nopic.gif')" src="" />
function imgError(oImg,oTxt,oErr)
{
	if(typeof(oImg)=="string"){
		try{ oImg = document.getElementById(oImg); }
		catch(e){ oImg = event.srcElement; }
	}else if(typeof(oImg)!="object") oImg = event.srcElement;
	
	var ErrorCount=0;
	try{ ErrorCount=parseInt("0"+oImg.getAttribute("ErrorCount")); }
	catch(e){}
	
	//alert("1.o="+o+"\n\nHTML=\n\n"+oImg.innerHTML);
	if(oTxt)
	{
		var sTxtImg="";
		var sErrImg="";

		var o=oTxt;
		if(typeof(o)=="string")
			oTxt = document.getElementById(o);

		try{ sTxtImg = oTxt.value; }
		catch(e){ if(typeof(o)=="string") sTxtImg = sErrImg = o+""; }

		if(typeof(oErr)=="string") sErrImg = oErr + "";
			
		//alert("[0] id："+oImg.id+"\n sTxtImg="+sTxtImg+"\n sErrImg="+sErrImg+"\n 第"+(ErrorCount+1)+"次出错\n src："+oImg.src);
		if(ErrorCount<1){
			//第1次出错，即oImg.src（第1个参数）引发。
			//alert("[1] id："+oImg.id+"\n 第"+(ErrorCount+1)+"次出错\n src："+oImg.src);
			oImg.setAttribute("ErrorCount","1");
			if(sTxtImg){
				oImg.src = sTxtImg;
				//alert(" src=\n"+oImg.src+"\n\n sTxtImg=\n"+sTxtImg);
			}else if(sErrImg){
				oImg.src = sErrImg;
				//alert(" src=\n"+oImg.src+"\n\n sErrImg=\n"+sErrImg);
			}
			return;
		}else if(ErrorCount<2){
			//第2次出错，即oTxt.value（第2个参数）引发。
			//alert("[2] id："+oImg.id+"\n 第"+(ErrorCount+1)+"次出错\n src："+oImg.src);
			oImg.setAttribute("ErrorCount","2");
			oImg.src = sErrImg;
			return;
		}else{
			//第3次出错，即（第3个参数）引发的。
			//alert("[3] id："+oImg.id+"\n 第"+(ErrorCount+1)+"次出错\n src："+oImg.src);
			oImg.setAttribute("ErrorCount","3");
		}
			
	}

	//alert("3次出错!!!!!");
	try{
		oImg.width=0;
		oImg.height=0;
	}catch(e){}
}

//移除图片
function removePicture(txt,img)
{
	document.getElementById(txt).value='';
	document.getElementById(img).src='/images/NoPic.gif';
}


