var html_form = "";

$(document).ready(function() {
	$("#btn_invia_nl").click(chiamaBlockUI);
	html_form = $('#form').html();
	$('#form').html("");
});

function chiudiBlockUI() {
	
	$.unblockUI();
	
}

function chiamaBlockUI() {	
	
	var str = '<div style="width:853px; text-align:right;"><span id="chiudi_blockui" style="font-size:10px; cursor:pointer">CHIUDI&nbsp;</span></div>';
	str = str + '<div id="div_content_form_nl">' + html_form + '</div>';		

	$.blockUI({ 
		css: { 
			border: 'none',			
			backgroundColor: '#FFFFFF', 
			color: '#C7C7C7',
			cursor: 'default',			
			top: '100px',
			width: '853px',
			left: '50%',
			marginLeft: '-426px',
			height: '234px',
			margin: 'auto',
			textAlign: 'left'
		},

		message: str,
		overlayCSS: {
			cursor: 'default',
			backgroundColor:'#FFCCFF', 
			opacity:'0.8' 
		}
	});
	
	$("input[@name='email_form_nl']").val($("#s").val());
	$("#s").val('');
	
	$("#chiudi_blockui").click(chiudiBlockUI);
	
	$("img[@name='btn_invia_form_nl']").click(function() {
		var msg = validate_form_nl();
		if (msg != '') {
			$("#msg_form_nl_text").html(msg);
			$("#div_form_nl").hide();
			$("#msg_form_nl").fadeIn();
			$("#chiudi_blockui").unbind('click', chiudiBlockUI);
			$("#chiudi_blockui").click(function() {
				chiudi_msg_form_nl();
			});
		} else {
			sendData();
		}
			
	});
	
}

function validate_form_nl() {

	var str = "";
	
	if ($("input[@name='nome_form_nl']").val() == "") {
		str = "Inserisci il nome";
		return str;
	}
	
	if ($("input[@name='cognome_form_nl']").val() == "") {
		str = "Inserisci il cognome";
		return str;
	}
	
	if ($("input[@name='email_form_nl']").val() == "" || !CheckMail($("input[@name='email_form_nl']").val(), true)) {
		str = "Inserisci un indirizzo email valido";
		return str;
	}
	
	if ($("select[@name='sesso_form_nl']").val() == "") {
		str = "Seleziona il sesso";
		return str;
	}
	
	if ($("select[@name='anno_form_nl']").val() == "") {
		str = "Seleziona l'anno di nascita";
		return str;
	}
	
	if ($("input[@name='privacy_form_nl']:checked").val() != 1) {
		str = "&Egrave; necessario accettare la legge sulla privacy";
		return str;
	}		
	
	return str;
		
}

function chiudi_msg_form_nl() {

	$("#msg_form_nl").hide();
	$("#div_form_nl").fadeIn();
	$("#chiudi_blockui").unbind('chiudi_msg_form_nl', chiudiBlockUI);
	$("#chiudi_blockui").click(chiudiBlockUI);
	
}

function CheckMail(string, req) {
	if(!req && !string.length) return true;
	if(req && !string.length) return false;
	else {
		regexpCtrl = /^(\w+(?:\.\w+)*(?:\-\w+)*)@((?:\w\.)*\w[\w-]{0,66})\.(\w{2,66}(?:\.\w{2,6})?)$/i
		if(!regexpCtrl.test(string)) return false;
	}
	return true;
}

function sendData() {
		
	var dataString = 'nome=' + $("input[@name='nome_form_nl']").val();
	dataString += '&cognome=' + $("input[@name='cognome_form_nl']").val();
	dataString += '&email=' + $("input[@name='email_form_nl']").val();
	dataString += '&sesso=' + $("select[@name='sesso_form_nl']").val();
	dataString += '&anno_nascita=' + $("select[@name='anno_form_nl']").val();

	$.ajax({
		type: "POST",		
		url: $("input[@name='url_form_nl']").val(),
		data: dataString,
		success: function(msg) {
			
			if (msg != 'null') {
				$("#msg_form_nl_text").html(msg);
				$("#div_form_nl").hide();
				$("#msg_form_nl").fadeIn();
			}
		}
	});

}

function privacy(url) {
	
	mywidth = 650;
	myheight = 400;
	myleft = (screen.width - mywidth) / 2;
	mytop = (screen.height - myheight) / 2;
	window.open(url, "", "width=" + mywidth + ",height=" + myheight + ",left=" + myleft + ",top=" + mytop + ",scrollbars=1,resizable=0,menubar=0,toolbar=0,status=0,location=0,directories=0");
	
}