﻿// JScript 文件

var listProducts=new Array();



///促销品在购物车的数量
function CheckChuXiao(gdsid)
{
 
    
 

}


///将产品加入到全局的list中
function ProductAdd( productcode, productname, price, quantity, typename,typeID)
{

                    var product = new Object();
                    product.code =productcode;
                    product.name = productname;
                    product.price=price;      
                    product.quantity=quantity;   
                    product.typename=typename;  
                    product.typeID=typeID;  
                     
                   if(ProductSearch(productcode)==0){listProducts.push(product);}
                   else
                   {   
                                for( i=0;i<listProducts.length;i++)
                                {
                                        if(listProducts[i].code==product.code)
                                           {
                                        listProducts[i].quantity=  listProducts[i].quantity+quantity;
                                            return 0;  
                                            }
                                } 
                    }
                    
                   
     
        
}

//删除特定的产品
function productDelete(code)
{
for( i=0;i<listProducts.length;i++)
{
if(listProducts[i].code==code)
{
listProducts.splice(i,1);

return 0;
}

}

}


//查找是否存在
function  ProductSearch(code)
{
for( i=0;i<listProducts.length;i++)
{
        if(listProducts[i].code==code)
        {
         
        return 1;
        }
}

return 0;
}

/// 计算总价
function caluTotalPrice()
{
var total=0;
for( i=0;i<listProducts.length;i++)
{
total+=listProducts[i].price * listProducts[i].quantity;
}

return total+parseInt($("#smallcart_product_price").val());

}


//左侧小型购物车显示
function SmallCartShow(){


$(document).ready(function(){

//按typename排序
listProducts.sort(function(a,b){
if(a.typename>b.typename){return 1;} 
if(a.typename<b.typename) {return -1;}
if(a.typename==b.typename) {return 0;}
})

var typename="";
var smallcart_html="";
var flag_endUl=0;//判断是否闭合的，0代表正常，1，代表出现了ul开头

if(listProducts.length<=0)
{
  smallcart_html="<div class=\"mark_b_div\">您还没有选择任何促销品</div>";
}

for( i=0;i<listProducts.length;i++)
{

//闭合ul
if(listProducts[i].typename!=typename&&flag_endUl==1)
{
 smallcart_html+="</ul>";
 flag_endUl=0;
}


if(listProducts[i].typename!=typename)
{

typename=listProducts[i].typename;
flag_endUl=1;
smallcart_html+="<ul class=\"ac_ul\"><strong>"+typename+"</strong>";
}
    if(typename=="推荐配件"){
    smallcart_html+="<li>"+listProducts[i].name+" <br/><span class=\"text_gray\">数量："+listProducts[i].quantity+" &nbsp;&nbsp;价格：&yen; "+listProducts[i].price+"</span>  <img  id='"+listProducts[i].code+"' width='16' height='16' hspace='5' align='absmiddle' style='margin-bottom:8px;' src=\"image/smallcart_cancelproduct.gif\" onclick=\"Smallcart_cancelproduct_click('"+listProducts[i].code+"') \"/> </li> ";

    }
    else
    {
    smallcart_html+="<li>"+listProducts[i].name+" <br/><span class=\"text_gray\">数量："+listProducts[i].quantity+" &nbsp;&nbsp;价格：&yen; "+listProducts[i].price+"</span>  </li> ";
    }
    
 
}

$("#Shopsmallcart").html(smallcart_html);

$("#totalPrice").html("&yen; "+caluTotalPrice());

});








}






///小购物车上取消按钮
function Smallcart_cancelproduct_click(id)
{
  productDelete(id);
  SmallCartShow();
  return 0;

}

