$(document).ready
(
	function()
	{
		//consignee infomation form check
		$('form#cart_form').submit
		(
			function()
			{
				if ($('input[name=c_f_name]', this).val() == ''){window.alert('Please enter shipping customer first name.');return false;}
				if ($('input[name=c_l_name]', this).val() == ''){window.alert('Please enter shipping customer surname name.');return false;}
				if ($('input[name=c_address]', this).val() == ''){window.alert('Please enter shipping customer address.');return false;}
				if ($('input[name=c_city]', this).val() == ''){window.alert('Please enter shipping customer city.');return false;}
				if ($('input[name=]', this).val() == ''){window.alert('Please select shipping customer country.');return false;}
			}
		);
		
		//tree
		$("#tree").treeview({collapsed: true, animated: "fast", persist: "location"});
	}
);
//set cookie
function setCookie(name, value, time)
{
	if (!time) time = 0;
	var expires = new Date();
	expires.setTime(expires.getTime() + time * 24 * 60 * 60 * 1000);
	document.cookie = name + '=' + encodeURIComponent(value) + ';path=\/;expires=' + expires.toGMTString();
}
//get cookie
function getCookie(name)
{
	var arr = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'));
	if (arr != null) return decodeURIComponent(arr[2]); return null;
}
//delete cookie
function delCookie(name)
{
  var _cookie = getCookie(name);
	if (_cookie != null)
	{
		setCookie(name, '' , -1);
	}
}
//add to shopping cart
function add2cart(product_id, product_info, confirm)
{
	var warn = true;
	if (confirm)
	{
		warn = window.confirm(confirm);
	}
	if (warn)
	{
		//window.alert(product_info);return;
		
		var product_number, product_price, cart_total;
		var ary_product_info = new Array();
		ary_product_info = product_info.split('|');
		product_number = ary_product_info[0];
		product_price = ary_product_info[1];
		product_number = /^[1-9][0-9]*$/.test(product_number) ? product_number : 1;
		
		var tmp = getCookie('fair_cart');
		if (('|' + tmp).indexOf('|' + product_id + ',') > -1)
		{
			window.alert('This product was in your shopping cart！');
		}
		else
		{
			cart_total = parseInt(product_number) * parseFloat(product_price);
			tmp = (tmp == null) ? '' : tmp;
			tmp += '|' + product_id + ',' + product_number;
			tmp = tmp.replace(/^\|/, '');
			setCookie('fair_cart', tmp, 1);
			window.alert('Add to your shopping cart successful！');
			//update shopping cart display
			update_cart_view(product_number, cart_total);
		}
	}
}
function update_cart_view(num, total)
{
	var data = '';
	data = $('#cart_items').html();
	$('#cart_items').html(parseInt(data) + parseInt(num));
	data = $('#cart_total_money').html().substr(3);
	$('#cart_total_money').html('$'+ (parseFloat(data) + parseFloat(total)));
}