//--- Posizionati nel primo campo
function fNome() {
	document.getElementById("nome").focus();
}

//------------------------- Controlla tutti i campi
function fCheckObbligatori(){
	var blnReturn	= true;
	
	if(!fCheckNome()) {
		blnReturn	= false;
		alert("Inserisci il campo: Nome.");
		document.getElementById("nome").focus();
		
	} else if(!fCheckCognome()) {
		blnReturn	= false;
		alert("Inserisci il campo: Cognome.");
		document.getElementById("cognome").focus();
		
	} else if(!fCheckCodiceFiscaleSubmit()) {
		blnReturn	= false;
		alert("Verifica il campo: Codice fiscale / Partita Iva.");
		document.getElementById("codice_fiscale").focus();
		
	} else if(!fCheckIndirizzo()) {
		blnReturn	= false;
		alert("Inserisci il campo: Indirizzo.");
		document.getElementById("indirizzo").focus();
		
	} else if(!fCheckCap()) {
		blnReturn	= false;
		alert("Inserisci il campo: C.a.p..");
		document.getElementById("cap").focus();
		
	} else if(!fCheckCitta()) {
		blnReturn	= false;
		alert("Inserisci il campo: Citta'");
		document.getElementById("citta").focus();
		
	} else if( ! fCheckProvincia() ) {
		
		blnReturn = false;
		alert("Inserisci il campo: Provincia");
		$("provincia").focus();
		
	} else if(!fCheckNazione()) {
		blnReturn	= false;
		alert("Inserisci il campo: Nazione.");
		document.getElementById("nazione").focus();
		
	} else if(!fCheckEmailSubmit()) {
		blnReturn	= false;
		alert("Verifica il campo: Email.");
		document.getElementById("email").focus();
		
	} else if(!fCheckUsernameSubmit()) {
		blnReturn	= false;
		alert("Verifica il campo: Username.");
		document.getElementById("username").focus();
		
	} else if(!fCheckPassword()) {
		blnReturn	= false;
		alert("Inserisci il campo: Password.");
		document.getElementById("password").focus();
		
	} else if(!fCheckPassword2()) {
		blnReturn	= false;
		alert("Conferma il campo: Password.");
		document.getElementById("password2").focus();
		
	} else if(!fCheckDomanda()) {
		blnReturn	= false;
		alert("Inserisci il campo: Domanda di sicurezza.");
		//document.getElementById("domanda").focus();
		
	} else if(!fCheckRisposta()) {
		blnReturn	= false;
		alert("Inserisci il campo: Risposta.");
		document.getElementById("risposta").focus();
		
	} else if(!fCheckPrivacy()) {
		blnReturn	= false;
		alert("Accettazione obbligatoria per l'informativa sulla privacy.");
		//document.getElementById("chkPrivacy").focus();
		
	}

	return blnReturn;

} //------------------------- Controlla tutti i campi








//--- Controllo nome
function fCheckNome(){
	var blnReturn	= true;
	if( document.getElementById("nome") != null && document.getElementById("nome").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblnome").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblnome").innerHTML = "";
	}
	return blnReturn;
}

//--- Controllo cognome
function fCheckCognome(){
	var blnReturn	= true;
	if( document.getElementById("cognome") != null && document.getElementById("cognome").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblcognome").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblcognome").innerHTML = "";
	}
	return blnReturn;
}

//--- Controllo codice_fiscale
function fCheckCodiceFiscale(){
	var blnReturn	= true;
	if( document.getElementById("codice_fiscale") != null && document.getElementById("codice_fiscale").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblcodice_fiscale").className =  "bform_errore";
		document.getElementById("lblcodice_fiscale").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblcodice_fiscale").className =  "bform_conferma";
		document.getElementById("lblcodice_fiscale").innerHTML =  "";
		sndReqCodiceFiscale("checkcodicefiscale", document.getElementById("codice_fiscale").value);
	}
	return blnReturn;
}

