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 addToShoppingCartNew1(sellid,quantity,combo_id){
	
	 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+'-'+combo_id;
        createCookie('shopping_cart',new_cart,1);
        window.location = '/?option=cart';
    }else{
        alert('Sản phẩm đã có trong giỏ hàng!');
    }
}

function addToShoppingCartNew(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';
		window.location = '?option=cart';
	}else{
		alert('Sản phẩm đã có trong giỏ hàng!');
	}
}


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');
		window.location = "/gio-hang.html";
		//document.getElementById('item_'+sellid).innerHTML='Đã thêm vào giỏ hàng';      
	}else{
		alert('Sản phẩm đã có trong giỏ hàng!');
	}
}

function addToShoppingCartDetail(sellid){
    var quantity = document.getElementById("quantity_cart").value;
    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');
		window.location = "/gio-hang.html";
		//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){document.write('[Trong giỏ hàng]');}else{}}
function checkItemInCartGrid(sellid){var current_cart=readCookie('shopping_cart');if(current_cart != null && current_cart.search(','+sellid+'-')!=-1){}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;	
	if(check =="none"){
		document.getElementById("icon_"+groupId).innerHTML = "<img src=\"/includes/images/circle_open.gif\">";
		$("#"+groupId).slideDown("slow");
	}else{
		document.getElementById("icon_"+groupId).innerHTML = "<img src=\"/includes/images/invi.gif\">";
		$("#"+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 selectThum(img_url){
	document.getElementById('productImageBox').innerHTML = "<img src="+ img_url +">";	
}

$(document).ready(function() {
	/* hien thi tooptip */
    $('.tooltip').tooltip();
	
    /*đếm số sản phẩm trong giỏ hàng */
    countShoppingCart('shopping_cart');

    //When page loads...
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active 1").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {

        $("ul.tabs li").removeClass("active 1"); //Remove any "active" class
        $(this).addClass("active 1"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content

        var activeTab = $(this).find("span").attr("title"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });

    $("ul.tabs li").mouseover(function() {
        if(!($(this).hasClass("1"))){
            $(this).addClass("active");
        }
    }).mouseout(function(){
        if(!($(this).hasClass("1"))){
            $(this).removeClass("active");
        }
    });

});

function productCompare(main_url){
    var list_product = ";"+getCheckBoxValue()+";";
    list_product = list_product.split(";");
    if(list_product.length < 4){
        alert("Chọn ít nhất 2 sản phẩm để so sánh.");
        return false;
    }

    if(list_product.length > 6){
        alert("Chọn nhiều nhất 4 sản phẩm để so sánh.");
        return false;
    }

    var hdn_compare_product_list = ";"+getCheckBoxValue()+";";
    window.location = main_url+"?option=compare&hdn_compare_product_list="+hdn_compare_product_list;
}

function getCheckBoxValue()
{
	var c_value = "";
	for (var i = 0; i < document.form_id.product_id.length; i++)
   	{
	   	if (document.form_id.product_id[i].checked)
	    {
	   		c_value += (c_value?';':'')+document.form_id.product_id[i].value;
	    }
   	}
 	return c_value;
}

//lay san pham trang chu ajax
function fetchProductAjax(type, catId, holderId){
	$.get("/fetch_product.php", {type:type, catId:catId}, function(data){$("#"+holderId).html(data);});
}
