jQuery(document).ready(function(){
  jQuery("#topKeywords").suggest(_frontPath+"/AutoComplete",{minchars:1,fn:'submitSearchForm()'});
});
function showProBox(){
  var obj=document.getElementById("proBox");
  obj.className="prohover";
  obj.setAttribute("class","prohover");
  document.getElementById("showProBox").style.display="block";
}
function hideProBox(){
  var obj=document.getElementById("proBox");
  obj.className="pro";
  obj.setAttribute("class","pro");
  document.getElementById("showProBox").style.display="none";
}
_sortType=1;
function selectedProBox(curr,num){
  var obj=document.getElementById("proBox");
  obj.className="pro";
  obj.setAttribute("class","pro");
  obj.innerHTML=curr.innerHTML;
  document.getElementById("showProBox").style.display="none";
  _sortType=num;
  if(num==1){
    document.getElementById("SortCid").value="603";
    document.getElementById("searchform").setAttribute("action", _frontPath+"/ProductList.do");
  }else{
    document.getElementById("InfoCid").value="904";
    document.getElementById("searchform").setAttribute("action", _frontPath+"/InfoCenter/InfoList.do");
  }
  document.getElementById("allBox").innerHTML="所有分类";
}
function showAllBox(){
  document.getElementById("allBox").className="other";
  document.getElementById("showAllBox"+_sortType).style.display="block";
  
}
function hideAllBox(){
  document.getElementById("allBox").className="othertwo";
  document.getElementById("showAllBox"+_sortType).style.display="none";
}
  function addTopEventHandler(target,eventType,func){
    if(target!=undefined){
      if(target.addEventListener){//dom
        target.addEventListener(eventType,func,false);
      }else if(target.attachEvent){//IE
        target.attachEvent("on"+eventType,func);
      }else{//Other
        target["on"+eventType]=func;
      }
    }
  }

  function onloadtop() {
	  var topdiv = document.getElementById("topdiv");
	  var topclose = document.getElementById("topclose");
		var allBox = document.getElementById("allBox");
	if (_webURL!="/index.jsp"){
		var showAllBox1 = document.getElementById("showAllBox1");
		addTopEventHandler(allBox, "mouseover", showAllBox);
		addTopEventHandler(allBox, "mouseout", hideAllBox);
		addTopEventHandler(showAllBox1, "mouseover", showAllBox);
		addTopEventHandler(showAllBox1, "mouseout", hideAllBox);
		allBox.className="othertwo";
		showAllBox1.style.display = "none";
		addTopEventHandler(topdiv, "mouseover", overtopdiv1);
		addTopEventHandler(topdiv, "mouseout", outtopdiv1);
	addTopEventHandler(topclose, "click", outtopdiv1);
	}else{
		allBox.className="index";
		addTopEventHandler(topdiv, "mouseover", overtopdiv2);
		addTopEventHandler(topdiv, "mouseout", outtopdiv2);
	addTopEventHandler(topclose, "click", outtopdiv2);
	}
}

_defaultsortTop =3;
_defaultsortStep = 40;
_tempsortCid = 1;
function showtopdiv(cid,idx){
	  var sortLayer=document.getElementById("topdiv");
	  var topicon=document.getElementById("top_icon");
	  /*if(cid==57218){
		  if(window.XMLHttpRequest){
			  sortLayer.style.top="288px";
			  topicon.style.top=_defaultsortTop+_defaultsortStep*(idx-1)-112+"px";
		   }else {    
		 	sortLayer.style.top="298px"; 
		 	topicon.style.top=_defaultsortTop+_defaultsortStep*(idx-1)-118+"px";
		   } 
		  
	  }else{
		  sortLayer.style.top="";
		  topicon.style.top=_defaultsortTop+_defaultsortStep*(idx-1)+"px";
	  }*/
      sortLayer.style.top="";
      topicon.style.top=_defaultsortTop+_defaultsortStep*(idx-1)+"px";
	  sortLayer.style.display="block";
	 
	  document.getElementById("subCol_"+cid).style.display="block";
	  _tempsortCid=cid;
	}