//--- Controllo codice_fiscale per submit form
function fCheckCodiceFiscaleSubmit(){
	var blnReturn	= true;
	if( document.getElementById("codice_fiscale") != null && document.getElementById("codice_fiscale").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblcodice_fiscale").className =  "bform_errore";
		document.getElementById("lblcodice_fiscale").innerHTML = "Campo obbligatorio.";
	}
	if( document.getElementById("codice_fiscale_error").value == "0"){ 
		blnReturn	= false;
	}
	return blnReturn;
}

//--- Controllo indirizzo
function fCheckIndirizzo(){
	var blnReturn	= true;
	if( document.getElementById("indirizzo") != null && document.getElementById("indirizzo").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblindirizzo").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblindirizzo").innerHTML = "";
	}
	return blnReturn;
}

//--- Controllo cap
function fCheckCap(){
	var blnReturn	= true;
	if( document.getElementById("cap") != null && document.getElementById("cap").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblcap").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblcap").innerHTML = "";
	}
	return blnReturn;
}

//--- Controllo citta
function fCheckCitta(){
	var blnReturn	= true;
	if( document.getElementById("citta") != null && document.getElementById("citta").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblcitta").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblcitta").innerHTML = "";
	}
	return blnReturn;
}

//--- Controllo provincia
function fCheckProvincia(){
	var blnReturn	= true;
	if( $("provincia") != null && $("provincia").value == ""){ 
		blnReturn	= false;
		$("lblprovincia").innerHTML = "Campo obbligatorio.";
	} else {
		$("lblprovincia").innerHTML = "";
	}
	return blnReturn;
}

//--- Controllo nazione
function fCheckNazione(){
	var blnReturn	= true;
	if( document.getElementById("nazione") != null && document.getElementById("nazione").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblnazione").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblnazione").innerHTML = "";
	}
	return blnReturn;
}

//--- Controllo email
function fCheckEmail(){
	var blnReturn	= true;
	if( document.getElementById("email") != null && document.getElementById("email").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblemail").className =  "bform_errore";
		document.getElementById("lblemail").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblemail").className =  "bform_conferma";
		document.getElementById("lblemail").innerHTML =  "";
		sndReqEmail("checkemail", document.getElementById("email").value);
	}
	if( document.getElementById("email_error") == "0"){ 
		blnReturn	= false;
	} else {
		
	}
	return blnReturn;
}

//--- Controllo email per submit form
function fCheckEmailSubmit(){
	var blnReturn	= true;
	if( document.getElementById("email") != null && document.getElementById("email").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblemail").className =  "bform_errore";
		document.getElementById("lblemail").innerHTML = "Campo obbligatorio.";
	}
	if( document.getElementById("email_error").value == "0"){ 
		blnReturn	= false;
	}
	return blnReturn;
}

//--- Controllo username
function fCheckUsername(){
	var blnReturn	= true;
	if( document.getElementById("username") != null && document.getElementById("username").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblusername").className = "bform_errore";
		document.getElementById("lblusername").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblusername").className =  "bform_conferma";
		document.getElementById("lblusername").innerHTML =  "";
		sndReqUsername("checkusername", document.getElementById("username").value);
	}
	return blnReturn;
}

//--- Controllo username
function fCheckUsernameSubmit(){
	var blnReturn	= true;
	if( document.getElementById("username") != null && document.getElementById("username").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblusername").className = "bform_errore";
		document.getElementById("lblusername").innerHTML = "Campo obbligatorio.";
	}
	if( document.getElementById("username_error").value == "0"){ 
		blnReturn	= false;
	}
	return blnReturn;
}

//--- Controllo password
function fCheckPassword(){
	var blnReturn	= true;
	if( document.getElementById("password") != null && document.getElementById("password").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblpassword").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblpassword").innerHTML = "";
	}
	return blnReturn;
}

