//购物车操作方法 $FUJUN$

/* 减数量 */
function jian(productid,cartid){
	var pro_num=  $("#num_"+productid).val();	
	if(pro_num>1){	
		$("#num_"+productid).val(parseInt(pro_num)-1);		
		reduce(productid,cartid);
	}	
}
/* 添加数量 */
function jia(productid,cartid){
	var pro_num=  $("#num_"+productid).val();
	
	if(pro_num<998){	
		$("#num_"+productid).val(parseInt(pro_num)+1);		
		reduce(productid,cartid);
	}
}

/* 更新购物车 */
function reduce(productid,cartid){
	var ajaxUrl=ROOT+"?"+VAR_MODULE+"=Ajax&"+VAR_ACTION+"=updateCart&";
	if(typeof(updateCartURL)!="undefined")
		ajaxUrl	= updateCartURL;
	var pro_num=  $("#num_"+productid).val();
	if(isNaN(pro_num)){
		alert("请修改商品数量为数字!");
		return false;
	}
	if(pro_num<1){
		alert("商品数量少于1");
		return false;
	}
	else if(pro_num>999){
		alert("商品数量大为1000");
		return false;
	}
	$.ajax({
		type: "POST",
        data: {pid:productid,cid:cartid,pno:pro_num},
        url: ajaxUrl,
		dataType: "json",
        success: function(result) {
        	if (result.error == 0)
            {
        		//alert(result.content.totalprice);
        		//updateCartResponse(result.content,productid);        		
        		location.href=location.href;        		
            }
        },
        error: function(){alert("服务器没有返回数据，可能服务器忙，请重试"); }
	});
}

/* *
 * 处理更新商品到购物车的反馈信息
 */
function updateCartResponse(result,productid)
{
	//alert(result.totalprice+"**");
	$("#subtotal_"+productid).html(result.subtotal);	//单一商品总价 	
	$("#totalPrice").html(result.totalprice);			//总价
	$("#totalintegral").html(result.totalintegral);		//总获得积分 
	$("#getbonus").html(result.give_bonus);				//总获得优惠券	
	
}
/**
 * 验证红包序列号
 * @param string bonusSn 红包序列号
 */
function validateBonus(bonusSn)
{
	var ajaxUrl=ROOT+"?"+VAR_MODULE+"=Ajax&"+VAR_ACTION+"=validateBonus&";
	if(typeof(validateBonusUrl)!="undefined")
		ajaxUrl	= validateBonusUrl;
	var product_amount=$("#orderPrice").html();
	if(bonusSn=='请输入优惠券序列号'||bonusSn==''){
		alert(bonusSn);
		return;
	}
	$.ajax({
		type: "POST",
        data: {bonus_sn:bonusSn,product_amount:product_amount},
        url: ajaxUrl,
		dataType: "json",
        success: function(result) {
    		//alert(result);
        	if (result.error == 1) alert(result.message);
        	else if(result.error == 0){
        		alert(result.message);
        		$("#bonus_price").html(result.content.type_money);
        		change_price();
        	}
        },
        error: function(){alert("服务器没有返回数据，可能服务器忙，请重试"); }
	});	
}

function selectBonus(){
	if($("#gm_youhuiquan01").val()!=''){ $("#bonus_price").html($("#gm_youhuiquan01").find("option:selected").attr('price')); }
	else $("#bonus_price").html('0');
	change_price();
}
function setBonus(){
	$(":radio[name='use_bonus'][value='1']").attr("checked","checked");
}
function change_price(){
	$("#total_price").html($("#order_amount").val()-$("#bonus_price").html());
}
$(function() {	
	$("#gm_youhuiquan02").click(function(){ 
		if($(this).val()=='请输入优惠券序列号') {
			$(this).val('');  
		}
		$(":radio[name='use_bonus'][value='2']").attr("checked","checked");
	});
	$("#gm_youhuiquan02").blur(function(){
		if($(this).val()==''){
			$(this).val('请输入优惠券序列号');
		}
	});
});

