//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
	//alert('Đã thêm vào giỏ hàng2');	alert(document.getElementById('count_shopping_cart').innerHTML+'thu');
	if(readCookie(name)==""){
		//createCookie(name,'',1);
		document.getElementById('count_shopping_cart').innerHTML = "0";
		//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;
		//alert(document.getElementById('count_shopping_cart').innerHTML+'thu2');
		document.getElementById('count_shopping_cart').innerHTML = number_product;
		//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){
    //eraseCookie('shopping_cart');
	//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;
		// alert('Đã thêm vào giỏ hàng');
		 loadShoppingCart();
		document.getElementById('item_' + productId).innerHTML = 'Đã thêm vào giỏ hàng';
		//alertItemInCart(productId,true);
	   
	   
	}else {		
		alert('Sản phẩm này đã có trong giỏ hàng!');		
	}
	//alert(document.URL);
}

function loadShoppingCart(){
    countShoppingCart('shopping_cart');
    //$("#shopping_cart_block").load("/Cart/LoadCart.aspx");
    
}

//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 = "./Cart.aspx";
	}else {
		alert('Sản phẩm này đã có trong giỏ hàng!');
	}
}
function alertItemInCart(productId,flag)
{
    if(flag == true)
    {
        document.write('Đã thêm vào giỏ hàng');
    }
    else
    {
        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');	    
	}
}
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ạn muốn xóa 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 ='./Cart.aspx';
	}
}
//Xoa bo item trong cart va trong danh sach hien thi gio hang
function deleteFromListCart(productId,quantity){	
	if(confirm('Bạn muốn xóa 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 ='Cart.aspx';		
		document.getElementById('tb_' + productId).style.display = "none";
	}
}

//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 > 20){
            alert('Bạn chỉ được phép mua tối đa số lượng 20 cho mỗi sản phẩm');
            newquantity = 20;
        }  
        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 ='./Cart.aspx';
    }
}

//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);
}
