//购物车
$(document).ready(function(){
	$('input[@name=quantity]').keydown(function(event){
	    var curobj = $(this);
	    curobj.attr('oldvalue',curobj.val());
	    if((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 8){
	    }else{return false;}
	}).keyup(function(event){
	    var curobj = $(this);
	    var min = $(this).siblings('[@name=min]').val();
	    if(curobj.attr('oldvalue') == curobj.val() || curobj.val() == ''){return false;};
	    if(Number(curobj.val()) < Number(min)){return false;}
        var IsDlvUnit = curobj.attr('IsDlvUnit');
        var DlvUnit = curobj.attr('DlvUnit');
//        if(IsDlvUnit == 1 && DlvUnit > 0){
//            if((curobj.val() % DlvUnit) > 0){return false;}
//        }
	    var memberPrice = curobj.siblings('input[@name=memberPrice]').val();   //会员单价
	    var oItemTotal = curobj.siblings('span.itemTotal');  //显示每项商品价格的元素
	    
	    var recordID = curobj.siblings('input[@name=recordID]').val();
	    var gid = curobj.siblings('input[@name=gid]').val();
	    var pid = curobj.siblings('input[@name=productID]').val();
		var weburl = "ajax.aspx?NoCopyRight=1&type=UpdateCartNum&qty=" + curobj.val() + "&gid=" + gid + "&pid=" + pid;
		$.ajax({
		    url:weburl,
		    cache:false,
		    //async:false,
		    success:function(data){
		        if(data == 'nostorage'){
		            alert('输入的数量大于了剩余库存量');
		        }else{
		            BindCartInfo(curobj,gid,data,"add");
		        }
		    },error:function(){alert('change quantity error');}
		});// end ajax
	});
	$('input[@name=quantity]').blur(function(){
	    var curobj = $(this);
	    var min = curobj.siblings('[@name=min]').val();
	    if(Number(curobj.val()) < Number(min) || curobj.val() == ""){
	        alert('购买数量不能小于最少购买数量' + min);
	        curobj.val(min);
	        curobj.keyup();
	        return false;
	    }//end if
	});
	//删除商品
	$('.delpdt').click(function(){
		var curobj = $(this);
		if(confirm('确定要从购物车中删除此商品吗？')){
			var gid = curobj.parent().siblings('.quantity').children('input[@name=gid]').val();
			var pid = curobj.parent().siblings('.quantity').children('input[@name=productID]').val();
			var weburl = "ajax.aspx?NoCopyRight=1&type=UpdateCartNum&qty=0&gid=" + gid + "&pid=" + pid;
			$.ajax({
				url:weburl,
				cache:false,
				//async:false,
				success:function(data){
					if(data != "error"){
						curobj.parent().parent().remove();
						BindCartInfo(curobj,gid,data,"del");
					};
				},error:function(){alert('change quantity error');}
			});// end ajax
		}//end confirm
	});	
	
	//减少数量
	$('[@id^=reduce_]').each(function(i){
	    var recID = $(this).attr('id').replace('reduce_','');
	    $(this).click(function(){
	        var objNum = $('#quantity' + recID);
	        var min = $('#min_' + recID).val();
	        if(Number(objNum.val()) <= Number(min)){
	            alert('购买数量不能小于最少购买数量' + min);
	            return false;
	        }else{
	            objNum.val(Number(objNum.val())-1);
	            objNum.keyup();
	        }
	    });
	});
	//增加数量
	$('[@id^=add_]').each(function(i){
	    var recID = $(this).attr('id').replace('add_','');
	    $(this).click(function(){
	        var objNum = $('#quantity' + recID);
	        objNum.val(Number(objNum.val())+1);
	        objNum.keyup();
	    });
	});
});//end ready

function BindCartInfo(curobj,gid,data,type)
{
	var cartinfo = eval('(' + data + ')');
	var Product = cartinfo.Product;
	var SelectedGift = cartinfo.SelectedGift;
	var Gift = cartinfo.Gift;
	if(type=="add"){
		$.each(Product,function(i,n){
			if(n.GoodsID == gid){
				curobj.parent().siblings('.submoney').text(Number(n.SubMoney).toFixed(2));
				curobj.parent().siblings('.lessmoney').text(Number(n.LessMoney).toFixed(2));
			}
		})
	}	
	var tbSelectedGift = $('#dgShoppingCart');
	tbSelectedGift.find('.trSelectedGifts').remove();
	$.each(SelectedGift,function(i,n){
		var row = '<tr class="trSelectedGifts">';
		row += '<td class="productno">' + n.productno + '</td>';
		row += '<td class="productname"><a href="/products/product-' + n.productid + '.html" target="_blank">' + n.productname + '　<span style="color:Red;">(' + n.rulename + ')</span>　' + '<span style="color:Red;">' + n.giftremark + '</span>' + '</a></td>';
		row += '<td>&nbsp;</td>';
		row += '<td class="marketprice">' + Number(n.marketprice).toFixed(2) + '</td>';
		row += '<td class="mbprice">' + Number(n.pmtprice).toFixed(2) + '</td>';
		row += '<td class="quantity">' + n.giftnumber + '</td>';
		row += '<td class="score"></td>';
		row += '<td class="submoney">' + Number(n.submoney).toFixed(2) + '</td>';
		row += '<td class="lessmoney">' + Number(n.lessmoney).toFixed(2) + '</td>';
		row += '<td class="cancel">' + (n.isSystemAdd == "0" ? ("<a href='Actions.aspx?action=DeleteGift&ruleuid=" + n.ruleuid + "&productid=" + n.productid + "'>取消此礼品</a>") : "") + '</td>';
		row += '</tr>';
		tbSelectedGift.append(row);
	});
	
	var tbGiftGoods = $('#dgGiftGoods');
	tbGiftGoods.find('tr:gt(0)').remove();
	$.each(Gift,function(i,n){
		var row = '<tr>';
		row += '<td class="productno">' + n.productno + '</td>';
		row += '<td class="productname"><a href="/products/product-' + n.productid + '.html" target="_blank">' + n.productname + '　<span style="color:Red;">(' + n.rulename + ')</span>　' + '<span style="color:Red;">' + n.giftremark + '</span>' + '</a></td>';
		row += '<td class="marketprice">' + Number(n.marketprice).toFixed(2) + '</td>';
		row += '<td class="mbprice">' + Number(n.promotionprice).toFixed(2) + '</td>';
		row += '<td class="quantity">' + n.giftnumber + '</td>';
		row += '<td class="cancel"><a href="/Actions.aspx?action=AddGiftCart&ruleuid=' + n.ruleuid + '&productid=' + n.productid + '&gnum=' + n.giftnumber + '">添加到购物车</a></td>';
		row += '</tr>';
		tbGiftGoods.append(row);
	});
	var oldtotalprice = Number(cartinfo.totalmoney)+Number(cartinfo.promotionprice);
	$('#total_quantity').html(cartinfo.totalnumber);
	$('#totalScore').html(cartinfo.totalscore);
	$('#oldtotalprice').html(oldtotalprice);
	$('#promotionprice').html(Number(cartinfo.promotionprice).toFixed(2));
	$('#totalprice').html(Number(cartinfo.totalmoney).toFixed(2));
	$('#totalweight').html(Number(cartinfo.totalweight).toFixed(2));
}