String.prototype.lenB   =   function(){return   this.replace(/[^\x00-\xff]/g,"**").length;}
String.prototype.GetLimitString = function(bytelen,limitstr,limitlen)
{
	if(this.lenB()<= bytelen+limitlen)
		return this;
	var i=0,j=0;
	var newstr = "",tmp="";
	while(i<bytelen)
	{
		tmp = this.substr(j,1);
		if(tmp.lenB()>1)
		{
			i++;
		}
		newstr += tmp;
		i++;
		j++;
	}
	return newstr+limitstr;
}

String.Format = function() {
 
 if(arguments.length == 0)
  return "";
 
 if(arguments.length == 1)
  return arguments[0];
 
 var reg = /{(\d+)?}/g;
 var args = arguments;
 var result = arguments[0].replace(
  reg,
  function($0, $1) {
   return  args[parseInt($1)+1];
  }
 )

 return result;
}

//图片最大宽1
var varPicMaxWidth1 = 600;
//图片最大宽2
var varPicMaxWidth2 = 300;
// 图片缩放脚本
var flag=true; 
function DrawImage(ImgD){ 
    var image=new Image(); 
    image.src=ImgD.src; 
    if(image.width>0 && image.height>0){ 
        flag=true; 
        if(image.width>=varPicMaxWidth1){ 
            ImgD.width=varPicMaxWidth1; 
            ImgD.height=(image.height*varPicMaxWidth1)/image.width; 
        }else{ 
            ImgD.width=image.width; 
            ImgD.height=image.height; 
        }  
    } 
} 
// 图片缩放脚本
var flag=true; 
function DownImage(ImgD){ 
    var image=new Image(); 
    image.src=ImgD.src; 
    if(image.width>0 && image.height>0){ 
        flag=true; 
        if(image.width>=varPicMaxWidth2){ 
            ImgD.width=varPicMaxWidth2; 
            ImgD.height=(image.height*varPicMaxWidth2)/image.width; 
        }else{ 
            ImgD.width=image.width; 
            ImgD.height=image.height; 
        }  
    } 
} 
function cgimg(o){
var zoom=parseInt(o.style.zoom, 10)||100;
zoom+=event.wheelDelta/12;
if(o.width*zoom/100 > varPicMaxWidth1)
	return false;
if (zoom>0) o.style.zoom=zoom+'%';
return false;
}


function procSelectAll(isChecked)
{
  var i;
  i=1;
  while(true)
  {
     obj=eval('window.ctlForm1.chid'+i);
	 if(obj)
	  obj.checked=isChecked;
	 else
	   break;
	 i++;
  }
}

function procBindSelect(objArr,objSelect,SelectedId)
{
	var oOption;
    var i=0;

	objSelect.options.length = 0;
	oOption = document.createElement("OPTION");
		oOption.text='-请选择-';
		oOption.value='';
		objSelect.add(oOption);
	while(objArr[i])
	{
		oOption = document.createElement("OPTION");
		oOption.text=objArr[i][1];
		oOption.value=objArr[i][0];
		objSelect.add(oOption);
		i++;
	}
	if(SelectedId>0)
		objSelect.value=SelectedId;
}

function procBindName(objArr,selectedid)
{
	for(var i=0;i<objArr.length;i++)
	{
		if(objArr[i][0] == selectedid){
			document.write(objArr[i][1]);
			return;
		}
	}
	return "";
}

function procBindSelect2(objArr,Selected)
{
	var oOption = '<option value="" >返回</option>';

	for(var i=0;i<objArr.length;i++)
	{
		if(objArr[i] != ''){
		if(objArr[i] == Selected)
		{
			oOption += '<option value="'+objArr[i]+'" selected>'+objArr[i]+'</option>';
		}
		else
		{
			oOption += '<option value="'+objArr[i]+'" >'+objArr[i]+'</option>';
		}
		}
	}
	document.write(oOption);
}

//history.go(-1)
function GoBack(url)
{
var lasturl = document.referrer;
if(lasturl != null && lasturl != "")
{
	history.go(-1);
}
else
{
	if(url != null && url != "")
		window.location.href=url;
}
}

