$(function()
{
	if($("#cartlist").length>0){loadcart();}
	//减少数量
	$("#cartlist .min").live("click",function(){
		var shopnum=$(this).attr("old");
		var id=$(this).attr("productid");
		shopnum--;
		if(shopnum==0){delcart(id);}else{editnum(id,shopnum);}
	})
	//增加数量
	$("#cartlist .max").live("click",function(){
		var shopnum=$(this).attr("old");
		var id=$(this).attr("productid");
		shopnum++;
		if(shopnum>=999){shopnum=999;}
		editnum(id,shopnum);
	})
	//修改数量
	$("#cartlist .quantity").live("blur",function(){
		var shopnum=$(this).val();
		var old=$(this).attr("old");
		var id=$(this).attr("productid");
		if(shopnum>=999){shopnum=999;}
		if(shopnum==0){delcart(id);}else{editnum(id,shopnum);}
	})
	
})

function loadcart()
{
	$.ajax({type:"post",cache:false,url:webroot+"cart.asp?act=loadcart",success:function(_){$("#cartlist").html(_);}});
}

//开始修改数量
function editnum(t0,t1)
{
	var r=/^\+?[0-9][0-9]*$/;
	if(!r.test(t1))
	{
		$.fn.tips({type:"warn",content:"请正确输入商品数量"});
		return false
	}
	$.ajax({type:"post",cache:false,url:webroot+"cart.asp?act=editcart&id="+t0+"&shopnum="+t1,success:function(_)
	{
		
		var act=_.substring(0,1);
		var info=_.substring(1);
		if(act==0){$.fn.tips({content:info,time:3000});loadcart();}
		else
		{loadtopcart();loadcart();get_cartnum();}
	
	}});
}

function delcart(id)
{
	if(confirm("您要从购物车中移除此商品吗？"))
	{
		$.ajax({type:"post",cache:false,url:webroot+"cart.asp?act=delcart&id="+id,success:function(_){if(_==1){loadtopcart();loadcart();get_cartnum();}}});
	}
}
