function add_to_cart()
{
	if(document.add_to_cart_form.onsubmit()) //this check triggers the validations
	{
		var url = '/catalog.php';
		var data = $('#add_to_cart_form').serialize();
		var pars = 'action=add_product&'+data;
    
    $.get(url,pars,function(res) {
				var response  = res.split('||');
				$('#header_items_in_cart').html(response[0]);
				$('#item_total_'+key).html(response[1]);		
				$('#my_cart_subtotal').html(response[2]);
				$('#my_cart_shipping').html(response[3]);
        $('#my_cart_coupon_discount').html(response[4]);
      },'html');
	}
}


function remove_product(key, description)
{
	var answer = confirm("Are you sure you want to remove this item?")
	if (!answer){ return; }
	var params = 'action=remove_product&key='+key;
  $.get('catalog.php',params,function(res) {
			var response = res.split('||');
			$('header_items_in_cart').html(response[0]);
			if(parseInt(response[1])>0)
			{
				$('#no_items').hide();
				$('#show_items').show();
				$('#my_cart_subtotal').html(response[2]);
				$('#my_cart_shipping').html(response[3]);
        $('#my_cart_coupon_discount').html(response[4]);
				$('#'+key).remove();
			}else{
				$('#no_items').show();
				$('#show_items').hide();
			}
    },'html');
}

function update_cart(key, description)
{
  var type = key.indexOf('ingram') >= 0 ? 'ingram' : 'smekens';
  
	var qty = $('#qty_'+key).attr('value');
	if(qty=='') return;
	qty = qty*1;
	if(qty==0){
		remove_product(key, description)
		return;
	}
  
  var url = 'cart.php';
  var params = '';
  if(type == 'smekens')
    { params = 'action=ajax_add_product&key='+key+'&qty='+qty; }
  else if(type == 'ingram')
    { 
    var upc = key.substr(7);
    params = 'action=ajax_add_ingram_product&upc='+upc+'&qty='+qty; 
    }
    
  if(!url) { alert("unable to determine product type, cannot update cart."); return false; }
  
  $.get(url,params,function(res) {
			var response = res.split('||');
			$('#header_items_in_cart').html(response[0]);
			$('#item_total_'+key).html(response[1]);		
			$('#my_cart_subtotal').html(response[2]);
			$('#my_cart_shipping').html(response[3]);
			$('#my_cart_coupon_discount').html(response[4]);
    },'html');
}				