﻿function   toUTF8(szInput){ 
  var   wch,x,uch="",szRet="";   
    
  for   (x=0;   x<szInput.length;   x++){   
  wch=szInput.charCodeAt(x);   
  if   (!(wch   &   0xFF80)){   
  szRet   +=   szInput.charAt(x);   
  }else   if   (!(wch   &   0xF000)){   
  uch   =   "%"   +   (wch>>6   |   0xC0).toString(16)   +     
      "%"   +   (wch   &   0x3F   |   0x80).toString(16);   
  szRet   +=   uch;     
  }else{   
  uch   =   "%"   +   (wch   >>   12   |   0xE0).toString(16)   +     
      "%"   +   (((wch   >>   6)   &   0x3F)   |   0x80).toString(16)   +   
      "%"   +   (wch   &   0x3F   |   0x80).toString(16);   
  szRet   +=   uch;     
  }   
  }   
   return(szRet);   
  }   
    

	function notice(s,theForm)
{
	alert(s);
	theForm.searchword.focus();
	return false;
}

function LTrim(s)
{
    for(var i=0;i<s.length;i++)
        if(s.charAt(i)!=' ')
         return s.substring(i,s.length);
     return "";
}

function RTrim(s)
{
    for(var i=s.length-1;i>=0;i--)
        if(s.charAt(i)!=' ')
            return s.substring(0,i+1);
        return "";
}

function Trim(s)
{
    return RTrim(LTrim(s));
}


function getsearchword(s)
{

	var _keyword = s;
	
	_keyword = Trim(_keyword);
	var _kwArray = _keyword.split(" ");
	
	for(var i=0;i<_kwArray.length;i++)
	{
		if(i==0)
		{
			_searchword = Trim(_kwArray[0]);
		}
		else
		{
			if(_kwArray[i]=="") 
			{   
				_searchword = _searchword; 			
			}
			else
			{ 
				_searchword = _searchword + "*" + Trim(_kwArray[i]) ;			
			}

		}

	}
	

		return (_searchword);;
	   
	
}

function submitsearch(theForm)
{

	var _keyword =theForm.searchword.value;

	_keyword = Trim(_keyword);
	if(_keyword.length>0&_keyword!="请输入关键字")
	{
		if(_keyword.indexOf("+")>=0)
		{
			return notice("搜索词中不能含有关键字'+'",theForm);
		}
		else if(_keyword.indexOf("*")>=0)
		{
			return notice("搜索词中不能含有关键字'*'",theForm);
		}
		else if(_keyword.indexOf("-")>=0)
		{
			return notice("搜索词中不能含有关键字'-'",theForm);
		}
		else if(_keyword.indexOf("(")>=0)
		{
			return notice("搜索词中不能含有关键字'('",theForm);
		}
		else if(_keyword.indexOf(")")>=0)
		{
			return notice("搜索词中不能含有关键字')'",theForm);
		}
		else if(_keyword.indexOf("=")>=0)
		{
			return notice("搜索词中不能含有关键字'='",theForm);
		}
		else if(_keyword.indexOf(";")>=0)
		{
			return notice("搜索词中不能含有关键字';'",theForm);
		}
		else if(_keyword.indexOf("；")>=0)
		{
			return notice("搜索词中不能含有关键字'；'",theForm);
		}
		else
		{
			gbtoUTF8(theForm);
			
		}
	}
	else
	{
			return notice("搜索词不能为空",theForm);
	}
	
}
function   gbtoUTF8(theForm){

var searchword=theForm.searchword.value;
var channelid=theForm.channelid.value;
searchword=getsearchword(searchword)
searchword=toUTF8(searchword);
var url="/wasdemo/search";
url=url+"?searchword="+searchword+"&channelid="+channelid+"&templet=search_whxx.jsp";
theForm.method="get";
theForm.action=url;
theForm.target="_blank";
//theForm.submit();
} 
function keyEnter(iKeyCode,theForm) 
	{
		
		if(iKeyCode==13) {
		
			 submitsearch(theForm);
			 theForm.submit();
return false;
		}
//return true;
	}

