//更换域名，请更改此配置
var CrmUrl="";
var ShopUrl="";

//网页收藏
function addBookmark(title,url) {
  if (window.sidebar) { 
    window.sidebar.addPanel(title, url,""); 
  }else if( document.all ) {
    window.external.AddFavorite( url, title);
  }else if( window.opera && window.print ){
    return true;
  }
}
/*删除导航栏购物车商品*/
function DeleteTopCartPro(rec_id){

      $.ajax({
        type: 'Get',
        url: '/Handler/AjaxHandler.ashx',
        data: 'action=DeleteCartSinglePro&rec_id=' + rec_id,
        cache: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        timeout:10000,
        dataType: 'text',
        success: function(result) {
           switch(parseInt(result)){
              case -1:
               alert("系统繁忙，请稍后再试！");
               break;
             case 1104:
                alert("删除失败！");
                break;
             case 1105:
                  load_Top_MenuCart();//重新加载导航条购物车
                break;
             case 1106:
                alert("参数异常!");
                break;
           }
        }
    });
}

/*加载导航条购物车*/
function load_Top_MenuCart(){
 $.ajax({
        type: 'GET',
        url: '../cn_asp/ajax_cart.asp',
        data: 'act=getTopCartData',
        cache: false,
        success: function(msg) {
            $("#Mall_Cart_Count_A span").text(msg);
        }
    });
}
$(function(){
    $("#Mall_Cart_Count_A").mouseover(load_Top_MenuCart);
});
/*购车车效果*/
function hoverisblokcornone(){

   $("#ulCard li").hover( function(){$("#gwcCard").show();},function(){$("#gwcCard").hide();});
}
function iMycartBind(){
//var object_mycart = jQuery("#o-mycart-list");
//jQuery("#i-mycart").bind("mouseover",function(){
//	//object_mycart.show();  
//	jQuery("#o-mycart-list").show();      
//}).bind("mouseleave",function() {
//	//object_mycart.hide(); 
//	jQuery("#o-mycart-list").hide(); 
//	});     
}

jQuery(function() {
        hoverisblokcornone();
	//show-all-category
	var object_category = jQuery("#all_category_warp");
	jQuery("#all_category_menu").bind("mouseover",function() {
		object_category.show();
	}).bind("mouseleave",function() {
		object_category.hide();   
	});
	//del-mycart-items
	jQuery(".pane .del").click(function() {
		jQuery(this).parents(".pane").hide();
	});
});

//收藏产品
function addFavorites(pid, IsBund){
    var _tempColor = $('dl.b .attr-value').eq(0).find('.current');
    var _tempSize = $('dl.b .attr-value').eq(1).find('.current');
	/*if(_tempColor.length == 0)
    {alert("请选择颜色");return;}
    if(_tempSize.length != 1)
    {alert('请选择尺码！');return;}*/
    var p_color_id = _tempColor.attr("attr");
    var p_size_id = _tempSize.attr("attr");
	 var pro_color = _tempColor.text();
    var pro_size = _tempSize.text();
    var jData = {
        action: 'addFavorites',
        pid: pid,
        IsBund: IsBund,
		size: escape(pro_size),
        color: escape(pro_color),
        p_color_id: p_color_id,
        p_size_id: p_size_id
    }          
    $.ajax({
            url: "../cn_asp/AjaxHandler.asp",
            cache: false,
            data: jData,
            success: function (result) {                
                   switch(parseInt(result))
                   {
                         case -1:
                            alert("系统繁忙，请稍后再试");
                           break;
                         case 1101:
                            alert("当前还未登录，请先登录");
                           break; 
						 case 1103:
                            alert("最多只能收藏10件商品");
                            break;
                         case 1104:
                            alert("添加失败");
                            break;
                         case 1105:
                            alert("您已经收藏过该商品");
                            break;
                         case 1106:
                            alert("参数异常");
                            break;
                         case 1120:
                            alert("成功收藏");
                            break;                 
                   }
            } 
    });       
}

//到货通知
function addNotice(pid){      
    var action="/Handler/AjaxHandler.ashx?action=addNotice&pid="+pid;
    $.ajax({
            type: "Get",
            url:action,
            cache: false,
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            timeout:10000,
            dataType:"text",
            error: function (XmlHttpRequest, textStatus, errorThrown) {                    
                
              }, 
            success: function (result) {                
               switch(parseInt(result)){
                 case -1:
                    alert("系统繁忙，请稍后再试！");
                   break;
                 case 1101:
                    alert("您当前还未登录！");
                    location.href='/Login.html';
                   break;                  
                 case 1104:
                    alert("添加失败！");
                    break;
                 case 1105:
                    alert("您已经对该商品发过通知了！");
                    break;
                 case 1106:
                    alert("参数异常!");
                    break;
                 case 1120:
                    alert("已成功发送通知！");
                    break;                 
               }
            } 
       });       
}


