
//关于div显示
function showwindownewtop(obj,objdiv,addx,addy){

	var x=getposOffset(obj,'left');
    var y=getposOffset(obj,'top');
    var div_obj=document.getElementById(objdiv);
		div_obj.style.left=(x+addx)+'px';
		div_obj.style.top=(y+addy)+'px';
		if(div_obj.style.display=="none"){
		div_obj.style.display="inline";}
		else{
			div_obj.style.display="none";
		}
	}
	
	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 getposOffset(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 GotoAllSearch(){
var allsearch=document.bootpagetopSearch;
		if(validatekey(allsearch)){
				allsearch.submit();
			}
	}
	
function GotoBookSearch(){
var allsearch=document.searchform;
		if(validatekey(allsearch)){
				allsearch.submit();
			}
	}	

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)>50)
	                       {
	                        alert("搜索词过长，请重新填写！");
		                    objfrm.key.focus();
		                    return false; 
		                    }
	             }	
	    }		
	}
	if(window.location.host.toLowerCase().indexOf("search.dangdang.com")!=-1)
	{
	  try{ objfrm.SearchFromTop.value=2; } catch(err){alert(err)}
	}
	return true;
}

function cnlength(str){
    return str.replace(/[^\x00-\xff]/gi,'oo').length;
}


//关于分类搜索
function gotosearch(){
	var objfrm=document.searchform;

	if(objfrm.catalog.value>=4000000)
	{
		window.location="http://category.dangdang.com/search?q="+objfrm.key.value+"&guan="+objfrm.catalog.value;
		return false;
	}
	
	if(objfrm.catalog.value=="01")
	{
		objfrm.action="http://search.book.dangdang.com/search.aspx"
	}
	if(objfrm.catalog.value=="03")
	{
		objfrm.action="http://search.music.dangdang.com/search.aspx"
	}
	if(objfrm.catalog.value=="05")
	{
		objfrm.action="http://search.movie.dangdang.com/search.aspx"
	}

	return true;
}

function changeSort(sortid,obj)
{
 var sortName=document.getElementById('sortName');
 sortName.innerText=obj.innerText;
 
  var sortcn=obj.innerHTML;        
	var objfrm=document.searchform;
	if(objfrm.key.value == "")
	    objfrm.key.focus();
	
	objfrm.catalog.value=sortid;
	
    if(sortid.length>2 && sortid!="mall")
        sortid=sortid.substr(0,2);
    //hot key
    //showHotWord(sortid);
}

function changeSort_new(sortid,obj)
{
	var objfrm=document.searchform;
	if(objfrm.key.value == "")
	    objfrm.key.focus();
	objfrm.catalog.value=objfrm.selectcatalog.options[objfrm.selectcatalog.selectedIndex].value;
	

}

function hideDiv()
{
if(document.getElementById("mediaListWindow").style.display!="none")
document.getElementById("mediaListWindow").style.display="none";
if(document.getElementById("bangListWindow").style.display!="none")
document.getElementById("bangListWindow").style.display="none";
}


//关于详细分类div




function showwindowExtra(obj,objdiv,addx,addy,ids){

    if(sortArray[ids].length>0) {
	document.getElementById("sortTitle").innerHTML=obj.innerText;
	document.getElementById("sortTitle").href=obj;
	document.getElementById("sortTitle").style.cursor=obj.style.cursor;

	if(sortArray[ids].length==0) 
	{
	sortId=-1;
	document.getElementById("sortContentBox").style.display="none";
	document.getElementById("sortTitle").className="flxf_left2";
	}
	else
	{
	sortId=ids;
	document.getElementById("sortTitle").className="flxf_left";
	document.getElementById("sortContentBox").style.display="inline";
	document.getElementById("sortContent"+sortId).style.display="";
	}
	var x=getposOffset(obj,'left');
    var y=getposOffset(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";
		document.getElementById("SortBoxShadowRight").style.height=(document.getElementById("SortBoxRight").offsetHeight+2)+"px";
	}
	}
	
	function hidewindowExtra(objdiv,ids){
    var div_obj=document.getElementById(objdiv);
    var ul_obj=document.getElementById("sortContent"+ids);
		if(div_obj) div_obj.style.display="none";
		if(ul_obj)	ul_obj.style.display="none";
	}
	
	function showwindowExtra2(){
	if(sortId!=-1)	document.getElementById("sortContent"+sortId).style.display="";
	document.getElementById('sortBox').style.display='inline';
	
	}
	
	function hidewindowExtra2(){
	if(sortId!=-1)	document.getElementById("sortContent"+sortId).style.display="none";
	document.getElementById('sortBox').style.display='none';
	}
	
	function loadOver(){
        if(!isIE())
        {
            document.getElementById("SortBoxShadowRight").style.width="173px";
            //document.getElementById("SortBoxShadowLeft").style.display="none";
            //document.getElementById("SortBoxShadowRight").style.display="none";
        }
        else
        {
            document.getElementById("SortBoxShadowLeft").style.height="2px";
            document.getElementById("SortBoxShadowLeft").style.position="static";
        }
        //lanterInit();
    
}



//ff支持
function isIE(){ //ie? 
   if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1) 
    return true; 
   else 
    return false; 
} 

