	// Espressione regolare dell'email
    var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	
	//Dialogo forgot
	var content_forgot = '<form id="forgot_password" style="padding:0; margin:0;">';
	content_forgot += 'Inserisca il suo indirizzo e-mail cortesemente';
	content_forgot += '<div style="padding-top:20px; float:left; width:280px;">';
	content_forgot += '<div style="float:left; width:60px;">E-mail:</div>';
	content_forgot += '<div style="float:left; width:220px;"><input type="text" id="email" name="email" style="width:220px;" /></div>';
	content_forgot += '<div id="email_error" style="float:left; width:220px; margin-left:60px; color:#d70023; padding-top:10px;"></div>';
	content_forgot += '</div>';
	content_forgot += '</form>';
	
	//Dialog change
	var content_change = '<form id="change_password" style="padding:0; margin:0;">';
	content_change += 'Compili cortesemente i seguenti campi';
	content_change += '<div style="padding-top:20px; float:left; width:380px;">';
	content_change += '<div style="float:left; width:140px; margin-bottom:10px;">E-mail:</div>';
	content_change += '<div style="float:left; width:220px; margin-bottom:10px;"><input type="text" id="email2" name="email2" style="width:220px;" /></div>';
	content_change += '<div id="email_error_password" style="float:left; margin-left:140px; width:220px; margin-bottom:10px; color:#d70023;"></div>';
	content_change += '<div style="float:left; width:140px; margin-bottom:10px;">Password:</div>';
	content_change += '<div style="float:left; width:220px; margin-bottom:10px;"><input type="password" id="password" name="password" style="width:220px;" /></div>';
	content_change += '<div id="password_error" style="float:left; margin-left:140px; width:220px; margin-bottom:10px; color:#d70023;"></div>';
	content_change += '<div style="float:left; width:140px; margin-bottom:10px;">Nuova password:</div>';
	content_change += '<div style="float:left; width:220px; margin-bottom:10px;"><input type="password" id="newpassword" name="newpassword" style="width:220px;" /></div>';
	content_change += '<div id="newpassword_error" style="float:left; margin-left:140px; width:220px; margin-bottom:10px; color:#d70023;"></div>';
	content_change += '<div style="float:left; width:140px; margin-bottom:10px;">Conferma password:</div>';
	content_change += '<div style="float:left; width:220px; margin-bottom:10px;"><input type="password" id="confpassword" name="confpassword" style="width:220px;" /></div>';
	content_change += '<div id="confpassword_error" style="float:left; margin-left:140px; width:220px; color:#d70023;"></div>';
	content_change += '</div>';
	content_change += '</form>';
	
	$(function(){
		$('#email_error').hide();
		// Dialog			
		$('#dialog_forgot').dialog({
		    draggable: false,
			bgiframe: true,
			resizable: false,
			modal: true,
			autoOpen: false,
			width: 310,
			height: 160			
		});
		
		$('#dialog_login').dialog({
		    draggable: false,
			bgiframe: true,
			resizable: false,
			modal: true,
			autoOpen: false,
			width: 250,
			height: 150
		});
		
		$('#dialog_contatti').dialog({
		    draggable: false,
			bgiframe: true,
			resizable: false,
			modal: true,
			autoOpen: false,
			width: 250,
			height: 150
		});
		
		$('#dialog_change').dialog({
			draggable: false,
			resizable: false,
			modal: true,
			autoOpen: false,
			width: 410,
			height: 300
		});
		
		// Dialog Link
		$('#dialog_link3').click(function(){
			$('#dialog_contatti').dialog({width: 400, height:270});
			$('#dialog_contatti').html("<div id='message'></div>");						 
			$('#message').html('<div style="padding-bottom:10px;"><strong>TUTELA DELLA PRIVACY</strong><br/></div><div style="padding-bottom:10px;"><strong>Informativa ai sensi del D. Lgs 196/03</strong></div><div style="padding-bottom:10px; text-align:justify">I dati da Lei forniti saranno trattati dal Titolare della ns. Associazione e saranno da noi utilizzati per poterLe inviare aggiornamenti tecnico-commerciali, inviti a manifestazioni e convegni, ecc. In ogni momento Le sar&agrave; possibile accedere ai suoi dati per richiesta di aggiornamento o cancellazione.</div><div align="justify"><strong>N.B.</strong> L\'invio di questo modulo costituisce adesione al trattamento dei dati così come sopra specificato.</div>');
			$('#dialog_contatti').dialog('open');
			$('#dialog_contatti').dialog({
				buttons : {
				"OK": function() {						
						$(this).dialog("close");
						document.contatto.telefono.focus();
					} 
				}
			});		
			return false;
		});
		
		$('#dialog_link1').click(function(){
						
			$('#dialog_forgot').dialog({
				height:160,
				buttons: {
					//Button invia
					"Invia": function() {
						// validate and process form here
					var email = $('#email').val();
					if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
						$(this).dialog({height: 190});
						$("#email_error").html('Inserisca un indirizzo e-mail corretto');
						$("#email_error").fadeIn(1000);
						$("input#email").focus();
						return false;
					}
					//loading animation
					$('#dialog_forgot').dialog({height: 160});
					$('#dialog_forgot').html('<div style="text-align:center; padding-top:10px; padding-bottom:10px;">Verifica indirizzo email</div><div style="text-align:center;"><img src="immagini/loadingAnimation.gif" width="208" height="13" /></div>');
					$(".ui-dialog-buttonset").hide();
					// Ajax result
					var dataString = 'email='+ email;
					$.ajax({
						type: "POST",
						url: "user/forgot_password.php",
						data: dataString,
						success: function(data) {
						  //Data						 
						  //Email exist and send
						  if(data == "ok"){
							  $('#dialog_forgot').dialog({
							  		height: 150,
									buttons: {
										"Chiudi" : function(){
											$('#email_error').hide();
											$(this).dialog({height: 150});
											$(this).dialog("close"); 	
										}
									}						
									
							  });
							  $('#dialog_forgot').html("<div id='message'></div>");						 
							  $('#message').html('<div style="background:url(immagini/icon_dialog_success.jpg) no-repeat; margin-top:10px; padding-left:40px;">Ricever&agrave; una notifica via email<br/>controlli il suo indirizzo di posta</div>');
							  $('#message').hide().fadeIn(1500);
							  
						  }
						  //Email not exist
						  if(data=="notexist"){
							$(".ui-dialog-buttonset").show();
							$('#dialog_forgot').html(content_forgot);
							$('#dialog_forgot').dialog({height: 190});
							$("#email_error").html('Indirizzo email inesistente');
							$("#email_error").show().fadeIn(1500);							
							$("input#email").focus();
							return false;
						  }
						}
					  });
					  return false;
					},
					//Button annulla
					"Annulla": function() {
						$('#email_error').hide();
						$(this).dialog({height: 160});
						$(this).dialog("close"); 
					}
				}
			});
			$('#dialog_forgot').html(content_forgot);			
			$('#email_error').hide();
			//Open dialog
			$('#dialog_forgot').dialog('open');
			return false;
		});
		
		$('#dialog_link2').click(function(){
						
			$('#dialog_change').dialog({
				buttons: {
					//Button invia
					"Invia": function() {
					// validate and process form here
					var password = $('#password').val();
					var email = $('#email2').val();					
					var newpassword = $('#newpassword').val();
					var confpassword = $('#confpassword').val();
					
					if(newpassword.length<8){
						hideAll();
						$(this).dialog({height: 320});
						$("#newpassword_error").html('Minimo 8 caratteri');
						$("#newpassword_error").fadeIn(1000);
						$('#newpassword').val('');
						$("input#newpassword").focus();
						return false;
					}
					
					if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
						hideAll();
						$(this).dialog({height: 320});
						$("#email_error_password").html('Inserire un indirizzo e-mail corretto');
						$("#email_error_password").fadeIn(1000);						
						$("input#email2").focus();
						return false;
					}
					
					if ((password == "") || (password == "undefined")) {
						hideAll();
						$(this).dialog({height: 320});
						$("#password_error").html('Inserisca la password');
						$("#password_error").fadeIn(1000);
						$("input#password").focus();
						return false;
					}
					
					
					if ((newpassword == "") || (newpassword == "undefined")) {
						hideAll();
						$(this).dialog({height: 320});
						$("#newpassword_error").html('Inserisca la nuova password');
						$("#newpassword_error").fadeIn(1000);						
						$("input#newpassword").focus();
						return false;
					}
					
					if ((confpassword == "") || (confpassword == "undefined")) {
						hideAll();
						$(this).dialog({height: 320});
						$("#confpassword_error").html('Confermi la nuova password');
						$("#confpassword_error").fadeIn(1000);
						$("input#confpassword").focus();
						return false;
					}
					
					//Confronto delle password
					if (password==newpassword) {
						hideAll();
						$(this).dialog({height: 320});
						$("#newpassword_error").html('Attenzione la nuova password coincide con quella attuale');
						$("#newpassword_error").fadeIn(1000);
						$('#newpassword').val('');
						$("input#newpassword").focus();
						return false;
					}
					
					if (newpassword!=confpassword) {
						hideAll();
						$(this).dialog({height: 320});
						$("#confpassword_error").html('Attenzione le password non coincidono');
						$("#confpassword_error").fadeIn(1000);
						$("#confpassword").val('');
						$("input#confpassword").focus();
						return false;
					}
					// Ajax result
					var dataString = 'email2='+ email + '&newpassword='+ newpassword + '&password='+ password;
					//loading animation
					$('#dialog_change').dialog({height: 160});
					$('#dialog_change').html('<div style="text-align:center; padding-top:10px; padding-bottom:10px;">Verifica dei dati inseriti</div><div style="text-align:center;"><img src="immagini/loadingAnimation.gif" width="208" height="13" /></div>');
					$(".ui-dialog-buttonset").hide();
					$.ajax({
						type: "POST",
						url: "user/new_password.php",
						data: dataString,
						success: function(data) {
						  //Data						  
						  //exist and send
						  if(data == "ok"){
							  $('#dialog_change').dialog({
							  		height: 150,
									buttons: {
										"Chiudi" : function(){
											hideAll();											
											$(this).dialog("close"); 	
										}
									}
							  });
							  $('#dialog_change').html("<div id='message'></div>");						 
							  $('#message').html('<div style="background:url(immagini/icon_dialog_success.jpg) no-repeat; margin-top:10px; padding-left:40px;">Cambio password avvenuto con successo<br/>Controlli il suo indirizzo di posta</div>');
							  $('#message').hide().fadeIn(1500);							  
						  }
						  //Email not exist
						  if(data=="notexist"){
							$(".ui-dialog-buttonset").show();
							$('#dialog_change').html(content_change);
							$('#dialog_change').dialog({height: 320});
							$("#password_error").html('Indirizzo email o password errati');
							$("#password_error").show().fadeIn(1500);							
							$("input#email").focus();
							return false;
						  }
						}
					  });
					  return false;
					}, 
					"Annulla": function() {
						hideAll();
						$(this).dialog({height: 300});
						$(this).dialog("close"); 
					} 
				} 
			});
			$('#dialog_change').html(content_change);
			//Open dialog
			$('#dialog_change').dialog({height: 300});
			$('#dialog_change').dialog('open');
			return false;
		});		
	});
		
	function hideAll(){
		$('#email_error_password').html('');
		$('#password_error').html('');
		$('#confpassword_error').html('');
		$('#newpassword_error').html('');
	}
