//kiem tra browser cho dung cookie khong
function checkBrowserEnableCookie(){
	var cookieEnabled=(navigator.cookieEnabled)? true : false
	
	//if not IE4+ nor NS6+
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
		document.cookie="testcookie"
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
	}
	
	if (cookieEnabled) return true;
	else return false;
}

//tao cookie
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=/";
}

//doc cookie
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 "";
}

//xoa bo cookie
function eraseCookie(name) {
	createCookie(name,"",-1);
}

//doc so luong san pham dang co trong gio hang
function countShoppingCart(name){
	//tao cookie gio hang neu cua co
	if(readCookie(name)==""){
		//createCookie(name,'',1);
		document.getElementById('count_shopping_cart').innerHTML = "<b>0</b>";
		//document.getElementById('count_shopping_cart_top').innerHTML = "<b>0</b> sản phẩm";
		//alert('create a new cookie shopping cart');
	}else{
		//add gia tri vao shopping cart
		var current_cart = readCookie(name);
		var ca = current_cart.split(',');
		number_product = ca.length - 1;
		document.getElementById('count_shopping_cart').innerHTML = "<b>(" + number_product + ")</b>";
		//document.getElementById('count_shopping_cart_top').innerHTML = "<b>" + number_product + "</b> sản phẩm";
	}
	//$("#shopping_cart_list").load("Cart/CartItemList.aspx");
}


function emptyShoppingCart(name) {
	createCookie(name,'-',1);
}

//them san pham vao gio hang
function addToShoppingCart(productId,quantity){
	//tao cookie gio hang neu cua co
	if(readCookie('shopping_cart')==null){
		createCookie('shopping_cart','',1);
		//alert('create a new cookie shopping cart');
	}
	//add gia tri vao shopping cart
	var current_cart = readCookie('shopping_cart');
	
	//kiem tra xem da duoc them vao cart hay chua
	if(current_cart.search(','+productId+'-')==-1){
		var new_cart = current_cart + ',' + productId + '-' + quantity;
		createCookie('shopping_cart',new_cart,1);
		//doc lai gia tri moi
		//alert(new_cart);
		//document.getElementById('value_shopping_cart').value = new_cart;
		countShoppingCart('shopping_cart');
		document.getElementById('item_'+productId).innerHTML = 'Đã thêm vào giỏ hàng';
	}else {
		
		alert('Sản phẩm này đã có trong giỏ hàng \nVui lòng xem chi tiết giỏ hàng để chỉnh sửa hoặc xóa bớt sản phẩm !');
		
	}
}

//Thêm sản phẩm giỏ hàng và chuyển tới trang giỏ hàng
function addToCartRedirect(productId, quantity){
	if(readCookie('shopping_cart')==null){
		createCookie('shopping_cart','',1);
	}
	var current_cart = readCookie('shopping_cart');

	if(current_cart.search(','+productId+'-')==-1){
		var new_cart = current_cart + ',' + productId + '-' + quantity;
		createCookie('shopping_cart',new_cart,1);
		countShoppingCart('shopping_cart');
		window.location = "/gio-hang.html";
	}else {
		alert('Sản phẩm này đã có trong giỏ hàng \nVui lòng xem chi tiết giỏ hàng để chỉnh sửa hoặc xóa bớt sản phẩm!');
	}
}

function checkItemInCart(productId){
	var current_cart = readCookie('shopping_cart');
	//kiem tra xem da duoc them vao cart hay chua
	if(current_cart.search(','+productId+'-')!=-1){
		document.write('Đang trong giỏ hàng');
	}
}

//xoa bo item trong cart
function deleteFromCart(productId,quantity){	
	if(confirm('Bỏ mua sản phẩm này khỏi giỏ hàng? ')){
		var current_cart = readCookie('shopping_cart');
		new_cart = current_cart.replace(","+productId+'-'+quantity,"");
		new_cart = new_cart.replace(","+productId+'-',"0");
		createCookie('shopping_cart',new_cart,1);
		countShoppingCart('shopping_cart');
		//ok nhay ve trang gio hang moi
		window.location.href = '/gio-hang.html';
	}
}

//cap nhat so luong san pham trong cart
function updateCart(productId,quantity){
    var newquantity = document.getElementById("item_"+productId).value;
    newquantity = parseInt(newquantity);
    if(newquantity < 1){
        //Nếu cập nhật số lượng < 1 thì coi như là xóa sản phẩm khỏi cart
        deleteFromCart(productId,quantity);
    }else{
        if(newquantity > 99){
            alert('Bạn chỉ được phép mua tối đa số lượng 99 cho mỗi sản phẩm');
            newquantity = 99;
        }  
        var current_cart = readCookie('shopping_cart');
        new_cart = current_cart.replace(","+productId+'-'+quantity,","+productId+'-'+newquantity);
        createCookie('shopping_cart',new_cart,1);
        countShoppingCart('shopping_cart');
        //ok nhay ve trang gio hang moi
        window.location.href = 'gio-hang.html';
    }
  }

//Luu danh sach san pham da xem
function addToViewHistory(productId){
	if(readCookie('product_history')==null){
		createCookie('product_history',',',1);
	}
	var current_list = readCookie('product_history');
	if(current_list.search(','+productId+',')==-1){
		var new_list = "," + productId + current_list;
		createCookie('product_history',new_list,1);
	}
	///alert(current_list);
}
