// JavaScript Document

// 配件子分類搜尋
function doSubmit(e)
{
	var id = $(e.target).attr('id');
	if( (id=='sub_id') ) {
		$("#form1").submit();
	}
}

// 把特價品放入購物車 (shop_ThickBox.php)
function putInCart_submit()
{
	var cart_id = $("#cart_id").val();
	var clothes_sn = $("#clothes_sn").val();
	$(this).parents("body").load('shop_ThickBox.php', {act:'putInCart_submit', clothes_sn:clothes_sn, cart_id:cart_id});
//	$("#put_into_cart").load('shop_ThickBox.php', {act:'putInCart_submit', clothes_sn:clothes_sn, cart_id:cart_id});
}

// 移除購物車的商品
function remove_item()
{
	var item_id = $(this).attr("item_id");
	var cart_id = $("#cart_id").val();
	var DOM_this = $(this);
	if(confirm('你確定要移除嗎?')){
		$.post('shop_ajax.php', {act:'remove_item', item_id:item_id, cart_id:cart_id}, 
		   function(errMsg){
			   if(errMsg.length == 0){
				   // reload html body				 
				   DOM_this.parents("body").load('shop_ThickBox.php', {act:'show_myCart', cart_id:cart_id}, 
						function(){
							alert('移除成功');
							// 重新 bind function 
							if($(".show_myCart").length){
								$(".show_myCart").bind('click', show_myCart);
							}
							if($(".remove_item").length){
								$(".remove_item").bind('click', remove_item);
							}
							if($(".tb_remove").length){
								$(".tb_remove").bind('click', function(){self.parent.tb_remove();});
							}
							if($("#checkout").length){
								$("#checkout").bind('click', checkout);
							}
						}); 				   
				}else{
					alert(errMsg);
				}
			}, "text");
	}else{
		return 0;
	}
}

// 特賣會結帳
function checkout()
{
	//	alert($(this).attr('id'));
	var cart_id = $("#cart_id").val();
	var member_num = $("#member_num").val();
	var DOM_this = $(this);
	
	if(confirm('結帳之後，就不能再購買特價品了，你確定要結帳嗎?')){
		$.post('shop_ajax.php', {act:'checkout', cart_id:cart_id, member_num:member_num}, 
			function(errMsg){
				if(errMsg.length==0){
					// reload html body
					DOM_this.parents("body").load('shop_ThickBox.php', {act:'checkout', cart_id:cart_id, member_num:member_num}, 
						function(){
							// 重新 bind function 
							if($(".show_myCart").length){
								$(".show_myCart").bind('click', show_myCart);
							}
							if($(".remove_item").length){
								$(".remove_item").bind('click', remove_item);
							}
							if($(".tb_remove").length){
								$(".tb_remove").bind('click', function(){self.parent.tb_remove();});
							}
						}); 					
				}else{
					errMsg = '<div class="tb_remove" style="position:relative; top:0px; width:100%; height:20px; margin-bottom:5px; text-align:right; font-size:16px; cursor:pointer; background:#FFCCFF;">關閉視窗</div>' + errMsg;
					DOM_this.parents("body").html(errMsg);
					$(".tb_remove").bind('click', function(){self.parent.tb_remove();});
				}
			}, "text");
	}else{
		return 0;
	}
}

// 查看我的購物車
function show_myCart()
{
	var cart_id = $("#cart_id").val();
	$(this).parents("body").load('shop_ThickBox.php', {act:'show_myCart', cart_id:cart_id}, 
		function(){
			$(".remove_item").bind('click', remove_item);
			$("#checkout").bind('click', checkout);
			$(".tb_remove").bind('click', function(){self.parent.tb_remove();});
		});
}

// 購物車登入
function login_submit()
{
	if(typeof loginWindow == 'function') { 
		var backURL = parent.document.URL;
		var msg = '登入後將會自動回到夢幻服飾館';
		parent.loginWindow(backURL, msg);
	}else{
		alert('function loginWindow(backURL, msg, cancelURL) 未載入!');
	}
}

// shop_clothes.php 關鍵字搜尋
function search_by_name()
{
	var i_data = encodeURI($.trim($("#i_data").val()));
	if(i_data != ''){
		if(document.URL.indexOf('&search=') != -1){
			var url = document.URL.substring(0, document.URL.indexOf('&search='));
		}else{
			var url = document.URL;
		}
		window.location = url + '&search=' + i_data;
	}else{
		alert("未輸入搜尋條件!");
	}
}
// shop_clothes.php 關鍵字搜尋
// 游標在關鍵字欄位的時候，直接按下 Enter 即可查詢
function i_data(e){
	var code = (e.keyCode ? e.keyCode : e.which);
	if(code == 13) { //Enter keycode
		search_by_name();
	}
}

// 價格搜尋
function price_search()
{
	var p1 = $("#p1").val(); 	// 最低價錢
	var p2 = $("#p2").val(); 	// 最高價錢
	var reg = /^[0-9]*$/;
	if(!reg.test(p1)){
		alert('最低價格必須是數字');
		return false
	}
	if(!reg.test(p2)){
		alert('最高價格必須是數字');
		return false
	}
	// 價格不為負數
	p1 = (p1 < 0)? 0 : parseInt(p1);
	p2 = (p2 < 0)? 0 : parseInt(p2);
	// 如果最低價格 > 最高價格，兩者對調
	if(p1 > p2){
		var p_temp = p1;
		p1 = p2;
		p2 = p_temp;
	}
	var url_to = parent.document.URL;
	if(/&p1/.test(url_to)){
		url_to = url_to.substring(0, url_to.indexOf('&p1'));
	}
	parent.window.location = url_to + '&p1=' + p1 + '&p2=' + p2 ;
}

function init()
{	
	if(/shop_clothes_search/.test(document.URL)){
		$("#price_search").bind('click', price_search);
	}
	if(/shop_clothes/.test(document.URL)){
		$("#sub_id").bind('change', doSubmit);
		$("#search_by_name").bind('click', search_by_name);
		$("#i_data").bind('keypress', i_data);
	}
	if(/shop_ThickBox/.test(document.URL)){
//		$().ajaxStart(alert('ajaxStart')).ajaxStop($.unblockUI);
		if($("#putInCart_submit").length){
			$("#putInCart_submit").bind('click', putInCart_submit);
		}
		if($("#login_submit").length){
			$("#login_submit").bind('click', login_submit);
		}
		if($(".show_myCart").length){
			$(".show_myCart").bind('click', show_myCart);
		}
		if($(".remove_item").length){
			$(".remove_item").bind('click', remove_item);
		}
		if($("#checkout").length){
			$("#checkout").bind('click', checkout);
		}
		if($(".tb_remove").length){
			$(".tb_remove").bind('click', function(){self.parent.tb_remove();});
		}
	}
}

$(document).ready(function(e){
	setTimeout(init, 100);
});

