function checkBrowserEnableCookie() {
	var cookieEnabled = (navigator.cookieEnabled) ? true: false
	if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
		document.cookie = "testcookie"
		cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true: false
	}
	if (cookieEnabled) return true;
	else return false;
}
function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name, "", -1);
}
function countShoppingCart(name) {
	if (readCookie(name) == null) {
		createCookie(name, '-', 1);
		document.getElementById('count_shopping_cart').innerHTML = 0;
	} else {
		var current_cart = readCookie(name);
		var ca = current_cart.split(',');
		number_product = ca.length;
		document.getElementById('count_shopping_cart').innerHTML = number_product - 1;
	}
}
function emptyShoppingCart(name) {
	createCookie(name, '-', 1);
}

function addToShoppingCart(sellid, quantity) {
	if (readCookie('shopping_cart') == null) {
		createCookie('shopping_cart', ',', 1);
	}
	var current_cart = readCookie('shopping_cart');

	if (current_cart.search(',' + sellid + '-') == -1) {
		var new_cart = current_cart + ',' + sellid + '-' + quantity;
		createCookie('shopping_cart', new_cart, 1);
		countShoppingCart('shopping_cart');
		document.getElementById('item_' + sellid).innerHTML = 'Đã thêm vào giỏ hàng';
	} else {
		alert('Sản phẩm đã có trong giỏ hàng!');
	}
}
function addToShoppingCartType(sellid, quantity, type) {
	if (readCookie('shopping_cart') == null) {
		createCookie('shopping_cart', ',', 1);
	}
	var current_cart = readCookie('shopping_cart');
	//alert(current_cart.search(','+sellid+'-'));
	if (current_cart.search(',' + type + '-') == -1) {
		var new_cart = current_cart + ',' + sellid + '-' + quantity + '-' + type;
		createCookie('shopping_cart', new_cart, 1);
		countShoppingCart('shopping_cart');
		document.getElementById('item_' + type).innerHTML = 'Đã vào giỏ hàng';
	} else {
		alert('Sản phẩm đã có trong giỏ hàng!');
	}
}
function checkItemInCart(sellid) {
	var current_cart = readCookie('shopping_cart');
	if (current_cart != null && current_cart.search(',' + sellid + '-') != -1) {
		$('span#item_' + sellid).html('[Trong giỏ hàng]');
	} else {}
}

function checkItemInCartHome(sellid) {
	var current_cart = readCookie('shopping_cart');
	if (current_cart != null && current_cart.search(',' + sellid + '-') != -1) {} else {}
}

function checkItemInCartType(type) {
	var current_cart = readCookie('shopping_cart');
	if (current_cart != null && current_cart.search(',' + type + '-') != -1) {
		document.write('[Trong giỏ hàng]');
	} else {}
}

function deleteShoppingCartItem(sellid, quantity) {
	if (confirm('Bạn muốn xóa bỏ sản phẩm này khỏi giỏ hàng ? ')) {
		var current_cart = readCookie('shopping_cart');
		new_cart = current_cart.replace("," + sellid + '-' + quantity, "");

		createCookie('shopping_cart', new_cart, 1);
		countShoppingCart('shopping_cart');
		window.location.href = '?option=cart';
	}
} //minified
function deleteShoppingCartItemType(sellid, quantity, type) {
	if (confirm('Bạn muốn xóa bỏ sản phẩm này khỏi giỏ hàng ? ')) {
		var current_cart = readCookie('shopping_cart');

		new_cart = current_cart.replace("," + sellid + '-' + quantity + '-' + type, "");
		//alert(new_cart);
		createCookie('shopping_cart', new_cart, 1);
		countShoppingCart('shopping_cart');
		window.location.href = '?option=cart';
	}
} //minified
//show menu
function showSub(groupId){
	var check = document.getElementById(groupId).style.display;
	var curren_open = document.getElementById('menu_open').value;

	if(check =="none"){
		$("#"+groupId).slideDown("slow");
	}else{
		$("#"+groupId).slideUp("slow");
	}
}

function showDateTime(showDate, dateFormat, showTime, showAsGMT) {
	var days = new Array("Ch&#7911; nh&#7853;t", "Th&#7913; hai", "Th&#7913; ba", "Th&#7913; t&#432;", "Th&#7913; n&#259;m", "Th&#7913; s&#225;u", "Th&#7913; b&#7843;y");
	var months = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
	var datetime = getDateTime(showDate, dateFormat, showTime, showAsGMT, days, months, "/", "/");
	document.write("<nobr>&nbsp;" + datetime + "&nbsp;</nobr>");
}

