function cnlength(str){
    return str.replace(/[^\x00-\xff]/gi,'oo').length;
}

function validatekey(objfrm){
	if (objfrm.key.value==""){
		alert("请填写搜索词！");
		objfrm.key.focus();
		return false;
	} else
	{
		if(objfrm.key.value=="全站商品"){
			alert("请填写搜索词！");
			objfrm.key.focus();
			return false;
		} else
	    if(cnlength(objfrm.key.value)<2){
	        alert("搜索词过短，请重新填写！");
		    objfrm.key.focus();
		    return false;
	    } else
	    if(cnlength(objfrm.key.value)>70){
	        alert("搜索词过长，请重新填写！");
		    objfrm.key.focus();
		    return false;
	    }			
	}
	if(window.location.host.toLowerCase().indexOf("search.dangdang.com")!=-1)
	{
	  try{ objfrm.SearchFromTop.value=2; } catch(err){}
	}
	return true;
}

function GotoAllSearch(){
var allsearch=document.bootpagetopSearch;
		if(validatekey(allsearch)){
				allsearch.submit();
			}
	}

function GotoBookSearch(){
var allsearch=document.searchform;
		if(validatekey(allsearch)){
				allsearch.submit();
			}
	}

function sortselect(sortid,obj){
    showtopsearch(sortid,obj);

	var objfrm=document.searchform;
    if(objfrm.key.value != "")
    {
	    if(!validatekey(objfrm))
	        return false;
    	if(objfrm.catalog.value=="mall")
            gotosearch()
        else 
            objfrm.submit();
    }
}


function getposOffset_top(what, offsettype)
{ 
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop; 
    var parentEl=what.offsetParent; 
    while (parentEl!=null)
    { 
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop; 
         parentEl=parentEl.offsetParent; 
    } 
    return totaloffset; 
}

function showgaoji(obj){
    var x=getposOffset_top(obj,'left');
    var y=getposOffset_top(obj,'top');
    var divgjsearch=document.getElementById("gaijisearch");
    if(document.getElementById("otherChannel").innerHTML.length==0)
        document.getElementById("otherChannel").innerHTML='<ul style="list-style:none;"><li class="ddindex_head_li5"><a href="http://book.dangdang.com">图书</a>  <a href="http://music.dangdang.com">音乐</a> <a href="http://movie.dangdang.com">影视</a></li><li class="ddindex_head_li6"></li>'+(isIE_c()?'':'<li class="clear"></li>')+'<li class="ddindex_head_li7"><a href="http://category.dangdang.com/3c">数码家电</a></li><li class="ddindex_head_li7"><a href="http://category.dangdang.com/beauty">美　　妆</a></li><li class="ddindex_head_li7"><a href="http://category.dangdang.com/baby">母　　婴</a></li><li class="ddindex_head_li7"><a href="http://category.dangdang.com/clothing">服装服饰</a></li><li class="ddindex_head_li7"><a href="http://category.dangdang.com/home">家　　居</a></li><li class="ddindex_head_li7"><a href="http://category.dangdang.com/sports">运动健康</a></li><li class="ddindex_head_li5"><a href="http://category.dangdang.com/food">食　　品</a></li>'+(isIE_c()?'':'<li class="clear"></li>')+'</ul>';
	var divotherChannel=document.getElementById("otherChannel"); 
	var img_obj=document.getElementById("img_otherchannel");

    var frm=document.searchadvformnewbook;
    var d=new Date();
    var yearn=d.getFullYear(); 
	if(frm){
	if(frm.catalog.value.substring(0,2)=="01"){
		if(frm.sy.options.length<2&&frm.ey.options.length<2){
			for(i=2;i<7;i++){
				var option = new Option(yearn,yearn);
				frm.sy.options.add(option,i);
				var option1 = new Option(yearn,yearn);
				frm.ey.options.add(option1,i);
				yearn--;
			}
		}

	}
	}
	if(obj.id=="a_clicksearch"){
		divgjsearch.style.left=(x-22)+'px';
		divgjsearch.style.top=(y-7)+'px';
		divgjsearch.style.display="inline";
		}
	if(obj.id=="a_otherchannel"){ 
		if(img_obj!=null){
		img_obj.src="http://img3.dangdang.com/Movie_channel/press_up.gif";
		}
		divotherChannel.style.left=(x-62)+'px';
		if(location.href.toLowerCase().indexOf("bang.dangdang.com")>=1){
			u=18;
		}else{
			if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1) 		
			u=14;
			else
			u=12;
		}
		divotherChannel.style.top=(y+u)+'px';
		divotherChannel.style.display="inline";
	}
    showhideobject(1);
	
}

