﻿//document.writeln(location.host);
function showAlert_shopCart(message) {
	alert(message);
}
function show_small_cart() {	
$.ajax({type:'post', datatype:'json', url:small_cart_url, data: {"action":"GetCart"} , success:function (json) {
		small_cart_html(json);
	}});
}
function small_cart_html(json) {
	//var data=eval("("+json+")");//תΪjson ,JSķõһС÷һ
	var result=$.evalJSON(json);
	var data=result.product_list;
	var html ='<div class="MyCart_List"><div class="MyCart_Tip">';
	for(var i=0;i<data.length;i++){
		html += '<div class="left"><a href=\"'+contextPath+'/product/goUrl.do?id='+data[i].goodCode+'\"><img src="'+contextPath+'/productsdetail/'+data[i].u3+'/images/'+(data[i].goodCode).substring(0,13)+'00-L.jpg" /></a></div><div  class="center"><a href=\"'+contextPath+'/product/goUrl.do?id='+data[i].goodCode+'\">'+data[i].goodName+'</a></div><div class="right"><strong>￥'+data[i].price+'×'+data[i].quantity+'</strong><p><a href=\"javascript:small_removeProduct(\''+data[i].goodCode+'\',\''+data[i].buyWay+'\');\">删除</a></p></div> <div class="clear"></div><div class="hr"></div>';
		}
	var pointsUse="";
	if(result.totla_pointsUse>0){
		pointsUse=';&nbsp;&nbsp;积分总计：<strong>'+result.totla_pointsUse+'</strong>';
		}
	html += '<div class="right1">共<strong>'+result.totla_quantity+'</strong>件商品&nbsp;&nbsp;&nbsp;金额总计：<strong>￥'+result.total_price+'</strong>'+pointsUse+'</div><div class="right2"><a href="'+contextPath+'/purchase/shopCart.do?action=list"><img src="'+contextPath+'/images/settlement.gif" border="0" /></a></div></div></div>';
	if(data.length==null){
		html = '<div class="MyCart_List"><div class="MyCart_Tip">您的购物车中暂无商品，赶快选择心爱的商品吧！</div></div>';
		}
	$("#cartnum").text(result.totla_quantity);
	$("#MyCart").html(html);
}

//删除购物车中的商品
function small_removeProduct(productId,buyway)
{
  if(confirm('确定不购买该商品？'))
   {   
	  $.post(small_cart_url, {"action":"removeProduct","goodCode":productId,"buyWay":buyway}, function (data) {
	  var info=data;	
	  if(info=="success"){
		  //showAlert_shopCart("在购物车中清除此商品成功"); 
		  show_small_cart();
		  //window.open(small_cart_url + "?action=list", "_self");
		}else{
			showAlert_shopCart("在购物车中清除此商品失败"); 					
			}
	
	});
   
}}
$(document).ready(function() { 
	show_small_cart();
});	
