function autoSizeImg(Contents,offsetWidth,offsetHeight,vlmiddle){
	//debugger;
	if ( !Contents ) return;
	var o = Contents.getElementsByTagName("img");
	var cwidth = window.getComputedStyle?window.getComputedStyle(Contents,null).width:Contents.currentStyle["width"];
	var cheight = window.getComputedStyle?window.getComputedStyle(Contents,null).height:Contents.currentStyle["height"];
	var ncwidth = parseInt(cwidth);
	var ncheight = parseInt(cheight);
	if ( ncwidth==NaN || ncheight==NaN ) {
		alert(ncwidth + "," + ncheight);
		return;
	}
	for(var i=0;i<o.length;i++){
		var img=o[i];
		var iw=img.width;
		var ih=img.height;
		var newiw = iw;
		var newih = ih;
		if ( iw==0 || ih==0 ) continue;
		if(iw>ncwidth-offsetWidth){
			var nw=ncwidth-offsetWidth;
			newiw = nw;
			newih = (nw * ih) / iw;
		}
		
		iw = newiw;
		ih = newih;
		
		if(iw<=ncwidth-offsetWidth && ih>ncheight-offsetHeight){
			var nh=ncheight-offsetHeight;
			newih=nh;
			newiw=(nh*iw)/ih;
		}
		
		if (vlmiddle) {
			img.style.marginTop= ((ncheight-newih)/2)+"px";
			img.style.marginLeft = ((ncwidth-newiw)/2) + "px";
		}
		img.style.visibility = 'visible';
		img.style.width = newiw + 'px';
		img.style.height = newih + 'px';
		Contents.style.paddingTop = '0px';
		Contents.style.paddingLeft = '0px';
	}
}

function trim(value) {
	if( value == undefined ) return "";
	return value.replace( /^\s*/, "").replace( /\s*$/, "");
}

function rebuildResource( sourceTa, output, baseUrl, sso4dl )
{
	var _value = trim( document.getElementById(sourceTa).value );
	if( _value == "" ) return;
	
	var outputVal = "<ul>";
	
	var regex = /(\d*) (.*)____(.*)____(.*)____(.*)/g;
	var matchGroup = [];    
	while(matchGroup = regex.exec( _value )) {
		outputVal += "<li>" + rebuildSingleResource( matchGroup, baseUrl, sso4dl ) + "</li>";
	}
	
	outputVal += "</ul>";
	
	var _output = document.getElementById( output );
	_output.innerHTML = outputVal;
}

function rebuildSingleResource( matchGroup, baseUrl, sso4dl )
{
	var brType = parseInt( matchGroup[1], 10 );
	var brId = matchGroup[2];
	var resourceType = trim(matchGroup[3]);
	var dicName = matchGroup[4];
	var encDicname = encodeURIComponent( dicName );
	var title = matchGroup[5];
	
	var titleT = substring2( title, 30, dicName );
	
	if( brType == 1 ){
		return "<a href=\""+baseUrl+"/resourceSearch?_method=oneTypeResource&brResourceType="+resourceType+
			"&dicName="+encDicname+"\" target=\"_blank\"><span class=\"blue\">["+dicName+"]</span></a>" +
			"<a href=\"javascript:downloadResource('"+brId+"','"+title+"','"+baseUrl+"/');\" title=\"["+dicName+"]"+title+"\">"+titleT+"</a>";
	}
	else if( brType == 2 ){
		return "<a href=\""+baseUrl+"/resourceSearch?_method=oneTypeResource&brResourceType="+resourceType+
			"&dicName="+encDicname+"\" target=\"_blank\"><span class=\"blue\">["+dicName+"]</span></a>" +
			"<a href=\""+baseUrl+"/resourceDownload?_method=downloadFreeResource&brId="+brId+"&"+sso4dl+"\" target=\"_blank\" title=\"["+dicName+"]"+title+"\">"+titleT+"</a>";
	}
	else if( brType == 3 ){
		return "<a href=\""+baseUrl+"/resourceSearch?_method=oneTypeResource&brResourceType="+resourceType+
			"&dicName="+encDicname+"\" target=\"_blank\"><span class=\"blue\">["+dicName+"]</span></a>" +
			"<a href=\""+baseUrl+"/resourceDownload?_method=downloadFreeResource&brId="+brId+"&"+sso4dl+"\" target=\"_blank\" title=\"["+dicName+"]"+title+"\">"+titleT+"</a>";
	}
}

function substring2( source, bytelen, preserve )
{
	var preserveLen = strlen2( preserve );
	var sourceLen = strlen2( source );
	
	if( preserveLen > bytelen ) return "...";
	
	var leftLen = bytelen - preserveLen;
	if( leftLen > sourceLen ){
		return source;
	}
	else{
		return strtrim2( source, leftLen ) + "...";
	}
}

function strlen2(str)
{
	if (str == undefined)
		return 0;
	
	var totalCount = 0;  

	for (var i = 0; i < str.length; i++) {
		var c = str.charCodeAt(i);
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			totalCount++;
		} else {
			totalCount += 2;
		}
	}
	
	return totalCount;
}

function strtrim2( str, bytelen )
{
	var newStr = "";
	var totalCount = 0;
	for( var i = 0;i<str.length;i++ ){
		var c = str.charCodeAt(i);
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			totalCount++;
		} else {
			totalCount += 2;
		}
		
		if( totalCount<bytelen )
			newStr += str.charAt(i);
		else
			break;
	}
	
	return newStr;
}



