// crea l'oggetto per la comunicazione AJAX con il server
// compatibile con tutti i browser che supportano AJAX
function crea_http_req() {
	var req = false;
	if (typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req && typeof ActiveXObject != "undefined") {
		try {
			req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				try {
					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
				} catch (e3) {
					req=null;
				}
			}
		}
	}

	if(!req && window.createRequest)
		req = window.createRequest();

	if (!req) alert("Il browser non supporta AJAX");

	return req;
}

// l'oggetto per comunicare con il server
var http_req = crea_http_req();

// invia i dati del form al server
function invia_dati() {
		var dati_post = "comunicazioni=" +
						encodeURIComponent( document.getElementById("comunicazioni").value ) +
						"&email_address=" +
						encodeURIComponent( document.getElementById("email_address").value ) + 
						"&nome=" +
						encodeURIComponent( document.getElementById("nome").value ) +
						"&catalogo=" +
						encodeURIComponent( document.getElementById("catalogo").checked ) + 
						"&rapp=" +
						encodeURIComponent( document.getElementById("rapp").checked );
		http_req.open('POST', 'manda_email.php', true);
		http_req.onreadystatechange = gestisci_risposta;
		
		http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http_req.setRequestHeader("Content-length", dati_post.length);
		http_req.setRequestHeader("Connection", "close");
		http_req.send(dati_post);
}

// recupero e gestisco la risposta inviata dal server
function gestisci_risposta() {

	if(http_req.readyState == 4) {
		var esito = http_req.responseText;
		alert(esito);
		}
}

function chiedi_indirizzo(cosa) {
	if (cosa=='cat')
	{
		if (document.getElementById("catalogo").checked)
			alert ('Please specify your address in the communication field to receive our catalogue. Thanks.');
	}
	else
	{
		if (document.getElementById("rapp").checked)
			alert ('Please specifty your address in the communication field for a sales rapresentative visit. Thanks.');
	}
}