//language
function ChangeLanguage(langurl)
{
	if(langurl != null && langurl != "")
		window.location.href = langurl;
}
function DoSearchByType()
{
	var search = document.getElementById('header_search_text').value;
	if(search)
		search = search.replace(" ","");
	var type = document.getElementById('header_search_type').value;
	if(type == 0)
		window.location.href = String.Format(electronicNameSearchUrl,escape(search));
	else if(type==2)
		window.location.href = String.Format(electronicMfgSearchUrl,escape(search));
	else
		window.location.href = String.Format(newsTitleSearchUrl,escape(search));
}

function SubmitPager(url,pageindex)
{
	window.location.href = String.Format(url,pageindex);
}

function InitSelected(id,value)
{
	var elem = document.getElementById(id);
	if(elem)
		elem.value = value;
}

function InitChecked(id,value)
{
	var elem = document.getElementById(id);
	if(elem){
		elem.checked = value;
	}
}

function ShowTopWindow(url,width,height)
{
	return window.showModalDialog(url,'','dialogWidth:'+width+'px;dialogHeight:'+height+'px;center:1;status=0;help:0;');
}

function ShowTopWindow2(url,args,width,height)
{
	return window.showModalDialog(url,args,'dialogWidth:'+width+'px;dialogHeight:'+height+'px;center:1;status=0;help:0;');
}

function opconfirm()
{
	return window.confirm('是否要执行操作？');
}

function confirmAndSubmit(f)
{
	if(opconfirm())
	{
		document.getElementById(f).submit();
	}
}

function $() 
{ 
  var elements = new Array(); 
  for (var i = 0; i < arguments.length; i++) 
  { 
    var element = arguments[i]; 
    if (typeof element == 'string') 
      element = document.getElementById(element); 
    if (arguments.length == 1) 
      return element; 
    elements.push(element); 
  } 
  return elements; 
}

function generateGuid2(format)
{
	var hex = new Array('0','1','2','3','4','5','6','7','8', '9','a','b','c','d','e','f');

	var outB = '';
	if(format == 'D')
		outB = '{';
				
	for (count = 0; count < 32; count++)
	{
		if ((count == 8) || (count == 12) || (count == 16) || (count == 20))
		outB += '-';
						
		outB += hex[Math.floor(Math.random() * 16)];
	}
	if(format == 'D')
		return outB.toUpperCase() + '}';
	else
		return outB.toUpperCase();
}

function procSelectAll(isChecked)
{
  var i;
  i=1;
  while(true)
  {
     obj=$('chid'+i);
	 if(obj)
	  obj.checked=isChecked;
	 else
	   break;
	 i++;
  }
}

function SubmitIt(whichForm,paramName,paramValue)
    {
        if(opconfirm()){
			if(!document.getElementById(paramName))
			{
				var input = new Element('input', { name: "ctlAdmin", id: "ctlAdmin", style:"display:none" }); 
				document.getElementById(whichForm).appendChild(input);
			}
            document.getElementById(paramName).value = paramValue;
            document.getElementById(whichForm).submit();
        }
    }
    
    function SubmitKnown(paramValue)
    {
        SubmitIt('ctlform1','ctlAdmin',paramValue);
    }
	function CheckAndSubmitThis(checkwho,v)
	{
		procSelectAll(false);
		document.getElementById(checkwho).checked = true;
		SubmitKnown(v);
	}

	function RemoveQuerParam(param)
{
	var search = document.location.href.split('?')[0]+"?";
	var aParams = document.location.search.substr(1).split('&') ;
	for (i=0 ; i < aParams.length ; i++) {
		if(aParams[i].replace(" ","") != "" && aParams[i].replace(" ","").indexOf(param+"=") != 0){
			search += '&'+aParams[i];
		}
	}
	return search;
}

function RemoveQuerParam2(search,param)
{
    if(search.indexOf('?') < 0)
        search += '?';
	var search1 = search.split('?')[0]+"?";
	var aParams = search.split('?')[1].split('&') ;
	for (i=0 ; i < aParams.length ; i++) {
		if(aParams[i].replace(" ","") != "" && aParams[i].replace(" ","").indexOf(param+"=") != 0){
			search1 += '&'+aParams[i];
		}
	}
	return search1;
}

function ReBuildQuerParam(param,value)
{
	var search = RemoveQuerParam(param)
	search = search+"&"+param+"="+value;
	search = search.replace("&&","&");
	return search;
}

