/**
 * 给指定表单中指定名称的复选框置选中标志
 * @param formObj    - 表单对象
 * @param eleNameArr - 存放指定元素名称的数组
 * @checkedFlag      - 选中标志
 * @param 
 */
function setCheckedFlag(formObj,eleNameArr,checkedFlag){
  for(var i=0;i<formObj.elements.length;i++){  
     var ele = formObj.elements[i];
     for(var j=0;j<eleNameArr.length;j++){
       if(ele.name == eleNameArr[j]){
          ele.checked = checkedFlag;
          break;
       }	
     }
  }
}


/**
 * 获得指定表单中指定名称的复选框的选中数量
 * @param formObj    - 表单对象
 * @param eleNameArr - 存放指定元素名称的数组
 * @param 
 */
function getCheckedRecordNum(formObj,eleNameArr){

  var checkedNum = 0;
  for(var i=0; i<formObj.elements.length; i++){

     var ele = formObj.elements[i];
     for(var j=0;j<eleNameArr.length;j++){
       if(ele.name == eleNameArr[j] && ele.checked){
          checkedNum ++;
          break;
       }	
     }	
  }
  
  //
  return checkedNum;
}

/**
 * 去除字符串头尾的空格
 * @param str 被TRIM的字符串
 */
function trim(str){
   return(trimExt(str,' '));	
}


/**
 * 去除字符串头尾的指定的符号
 * @param str 被TRIM的字符串
 * @param ch 指定的字符
 */
function trimExt(str,ch){
   if(str == null) return(null);
   	
   //去除头上的空格	
   var start = 0,end = 0;   //记录字符串的起始和结束位置
   var i = 0;
   while(i<str.length){
   	  //判断是否为空格
   	  if(str.charAt(i) == ch){
   	  	i++;
   	  }else{
   	    start = i;
   	    break;
   	  }
   }
   
   i = str.length -1;
   while(i>=0){
   	  //判断是否为空格
   	  if(str.charAt(i) == ch){
   	  	i--;
   	  }else{
   	    end = i + 1;
   	    break;
   	  }
   }   
   
   return(str.substring(start,end));
}


/**
 * 检查是否整数
 * @param value   要检查的值
 * @param return  是整数就返回TRUE
 */
function isNumber(value){
	if(trim(value)!=''&&!isNaN(value)&&(value.indexOf('.')==-1)){
		return(true);
	}
	
	return(false);
}

/**
 * 根据原先的值初始化下拉列表框
 * @param selectObj - 下拉列表框对象
 * @param oldValue  - 原先的值
 */
function initSelectWithValue(selectObj,oldValue){
  for(var i=0;i<selectObj.length;i++){
    if(selectObj.options[i].value == oldValue){
      selectObj.options[i].selected = true;
      break;
    }
  }
}

/**
 * 在搜索结果页面根据原先的值初始化下拉列表框
 *在其它的页面根据页面的类型设置初始值
 * @param selectObj - 下拉列表框对象
 * @param oldValue  - 原先的值
 *@param searchtype -搜索类型
 */


function initGsSelectWithValue(selectObj,oldValue,searchtype){
 if("offer_en"==searchtype||"product_en"==searchtype){
 	for(var i=0;i<selectObj.length;i++){
    if(selectObj.options[i].value == searchtype){
      selectObj.options[i].selected = true;
      break;
    }
 }
 }
 else{
  for(var i=0;i<selectObj.length;i++){
    if(selectObj.options[i].value == oldValue){
      selectObj.options[i].selected = true;
      break;
    }
  }
  }
}

/**
 * javascript中的urlencode函数
 * 因为js中的escape的处理和java中的encode不完全一样
 * 除了才有了这个函数
 * @param str - 需要被encode的字符串
 */
function encode(str){
  if(str == null) return '';

  //先做一次编码
  var retStr = escape(str);

  //处理url中所有的+号，该字符escape中不处理
  if(str.indexOf('+') != -1){
    retStr = retStr.replace(/\+/g,'%2B');
  }
  
  //
  return retStr;
}
 

/**
 * 编码
 * 这个函数暂时不需要  
 * @param str - 
 */
function XMLEncode(str){
     str = trim(str);
     str = str.replace("&","&amp;");
     str = str.replace("<","&lt;");
     str = str.replace(">","&gt;");
     str = str.replace("'","&apos;");
     str = str.replace("\"","&quot;");
     return str;
}

/**
 * 解码
 * 这个函数暂时不需要  
 * @param str - 
 */
function XMLDecode(str){
     str = trim(str);
     str = str.replace("&amp;","&");
     str = str.replace("&lt;","<");
     str = str.replace("&gt;",">");
     str = str.replace("&apos;","'");
     str = str.replace("&quot;","\"");
     return str;
}

