﻿$(document).ready(function(){
	$("#orderClear").click(function(){
		if(!confirm("您真地要清除购物车中的所有商品信息吗？")) return;
		$.get(jc.root+jc.servlet+"OrderServlet.jc", {s:"orderclear"},function(d){
			try{eval(d);}catch(e){alert('服务器响应出错，返回如下代码：\r\n'+d);}
		});
	});
});

var numAjaxLoading=false;
function changeNum(obj, step, price, orderInfoId){
	if(numAjaxLoading) return;
	var num=parseInt($("#num_"+orderInfoId).val());
	if(step==-1 && num==1) return;
	var newNum=num+Number(step);
	var oldTPrice=Number($("#totalPrice").val());
	var newTPrice=newNum*price;//当前商品新总价
	var allTPrice=oldTPrice+(newTPrice-num*price);//合计总价
	numAjaxLoading=true;
	$.ajax({
		type:"GET",
		url :jc.root+jc.servlet+"OrderServlet.jc",
		data:{s:"orderInfoNumChange", id:orderInfoId, itemNum:newNum, itemTPrice:newTPrice, itemsTPrice:allTPrice},
		success:function(rtn){
			var rtnA=rtn.split("|");
			if(rtnA.length==2){
				$("#num_"+orderInfoId).val(newNum);
				$("#totalPrice").val(allTPrice);
				$("#js_tPrice_"+orderInfoId).html(rtnA[0]);
				$("#totalDiv").html(rtnA[1]);
			}else{
				document.location.reload();
			}
			numAjaxLoading=false;
		}
	});
}

function changeNumDz(obj, step, price, orderInfoId){
	if(numAjaxLoading) return;
	var num=parseInt($("#num_"+orderInfoId).val());
	if(step==-1 && num==1) return;
	var newNum=num+Number(step);
	var oldTPrice=Number($("#totalPrice").val());
	var newTPrice=newNum*price;//当前商品新总价
	var allTPrice=oldTPrice+(newTPrice-num*price);//合计总价
	numAjaxLoading=true;
	$.ajax({
		type:"GET",
		url :jc.root+jc.servlet+"OrderServlet.jc",
		data:{s:"orderInfoNumChangeDz", id:orderInfoId, itemNum:newNum, itemTPrice:newTPrice, itemsTPrice:allTPrice},
		success:function(rtn){
			var rtnA=rtn.split("|");
			if(rtnA.length==2){
				$("#num_"+orderInfoId).val(newNum);
				$("#totalPrice").val(allTPrice);
				$("#js_tPrice_"+orderInfoId).html(rtnA[0]);
				$("#totalDiv").html(rtnA[1]);
			}else{
				//var win=window.open();win.document.write(rtn);
				document.location.reload();
			}
			numAjaxLoading=false;
		}
	});
}

function delGift(id, sign){
	if(confirm("您真地要从购物车中删除此礼包吗？"))
		$.get(jc.root+jc.servlet+"OrderServlet.jc", {
				s:"carRemoveGift",
				orderinfId:id,
				signer:sign
			  },
			  function(d){
				  document.location.reload();
			  }
		);	
}