function ReBuildQuerParam2(search,param,value)
{
	search = RemoveQuerParam2(search,param)
	search = search+"&"+param+"="+value;
	search = search.replace("&&","&");
	return search;
}

function AddParamToQuer(search,param,value)
{
	if(search.indexOf("?")==-1)
		search += "?";
	search = search + "&"+param+"="+value;
	search = search.replace("&&","&");
	return search;
}

//var staticBadWrodsArr = new Array(' ');
function GetFormatedName(value)
{
    value = value.replace(/ /g,'-');
    value = value.replace(/~/g,'-');
    value = value.replace(/@/g,'-');
    value = value.replace(/#/g,'-');
    //value = value.replace(/$/g,'-');
    value = value.replace(/%/g,'-');
    //value = value.replace(/^/g,'-');
    value = value.replace(/&/g,'-');
    value = value.replace(/\*/g,'-');
    value = value.replace(/\\/g,'-');
    value = value.replace(/\//g,'-');
    value = value.replace(/=/g,'-');
    value = value.replace(/\?/g,'-');
    value = value.replace(/`/g,'-');
    value = value.replace(/\+/g,'-');
    value = value.replace(/\:/g,'-');
    return value;
}

function OrderBy(colName)
{
	colName = colName.toLowerCase();
	var flag = false;
	var ab = 'a';
	flag = (document.location.search.substr(1).toLowerCase().indexOf('order='+colName)>-1);
	if(flag)
	{
		if(document.location.search.substr(1).toLowerCase().indexOf('ab=a')==0 || document.location.search.substr(1).toLowerCase().indexOf('&ab=a')>=0)
		{
			ab = 'b';
		}
		else
		{
			ab = 'a';
		}
	}
	var search = document.location.href.split('?')[0]+"?";
	search = RemoveQuerParam2(search,'order');
	search = RemoveQuerParam2(search,'ab');
	search = search+"&order="+colName;
	search = search+"&ab="+ab;
	search = search.replace("&&","&");
	window.location.href = search;
}

function OrderByWithAB(colName,ab)
{
	colName = colName.toLowerCase();
	var flag = false;
	flag = (document.location.search.substr(1).toLowerCase().indexOf('order='+colName)>-1);
	if(flag)
	{
		if(document.location.search.substr(1).toLowerCase().indexOf('ab=a')==0 || document.location.search.substr(1).toLowerCase().indexOf('&ab=a')>=0)
		{
			ab = 'b';
		}
		else
		{
			ab = 'a';
		}
	}
	var search = document.location.href.split('?')[0]+"?";
	search = RemoveQuerParam2(search,'order');
	search = RemoveQuerParam2(search,'ab');
	search = search+"&order="+colName;
	search = search+"&ab="+ab;
	search = search.replace("&&","&");
	window.location.href = search;
}

function BuildOrderLink(colName,dispName)
{
	colName = colName.toLowerCase();
	var ab = 'a';
	var showCross = "";
	if(document.location.search.substr(1).toLowerCase().indexOf('order='+colName)>-1)
	{
		if(document.location.search.substr(1).toLowerCase().indexOf('ab=b')==0 || document.location.search.substr(1).toLowerCase().indexOf('&ab=b')>=0)
		{
			ab = 'a';
			showCross = "↓";
		}
		else
		{
			ab = 'b';
			showCross = "↑";
		}
	}
	//var search = document.location.href.split('?')[0]+"?";
	var search = document.location.href;
	if(search.indexOf('?') == -1)
		search += '?';
	search = RemoveQuerParam2(search,'order');
	search = RemoveQuerParam2(search,'ab');
	search = search+"&order="+colName;
	search = search+"&ab="+ab;
	search = search.replace("&&","&");
	var link = '<a href="'+search+'">'+dispName.replace('↓','').replace('↑','')+showCross+'</a>';
	return link;
}
function BuildAllOrderLinks(objTr)
{
	for (var i=0;i<objTr.cells.length ;i++ )
	{
		var order = objTr.cells[i].getAttribute("ordercol");
		if(order && order != "")
		{
			var linkhtml = BuildOrderLink(order,objTr.cells[i].innerText);
			objTr.cells[i].innerHTML = linkhtml;
		}
	}
}
