<!--

function check_buy()
{
	if(sottoscorta == 0) {
		if(giacenza < 1) {
			alert("Il prodotto non \xE8 disponibile");
			return false;
		}
		if(F.elements['product_quantity'].value > giacenza) {
			if(confirm("La quantit\xE0 richiesta eccede la disponibilit\xE0.\n\xC8 possibile ordinare solo " + giacenza + " pezzi.\n\nVuoi continuare ?")) {
				F.elements['product_quantity'].value = giacenza;
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}

	var q = parseInt(F.elements['product_quantity'].value);
	if ( q == 0 || isNaN(q) ) {
		alert("Specificare una quantit\xE0 valida!");
		return false;
	}

	var check_cf = check_custom_fields();
	if(check_cf) {
		alert(check_cf);
		return false;
	}

	var values = [];
	var index = -1;
	$(".custom-values").each(
		function() {
			if( $(this).val() != "" ) {
				values[++index] = $(this).val();
			}
		}
	);

	if($(".custom-values").length) {
		$.ajax({
			type: "POST",
			url: SITE_PATH_REL + "ajax/jajax.php",
			data: "m=base64_encode&data=" + values.join("___"),
			success: function(result) {
				if(result.match(/^Error:/)) {
					alert( "Si \xE8 verificato un errore" );
					return;
				}
	            window.location.replace( SITE_PATH_REL + "cart/add/" + product_code + "/" + q + "/0/index.html?cf=" + result );
			}
		});
	} else {
        window.location.replace( SITE_PATH_REL + "cart/add/" + product_code + "/" + q + "/0/index.html" );
	}
}

function check_custom_fields()
{
	var tot_fields = $(".custom-values").length;
	var errors = [];
	var _errors = -1;
	if(tot_fields > 0) {
		$(".custom-values").each(
			function() {
				if($.trim($(this).val()) == "") {
					errors[++_errors] = '[' + $("#field-" + this.id.replace("values-","") ).html() + '] devi selezionare un valore';
				}
			}
		);
	}
	return (errors.length > 0) ? errors.join('\n') : '';
}

$(document).ready(
	function() {
		$("#buy-form-submit").click( check_buy );

		$(".show-img-gallery").lightBox({
			overlayOpacity: 0.3,
			imageBlank: SITE_PATH_REL + 'img/lightbox-blank.gif',
			imageLoading: SITE_PATH_REL + 'img/lightbox-ico-loading.gif',
			imageBtnClose: SITE_PATH_REL + 'img/lightbox-btn-close.gif',
			imageBtnPrev: SITE_PATH_REL + 'img/lightbox-btn-prev.gif',
			imageBtnNext: SITE_PATH_REL + 'img/lightbox-btn-next.gif',
			txtImage: 'Immagine',
			txtOf: 'di'
		});

		$("#close-div").click(
			function() {
				$("#div-marca").hide("normal");
				return false;
			}
		);

		$("#a-txt-marca").click(
			function() {
				$("div#div-marca").toggle("normal");
				return false;
			}
		).hover(
			function() {$(this).css("text-decoration","underline");},
			function() {$(this).css("text-decoration","none");}
		);
		
		$("#a-img-marca").click(
			function() {
				$("div#div-marca").toggle("normal");
				return false;
			}
		);
	}
);

-->