function hidetopdiv(){
	  var obj=document.getElementById("allBox");
	  document.getElementById("subCol_"+_tempsortCid).style.display="none";
	  document.getElementById("topdiv").style.display="none";
}
function overtopdiv1(){
	 document.getElementById("allBox").className="other";
	var curr = document.getElementById("topdiv");
	curr.style.display="block";
	  var sort=document.getElementById("topsort_"+_tempsortCid);
      sort.className="cur";
	  document.getElementById("subCol_"+_tempsortCid).style.display="block";
	  document.getElementById("topdiv").style.display="block";
	  document.getElementById("showAllBox1").style.display="block";
}
function outtopdiv1(){
	document.getElementById("allBox").className="othertwo";
	var curr = document.getElementById("topdiv");
	curr.style.display="block";
     var sort=document.getElementById("topsort_"+_tempsortCid);
     sort.className="";
	  document.getElementById("subCol_"+_tempsortCid).style.display="none";
	  document.getElementById("topdiv").style.display="none";
	  document.getElementById("showAllBox1").style.display="none";
}
function overtopdiv2(){
	var curr = document.getElementById("topdiv");
	curr.style.display="block";
	  var sort=document.getElementById("topsort_"+_tempsortCid);
      sort.className="cur";
	  document.getElementById("subCol_"+_tempsortCid).style.display="block";
	  document.getElementById("topdiv").style.display="block";
	  document.getElementById("showAllBox1").style.display="block";
}
function outtopdiv2(){
	var curr = document.getElementById("topdiv");
	curr.style.display="block";
     var sort=document.getElementById("topsort_"+_tempsortCid);
     sort.className="";
	  document.getElementById("subCol_"+_tempsortCid).style.display="none";
	  document.getElementById("topdiv").style.display="none";
	  document.getElementById("showAllBox1").style.display="block";
}
function selectedAllBox(curr,id){
  var obj=document.getElementById("allBox");
  obj.className="all";
  obj.setAttribute("class","all");
  obj.innerHTML=curr.innerHTML;
  document.getElementById("showAllBox"+_sortType).style.display="none";
  if(_sortType==1)document.getElementById("SortCid").value=id;
  else document.getElementById("InfoCid").value=id;
}
function showTopCartBox(count){
  if(count>0)document.getElementById("cartOpen").style.display="";
}
function hideTopCartBox(){
  document.getElementById("cartOpen").style.display="none";
}
function showSaleBox(count){
  if(count>0)document.getElementById("featureOpen").style.display="";
}
function hideSaleBox(){
  document.getElementById("featureOpen").style.display="none";
}
function checkTopSearchForm(){
  var keyword=jQuery("#topKeywords");
  if(_sortType==1){
    if(keyword.val()!=""){
      jQuery("#thKeywords").val(jQuery.trim(keyword.val()));//+",title,content"
    }else{
      alert("请输入搜索关键字！");
      keyword.focus();
      return false;
    }
  }else{
    if(keyword.val()!=""){
      jQuery("#infoKeyword").val(jQuery.trim(keyword.val()));
    }else{
      alert("请输入搜索关键字！");
      keyword.focus();
      return false;
    }
  }
  return true;
}
function submitSearchForm(){
  if(!checkTopSearchForm())return false;
  document.getElementById("searchform").submit();
}
var scrollInterval=0;
function sideCartScroll (id,_top){
  var me=id.charAt?document.getElementById(id):id,d1=document.body,d2=document.documentElement;
  d1.style.height=d2.style.height='100%';
  me.style.top=_top?_top+'px':0;//me.style.left=_left+"px";//[(_left>0?'left':'left')]=_left?Math.abs(_left)+'px':0;
  intervalSideCart(me,d1,d2,_top);
  return arguments.callee;
}
function intervalSideCart(me,d1,d2,_top){
  scrollInterval=setInterval(function (){
    me.style.top=parseInt(me.style.top)+(Math.max(d1.scrollTop,d2.scrollTop)+_top-parseInt(me.style.top))*0.1+'px';
  },10+parseInt(Math.random()*20));
}
function showSideCart(id){
  clearInterval(scrollInterval);
  var top=document.getElementById(id).offsetTop;
  var sideCart=jQuery("#scroll");
  sideCart.css("top",top+"px").slideDown(10);
}
function hideSideCart(){
  var sideCart=document.getElementById("scroll");
  sideCart.style.display="none";
}
function overSideCart(){
  clearInterval(scrollInterval);
  var sideCart=document.getElementById("scroll");
  sideCart.style.display="block";
}
function outSideCart(){
  var sideCart=document.getElementById("scroll");
  sideCart.style.display="none";
  var me=document.getElementById("sideScroll"),d1=document.body,d2=document.documentElement;
  intervalSideCart(me,d1,d2,185);
}
_sideCartBoxTop=0,_currIdx=1;
function sideCartUp(){
  if(_currIdx==1)return;
  var box=document.getElementById("sideCartBox");
  _sideCartBoxTop+=50;
  _currIdx--;
  box.style.top=_sideCartBoxTop+"px";
}
function sideCartDown(count){
  if(_currIdx+2>=count)return;
  var box=document.getElementById("sideCartBox");
  _sideCartBoxTop-=50;
  _currIdx++;
  box.style.top=_sideCartBoxTop+"px";
}
function loginOutSNS(snsLoginConn){
//  if(snsLoginConn=="yes"){
//    jQuery.ajax({timeout:2000,type:"GET",dataType:"script",url:_snsUrl+"emall/exitControl.jsp",cache:false,complete:function(){window.location=_frontPath+"/Member/Exit.do";},error:function(){window.location=_frontPath+"/Member/Exit.do";}});
//  }else{
//    window.location=_frontPath+"/Member/Exit.do";
//  }
//  window.location.href=_frontPath+"/clearFrontendSessionForSNS.jsp?emallUrl="+_snsUrl+"emall/exitControl.jsp";
}

