var form = "";

$(document).ready(function() {
    $("#corpo-contenuti-dati").dialog({ title: "Login",
	                                    modal: true,
	                                    autoOpen: false, 
	                                    width: 480, 
									    height: 370, 
									    overlay: { opacity: 0.7, background: "black" },
										buttons: {
										    "Ok": function () {
												if (ControllaLogin("formLoggati2")) {
													$.post("../e-commerce/logga.asp", $("#formLoggati2").serialize(), function(response) {
														if (response.toUpperCase() == "VERO" || response.toUpperCase() == "TRUE" ) {
															mostraErrore();
														} else {
															$.post("/include/estrai_tipo_utente.asp", {}, function(response) {
																form.attr("action", response);
																form.submit();
															});
														
														}
														
													});
													
												}
										    },
										    "Annulla": function () {
										        $(this).dialog("close");
										    }
										}
									 });
  
    $(".button_add_to_cart").click(function(e) {
	    /*
		e.preventDefault();
		for (i = 0; i<10; ++i) {
		  var el = $(this).parents().get(i);
		  if (el.tagName.toUpperCase() == "FORM") {
			form = $(el);
			break;
		  }
		}
		*/
	    e.preventDefault();
		if (this.parentNode.tagName.toUpperCase() != "FORM" && this.parentNode.parentNode.tagName.toUpperCase() != "FORM") {
			form = $(document.getElementById("form"));
		} else {
			for (i = 0; i<10; ++i) {
		  		var el = $(this).parents().get(i);
		  		if (el.tagName.toUpperCase() == "FORM") {
					form = $(el);
					break;
		  		}
			}
		}
        $("#corpo-contenuti-dati").dialog("open");
	});
  
  });