/**
 * 调节图片的长度和宽度做限制
 * 长度和宽度哪个超出就限制哪个
 * img src="image" onload="adjustImageSize(this,45,160)"
 * @param imageObj  - img 控件
 * @param maxHeight - 图片长度限制
 * @param maxWidth  - 图片宽度限制
 */
function adjustImageSize(imageObj,maxHeight,maxWidth){
  if(maxHeight<imageObj.height){
    imageObj.height=maxHeight;
  }
  if(maxWidth<imageObj.width){
    imageObj.width=maxWidth;
  }
}

/**
 * 调节图片的长度和宽度做限制
 * 长度和宽度哪个超出就限制哪个，如果都不超出则原尺寸
 * img src="image" onload="setImgSize(this.src,this,[size])"
 * @param imageObj  - img 控件
 * @sizeStand  - 图片标准高宽
 */

function setImgSize(theURL,sImage){
var imgObj;
var sizeStand = 180;
if(arguments.length>=3){
sizeStand = arguments[2];
}
imgObj = new Image();
imgObj.src = theURL;

if ((imgObj.width != 0) && (imgObj.height != 0)) {
	
	if(imgObj.width>sizeStand || imgObj.height>sizeStand){
		if(imgObj.width > imgObj.height) { 
		var iHeight = imgObj.height*sizeStand/imgObj.width;
		sImage.height = iHeight;
		sImage.width = sizeStand;
		} else {
		var iWidth = imgObj.width*sizeStand/imgObj.height;
		sImage.width = iWidth;
		sImage.height= sizeStand;
		}
	}else{
	sImage.width=imgObj.width;
	sImage.height=imgObj.height;
	}

}else{
sImage.width = sizeStand;
sImage.height= sizeStand;
}
}


/**
 * 调节图片的长度和宽度做限制,自定义高宽
 * 长度和宽度哪个超出就限制哪个，如果都不超出则原尺寸
 * img src="image" onload="setImgSizeHW(this.src,this,sizew,sizeh)"
 * @param theURL  - img 地址
 * @param sImage  - img object
 * @param imgW  - 限制宽度
 * @param imgH  - 限制高度
 */

function setImgSizeWH(theURL,sImage,imgW,imgH){
var imgObj;
imgObj = new Image();
imgObj.src = theURL;
if ((imgObj.width != 0) && (imgObj.height != 0)) {
	
	if(imgObj.width>imgW || imgObj.height>imgH){
		
		var iHeight = imgObj.height*imgW/imgObj.width;
		
		if(iHeight<=imgH){
			sImage.width=imgW;
			sImage.height=iHeight;
		}else{
		var iWidth=imgObj.width*imgH/imgObj.height;
		sImage.width=iWidth;
		sImage.height=imgH;
		}
	}else{
	sImage.width=imgObj.width;
	sImage.height=imgObj.height;
	}

}else{
sImage.width = imgW;
sImage.height= imgH;
}
}





/**
 * 调节图片的长度和宽度做限制
 * 长度和宽度哪个超出就限制哪个，如果都不超出则原尺寸
 * img src="image" onload="setImgSize(this.src,this,[size])"
 * @param imageObj  - img 控件
 * @sizeStand  - 图片标准高宽
 */

function setBigImgSize(theURL,sImage){
var imgObj;
var sizeStand = 360;
if(arguments.length>=3){
sizeStand = arguments[2];
}

imgObj = new Image();
imgObj.src = theURL;
imgObj.onLoad = setBigImgInner(imgObj,sImage,sizeStand);

}

/*
 * 当图片有onmousewheel时，调节外部容器属性
 * img src="image" onmousewheel="return bbimg(this)" onLoad="setGsBigImgSize(this.src,this);fixOverflowBox('big_image_box',700,500);"
 * @param objID  - 容器ID
 * @maxWidth  - 1024下的宽度
 * @minWidth  - 800下的宽度
*/
function fixOverflowBox(objID,maxWidth,minWidth){
obj=document.getElementById(objID);
if(!obj){return;}
obj.style.overflow="hidden";
if (isIEBrowse()){
	if (window.screen.width>=1024){
			obj.style.width=maxWidth+"px";
		}else if (window.screen.width<1024){
			obj.style.width=minWidth+"px";
		}
	}
}

