function clear_cart(main_div)
{
	$("#clear-cart-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'clear_cart', action_type : 'ajax'}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#"+main_div).attr('innerHTML', msg[0]);
			//$("#cartbox").attr('innerHTML', msg[1]);
			if(msg[1] > 0)
			{
				$("#count-of-items").attr("innerHTML", msg[1]);
				$("#cartbox-empty").hide();
				$("#cartbox-full").show();
			}
			else
			{
				$("#cartbox-empty").show();
				$("#cartbox-full").hide();
			}
		}
	});
}

function add_to_cart(item_cat, item_id, type)
{
	//var q = $("#"+item_cat+"-item_quantity-"+item_id).attr('value');
	//var product = $("#buyoption-"+item_id).val();
	var q = 1;
	var product = type;
	
	//$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").hide();
	//$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'add_to_cart', action_type : 'ajax', item_cat : item_cat, item_id : item_id, q : q, product: product}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			//$("#buy-option-icon-add-"+item_id+"-"+type).fadeOut(500, function() {
			//	$("#buy-option-icon-remove-"+item_id+"-"+type).fadeIn(500);
			//});
			
			$("#buy-option-icon-add-"+item_id+"-"+type).hide();
			$("#buy-option-icon-remove-"+item_id+"-"+type).show();			
			
			/*
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-icon").hide();
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").attr('innerHTML', msg[0]);
			$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").show();
			$("#buyoption-"+item_id).hide();
			*/
			if(msg[1] > 0)
			{
				$("#count-of-items").attr("innerHTML", msg[1]);
				$("#cartbox-empty").hide();
				$("#cartbox-full").show();
			}
			else
			{
				$("#cartbox-empty").show();
				$("#cartbox-full").hide();
			}
		}
	});
}

function remove_from_cart(item_cat, item_id, type)
{
	//$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").hide();
	//$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'remove_from_cart', action_type : 'ajax', item_cat : item_cat, item_id : item_id, product : type}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			
			//$("#buy-option-icon-remove-"+item_id+"-"+type).fadeOut(500, function() {
			//	$("#buy-option-icon-add-"+item_id+"-"+type).fadeIn(500);
			//});			
			
			$("#buy-option-icon-remove-"+item_id+"-"+type).hide();
			$("#buy-option-icon-add-"+item_id+"-"+type).show();
			
			//$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-icon").hide();
			//$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").attr('innerHTML', msg[0]);
			//$("#"+item_cat+"-cart_add_remove_div-"+item_id+"-functions").show();
			//$("#buyoption-"+item_id).show();
			if(msg[1] > 0)
			{
				$("#count-of-items").attr("innerHTML", msg[1]);
				$("#cartbox-empty").hide();
				$("#cartbox-full").show();
			}
			else
			{
				$("#cartbox-empty").show();
				$("#cartbox-full").hide();
			}
		}
	});
}

var last_item = 0;
var last_qty = 0;
var last_product = 0;
function change_qty(val, id, cat, product)
{
	var v = val;
	if(val == '')
		return;
		
	val = (parseInt(val) >= 0) ? parseInt(val) : 0;
	document.getElementById('qty-value-'+id+product).value = val;
	if((val == last_qty) && (id == last_item))
		return;
		
	//var shipping_id = document.getElementById('cart-shipping').options[document.getElementById('cart-shipping').selectedIndex].value;
	//var promocode = document.getElementById('cart-promocode').value;
	var shipping_id = 0;
	var promocode = 0;
		
	$("#qty-value-"+id+product).attr('disabled', true);
	$("#change-qty-icon-"+id+product).show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'change_qty', action_type : 'ajax', item_cat : cat, item_id : id, q : val, shipping_id : shipping_id, promocode : promocode, product : product}),
		dataType: "html",
		success: function(msg)
		{
			last_item = id;
			last_qty = val;
			last_product = product;
		
			msg = ajax_retrieve_messages(msg);
			$("#change-qty-icon-"+id+product).hide();
			
			$("#cart-"+id+product+"-free-price").attr('innerHTML', msg[2]);
			//$("#cart-"+id+product+"-tax").attr('innerHTML', msg[1]);
			//$("#cart-"+id+product+"-price").attr('innerHTML', msg[2]);
			$("#cart-total-free-price").attr('innerHTML', msg[0]);
			$("#cart-total-tax").attr('innerHTML', msg[4]);
			$("#cart-total-price").attr('innerHTML', msg[3]);
			$("#cart-shipping-price").attr('innerHTML', msg[5]);
			//$("#cart-promocode-price").attr('innerHTML', msg[6]);
			
			$("#qty-value-"+id+product).removeAttr('disabled');
		}
	});	
}

function change_shipping()
{
	var shipping_id = document.getElementById('cart-shipping').options[document.getElementById('cart-shipping').selectedIndex].value;
	var promocode = document.getElementById('cart-promocode').value;
	
	$("#change-shipping-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'change_shipping', action_type : 'ajax', shipping_id : shipping_id, promocode : promocode}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#change-shipping-icon").hide();
			
			$("#cart-total-price").attr('innerHTML', msg[0]);
			$("#cart-shipping-price").attr('innerHTML', msg[1]);			
		}
	});		
}

function enter_promocode()
{
	var shipping_id = document.getElementById('cart-shipping').options[document.getElementById('cart-shipping').selectedIndex].value;
	var promocode = document.getElementById('cart-promocode').value;
	
	$("#cart-promocode-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'enter_promocode', action_type : 'ajax', shipping_id : shipping_id, promocode : promocode}),
		dataType: "html",
		success: function(msg)
		{
			msg = ajax_retrieve_messages(msg);
			$("#cart-promocode-icon").hide();
			
			$("#cart-total-price").attr('innerHTML', msg[0]);
			$("#cart-promocode-price").attr('innerHTML', msg[1]);			
		}
	});		
}

function remove_item(item_id, item_cat, main_div, product)
{
	//var shipping_id = document.getElementById('cart-shipping').options[document.getElementById('cart-shipping').selectedIndex].value;
	//var promocode = document.getElementById('cart-promocode').value;
	var shipping_id = 0;
	var promocode = 0;
	
	$("#change-item-"+item_id+product+"-icon").show();
	$.ajax({
		url: "index.php",
		type: "POST",
		data: ({action : 'remove_item', action_type : 'ajax', item_id : item_id, item_cat : item_cat, shipping_id : shipping_id, promocode : promocode, product : product}),
		dataType: "html",
		success: function(msg)
		{
			$("#"+item_id+product).fadeOut('fast');
			msg = ajax_retrieve_messages(msg);

			if(msg[0] == 'none')
			{
				$("#"+main_div).attr('innerHTML', msg[1]);
			}
			else
			{
				$("#cart-total-price").attr('innerHTML', msg[0]);
				$("#cart-shipping-price").attr('innerHTML', msg[1]);
				$("#cart-promocode-price").attr('innerHTML', msg[2]);
				$("#cart-total-free-price").attr('innerHTML', msg[4]);
				$("#cart-total-tax").attr('innerHTML', msg[5]);
			}
		}
	});
}