function deleteCartItem(mid,itemId,cartId,articleId,cartType){
    jQuery.post(_frontPath+"/DelCartItem.do",{mid:mid,ObjectId:articleId,ItemId:itemId,cartId:cartId,orderType:cartType},function(){
    jQuery("#IncludeShoppingCarts").load("/NewDefault/FInclude/ShoppingCart.do");
     jQuery.ajax({url:_frontPath+"/tianhong/jsp/SideShoppingCartContent.jsp",data:{},async:true,cache:false,success:function(data){
          jQuery("#IncludeInnerBoxId").html(data)
        }});
    });

}
function updateAmount1(action,pStateOOS,articleId,cartItemId,mid,userId,cartId,price,integral,canBuyCount,maxBuy,farticleorderamount,marticleorderamount,tipLargerCustomerPhoneFlag){
  var amountInput=document.getElementById("amount1"+cartItemId);
  var amount=amountInput.value;
  oldamount = amount;
  var tmpAmount=0;//库存不足时使用
  if(action=="add"){
    amount++;
    if(maxBuy!=""){
      if(amount>Number(maxBuy)){
        alert("此商品为抢购商品，最大购买数量为"+maxBuy+"！");
        amount=Number(maxBuy);
      }
    }
    if(amount>canBuyCount){
        //add by xiaohl for 提示大客户专线电话
    	var alertMsg = "销售库存不足，仅剩" + canBuyCount + "件可以订购!";
    	if(tipLargerCustomerPhoneFlag=="1"){
    		alertMsg = alertMsg + "\n"+largerCustomerPhone;
    	}
    	alert(alertMsg);
      //alert("销售库存不足，仅剩"+canBuyCount+"件可以订购！");
      tmpAmount = amount-1;
      amount=canBuyCount;

//      // 取消购物车列表中，异常商品的高亮显示样式
//      cancleErrorArticleStyle(articleId,cartItemId);

    }
    if(marticleorderamount>0){
      if(amount>Number(marticleorderamount)){
        alert("此商品的限购量为"+marticleorderamount+"!");
        amount=marticleorderamount;
      }
    }
    if(tmpAmount==0){
    	tmpAmount = amount;
    }
  }else if(action=="dec"){

    if(farticleorderamount>0){
      if(amount==farticleorderamount){
        alert("此商品的起订数量为"+farticleorderamount+"!");
        return false;
      }
    }
    if(amount<=1) {
    	return false;
    }
    amount--;

  }else{
    if(isNaN(amount)){
      alert("数量应为数字！");
      amount=farticleorderamount>0?farticleorderamount:1;
      //amountInput.focus();
      //return false;
    }
    if(amount=="")amount=1;
    if(farticleorderamount>0){
      if(amount<farticleorderamount){
        alert("此商品的起订数量为"+farticleorderamount+"!");
        amount=farticleorderamount;
      }
    }
    if(amount<1){
      alert("数量应大于零！");
      amount=1;
//      amountInput.focus();
//      return false;
    }
    var strAlert="";
    if(maxBuy!=""){
      if(amount>Number(maxBuy)){
        strAlert="此商品为抢购商品，最大购买数量为"+maxBuy+"！";
        amount=Number(maxBuy);
      }
    }
    if(amount>canBuyCount){
    	//add by xiaohl for 提示大客户专线电话
    	var alertMsg = "销售库存不足，仅剩" + canBuyCount + "件可以订购!";
    	if(tipLargerCustomerPhoneFlag=="1"){
    		alertMsg = alertMsg + "\n"+largerCustomerPhone;
    	}
    	strAlert=alertMsg;
      //strAlert="销售库存不足，仅剩"+canBuyCount+"可以订购！";
      tmpAmount = amount;
      amount=canBuyCount;

//      // 取消购物车列表中，异常商品的高亮显示样式
//      cancleErrorArticleStyle(articleId,cartItemId);
    }
    if(marticleorderamount>0){
      if(amount>Number(marticleorderamount)){
        strAlert="此商品的限购量为"+marticleorderamount+"!";
        amount=marticleorderamount;
      }
    }
    if(strAlert!="")alert(strAlert);
  }
  amountInput.value=amount;


  //DWREngine.setAsync(false);
   jQuery.post(_frontPath+"/tianhong/jsp/updateAmount.jsp?T="+Math.random(),{cartItemId:cartItemId,amount:amount,mid:mid,userId:userId,frontEnd:true,cartId:cartId},function(datas){
    var dataStr=jQuery.trim(datas);
  // debugger;
    data=dataStr.split(",");
    var len=data.length;

    if(len==1||len==2){
      alert(data[0]);
      if(action=="dec"){
      	//amountInput.value=amount+1;
      	if(data[1]){
      		amountInput.value= data[1];
      	}else{
      		amountInput.value=amount+1;
      	}
      }else{
      	//amountInput.value = tmpAmount;
      	if(data[1]){
      		amountInput.value= data[1];
      	}else{
      		amountInput.value=tmpAmount;
      	}
      }

      //window.location.reload();
    }else if(len==5){


     /* document.getElementById("totalPrice"+cartItemId).innerHTML="￥"+(price*amount).toFixed(2);
      document.getElementById("integral"+cartItemId).innerHTML=(integral*amount).toFixed(2);*/

    }
   /* if(oldamount!=amountInput.value){
     var totalamount=Number(document.getElementById("cartItemsAmount").innerHTML);
    var newAmount = Number(totalamount) + Number(amountInput.value) -Number(oldamount);
    document.getElementById("cartItemsAmount").innerHTML=Number(newAmount);
    document.getElementById("topcartItemsAmount").innerHTML=Number(newAmount);
    }else{
      jQuery("#IncludeShoppingCarts").load("/NewDefault/FInclude/ShoppingCart.do");
    }*/
    jQuery("#IncludeShoppingCarts").load("/NewDefault/FInclude/ShoppingCart.do");
    /*if(oldamount!=amountInput.value){
     var totalamount=Number(document.getElementById("cartItemsAmount").innerHTML);
     *//*var newAmount = Number(totalamount) + Number(amountInput.value) -Number(oldamount);alert(totalamount);alert(amountInput.value);alert(oldamount);*//*
     *//*if(newAmount >= 0){*//*
      document.getElementById("cartItemsAmount").innerHTML=amountInput.value;
      document.getElementById("topcartItemsAmount").innerHTML=amountInput.value;
     *//*}*//*
    }else{alert(amount);
     document.getElementById("cartItemsAmount").innerHTML=Number(amountInput.value);
     document.getElementById("topcartItemsAmount").innerHTML=Number(amountInput.value);
    }*/
   /* document.getElementById("cartTotalPrice").innerHTML=data[2];*/
  });
  return true;
}