function setBigImgInner(imgObj,sImage,sizeStand){
	if ((imgObj.width != 0) && (imgObj.height != 0)) {
	
			if(imgObj.width>sizeStand || imgObj.height>sizeStand){
				if(imgObj.width > imgObj.height) { 
		var iHeight = imgObj.height*sizeStand/imgObj.width;
		sImage.height = iHeight;
		sImage.width = sizeStand;
		} else {
		var iWidth = imgObj.width*sizeStand/imgObj.height;
		sImage.width = iWidth;
		sImage.height= sizeStand;
		}
	}else{
	sImage.width=imgObj.width;
	sImage.height=imgObj.height;
	}

}else{
sImage.width = sizeStand;
sImage.height= sizeStand;
}
}

		function setGsBigImgSize(theURL,sImage){
		var imgObj;
		var sizeStand = 360;
		imgObj = new Image();
		imgObj.src = theURL;
		imgObj.onLoad = setGsImgInner(imgObj,sImage,sizeStand);
		}
		
		
		function setGsImgInner(imgObj,sImage,sizeStand){
			if ((imgObj.width != 0) && (imgObj.height != 0)) {
				if(imgObj.width>sizeStand){
				var iHeight = imgObj.height*sizeStand/imgObj.width;
		   	sImage.height = iHeight;
		   	sImage.width = sizeStand;
		 }else{
		 	sImage.width=imgObj.width;
			sImage.height=imgObj.height;
		}
	}else{
			sImage.width = sizeStand;
			sImage.height= sizeStand;
	}
}








function bbimg(o){
	var zoom=parseInt(o.style.zoom, 10)||100;zoom+=event.wheelDelta/12;if (zoom>0) o.style.zoom=zoom+'%';
	return false;
}
//临时跟踪函数开始
function clk(tl)
{if(document.images)
{new Image().src="http://tracelog.www.alibaba.com/null.gif?tracelog="+escape(tl);
}
return true;}
//临时跟踪函数结束

var initText = new Array('Enter product keyword');
function clearSearchText(obj){
	if (obj==null) return ;
	for (var i = 0; i < initText.length; i++) {
	    if (initText[i]==obj.value){
	  	obj.value='';
	  	return ;
	  }
   }
}



function isIEBrowse(){
	var name = navigator.appName;
	if (name == "Microsoft Internet Explorer"){
		return true;
	} else {
       		return false; 	
  } 
}




function adjustScreenWidth(divId){
	 if (document.getElementById(divId)!=null) return ;
	 if (isIEBrowse()){
		  if (window.screen.width>=1024){
		  	 document.write("<div id='"+divId+"' style='width:982px'>");
		  	 document.isBigerScreen = true;
		  }else if (window.screen.width<1024){
		  	document.write("<div id='"+divId+"' style='width:758px'>");
		  }
	 }
	 else{
	 	   document.write("<div id='"+divId+"'>");
	 }
}

function encodeKeyword(str){
     if (str==null) return '';
     str = str.replace(/(^\s*)|(\s*$)/g, "");
     str = str.replace(/(\s+)/g, "_");
     str = encode(str);
     return encode(str);
 }


function loadscript(newSrc,replaceID) {
	var oldscript = document.getElementById(replaceID)
	var newscript = document.createElement("script")
	newscript.type = "text/javascript";
	newscript.id = replaceID;
	newscript.src = newSrc ;
	if (oldscript==null){
	   document.getElementsByTagName('body')[0].appendChild(newscript);
	}
	else{
       document.getElementsByTagName('body')[0].replaceChild(newscript, oldscript); 
	}
}

function tempTrackObj(code){
  if(document.images)
	  {
		 new Image().src="http://tracelog.www.alibaba.com/null.gif?tracelog="+escape(code);
	  }
	return true;
}


function agTrack(code1,code2){
  return tempTrackObj("angel_"+code1+"_"+code2);
}
/**
*解释执行一段html,目前是通过iframe执行的,应该更好的办法
*msdn有这样的说法As of Internet Explorer 5, you can create all elements programmatically, except for frame and iframe
*我测试一下ie6,ff都能正常使用
*
*/
function loadHtml(theSrc){
	if (theSrc==null) return ;
	var iframe = document.createElement("iframe");
	iframe.frameborder = '0';
	iframe.height = '0';
	iframe.width = '0';
	iframe.scrolling='no'
	iframe.style.display="none";
	iframe.src = theSrc;
	document.getElementsByTagName('body')[0].appendChild(iframe);
}

function displayKeyword(isMore){
			if(isMore == 1)
			{
				var div = document.getElementById("hotkeyword");
				div.style.display = "none";
				
				div = document.getElementById("hotkeywordmore");
				div.style.display = "";
			}
			else
			{
				var div = document.getElementById("hotkeywordmore");
				div.style.display = "none";
				
				div = document.getElementById("hotkeyword");
				div.style.display = "";
			}
				
		}
		function DrawImage(ImgD,iwidth,iheight){

	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		flag=true;
		if(image.width/image.height>= iwidth/iheight){
			if(image.width>iwidth){ 
				ImgD.width=iwidth;
				ImgD.height=(image.height*iwidth)/image.width;
			}else{
				ImgD.width=image.width; 
				ImgD.height=image.height;
			}
		}else{
			if(image.height>iheight){ 
				ImgD.height=iheight;
				ImgD.width=(image.width*iheight)/image.height; 
			}else{
				ImgD.width=image.width; 
				ImgD.height=image.height;
			}
		}
	}
}