
function add2cart_waiting(prod_id)
{
	var box_custom_values = $("#cf-prod-" + prod_id).css("display");

	// Specifico delle liste
	if( $("#li-" + prod_id).css("height") == "95px" ) {
		$("#li-" + prod_id).css("height", "50px")
	}
	
	if(box_custom_values && box_custom_values == "block") {
		$("#cf-prod-" + prod_id).hide(1,
			function() {
				$("#li-content-container-" + prod_id).fadeTo(1, 0.2,
					function() {
						$("#li-" + prod_id).append( load_img );
					}
				);
			}
		);
	} else {
		$("#li-content-container-" + prod_id).fadeTo(1, 0.2,
			function() {
				$("#li-" + prod_id).append( load_img );
			}
		);
	}
	buying_process_active = true;
}

function add2cart_img_ok(prod_id)
{
	$("#li-" + prod_id).children("img.load-img").remove();
	$("#li-" + prod_id).append( buy_ok_img );
}

function add2cart_ok(prod_id, q)
{
	window.setTimeout('add2cart_img_ok("' + prod_id + '");', 150);
	window.setTimeout('add2cart_end_process("' + prod_id + ', ' + q + '");update_cart_info();', 500);
}

function add2cart_end_process(prod_id, q)
{
	$("#li-" + prod_id).children("img.buy-img,img.load-img").remove();
	$("#li-content-container-" + prod_id).fadeTo(1,1);
	$("#prod-q-" + prod_id).val(q);
	buying_process_active = false;
}

function add2cart_error(error_message, prod_id, q)
{
	alert(error_message);
	add2cart_end_process(prod_id, q);
}

function add2cart(errors, values, prod_id, q)
{
	if(errors > 0) {
		alert("Tutte le opzioni sono obbligatorie!");
		return false;
	}

	var q = $("#prod-q-" + prod_id).val();
	if(q.match(/^[0-9]+$/) == null || parseInt(q) < 1) {
		alert("Quantit\xE0 non indicata o non corretta!");
		$("#prod-q-" + prod_id).focus();
		return false;
	}

	var data_encoded = "";
	if(values.length > 0) {
		data_encoded = Base64.encode(values.join("___"));
	}

	add2cart_waiting(prod_id);

	var qs = "pid=" + prod_id + "&cf=" + data_encoded + "&q=" + q;
	$.ajax({
		type: "POST",
		url: SITE_PATH + "ajax/jajax.php",
		data: "m=add2cart&" + qs,
		success: function(result) {
			if(result.match(/^Error:/)) {
				var split_result = result.split("|");
				if(split_result.length == 1) split_result[1] = 1;
				add2cart_error(split_result[0].replace("Error:",""), prod_id, split_result[1]);
				return;
			}
			var split_result = result.split(",");
			if(split_result[0] == "ok") {
				add2cart_ok(prod_id, split_result[1]);
			} else {
				// Solo per debugging
				//alert(result);
				//$("body").append(result);
				add2cart_end_process(prod_id, split_result[1]);
			}
		}
	});
}