
//=====================================================
//  FUNZIONE CHE CONTROLLA IL CAMPO EMAIL DI UN FORM
//=====================================================			
function isEmailOk(data) {
  if (data.indexOf(".") != -1) {
      if (data.indexOf("@") != -1)  { 
	      return true;
		  }
		}
	else return false;	  
}

//=============================================================
//  FUNZIONE CHE CONTROLLA IL RIEMPIMENTO DEL CAMPO DI UN FORM
//=============================================================		
function isSpace(data) {
	var contr2=0;
   	for(var i=0; i < data.length; i++){
		if(data.substring(i, i+1) != " ")
	   		contr2=1; 
	}   
	if  (contr2==1)   
		return(false);  
return(true);
}

//=============================================================
//  FUNZIONE CHE CONTROLLA I CAMPI DI UN FORM ( FormN )
//=============================================================			
function checkRequiredFields(lang, tipo) {
		
	var message = new Array();
	var errorLevel;
	
	message[0]  = "";
	
	switch (lang) {
		case "it":
	    	message[1] = "Inserisci il tuo Nome.";
			message[2] = "Inserisci il tuo Cognome";
			message[3] = "Inserisci il tuo Telefono";
			message[4] = "Inserisci il tuo Indirizzo Email";
			message[5] = "Per poter gestire la vostra richiesta di informazioni è necessario il consenso al trattamento dei dati per i fini elencati nell'informativa dal punto A1 al punto A4.";
			message[6] = "Seleziona una delle due opzioni relative al consenso per il trattamento dei dati secondoi fini elencati nell'informativa al punto A5.";
			break; 
	   	case "uk":
	    	message[1] = "Insert your Name";
			message[2] = "Insert your Second Name";
			message[3] = "Insert your Phone Number";
			message[4] = "Insert your Email address.";
			message[5] = "Per poter gestire la vostra richiesta di informazioni è necessario il consenso al trattamento dei dati per i fini elencati nell'informativa dal punto A1 al punto A4.";
			message[6] = "Seleziona una delle due opzioni relative al consenso per il trattamento dei dati secondoi fini elencati nell'informativa al punto A5.";
			break; 
	   	default :
	    	message[1] = "Insert your Name";
			message[2] = "Insert your Second Name";
			message[3] = "Insert your Phone Number";
			message[4] = "Insert your Email Address.";
			message[5] = "Per poter gestire la vostra richiesta di informazioni è necessario il consenso al trattamento dei dati per i fini elencati nell'informativa dal punto A1 al punto A4.";
			message[6] = "Seleziona una delle due opzioni relative al consenso per il trattamento dei dati secondoi fini elencati nell'informativa al punto A5.";
			break; 
	} 
		
	errorLevel = 0;  
	
	if ((errorLevel == 0) && ((document.FormN.nome.value.length == 0) || isSpace(document.FormN.nome.value))) {
		errorLevel = 1;	
		document.FormN.nome.focus();
	}
	if ((errorLevel == 0) && ((document.FormN.cognome.value.length == 0) || isSpace(document.FormN.cognome.value))) {
		errorLevel = 2;	
		document.FormN.cognome.focus();
	}
	if ((errorLevel == 0) && ((document.FormN.telefono.value.length == 0) || isSpace(document.FormN.telefono.value))) {
		errorLevel = 3;	
		document.FormN.telefono.focus();
	}
	if ((errorLevel == 0) && ((document.FormN.email.value.length == 0) || isSpace(document.FormN.email.value))) {
		errorLevel = 4;	
		document.FormN.email.focus();
	}
    if ((errorLevel == 0) && (!(isEmailOk(document.FormN.email.value)))) {
		errorLevel = 4;
		document.FormN.email.focus();
	}
	
	if ((errorLevel == 0) && (!(document.FormN.consenso_privacy_1[0].checked))) {
		errorLevel = 5;
		document.FormN.consenso_privacy_1[0].focus();
	}
	
	if ((errorLevel == 0) && (!(document.FormN.consenso_privacy_2[0].checked)) && (!(document.FormN.consenso_privacy_2[1].checked))) {
		errorLevel = 6;
		document.FormN.consenso_privacy_2[0].focus();
	}
	
	if (errorLevel > 0 ) { 
		alert(message[errorLevel]); 
		
		if (tipo == 'submit') {
			return false;
		}
	} else{
		if (tipo == 'link') {
			document.FormN.submit();
		} else{
			return true;
		}
	}
}
