$(document).ready(function() {
	
	resizeimage();
	
	$(".testo").show();

	$(".click").click(function(e) {
		e.preventDefault();
		location.href = "index.php?"+$(this).attr("rel");
	});
	
	$(".visall").live("click", function(e) {
		e.preventDefault();
		$(".hideable").toggle();
	});

	$("#check_voucher").click(function(e) {
		e.preventDefault();

		var codice_voucher = $("#v_voucher").val();
		if (codice_voucher!="") {
			$.ajax({
				type: "POST",
				cache: "false",
				url: "ajax/checkVoucher.php",
				data: "codice_voucher="+codice_voucher,
				success: function(sel) {
					if (sel=="ok") {
						location.href = "index.php?p=3&step=1";
					} else if (sel=="no") {
						$("#voucher_error").html("Codice non valido!");
					} else if (sel=="already") {
						$("#voucher_error").html("Codice già utilizzato");
					}
				}
			});
		}
	});
	
	$("#go_anagrafica").click(function(e) {
		e.preventDefault();

		var c_dest = $("#c_dest").val();
		var c_dest_ind = $("#c_dest_ind").val();
		var c_dest_cap = $("#c_dest_cap").val();
		var c_dest_com = $("#c_dest_com").val();
		var c_dest_prov = $("#c_dest_prov").val();
		var c_dest_email = $("#c_dest_email").val();

		var i = 0;

		$("#c_dest_prov").css("border","1px solid #ccc");
		
		$("input").each(function() {
			$(this).css("border","1px solid #ccc");
			if ($(this).val()=="") {
				$(this).css("border","1px solid #c00");
				i++;
			}
		});
		
		if ($("#c_dest_prov").val()=="") {
			$("#c_dest_prov").css("border","1px solid #c00");
			i++;			
		}
		
		if (c_dest_email!="") {
			$("#c_dest_email").css("border","1px solid #ccc");
			if (!indirizzoEmailValido(c_dest_email)) {
				$("#c_dest_email").css("border","1px solid #c00");
				i++;
			}
		}
		
		if (i>0) alert("Dati mancanti o errati");
		
		if (i==0) {
			$.ajax({
				type: "POST",
				cache: "false",
				url: "ajax/putAnagrafica.php",
				data: "c_dest="+c_dest+"&c_dest_ind="+c_dest_ind+"&c_dest_cap="+c_dest_cap+"&c_dest_com="+c_dest_com+"&c_dest_prov="+c_dest_prov+"&c_dest_email="+c_dest_email,
				success: function(sel) {
					if (sel=="ok") {
						location.href = "index.php?p=3&step=4";
					}
				}
			});
		}
	});

	$("#brodit_codice_vai").click(function(e) {
		e.preventDefault();

		var codice_brodit = $("#brodit_codice").val();
		if (codice_brodit=="codice") codice_brodit = "";
		
		if (codice_brodit!="") {
			$.ajax({
				type: "POST",
				cache: "false",
				url: "ajax/getBrodit.php",
				data: "codice_brodit="+codice_brodit,
				success: function(sel) {
					$("#elenco_brodit").html(sel);
					$(".single").lightbox({
						fitToScreen: true,
						imageClickClose: true
					});
					resizeimage();
					h5hide();
					$("#pn").hide();
					$("li.sel").removeClass("sel");
					$(".testo").hide();
					$(".noricerca").show();
				}

			});
		}

	});
	
	$("#aggiornacarrello").click(function(e) {
		e.preventDefault();
		
		var quantita = 1;
		var idprz = "";
		var idprztot = "";
		prezzi = new Array(); 
		
		$(".cart_qta").each(function() {
			quantita = $(this).val();
			codice = $(this).attr("rel");
			idcart = $(this).attr("id").substring(9);

			idprz  = "cart_prz_"+$(this).attr("id").substring(9);
			idprztot  = "cart_prz_tot_"+$(this).attr("id").substring(9);

			printPrice(quantita,codice,idcart,idprztot);

		});

		printCodice();
		setTimeout(function() {printPriceTotal()}, 1000);

	});

		$("#s1_casa").change(function() {
			if ($(this).val()!="") {
			var casa = $(this).val();
			get_modelli(casa);
			}
		});
		
		$("#s1_modello").change(function() {
			if ($(this).val()!="") {
				var modello = $(this).val();
				get_anni(modello);
			}
		});
		
		$("#s1_anno").change(function() {
			if ($(this).val()!="") {
				var anno = $(this).val();
				get_elencoproclip(anno);
			}	
		});
		
		$("#s1_marca").change(function() {
			if ($(this).val()!="") {
				var marca = $(this).val();
				get_dispositivi(marca);
			}
		});
		
		$("#s1_dispositivo").change(function() {
			if ($(this).val()!="") {
				var dispositivo = $(this).val();
				get_elencoholder(dispositivo);
			}	
		});
		
		$("#brodit_codice").focus(function() {
			if ($(this).val()=="codice") $(this).val("");
		}).blur(function() {
			if ($(this).val()=="") $(this).val("codice");			
		});
		
		$(".a_ord").live("click", function(e) {
			e.preventDefault();
			var thisbutton = $(this);
			var notitle = ""
			if ($(thisbutton).attr("title")=="notit") notitle = "yes"; else notitle = "no";
			if ($(this).attr("rel")!="") {
				var codice = $(this).attr("rel");
					$.ajax({
					type: "POST",
					url: "ajax/putProclip.php",
					data: "notitle="+notitle+"&tipologia="+$("#get_p").val()+"&casa="+$("#h_s1_casa").val()+"&modello="+$("#h_s1_modello").val()+"&anno="+$("#h_s1_anno").val()+"&codice="+codice,
					success: function() {
						if ($("#get_p").val()==1) {
						location.href = 'index.php?p=1&step=2';
						} else if ($("#get_p").val()==2) {
						var cart_items = $("#carrello_items").val() + 1;
						$("#carello_items").val(cart_items);
						$(thisbutton).after("<span class=\"addprodcart\">&nbsp;&nbsp; Prodotto aggiunto al carrello</span>");
						setTimeout("disapper_addprodcart()",2000);
						} else if ($("#get_p").val()==3) {
						location.href = 'index.php?p=3&step=2';
						}
					}
				});
			}
		});


		$(".a_hord").live("click", function(e) {
			e.preventDefault();
			var thisbutton = $(this);
			if ($(this).attr("rel")!="") {
				var codice = $(this).attr("rel");
					$.ajax({
					type: "POST",
					url: "ajax/putHolder.php",
					data: "tipologia="+$("#get_p").val()+"&marca="+$("#h_s1_marca").val()+"&dispositivo="+$("#h_s1_dispositivo").val()+"&codice="+codice,
					success: function() {
						if ($("#get_p").val()==1) {
						location.href = 'index.php?p=1&step=3';
						} else if ($("#get_p").val()==2) {
						var cart_items = $("#carrello_items").val() + 1;
						$("#carello_items").val(cart_items);
						$(thisbutton).after("<span class=\"addprodcart\">&nbsp;&nbsp; Prodotto aggiunto al carrello</span>");
						setTimeout("disapper_addprodcart()",2000);						
						} else if ($("#get_p").val()==3) {
						location.href = 'index.php?p=3&step=3';
						}
					}
				});
			}
		});

		$(".ac_ord").live("click", function(e) {
			e.preventDefault();
			var thisbutton = $(this);
			if ($(this).attr("rel")!="") {
				var codice = $(this).attr("rel");
					$.ajax({
					type: "POST",
					url: "ajax/putAccessori.php",
					data: "codice="+codice,
					success: function() {
						var cart_items = $("#carrello_items").val() + 1;
						$("#carello_items").val(cart_items);
						$(thisbutton).after("<span class=\"addprodcart\">&nbsp;&nbsp; Prodotto aggiunto al carrello</span>");
						setTimeout("disapper_addprodcart()",2000);
					}
				});
			}
		});
	
	$(".del").live("click", function(e) {
		e.preventDefault();
		var idriga = $(this).attr("rel");
			$.ajax({
				$type: "GET",
				url: "ajax/delBrodit.php",
				data: "id="+idriga,
				success: function(msg) {
					if ($("#get_p").val()==1) {
						if (msg==1) location.href = "index.php?p=1&step=1";
						if (msg==2) location.href = "index.php?p=1&step=2";
					} else {
						$("#tr"+idriga).fadeOut("fast");
						printCodice();
						setTimeout(function() {printPriceTotal()}, 1000);
//						$("#rigatotale span").html(msg);
					}					
				}
			});
	});
	
	$(".single").lightbox({
		fitToScreen: true,
		imageClickClose: true
	});
	
	$(".cart_qta").blur(function(e) {
		e.preventDefault();
			
		var quantita = 1;
		var idprz = "";
		var idprztot = "";
		prezzi = new Array(); 

		
		quantita = $(this).val();
		if (quantita=="") quantita = 1;
		if (quantita==0) quantita = 1;
		$(this).val(quantita);
		codice = $(this).attr("rel");
		idcart = $(this).attr("id").substring(9);
	
		idprz  = "cart_prz_"+$(this).attr("id").substring(9);
		idprztot  = "cart_prz_tot_"+$(this).attr("id").substring(9);
	
		printPrice(quantita,codice,idcart,idprztot);
	
		printCodice();
		setTimeout(function() {printPriceTotal()}, 1000);

	});
	
	$("#codice_sconto").blur(function(e) {
		e.preventDefault();
		printCodice();
		setTimeout(function() {printPriceTotal()}, 1000);		
	});
	
	$("#go_to_cart").click(function() {
		self.parent.location.href = 'http://www.infovi.it/cx_carrello1.php?proclipwizard=1';
		self.parent.Shadowbox.close();
	});

});