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

//------------------------- Controlla tutti i campi
function fCheckObbligatori(){
	var blnReturn	= true;
	
	if(!fCheckNominativo()) {
		blnReturn	= false;
		alert("Inserisci il campo: Nominativo.");
		document.getElementById("nominativo").focus();
		
	} else if(!fCheckCitta()) {
		blnReturn	= false;
		alert("Inserisci il campo: Citta'");
		document.getElementById("citta").focus();
		
	} else if(!fCheckEmailSubmit()) {
		blnReturn	= false;
		alert("Verifica il campo: Email.");
		document.getElementById("email").focus();
		
	} else if(!fCheckRichiesta()) {
		blnReturn	= false;
		alert("Verifica il campo: Richiesta.");
		document.getElementById("richiesta").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 nominativo
function fCheckNominativo(){
	var blnReturn	= true;
	if( document.getElementById("nominativo") != null && document.getElementById("nominativo").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblnominativo").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblnominativo").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 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 richiesta
function fCheckRichiesta(){
	var blnReturn	= true;
	if( document.getElementById("richiesta") != null && document.getElementById("richiesta").value == ""){ 
		blnReturn	= false;
		document.getElementById("lblrichiesta").innerHTML = "Campo obbligatorio.";
	} else {
		document.getElementById("lblrichiesta").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;
}







//--- 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 email
function sndReqEmail(action, email) {
	document.getElementById("lblemail").className =  "bform_conferma";
	document.getElementById("lblemail").innerHTML =  "<img src='immagini/preload/loader_bform.gif' alt='loader' /> Attendere prego...";
            	
    http.open('get', '_request.php?action='+action+'&email='+email);
    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[4] != "emailnoncorretta"){
            			//var oAction	= document.getElementById("action");
            			oDiv.className =  "bform_conferma";
            			oDiv.innerHTML = "L'indirizzo sembra corretto. Ok.";
            			oError.value	= 1;
            			
            		} else {
            			oDiv.className =  "bform_errore";
            			oDiv.innerHTML = "L'indirizzo non sembra essere corretto.";
            			oError.value	= 0;
            		}
            		
            		break;
            		

            	default:
            		//alert(response);
            		break;

            }

        }
    }
}