//--- Controllo password2
function fCheckPassword2(){
	var blnReturn	= true;
	if( document.getElementById("password2") != null && document.getElementById("password2").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblpassword2").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblpassword2").innerHTML = "";
		//-- verifica conferma
		if( document.getElementById("password").value != document.getElementById("password2").value ){
			blnReturn	= false;
			document.getElementById("lblpassword2").innerHTML = "La password non &egrave; uguale.";
		}
	}
	return blnReturn;
}

//--- Controllo domanda
function fCheckDomanda(){
	var blnReturn	= true;
	var oIdDomanda				= document.getElementById("id_domanda");
	var oIdDomandaLabel			= document.getElementById("lblid_domanda");
	var oDomandaCustom			= document.getElementById("domanda_custom");
	var oDomandaCustomLabel		= document.getElementById("lbldomanda_custom");
	var oBoxListaDomande		= document.getElementById("box_lista_domande");
	var oBoxDomandaCustom		= document.getElementById("box_domanda_custom");

	if ( oIdDomanda != null ) {
		var oIdDomandaIndex = oIdDomanda.options[oIdDomanda.selectedIndex].value;
		
		if( oIdDomandaIndex == "custom"  ){
			oIdDomandaLabel.innerHTML 		= "";
			oBoxListaDomande.className 		= "nascondi";
			oBoxDomandaCustom.className 	= "";
			if( oDomandaCustom.value != "" ){
				oDomandaCustomLabel.innerHTML 	= "";
			} else {
				blnReturn	= false;
				oDomandaCustomLabel.innerHTML 	= "Inserisci la domanda.";
				domanda_custom.focus();
			}
			
		} else if(oIdDomandaIndex == "none" || oIdDomanda.selectedIndex == 0) {
			blnReturn	= false;
			oIdDomandaLabel.innerHTML 		= "Scegli una domanda.";
			oBoxListaDomande.className 		= "";
			oBoxDomandaCustom.className 	= "nascondi";
			
		} else {
			oIdDomandaLabel.innerHTML 		= "";
			oBoxListaDomande.className 		= "";
			oBoxDomandaCustom.className 	= "nascondi";
		}
		
	}
	return blnReturn;
}

//--- Controllo risposta
function fCheckRisposta(){
	var blnReturn	= true;
	if( document.getElementById("risposta") != null && document.getElementById("risposta").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblrisposta").innerHTML = "Inserisci la risposta.";
	} else {
		document.getElementById("lblrisposta").innerHTML = "";
	}
	return blnReturn;
}


//--- Controllo privacy
function fCheckPrivacy(){
	var blnReturn	= true;
	if( document.getElementById("privacy").checked != null && document.getElementById("privacy").checked == ""){ 
		blnReturn	= false;
		document.getElementById("lblprivacy").innerHTML = "Accettazione obbligatoria.";
	} else {
		document.getElementById("lblprivacy").innerHTML = "";
	}
	return blnReturn;
}







//--- Visualizza la lista delle domande
function fVisualizzaListaDomande() {
	document.getElementById("domanda_custom").value 			= "";
	document.getElementById("lbldomanda_custom").innerHTML 		= "";
	document.getElementById("box_domanda_custom").className 	= "nascondi";
	document.getElementById("id_domanda").selectedIndex 		= 0;
	document.getElementById("lblid_domanda").innerHTML 			= "Scegli una domanda.";
	document.getElementById("box_lista_domande").className 		= "";
}

//--- Evidenzia il box con il testo della Privacy
function fEvidenziaPrivacy(){
	var oBox = document.getElementById("box_privacy");
	oBox.className = (oBox.className=="bform_privacy" ? "bform_privacy_on" : "bform_privacy");
	
	var oPrivacy		= document.getElementById("privacy");
	var oPrivacyLabel	= document.getElementById("lblprivacy");
	if( oPrivacy.checked != null && oPrivacy.checked == false ){
		blnReturn	= false;
		oPrivacyLabel.innerHTML = "Accettazione obbligatoria.";
	} else {
		oPrivacyLabel.innerHTML = "";
	}
}






