function getCart(){
	$("#cartMsg").html("更新購物車內容");
	$.ajax({
		url:"ajax/get_cart.php",
		dataType:'json',
		timeout:5000,
		success:function(response){
			$("#cartMsg").html("");
			$("#cartQuantity").html(response.itemCount);
			$("#cartAmount").html(response.totalPrice);
		},
		error:function(jqXHR,textStatus){
			$("#cartMsg").html("購物車更新失敗");
			$("#cartQuantity").html("---");
			$("#cartAmount").html("---");
		}
	});
}
function btnShopClick(){
	$("#btnShop").attr("disabled",true);
	var productID=$("#productID").val();
	var productNo=$("#productNo").val();
	var productName=$("#productName").val();
	var productQty=$("#quantity").val();
	var productPrice=$("#price").val();
	var postData={
		pID:productID,
		pNo:productNo,
		pName:productName,
		qty:productQty,
		price:productPrice
	};
	$.ajax({
		url:"ajax/add_cart.php",
		data:postData,
		dataType:'json',
		timeout:5000,
		type:'post',
		success:function(response){
				if(response.errCode==0){
					alert("已加入購物車")
					getCart();
				}
				else{
					alert(response.errMsg);
				}
		},
		error:function(jqXHR,textStatus){
				alert("無法由伺服器取得資料,請稍後再試");
		},
		complete:function(){
			$("#btnShop").attr("disabled",false);
		}
	});
}
function btnQuickShopClick(){
	$("#btnShop").attr("disabled",true);
	var productID=$("#productID").val();
	var productNo=$("#productNo").val();
	var productName=$("#productName").val();
	var productQty=$("#quantity").val();
	var productPrice=$("#price").val();
	var postData={
		pID:productID,
		pNo:productNo,
		pName:productName,
		qty:productQty,
		price:productPrice
	};
	$.ajax({
		url:"ajax/add_cart.php",
		data:postData,
		dataType:'json',
		timeout:5000,
		type:'post',
		success:function(response){
				if(response.errCode==0){
					alert("已加入購物車")
					getCart();
					location.replace("spCar2.php");
				}
				else{
					alert(response.errMsg);
				}
		},
		error:function(jqXHR,textStatus){
				alert("無法由伺服器取得資料,請稍後再試");
		},
		complete:function(){
			$("#btnShop").attr("disabled",false);
		}
	});
}
function quantityChange(){
	var totalPrice=$("#quantity").val()*$("#price").val();
	$("#totalPrice").text(totalPrice);
}
function initFloatMessageBox($msgBox,_diffX,_diffY,_moveSpeed){
	var $win = $(window),
	_width = $msgBox.width(),
	_height = $msgBox.height();
 	$msgBox.show();
	// 先把 $msgBox 移動到定點
	$msgBox.css({
		top: $(document).height(),
		left: $win.width() - _width - _diffX
	});
	// 幫網頁加上 scroll 及 resize 事件
	$win.bind('scroll resize', function(){
		var $this = $(this);
	// 控制 $msgBox 的移動
		$msgBox.stop().animate({
			top: $this.scrollTop() + $this.height() - _height - _diffY,
			left: $this.scrollLeft() + $this.width() - _width - _diffX
		}, _moveSpeed);
	}).scroll();	// 觸發一次 scroll()	
	getCart();
}
