$(document).ready(function() {
	load();
	count();
	
	$('select[name="items_quantity"]').bind('change', function() {
		var maxitems = $('#maxitems').val();
		if (maxitems) {
			var _count = 0;
			$.each($('select[name="items_quantity"]'), function() {
				_count+=parseInt($(this).val());
			})
			if (_count > maxitems) {
				alert('Total QTY must be '+maxitems+' pcs, \nplease select again.');
				$(this).val('0');
				return false;
			}
			$('#totalqty').text(_count);
		}
	});
	
	$('a[href$="add2cart"]').bind('click', function() {
		var maxitems = $('#maxitems').val();
		if (maxitems) {
			var _count = 0;
			$.each($('select[name="items_quantity"]'), function() {
				_count+=parseInt($(this).val());
			})
			if (_count != maxitems) {
				alert('Total QTY must be '+maxitems+' pcs, \nplease select again.');
				return false;
			}
		}
		var items = '';
		$.each($('td[id^="items_"]'), function() {
			if (parseInt($(this).children().eq(1).val())) {
				items+= $(this).children().eq(0).val()+' * '+$(this).children().eq(1).val()+'<br>';
			}
		}),
		
		add($(this).attr('name'), items);
		return false;
	});
	
	$('input[name="amount"]').bind('change', function() {
		$(this).val(parseInt($(this).val()));
		
		modify($(this).parent().next().next().children().attr('name'), $(this).val());
		count();
	});
	
	$('a[href$="remove"]').bind('click', function() {
		if (confirm('Confirm to delete the product?')) {
			remove($(this).attr('name'));
			$(this).parent().parent().remove();
			count();
		}
		load();
		if (!$('#tab_amount .amount').length) $('#_total').text('0.00');
	});
	
	$('#submit').bind('click', function() {
		/*
		if (!$('#tab_amount .amount').length || !parseInt($('#_total').text())) {
			alert('Your shopping cart is emty.');
			return false;
		};*/
		if ($('#tab_amount a').length == 0 ){
			alert('Your shopping cart is emty.');
			return false;
		}
		
		//if(confirm('Total Amount (HKD):\t'+$('#_total').text()+'\n\nClick [OK] to fill orders.\n\nIf you want to modify, click [Cancel].')) {
			location.href = 'orderform.php';
		//}
		return false;
	});
});

function count() {
	load();
	var total = 0;
	$.each($('#tab_amount .amount'), function() {
		var price = parseFloat($(this).prev().prev().text());
		var QTY = parseInt($(this).prev().children().val());
		var amount = parseFloat(price * QTY).toFixed(2);
		total = parseFloat(total)+parseFloat(amount);
		$(this).text(amount);
		$('#_total').text(parseFloat(total).toFixed(2));
	});
}

function load() {
	$.ajax({
		url: 'shoppingcart.inc.php?method=load',
		success: function(data) {
			var obj = $.parseJSON(data);
			$('#top_count').text(obj.count);
			$('#top_total').text(obj.total);
		},
		error: function() {
			//alert('獲取購物車信息失敗');	
		}
	});
}

function add(id, items) {
	$.ajax({
		url: 'shoppingcart.inc.php?method=add&id='+id+'&items='+items,
		success: function(data) {
			var obj = $.parseJSON(data);
			if (obj) {
				if (confirm('Product has been added to cart.\nGo to Shopping Cart?')) {
					location.href = 'shoppingcart.php';
				} else {
					load();
				}
				//if (confirm('添加成功，是否前往購物車？')) {
				//	location.href = 'shoppingcart.php';
				//}
			}
			else {
				//alert('購物車中已有該商品。');
				alert('This product is already included in the Shopping Cart');
			}
		},
		error: function() {
			//alert('獲取購物車信息失敗');
		}
	});
}

function modify(id, amount) {
	$.ajax({
		url: 'shoppingcart.inc.php?method=modify&id='+id+'&amount='+amount,
		error: function() {
			//alert('獲取購物車信息失敗');	
		}
	});
}

function remove(id) {
	$.ajax({
		url: 'shoppingcart.inc.php?method=remove&id='+id,
		error: function() {
			//alert('獲取購物車信息失敗');	
		}
	});
}