//--- Invia richiesta per controllo codice_fiscale
function sndReqCodiceFiscale(action, codice_fiscale) {
	document.getElementById("lblcodice_fiscale").className =  "bform_conferma";
	document.getElementById("lblcodice_fiscale").innerHTML =  "<img src='immagini/preload/loader_bform.gif' alt='loader' /> Attendere prego...";
            	
    http.open('get', '_request.php?action='+action+'&codice_fiscale=' + codice_fiscale);
    http.onreadystatechange = handleResponse;
    http.send(null);
}


//--- Invia richiesta per controllo email
function sndReqEmail(action, email) {
	document.getElementById("lblemail").className =  "bform_conferma";
	document.getElementById("lblemail").innerHTML =  "<img src='immagini/preload/loader_333.gif' alt='loader' /> Attendere prego...";
            	
    http.open('get', '_request.php?action='+action+'&email='+email);
    http.onreadystatechange = handleResponse;
    http.send(null);
}


//--- Invia richiesta per controllo username
function sndReqUsername(action, username) {
	document.getElementById("lblusername").className =  "bform_conferma";
	document.getElementById("lblusername").innerHTML =  "<img src='immagini/preload/loader_333.gif' alt='loader' /> Attendere prego...";
            	
    http.open('get', '_request.php?action='+action+'&username='+username);
    http.onreadystatechange = handleResponse;
    http.send(null);
}



//--- Crea richiesta Ajax
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();


function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');

            switch( update[0]){
            	
            	case "lblemail":
            		
            		var oDiv	= document.getElementById(update[0]);
            		var oError	= document.getElementById("email_error");
            	
            		if(update[1] == 1 && update[4] == "emailnontrovata"){
            			//var oAction	= document.getElementById("action");
            			oDiv.className =  "bform_conferma";
            			oDiv.innerHTML = "L'indirizzo sembra corretto. Ok.";
            			oError.value	= 1;
            			
            		} else if(update[4] == "emailtrovata") {
            			oDiv.className =  "bform_errore";
            			oDiv.innerHTML = "L'indirizzo risulta gi&agrave; iscritto al sito.";
            			oError.value	= 0;
            			
            		} else if(update[4] == "emailnoncorretta") {
            			oDiv.className =  "bform_errore";
            			oDiv.innerHTML = "L'indirizzo non sembra essere corretto.";
            			oError.value	= 0;
            		}
            		
            		break;
            		
            		
            	case "lblusername":
            		
            		var oDiv	= document.getElementById(update[0]);
            		var oError	= document.getElementById("username_error");
            		
            		if(update[1] == 1){
            			oDiv.className 	= "bform_conferma";
            			oDiv.innerHTML 	= "La username &egrave; unica. Ok.";
            			oError.value	= 1;
            		} else {
            			oDiv.className = "bform_errore";
            			oDiv.innerHTML = "La username gi&agrave; esiste. Sceglierne un'altra.";
            			oError.value	= 0;
            		}
            		
            		break;
            		
            		
            	case "lblcodice_fiscale":
            		
            		var oDiv	= document.getElementById(update[0]);
            		var oError	= document.getElementById("codice_fiscale_error");
            		
            		if(update[1] == 1){
            			oDiv.className 	= "bform_conferma";
            			oDiv.innerHTML 	= "Il codice inserito &egrave; unico. Ok.";
            			oError.value	= 1;
            		} else {
            			oDiv.className = "bform_errore";
            			oDiv.innerHTML = "Il codice inserito risulta gi&agrave; iscritto al sito.";
            			oError.value	= 0;
            		}
            		
            		break;
            		

            	default:
            		//alert(response);
            		break;

            }

        }
    }
}
