var pageTracker;

function checkForm() {
	var valid=true;
	if(jQuery("#ship-2").size() > 0) {
		if(jQuery("#ship-2").get(0).checked) {
			jQuery(".custom input.mand").each(function (i, e) {
				if(jQuery.trim(jQuery(e).val()) == "") {
					alert("Se desideri ricevere la merce ad un indirizzo alternativo\n è necessario compilare tutti i campi");
					valid = false;				
					return false;
				}
			});
			if(valid)
				jQuery("form").get(0).submit();
		} else {
			jQuery("form").get(0).submit();
		}
	} else {
		jQuery("#shipment-data input[type=text]").each(function (i, e) {
			if(jQuery.trim(jQuery(e).val()) == "") {
				alert("Se desideri ricevere la merce ad un indirizzo alternativo\n è necessario compilare tutti i campi");
				valid = false;				
				return false;
			}
		});
		if(valid)
			jQuery("form").get(0).submit();
	}
}
		
var Danze = function() {
	
	function _add_to_cart(pid, prid, qty, name, callback)
	{
		var data = {
			"pid":pid,
			"prid":prid,
			"qty":qty,
			"name":name
		};
		$.post("/data/addtocart.php", data, function (cart) {
			$("#Cart_content").load("/data/cart.php");
			alert("prodotto aggiunto");
            
            if($.isFunction(callback))
                callback.call(window);
		});	
	}
	
	
	return {
		add_one_to_cart : function(pid, prid, name, callback)
		{
			_add_to_cart(pid, prid, 1, name, callback);
		},
		add_to_cart : function(pid, prid, e, name, callback) {
			var qty = jQuery(e).parent().parent().find(".qty").val();
			_add_to_cart(pid, prid, qty, name, callback);
		},
		submit : function (c) {
			$("form."+c).get(0).submit();
		},
		remove_from_cart: function(e) {
			$(e).parent().parent().find("select").val("0");
			Danze.submit("cart");
		},
		change_payment: function(e)
		{
			location.href = "/riassunto-ordine?pagamento="+e.value;
		},
		order_detail: function(id)
		{
			var $div = $("<div></div>");
			$div.attr("title", "Dettaglio ordine " + id);
			$div.load('/orderdetail.php?id='+id);
			$div.dialog({
				width:650,
				height:400,
				modal:true,
				buttons: {
					"Chiudi" : function() {  $(this).dialog("close"); }
				}
			});
		},
		switch_profile: function(what)
		{
			$('.profile .'+what).slideDown(function() {
				location.href = "#" + what;
			});
		},
		swap_menu_icons: function(i) {
			var $items = $("#Top_menu .links a").removeClass("hover");
			$items.eq(i).addClass("hover");
			setTimeout(function() {
				Danze.swap_menu_icons(i+1);
			}, 100);
		},
		pay: function()
		{
			// if($("#payradio-4:checked").size() > 0)
			// {
				// var $div = $("<div></div>");
				// $div.attr("title", "Pagamento con carta di credito").attr("id", "paypal-info");
				// $div.html("<p>Verrete ora rediretti sul sito di PayPal, dovre potrete effettuare il pagamento " + 
							// " <b>anche senza un account di PayPal</b>, utilizzando quindi <b>la vostra carta di credito<b></p>");
				// $div.append("<img src='/images/pp-form.gif' />");
				// $div.append("<p class='link'><a href='#'>Vai adesso sul PayPal</a></p>");
				// $("p.link a", $div).click(function() { Danze.submit('order-summary'); });
				// $("img", $div).click(function () {Danze.submit('order-summary');});
				// $div.dialog({
					// modal:true,
					// width:750,
					// height:400
				// });
			// }
			// setTimeout(function() { Danze.submit('order-summary'); }, 1000);
			
			pageTracker._trackPageview('paypal.html');
			Danze.submit('order-summary');
		},
		subscribe: function() {
			var email = $(".subscribe-newsletter").val();
			var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test(email)) {
				alert('Inserisci un indirizzo e-mail valido');
				$(".subscribe-newsletter").get(0).focus();
				return false;
			}
			$.post("/data/subscribe.php", {"email": email}, function () {
				$(".newsletter-top").html("La tua e-mail è stata registrata con successo");
				setTimeout(function() {
					$(".newsletter-top").fadeOut("slow");
				}, 2000);
			});
		}
	}
}();

$(function() {
	$('a.zoom').lightBox();
});