if(!isIE()){ //firefox innerText define
   HTMLElement.prototype.__defineGetter__(     "innerText", 
    function(){
     var anyString = "";
     var childS = this.childNodes;
     for(var i=0; i<childS.length; i++) {
      if(childS[i].nodeType==1)
       anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText;
      else if(childS[i].nodeType==3)
       anyString += childS[i].nodeValue;
     }
     return anyString;
    } 
   ); 
   HTMLElement.prototype.__defineSetter__(     "innerText", 
    function(sText){ 
     this.textContent=sText; 
    } 
   ); 
}

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 showHotWord(sortid)
{
    var sortindex=0;
    if(sortid!=""){
        var index=listsortid.indexOf(sortid+";");
        if(index!=-1) sortindex=index/3+1;
    }
    var searchurl="http://search.dangdang.com/search.aspx?key="
	if(sortid=="01"){
		searchurl="http://search.book.dangdang.com/search.aspx?key=";
	}
	if(sortid=="03"){
		searchurl="http://search.music.dangdang.com/search.aspx?key=";
	}
	if(sortid=="05"){
		searchurl="http://search.movie.dangdang.com/search.aspx?key=";
	}	
    var hotword="";
    for(var i=0;i<listkeycode[sortindex].length-1;i++){
        hotword+="<a name=hotword name='hotword' href='";

        if(sortid=="mall")
            hotword+="http://search.mall.dangdang.com/search.cgi?KeyWord="+listkeycode[sortindex][i]+"&type=hot'>"+listkeycn[sortindex][i];
        else
            hotword+=searchurl+listkeycode[sortindex][i]+"&type=hot&catalog="+sortid+"'>"+listkeycn[sortindex][i];
        if(i==listkeycode[sortindex].length-2)		
        hotword+="</a> | <a href=\"http://bang.dangdang.com/searchkey/\" class=\"fybmore\">更多>></a>";
		else
		hotword+="</a> | ";
    }
    document.getElementById("div_word").innerHTML=hotword;
}

function showpage(vbook,vmusic,vmovie)
{
    var inputkey=document.searchform;
    inputkey.key.focus();
    showHotWord(inputkey.catalog.value);
}
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");
	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(!isIE())
		divotherChannel.style.top=(y+10)+'px';
		else
		divotherChannel.style.top=(y+12)+'px';
		divotherChannel.style.display="inline";
	}
    showhideobject(1);
	

    
}

function showhideobject(tr){
	return ;
}

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.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 checkSearchSubmit(){
    
    if(advsearchbook()){
        document.searchadvformnewbook.submit();    
    }
}


function isValidDate(str)
{
    var result=str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if(result==null) return false;
    var d=new Date(result[1], result[3]-1, result[4]);
    return (d.getFullYear()==result[1]&&d.getMonth()+1==result[3]&&d.getDate()==result[4]);
}

function advsearchbook()
{
    o=document.searchadvformnewbook;

    
if(o.catalog.value.substring(0,2)=="01"){
			if(o.sy!=null)
            sy=parseInt(o.sy.value);
            sm=parseInt(o.sm.value);
            sd=parseInt(o.sd.value);
            if(sy>0) o.begin_date.value=sy*10000+sm*100+sd;

        


            ey=parseInt(o.ey.value);
            em=parseInt(o.em.value);
            ed=parseInt(o.ed.value);
            if(ey>0) o.end_date.value=ey*10000+em*100+ed;
}

    if(o.minprice && o.maxprice)
    {
        if((o.minprice!="") && isNaN(o.minprice.value))
        {
            o.minprice.focus();
            alert("请填写正确的当当价！");
            return false;
        }
        if((o.maxprice!="") && isNaN(o.maxprice.value))
        {
            o.maxprice.focus();
            alert("请填写正确的当当价！");
            return false;
        }
    }
    if(o.key1.value==""&&o.key2.value==""&&o.key4.value==""&&o.key3.value==""){
        alert("请填写要搜索的内容");
        return false;
    }
    return true;
}


function SignOut(){
    var ifru=document.getElementById("usernameifr");
    ifru.src="http://login.dangdang.com/SignOut.aspx?returnurl=http://www.dangdang.com/customer/signout.asp";
   changeuser();
}

function changeuser(){
var nkname=parent.document.getElementById("nickname");
nkname.innerHTML='您好，欢迎光临当当网。<a href="http://reco.dangdang.com/">当当为您推荐！</a>[请<a href="http://login.dangdang.com/Signin.aspx" name="sign_in" target="_self" class="blue12a">登录</a>/<a href="http://login.dangdang.com/Register.aspx" target="_self" name="Register" class="blue12a">注册</a>]';
}

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();
    }
};