//清除最近访问产品记录
function clearHistoryProduct() {
     //var action="/Handler/AjaxHandler.ashx?action=clearHistoryProduct";
	 var action="AjaxHandler.asp?action=clearHistoryProduct";
     $.ajax({
         type: "Get",
         url: action,
         cache: false,
         contentType: "application/x-www-form-urlencoded; charset=utf-8",
         timeout: 10000,
         dataType: "text",
         error: function(XmlHttpRequest, textStatus, errorThrown) {

         },
         success: function(result) {
             switch (parseInt(result)) {
                 case -1:
                     alert("清除Cookies失败！");
                     break;
                 case 1120:
                     if ($("#re-viewed-div").length > 0) {
                         $("#re-viewed-div").html("<dl><dd>暂时没有任何访问记录！</dd></dl>");
                     }
                     break;
             }
         }
     });       
}

function clearTag(str, charString) {
   str = str.substring(0, str.indexOf(charString)) || str;
   return str;
}

function ajaxPanel(url,panelIDs){      
        
        if(!panelIDs|| panelIDs==''){
            return false;
        } 
        url = url || (document.forms[0] && document.forms[0].action) || ajaxHelper.clearTag(location.href, '#'); 
        var IDs = Array.prototype.slice.apply(arguments).slice(1 || 0);
        var callback = IDs[IDs.length - 1];
       if (typeof callback == 'function') {
          IDs = IDs.slice(0, IDs.length - 1);
       }
       else {
         callback = null;
       }
       var panelStrings;
       if( IDs.length > 0){
         panelStrings =IDs.join(',');
       }
       else{
         return false ;
       }
     
        $.ajax
        (
            {
                type: 'get',
                dataType: 'xml',
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                cache: false,
                url: url,
                data: '_AjaxTags=' + panelStrings,                
                success:
                function (xml) { 
                   
                    if(xml){                     
                       $(xml).find("panellist> panel").each(function(){    
                           var id=$(this).attr("id");
                           var text=$(this).text();
                           if($('#'+id).length >0){                        
                             $('#'+id).html($.trim(text));
                           }
                       });
                        
                        $(".pd-list li,.pd-l-list li").mouseover(function(){                                
                               $(this).addClass("hover");
                               }).mouseout(function(){
                               $(this).removeClass("hover");
                         });
                 
                       
                        if(orderfiled=="SaleNum"){//人气     
                            if($("#selectordertypebtn").length>0 &&$("#selectordersellcounttype").length>0){ 
                                  $("#selectordertypebtn").removeClass();           
                                  $("#selectordertypebtn").addClass("price");
                                   
                                  $("#selectordersellcounttype").removeClass();
                                  $("#selectordersellcounttype").addClass("hot-s");       
                               }
                            }
                            else{
                              if(orderfiled.indexOf("Ret_Price")!=-1){       
                                  if($("#selectordertypebtn").length>0 &&$("#selectordersellcounttype").length>0){ 
                                    
                                   $("#selectordertypebtn").removeClass();            
                                      
                                  if(ordertype==0){
                                     $("#selectordertypebtn").addClass("price-up");     
                                  }
                                  else{
                                     $("#selectordertypebtn").addClass("price-down");
                                  }            
                                                
                                   $("#selectordersellcounttype").removeClass();
                                   $("#selectordersellcounttype").addClass("hot");  
                                  }   
                              }
                          }
                          
                          $("#listorderfiled").val(hideOrderFiled);                
                    }            
                    
                    if (callback != null) 
                    {                   
                        callback(xml);
                    }                    
                    
                   
                },
                complete:
                function () {    
                    
                             
                },
                beforeSend:
                function () {                
                },
                error:
                function (XMLHttpRequest, textStatus, errorThrown) {       
                 // alert(XMLHttpRequest.responseText+"---前面为错误内容！！！");     
                  //alert(textStatus); 
                },
                timeout:
                function () {               
                }
            }
        );

   } 

//====================分页查询条件参数   START
var bigsort;
var midsort;
var smallsort=0;
var classsort=0;
var brandsort=0;

var minprice=0;
var maxprice=0;

var minweight=0;
var maxweight=0;

var keyword='';

var orderfiled='';
var hideOrderFiled='SaleNum';
var ordertype=0;

var pageindex;
 

//====================分页查询条件参数   END

//分类选择
function selectproductsort(bid,mid,sid,obj){   
   bigsort=bid;
   if(mid==0){
      midsort=sid;
      sid=0;
   }
   else{
      midsort=mid;
   }    
   smallsort=sid; 
   $("[name=selectproductsort]").removeClass();    
   obj.className="current";

   searchProduct();
}

//品牌选择
function selectbrandsort(brandid,obj){
    brandsort=brandid;
    $("[name=selectbrandsort]").removeClass();
    obj.className="current";    
    searchProduct();
}
//类别选择
function selectclasssort(classid,obj)
{ 
    
    classsort=classid;
    $("[name=selectclasssort]").removeClass();
    obj.className="current";    
    searchProduct();
}
//重量选择
function selectweight(minvalue,maxvalue,obj){
  
   minweight=minvalue;
   maxweight=maxvalue;
   $("[name=selectweight]").removeClass();
   obj.className="current";
   searchProduct();
}