///checkbox点击
function CheckBoxClick(id,typename,typeID)
{

///返回id的前面部分,这个id我们是自己构造的
  mystr=id.split("_");   
  if(mystr.length>0)
{product_html_id=mystr[0]+"_code_"+mystr[mystr.length-1];
price_html_id=mystr[0]+"_price_"+mystr[mystr.length-1];;
productname_html_id=mystr[0]+"_name_"+mystr[mystr.length-1];
}
else
{return 0;}  
  //////////

    productcode=$("#"+product_html_id).val();
    productname=$("#"+productname_html_id).val();
    price=$("#"+price_html_id).val();
    quantity=1;  
     mainProductcode=$("#mainproductCode").val();
    
 if($("#"+id).attr("checked")==true)
      { 
      
   
   
    
      $.ajax({
   type: "get",
   url: "LenovoShop02_Cart.ashx?actioncode=4&presentID="+productcode+"&gdsid="+mainProductcode +"&date="+new Date().getTime(),
   dataType: "text",
   beforeSend:function(){ },
   complete:function(){ },
   error:function(xhr,ajaxOptions,thrownError){alert("调用出错");}, 
   success: function(msg){
    if(msg!=-1){  
                 var MaxQuantity=msg;
                  var ActureQuantity=0
                 for(i=0;i<listProducts.length;i++)
                 {
                        if(listProducts[i].code==productcode)
                        {
                      ActureQuantity=parseInt(listProducts[i].quantity)
                        }     
                 }
     
                   if(parseInt(MaxQuantity)>parseInt(ActureQuantity))
                   {   ProductAdd( productcode, productname, price, quantity, typename,typeID);
                    SmallCartShow();
                   }
                   else
                   {
                    $("#"+id).attr("checked","");
                   alert("已经达到最大数量");
                    SmallCartShow();
                   }
    
     }}
});
   
         
                    
      }
      else
      { 
      productDelete(productcode);      
      }   
   
   
       SmallCartShow();

}


//按钮点击
function buttonClick(id,typename,typeID)
{
///返回id的前面部分,这个id我们是自己构造的
  mystr=id.split("_");   
  if(mystr.length>0)
{product_html_id=mystr[0]+"_code_"+mystr[mystr.length-1];
price_html_id=mystr[0]+"_price_"+mystr[mystr.length-1];;
productname_html_id=mystr[0]+"_name_"+mystr[mystr.length-1];
}
else
{return 0;}  
  //////////
  
    productcode=$("#"+product_html_id).val();
    productname=$("#"+productname_html_id).val();
    price=$("#"+price_html_id).val();
    quantity=1;     
    
    
    mainProductcode=$("#mainproductCode").val();
    
    
    
      $.ajax({
   type: "get",
   url: "LenovoShop02_Cart.ashx?actioncode=4&presentID="+productcode+"&gdsid="+mainProductcode+"&date="+new Date().getTime(),
   dataType: "text",
   beforeSend:function(){ },
   complete:function(){ },
   error:function(xhr,ajaxOptions,thrownError){alert("调用出错");}, 
   success: function(msg){
    if(msg!=-1){  
                 var MaxQuantity=msg;
                  var ActureQuantity=0
                 for(i=0;i<listProducts.length;i++)
                 {
                        if(listProducts[i].code==productcode)
                        {
                      ActureQuantity=parseInt(listProducts[i].quantity)
                        }     
                 }
     
                   if(parseInt(MaxQuantity)>parseInt(ActureQuantity))
                   {   ProductAdd( productcode, productname, price, quantity, typename,typeID);
                    SmallCartShow();
                   }
                   else
                   {alert("已经达到最大数量");
                    SmallCartShow();
                   }
    
     }}
});
  

}







///小购物车结算按钮
function CheckoutClick(gdsid,BelongTo)
{
var parm="";
for(i=0;i<listProducts.length;i++)
{
        if(i!=listProducts.length)
        {
        parm+=listProducts[i].code+"_"+listProducts[i].quantity+"_"+listProducts[i].typeID+"_"+BelongTo+",";
        }
        else
        {
        parm+=listProducts[i].code+"_"+listProducts[i].quantity+"_"+listProducts[i].typeID+"_"+BelongTo;
        }
}



parm=parm.substring(0,parm.length-1)
$("#gdsid").val(gdsid);
$("#a").val(parm);
if(BelongTo=="1"){
$("#form_product").attr("action","LenovoShop_ShopCart.aspx");}

if(BelongTo=="0"){
$("#form_product").attr("action","LenovoShop_ShopCart_fb.aspx");}

$("#form_product").submit();

}