function showwindow(obj,objdiv,addx,addy){
	var x=getposOffset_top(obj,'left');
    var y=getposOffset_top(obj,'top');
    var div_obj=document.getElementById(objdiv);
		div_obj.style.left=(x+addx)+'px';
		div_obj.style.top=(y+addy)+'px';
		div_obj.style.display="inline";
	}
	
function Hidewindow(objdiv){
    var div_obj=document.getElementById(objdiv);
		if(div_obj){
			div_obj.style.display="none";
		}
	}	

function showhideobject(tr){
var key=document.getElementsByTagName("select")

    if(tr==1){    
    for(n=0;n<key.length;n++){        
        if(key[n].name!="sy"&&key[n].name!="sm"&&key[n].name!="ey"&&key[n].name!="em"&&key[n].name!="rebate_rang"&&key[n].name!="medium"&&key[n].name!="laststock"){
        key[n].style.visibility='hidden';
        }
    }
    }else{
        for(n=0;n<key.length;n++){
        key[n].style.visibility='visible';
        }
    }
}

document.onclick=function(e){
e=window.event||e;
obj = e.srcElement ? e.srcElement : e.target;
    if(obj.id!="a_clicksearch"&&obj.id!="img_otherchannel"&&obj.id!="sortName"){
     hidegaoji();
    }
};

function hideotherchannel(){
		var divotherChannel=document.getElementById("otherChannel");
	var img_obj=document.getElementById("img_otherchannel");
    if(divotherChannel.style.display!="none"){
    divotherChannel.style.display="none";
		if(img_obj!=null){
		img_obj.src="http://img3.dangdang.com/Movie_channel/press_down.gif";
		}	
    }
	}
function hidegaoji(){    
    
    var divgjsearch=document.getElementById("gaijisearch");
    if(divgjsearch==null) return;
    if(divgjsearch.style.display!="none"){
    divgjsearch.style.display="none";    
    }
	var divotherChannel=document.getElementById("otherChannel");
	var img_obj=document.getElementById("img_otherchannel");
    if(divotherChannel.style.display!="none"){
    divotherChannel.style.display="none";
		if(img_obj!=null){
		img_obj.src="http://img3.dangdang.com/Movie_channel/press_up.gif";
		}	
    }
    showhideobject(0);
	Hidewindow('sortListWindow');	
}

function mouseout_click(){
    document.onclick=function(){
    hidegaoji();
    }
}

function SignOut(){
    var ifru=document.getElementById("usernameifr");
    ifru.src="https://login.dangdang.com/SignOut.aspx?returnurl=http://www.dangdang.com/customer/signout.asp";
   changeuser();
}
function changeuser(){
var nkname=parent.document.getElementById("nickname");
nkname.innerHTML='<b>您好，欢迎光临当当网</b> [<a href=\"https://login.dangdang.com/Signin.aspx\" target=\"_self\" class=\"b\">登录</a> | <a href=\"https://login.dangdang.com/Register.aspx\" target=\"_self\" class=\"b\">注册</a>]';
}

function isIE_c(){ 
   return window.navigator.userAgent.toLowerCase().indexOf("msie")>=1?true:false;
} 

