// JavaScript Document
/* -----------------------------------
 給實體商店用的 js
 網頁位置 : 
   diary/shop_physical.php
   diary/html/shop_physical/*
   
   修改動作 : 
   ----------
   將 <img src="images/button1.jpg" width="137" height="35" />
   替換成 <a class="put_in_cart" href="javascript: return false;"><img src="images/button1.jpg" width="137" height="35" border="0" /></a><br />
----------------------------------- */
var act_path = '../../';

// 加入購物車
function put_in_cart(){
	var member_num = $("#member_num").val();
	// 要求先登入
	if(member_num==''){
		alert('請先登入');
		parent.window.location = '../../../?url=member_login_________';
		return false;
	}
	$(this).siblings(".put_num").css("display","block");
}

// 加入購物車: 確定
function put_submit(){
	var pro_name = $("#pro_name").html();
	var num = $(this).siblings(".sel_num").val();
	var phy_id = $("#phy_id").val();
	
	var confirm_msg = 
	  "-------------------- \n" +
	  "你確定要將以下商品加入購物車嗎? \n" + 
	  "-------------------- \n" + 
	  "商品名稱：" + pro_name + "\n" + 
	  "商品數量：" + num + "\n\n";
	
	// 查詢該商品是否已經有在購物車中
	$.post(act_path + "shop_physical_act.php", {phy_id:phy_id, act:'check_cart'}, function(data){
		if(data!=0){
			confirm_msg = "你的購物車中已經有這項商品了，數量是: " + data + "\n\n" + confirm_msg;
		}
		if(confirm(confirm_msg)==true){
			$.post(act_path + "shop_physical_act.php", {phy_id:phy_id, num:num, act:'put_in_cart'}, function(order_id){
				alert('已加入');
				parent.window.location = act_path + 'shop_form.php?id='+order_id;
			});
		}
	});
}

// 加入購物車: 取消
function put_cancel(){
	$(this).parent(".put_num").css("display","none");
}

// 檢視購物車
function check_cart(){
	// QQ 商店首頁，從購物車列表挑選檢視
	if(typeof($(this).attr('cart_id'))!='undefined'){
		var cart_id = $(this).attr('cart_id');
		parent.location = 'shop_form.php?id='+cart_id;
	// 商品內頁，檢視目前的購物車
	}else{
		var member_num = $("#member_num").val();
		if(member_num==''){
			alert('請先登入');
			parent.window.location = '../../../?url=member_login_________';
			return false;
		}
		$.post(act_path + "shop_physical_act.php", {act:'get_cart', member_num:member_num}, function(cart_id){
			if(cart_id==''){
				alert('目前沒有新的購物車，如果要檢視以前的購物紀錄，請從QQ商店首頁點選 "檢視我的購物紀錄"');
			}else{
				parent.location = act_path + 'shop_form.php?id='+cart_id;
			}
		});
	}
}

// QQ 商店首頁，檢視購物車列表
function view_cart(){
	
}

$(document).ready(function(){
	if($("#.view_cart").length){$("#.view_cart").bind('click', view_cart);}
	$(".put_in_cart").bind('click', put_in_cart);
	$(".check_cart").bind('click', check_cart);
	$(".put_submit").bind('click', put_submit);
	$(".put_cancel").bind('click', put_cancel);
});
