G.app.cart={
	lang:[
		//Please fill out the right to purchase the number of
		//Can not continue to increase the number
		//Can not continue to reduce the number of
		//Enter the number of incorrect
		//Number greater than the purchase of quantity
		//Successfully
		"请填写正确的购买件数",
		"不能继续增加数量",
		"不能继续减少数量",
		"输入的数量有误",
		"数量大于限购数量",
		"修改成功"
	],
	_buf:{_data:{},get:function(a){return this._data[a]},set:function(b,a){this._data[b]=a},reset:function(a){if(a in this._data){delete this._data[a]}}},
	delProduct:function(i){	
		var sdata = "inajax=1&act=del&id="+cartInfo[i].id;
		$.ajax({
			type:"POST",
			url:"/purchase/shoppingcart",
			data:sdata,
			cache:false,
			dataType:"json",
			success:function(data){
				if($.type(data)=="object"){
					if(data.success){
						cartInfo.splice(i,1);
						G.app.cart.init();
						G.app.cart.refreshCartNum();
					}else{
						G.ui.popup.showMsg(data.message,{"type":data.type});
					}
				}
			}
		});
	},
	addPnum:function(f,i){
		var n=G.app.cart,l=$("#pnum_"+f),j=l.val();//alert(f);
		var inventory = l.attr("numlimit");	//alert(inventory)
		inventory = parseInt(inventory,10);
		if(!G.logic.validate.checkNumber(j)){l.val(l.attr("curNum"));return G.ui.tips.warn(this.lang[0],l.parent().parent())}
		if(j>=inventory&&i==1){G.ui.tips.warn(this.lang[1],l.parent().parent());return}else{if(j==1&&i==-1){G.ui.tips.warn(this.lang[2],l.parent().parent());return}}
		var d=(j-0)+(i-0);
		if(d<=0||d>inventory){l.val(l.attr("curNum"));return G.ui.tips.warn(this.lang[3],l.parent().parent())}
		var e=G.app.cart._buf.get("cartInfo");
		var k=l.attr("numlimit");
		if(k>0&&d>k){l.val(l.attr("curNum"));return G.ui.tips.warn(this.lang[4],l.parent().parent())}

		var sdata = "inajax=1&act=edit&col=pnum&val="+d+"&id="+cartInfo[f].id;
		
		$.ajax({
			type:"POST",
			url:"/purchase/shoppingcart",
			data:sdata,
			cache:false,
			dataType:"json",
			success:function(data){
				//alert(data);
				if($.type(data)=="object"){
					if(data.success){
						l.attr("curNum",d);
						l.val(d);
						G.ui.tips.suc(G.app.cart.lang[5],l.parent().parent());
						price = cartInfo[f].price;
						cartInfo[f].pnum = d;
						G.app.cart.setPrice();
					}else{
						G.ui.popup.showMsg(data.message);
					}
				}
			},
			error:function(x){
				alert(x.responseText);
			}
		});
	},
	remark:function(f,v){
		var sdata = "inajax=1&act=edit&col=remark&val="+v+"&id="+cartInfo[f].id;
		$.ajax({
			type:"POST",
			url:"/purchase/shoppingcart",
			data:sdata,
			cache:false,
			dataType:"json",
			success:function(data){
				//alert(data);
				if($.type(data)=="object"){
					if(data.success){
						
					}else{
						//G.ui.popup.showMsg(data.message);
					}
				}
			},
			error:function(x){
				alert(x.responseText);
			}
		});
	},
	init:function(){
		/*
		var price = 0;
		var html="";
		for(var i=0;i<cartInfo.length;i++){
			var item = cartInfo[i];
			price += item.price*item.pnum;
			html += '<tr><td class="todo_link"><a onclick="G.app.cart.addToFavor(\''+i+'\');return false;" href="javascript:;" class="hide"></a><a onclick="G.app.cart.delProduct(\''+i+'\');return false;" href="javascript:;">删除</a></td><td><div class="wrap_goods"><span class="img"><a href="'+item.url+'" target="_blank"><img style="width: 80px;" alt="'+item.name+'" src="'+item.thumb+'" /></a></span></div></td><td><p class="name"><a href="'+item.url+'" target="_blank">'+item.name+'</a></p></td><td><span class="wrap_input"><span class="jian" onclick="G.app.cart.addPnum(\''+i+'\',-1);return false">-</span><span class="input"><input id="pnum_'+i+'" class="pnum" onblur="G.app.cart.addPnum(\''+i+'\',0);" type="text" curNum="1" numlimit="'+item.inventory+'" value="'+item.pnum+'" /></span><span class="jia" onclick="G.app.cart.addPnum(\''+i+'\',1);return false">+</span></span></td><td><p class="strong"><span class="i_yuan">$</span><span class="pprice" id="amount_'+i+'">'+item.amount+'</span></p></td></tr>';
		}
		$("span#totalPrice").html(price);
		$("tbody#cartList").html(html);
		*/
		if(cartInfo.length==0){
			var html = '';
			if($.cookie('account')){
				html = '<div class="cart_goods_empty_inner"><h3>篮子空空~去买点啥吧！</h3><p>您的购物车暂时无任何商品，请尽快在选购商品吧！<a class="c_tx0" href="/" target="_blank">随便逛逛</a></p></div>';
			}else{
				html = '<div class="cart_goods_empty_inner"><h3>篮子空空~去买点啥吧！</h3><p>温馨提醒：您可以<a title="登录" href="javascript:login()" class="mod_btn mod_btn_bg1 mod_btn_mini c_tx0">登录</a>购物车，查看之前加入的商品，或者 <a class="c_tx0" href="/">随便逛逛</a></p></div>';
			}
			$(".cart_goods_empty").show().html(html);
			$(".table_cart").hide();		
		}
		 $.Template.stuff(cartInfo,"cartList");
		 G.app.cart.setPrice();
	},
	setPrice:function(){
		var price = 0;
		var pricePay = 0;
		var prizePrice = 0;
		var scorePrice = 0;
		var interestAmount = 0;
		var downPayment = 0;
		var divideMoney = 0;
		for(i=0;i<cartInfo.length;i++){
			var item = cartInfo[i];
			amount = parseFloat(cartInfo[i].price)*parseFloat(cartInfo[i].pnum);
			$("#amount_"+i).html(amount.toFixed(2));
			price += amount;
			if(cartInfo[i].buyWay!=2){
				pricePay += amount;
			}
			prizePrice += parseFloat(cartInfo[i].prizePrice);
			scorePrice += parseInt(cartInfo[i].scorePrice);
		}
		var prizeItem = $("span#prizePrice").parent('p').parent('div.item');
		var scoreItem = $("span#scorePrice").parent('p').parent('div.item');
		$("span#totalPrice").html(price.toFixed(2));
		$("span#prizePrice").html(prizePrice.toFixed(2));if(prizePrice.toFixed(2)<=0){prizeItem.hide();}else{prizeItem.show();}
		$("span#totalPay").html(pricePay.toFixed(2)-prizePrice.toFixed(2));
		$("span#scorePrice").html(scorePrice);if(scorePrice<=0){scoreItem.hide();}else{scoreItem.show();}
		$("span#interestAmount").html(interestAmount);
		$("span#downPayment").html(downPayment);
		$("span#divideMoney").html(divideMoney);
	},
	checkOrder:function(){
		$("#checkorder_btn").attr('disabled','disabled');
		var scorePrice = $("span#scorePrice").html();
		scorePrice = parseInt(scorePrice,10);
		var myScore = $("span#myScore").html();
		myScore = parseInt(myScore,10);
		if(myScore<scorePrice){G.ui.popup.showMsg("您的积分不足");$("#checkorder_btn").removeAttr('disabled');return;}
		if(cartInfo.length==0){G.ui.popup.showMsg("购物车为空，请先选择商品");$("#checkorder_btn").removeAttr('disabled');return;}
		$.ajax({url:"/data/user",type:'POST',data:"inajax=1&act=info",dataType:'json',success:function(json){ if(json.success){ location.href='/purchase/order';return; }else if(json.code && json.code==1000){ 
					COLDialog = COL.dialog.open('/user/login?style=dialog&mod=login',{width:430,boder : 'thin',delay:0,head:'none',close:function(){ $("#checkorder_btn").removeAttr('disabled');userHello(); }});
					
			}else{ COL.dialog.mini({ type:'error','content':json.message });$("#checkorder_btn").removeAttr('disabled'); } },error:function(){ alert('系统错误，请稍候再试');$("#checkorder_btn").removeAttr('disabled'); }});
		
	},
	orderInit:function(){
		var price=0;
		var html="";//alert(cartInfo[0].name);
		for(var i=0;i<cartInfo.length;i++){
			var item = cartInfo[i];
			price += item.price*item.pnum;
			html+='<tr><td><div class="wrap_goods"><div><p></p><p class="name"><a href="'+item.url+'" target="_blank">'+item.name+'</a></p><p class="condition"><span></span>&nbsp;&nbsp;&nbsp;&nbsp;<span></span></p></div></div></td><td><strong class="strong"><span class="i_yuan">¥</span>'+item.price+'</strong></td><td>'+item.pnum+'</td>';
			if(i==0){
				html+='<td class="total" rowSpan="'+cartInfo.length+'"><p psubid="1">¥<span id="ppriceAll"></span></p><span psdes="1">（不含运费）</span></td>';
			}
			html+='</tr>';
		}
		
		$("tbody#cartList").html(html);
		$("span#ppriceAll").html(price);
	},
	clearCart:function(){
		if(!confirm("是否确认清空购物车？")){ return false; };
		$.ajax({
			type:"POST",
			url:"/purchase/shoppingcart",
			data:"inajax=1&act=clear",
			dataType:"json",
			success:function(json){
				if(json.success){
					cartInfo = [];
					G.app.cart.init();
				}else{
					alert(json.message);
				}		
			}
		})
	},
	refreshCartNum:function(){
		$.ajax({url:"/data/cartNum?r="+Math.random(),dataType:"json",scriptCharset:"utf-8",success:function(a){a&&a.data&&$("#cartInfo_number").html(a.data.num)}});
	}
}