//价格选择
function selectprice(minpvalue,maxvalue,obj){
   minprice=minpvalue;
   maxprice=maxvalue;
   $("[name=selectprice]").removeClass();
   obj.className="current";
   searchProduct();
}

//排序字段
function selectorderfiled(){   
   if($("#listorderfiled").val()=="Ret_Price_0"){
     orderfiled="Ret_Price";
     ordertype=0;
      hideOrderFiled="Ret_Price_0"; 
   }
   else if($("#listorderfiled").val()=="Ret_Price_1"){
     orderfiled="Ret_Price";
     ordertype=1;
      hideOrderFiled="Ret_Price_1"; 
   }
   else{
     orderfiled=$("#listorderfiled").val();
   }   
   searchProduct();
}

//根据价格排序类型
function selectorderpricetype(){
      
   orderfiled="Ret_Price";
   if(ordertype==0){
     ordertype=1;    
     $("#listorderfiled").val("Ret_Price_1");    
     hideOrderFiled="Ret_Price_1";    
   }
   else{
     ordertype=0;
     $("#listorderfiled").val("Ret_Price_0"); 
     hideOrderFiled="Ret_Price_0";    
  
   }     
   searchProduct();
}

//根据人气排行
function selectordersellcounttype(){
   orderfiled="SaleNum";
   hideOrderFiled="SaleNum";
   $("#listorderfiled").val("SaleNum");   
   searchProduct();
}


function searchProduct(){ 
   if(keyword.indexOf('%')==-1){
       keyword=encodeURIComponent(keyword);    
   } 
  
   var url='?bid='+bigsort+'&mid='+midsort+'&sid='+smallsort+'&brandid='+brandsort+'&minprice='+minprice
            +'&maxprice='+maxprice+'&minweight='+minweight+'&maxweight='+maxweight+'&orderfiled='+orderfiled
            +'&ordertype='+ordertype+"&keyword="+keyword+'&cid='+classsort;     
   ajaxPanel(url,"productlistajax");
   
}

$(function() {
    if ($("#search").val() == '') {
        $("#search").val('搜索商城商品!');
    }
    $('#search').focus(function() {
        if ($(this).val() == '搜索商城商品!') {
            $(this).val('');
        }
    }).blur(function() {
        if ($(this).val().length == 0) {
            $(this).val('搜索商城商品!');
        }
    }).bind('keydown',
    function(event) {
        if (event.keyCode == 13) {
            $("#btnsearch").click();
			$("#btnsearchly").click();
            return false;
        }
    });
    $('#ipt_yzm').bind('keydown',
    function(event) {
        if (event.keyCode == 13) {
            $("#login").click();
            return false;
        }
    });

    //头部搜索商品
    $("#btnsearch").click(function() {
        var keyword = $("#search").val();
        if (keyword == '' || keyword.indexOf('搜索') != -1) {
            alert("请输入要搜索的商品关键字");
            $("#search").val('');
            $("#search").focus();
        } else {
            location.href = 'cn_asp/products.asp?keyword=' + encodeURIComponent(keyword);
        }
    });
	$("#btnsearchly").click(function() {
        var keyword = $("#search").val();
        if (keyword == '' || keyword.indexOf('搜索') != -1) {
            alert("请输入要搜索的商品关键字");
            $("#search").val('');
            $("#search").focus();
        } else {
            location.href = 'products.asp?keyword=' + encodeURIComponent(keyword);
        }
    });
});

/*把用户退出商城系统*/
function logout(userId, Obj) {
	
    CrmUrl = Obj.getAttribute('crm');
    ShopUrl = Obj.getAttribute('mall');
    //var postUrl = CrmUrl +"/Tool/Ajax.ashx"; 
	var postUrl = "../cn_asp/Ajax.asp"; 
    var re = false;  
    var param = $.param({ op: "shoplogout", userId: userId, successUrl: "login.asp"});
    jQuery.getScript(postUrl+ "?" + param + "&callback=?", function(data) {
  
    }); 
}

function jsonp_callback(t){
        var j = eval('(' + t + ')');
        if (j.result == 0) {
            re = true;                     
            //location.href= ShopUrl +"/Login.html";
			location.href= "../cn_asp/Login.asp";
        }
        else {
            alert(j.msg);           
            re = false;
        }
    }     
function TurnToLocation(v){
 location.href=v;
}
//ajax提交
function submit(url,callback){    
     $.ajax({
               type: 'get',
               dataType: 'html',
               contentType: "application/x-www-form-urlencoded; charset=utf-8",
               cache: false,
               url: url,                
               success:
               function (result) { 
                  if(callback){
                     callback(result);
                  }
               },
               error:
               function (XMLHttpRequest, textStatus, errorThrown) {       
                  alert(XMLHttpRequest.responseText+"---前面为错误内容！！！");     
                  alert(textStatus); 
               }
                
            }
        ); 
}

function toFixedDec(num, len) {
    num = Math.round(num * Math.pow(10, len)) / Math.pow(10, len);
    return num;
}