function buyProduct(product_id)
{
    var quantity = jQuery('#quantity_'+product_id).val();
    var maxquantity = jQuery('#maxquantity').html();
    var hasstock = jQuery('#hasstock').html();
    var option = jQuery('#options_'+product_id).val();;
    
    if(option == undefined)
	option = 'default';
    if(quantity == undefined)
        quantity = 1;
    if(maxquantity == undefined || hasstock == undefined || hasstock==0)
        maxquantity = -1;
    
    if(quantity <= 0 || (maxquantity > -1 && quantity > maxquantity))
	ecommerce_growl('Cannot add the quantity specified to the cart. Please specify a quantity between 1 and '+maxquantity+'.');
    else
    {
	jQuery.post(baseurl + "support/includes/ecommercev2.ajax.php", {todo: "buy_product", product_id: product_id, quantity: quantity, option: option},
	    function(data)
	    {
		data = data.substr(data.indexOf('BEGIN:', 0)).split(':');
		if(data[1] == 'NOSTOCK')
		    var message = 'This product no longer has the request quantity available.';
		if(data[1] == 'EXISTSONCART')
		    var message = 'You can only have one "'+data[2]+'" on the shopping cart.';
		if(data[1] == 'ALREADYPURCHASED')
		    var message = 'You have already bought the "'+data[2]+'" at a previous time. You can only buy it once.';
		if(data[1] == 'PRODUCTADDED')
		{
		    if(data[3] == 1)
			var message = 'You have added the "'+data[2]+'" to the shopping cart.';
		    else
			var message = 'You have added '+data[3]+'x "'+data[2]+'" to the shopping cart.';
			
		    if(maxquantity!=-1)
			jQuery('#maxquantity').html(Math.max(0, maxquantity-data[3]));
		}
		
		$.jGrowl(message, {
		    life: 10000,
		    theme: 'notification',
		    corners: '3px'}
		);
	    });
    }
}

function ecommerce_growl(message)
{
    $.jGrowl(message, {
                life: 10000,
                theme: 'notification',
                corners: '3px'}
            );
}
