// 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;
}



// invia i dati del form al server
function invia_dati() {
	var http_req = crea_http_req();
	
	var dati_post = "username=" +
					encodeURIComponent( document.getElementById("username").value ) +
					"&password=" +
					encodeURIComponent( document.getElementById("password").value );

	http_req.open('POST', 'ajax_make_login.php', true);
	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);
	
	http_req.onreadystatechange = function() {
								  		gestisci_risposta(http_req);
									}
	
}

// recupero e gestisco la risposta inviata dal server
function gestisci_risposta(http_req) {
	if(http_req.readyState == 4) {
		var esito = http_req.responseText;
		var msg = document.getElementById('msg_login');
		switch (esito) {
		  case '1':
			msg.innerHTML = 'username o password non inserite';
		  break;

		 case '2':
			msg.innerHTML = 'username e/o password errata';
		  break;

		  case '3':
		  	location.href = 'index.php';
		  break;

		  default:
			msg.innerHTML = 'Risposta del server non riconosciuta: ' + esito;
		}
	}
}


// invia i dati del form al server
function invia_dati_sito() {
	var http_req = crea_http_req();
	
	var dati_post = "username=" +
					encodeURIComponent( document.getElementById("usr").value ) +
					"&password=" +
					encodeURIComponent( document.getElementById("pwd").value );

	http_req.open('POST', 'ajax_make_login.php', true);
	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);
	
	http_req.onreadystatechange = function() {
								  		gestisci_risposta_sito(http_req);
									}
	
}

// recupero e gestisco la risposta inviata dal server
function gestisci_risposta_sito(http_req) {
	if(http_req.readyState == 4) {
		var esito = http_req.responseText;
		var msg = document.getElementById('msg_login');
		switch (esito) {
		  case '1':
			msg.innerHTML = 'username o password non inserite';
		  break;

		 case '2':
			msg.innerHTML = 'username e/o password errata';
		  break;

		  case '3-q':
		  	location.href = 'private.php';
		  break;
		  
		  case '3-r':
		  	location.href = 'request.php';
		  break;
		  
		  case '3-v':
		  	location.href = 'videotutorial.php';
		  break;

		  default:
			msg.innerHTML = 'Risposta del server non riconosciuta: ' + esito;
		}
	}
}


function richiama_pagina(url, method, idDiv, isthick) {
	// l'oggetto per comunicare con il server
	var http_req = crea_http_req();

	http_req.open(method, url, true);
	http_req.setRequestHeader("Content-Type","text/html; charset:ISO-8859-1");
	http_req.send(null);
	
	http_req.onreadystatechange = function() {
								  		stampa_contenuto(http_req, idDiv);
										if(typeof(isthick) != 'undefined' && isthick) tb_init('a.thickbox, area.thickbox, input.thickbox');
									}
	
}

function stampa_contenuto(http_req, idDiv) {
	if(http_req.readyState == 4) {
		var esito = http_req.responseText;
		var content = document.getElementById(idDiv);
		content.innerHTML = esito;
	}
}


function esegui_ricerca(f){
	qstring = "Last_Name=" + f.Last_Name.value + "&First_Name=" + f.First_Name.value + "&Company=" + f.Company.value + "&City=" + f.City.value + "&Region=" + f.Region.value;
	if(f.richiesta_kit.checked) qstring += "&richiesta_kit=" + f.richiesta_kit.value;
	if(f.richiesta_demo.checked) qstring += "&richiesta_demo=" + f.richiesta_demo.value;
	if(f.da_evadere.checked) qstring += "&da_evadere=" + f.da_evadere.value;
	if(f.evasi.checked) qstring += "&evasi=" + f.evasi.value;
	
	richiama_pagina('ajax_lista_anagrafica.php?' + qstring, 'GET','lista_anagrafica',true);
}