function getDateTime(showDate, dateFormat, showTime, showAsGMT) {
	var days = arguments[4] || new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var months = arguments[5] || new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var monthSeperate = arguments[6] || "-";
	var yearSeperate = arguments[7] || "-";
	var now = new Date();
	var month = "";
	var day = "";
	var time = "";
	var first_date_num = "";
	var result = "";

	// show date
	if (showDate) {
		// long date
		if (dateFormat != "0") day = days[now.getDay()] + ", ";
		month = months[now.getMonth()];
		if (now.getDate() < 10) first_date_num = "0";
		result += day + first_date_num + now.getDate() + monthSeperate + month + yearSeperate + now.getFullYear();
	}

	// show time
	if (showTime) {
		var symbol = "AM";
		if (showAsGMT) time = new String(now.getUTCHours());
		else time = new String(now.getHours());
		if (time >= 12) {
			time = time - 12;
			symbol = "PM";
		}
		if (time.length < 2) time = "0" + time;
		var minute = new String(now.getMinutes());
		if (minute.length < 2) minute = "0" + minute;
		time = time + ":" + minute + "&nbsp;" + symbol;
		if (showAsGMT) time += " (GMT)";
		if (result != "") result += "&nbsp;&nbsp;";
		result += time;
	}

	return result;
}

function createPriceSearch(min_max) {
	var price_array = min_max.split(",");
	var max_price = price_array[1];
	var min_price = price_array[0];
	document.getElementById('max_price').value = max_price;
	document.getElementById('min_price').value = min_price;
}

function show(elementId) {
	var checked = document.getElementById(elementId).style.display;
	if (checked == "none") {
		document.getElementById(elementId).style.display = "block";
	} else {
		document.getElementById(elementId).style.display = "none";
	}
}

function chooseShippingOption(method_id) {
	document.getElementById('selectShippingMethod').value = method_id;
}

function choosePaymentOption(option) {
	var checked = document.getElementById(option).checked;
	var instId = document.getElementById(option).value;
	if (checked) {
		document.getElementById("selectPaymentMethod").value = instId;

		//Hide other options
		for (var i in paymentOptionList) {
			if (option != paymentOptionList[i]) {
				document.getElementById("pay_" + paymentOptionList[i]).style.display = "none";
				document.getElementById(paymentOptionList[i]).checked = false;
			}
		}
		//show the instruction
		document.getElementById("block_" + instId).style.display = "block";
	} else {
		document.getElementById("selectPaymentMethod").value = "0";
		//Uncheck, show other options again
		for (var i in paymentOptionList) {
			document.getElementById("pay_" + paymentOptionList[i]).style.display = "block";
			document.getElementById(paymentOptionList[i]).checked = false;
			var elt = document.getElementById(paymentOptionList[i]).value;
			document.getElementById("block_" + elt).style.display = "none";
		}
	}
}

function add_to_listSS(chk_name, hdn_id, productId) {
	var checked = document.getElementById("chk_" + productId).checked;
	var currentList = document.getElementById("hdn_compare_product_list").value;
	var currentNumItem = currentList.split(";").length - 2;
	var productImageUrl = document.getElementById("productHiddenImage_" + productId).value;
	var iconHtml = "<img src=\"" + productImageUrl + "\" width=26 height=26 style='border:1px solid #FF9900'>";

	if (checked == true) {

		if (currentNumItem > 4) {
			//Cho phep so sánh tối đa 5 sản phẩm
			document.getElementById("chk_" + productId).checked = "";
			alert("Bạn chỉ có thể so sánh tối đa 5 sản phẩm\nDanh sách đã có đủ 5");
		} else {

			document.getElementById("hdn_compare_product_list").value = currentList + productId + ";";

			//Thêm tiếp ảnh
			nextContainer = currentNumItem + 1;
			document.getElementById("compareItemContain_" + nextContainer).innerHTML = iconHtml;
			document.getElementById("productItemContain_" + nextContainer).value = productId;
		}
	} else {
		document.getElementById("hdn_compare_product_list").value = currentList.replace(";" + productId + ";", ";");
		//Xóa bỏ ảnh
		var containId = 0;
		for (var i = 1; i <= currentNumItem; i++) {
			var productContainer = document.getElementById("productItemContain_" + i).value;
			if (productContainer != "") {
				if (productId == productContainer) {
					document.getElementById("compareItemContain_" + i).innerHTML = "";
					document.getElementById("productItemContain_" + i).value = "";
					containId = i;
					break;
				}
			}
		}
		//Sắp xếp lại ảnh
		if (containId != 0) {
			arrangeImageContainer(containId, 3);
		}
	}
}

function arrangeImageContainer(startId, maxNum) {
	var nextStartId = 0;
	for (var i = startId + 1; i <= maxNum; i++) {
		var contentHtml = document.getElementById("compareItemContain_" + i).innerHTML;
		var productContainer = document.getElementById("productItemContain_" + i).value;
		if (contentHtml.length > 2) {
			document.getElementById("compareItemContain_" + startId).innerHTML = contentHtml;
			document.getElementById("productItemContain_" + startId).value = productContainer;
			document.getElementById("compareItemContain_" + i).innerHTML = "";
			document.getElementById("productItemContain_" + i).value = "";
			nextStartId = i;
			arrangeImageContainer(nextStartId, maxNum);
			break;
		}
	}
}
function submitSearch() {
    if(document.getElementById('mod_search_searchword').value=='Điền tên hoặc mã sản phẩm') document.getElementById('mod_search_searchword').value='';
}
function getBestsellerProducts(cat_id){
    $(".product_lists").load("modules/products/product_bestseller.php?cid="+cat_id);
}
function selectThum(img_src){document.getElementById('product_image_holder_content').innerHTML = "<img src=\""+img_src+"\" />";}
