
//LA FUNZIONE VA RICHIAMATA NEL TAG form IN QUESTO MODO:
//<form name="modulo" action="sendmail.asp" method="post" onsubmit="return controllo('modulo', 'nome,email,msg'); return false;">
//INDICANDO NELL' onsubmit= IL NOME DEL FORM STESSO E IL NOME DEI CAMPI DA CONTROLLARE SEPARATI DA VIRGOLA

function controllo(formnome, arr) {
	// Controllo dei campi form (text-textarea-radio-select-checkbox)
	 
	var col_array = arr.split(",");

	var part_num=0;
	
	while (part_num < col_array.length)
	{
		var nomecampo = col_array[part_num];
		//
		var tipocampo = document.forms[formnome].elements[nomecampo].type; 
				
// ############################################################

			// Campo testo
			if (tipocampo ==  'text' || tipocampo == 'password') {
			
				// Se campo testo email
				var valorecampo = document.forms[formnome].elements[nomecampo].value;
				//alert(valorecampo.length);
				if (nomecampo.indexOf("email") != -1 && (valorecampo.indexOf("@") == -1 || valorecampo.indexOf(".") == -1 || valorecampo < 8)) {
					alert('Formato email non valido');
					return false;
				
				} else {
					// Se campo testo non email
					if (!valorecampo) {
						alert('Compila il campo ' + nomecampo);
						return false;
					}
				}
						
			}
		

// ############################################################
				
		// Campo textarea
		else if (tipocampo ==  'textarea') {
				
				// Se campo textarea
				var valorecampo = document.forms[formnome].elements[nomecampo].value;
				if (!valorecampo) {
					alert('Compila il campo ' + nomecampo);
					return false;
				}
		
		}

// ############################################################
		
		// Campo checkbox
		else if (tipocampo ==  'checkbox') {
				// Se campo checkbox
				var valorecampo = document.forms[formnome].elements[nomecampo].checked;
				if (!valorecampo) {
					alert('Prima di poter inviare la mail dare il \n consenso al trattamento dei propri dati personali');
					return false;
				}
						
		}

	
// ############################################################
		
		// Campo select
		else if (tipocampo ==  'select-one') {
			var valorecampo = document.forms[formnome].elements[nomecampo].value;
			if(!valorecampo){
				alert('Compila il campo ' + nomecampo);
				return false;
			}		
		}

// ############################################################
		
		// Campo radio
		//else {
		//	var valorecampo = document.modulo.sesso.value;
		//	if (!valorecampo) {
		//		alert('Campo ' + nomecampo + ' non selezionato');
		//	}
		
		//}

// ############################################################		


		part_num+=1;
	}
}


// Controllo lunghezza TEXTAREA
		// Campo NOTE
		function countChars(){
			
			var str = document.modulo_1.note.value;
			var strLong = document.modulo_1.note.value.length;
			if (strLong > 100) {
				alert('Il campo NOTE puo\' contenere un massimo di 100 caratteri');
				var strNew = str.substr(0,100);
				document.modulo_1.note.value = strNew;
				document.modulo_1.note.focus();
				
				return false;
			}
		}
		// 75 CARATTERI MAX - attrezzature	particolari necessarie
		function countChars2(){
		
		var str = document.modulo_1.attrezzature.value;
		var strLong = document.modulo_1.attrezzature.value.length;
		if (strLong > 75) {
			alert('Il campo ATTREZZATURE PARTICOLARI NECESSARIE puo\' contenere un massimo di 75 caratteri');
			var strNew = str.substr(0,75);
			document.modulo_1.attrezzature.value = strNew;
			document.modulo_1.attrezzature.focus();
			
			return false;
			}
		}
		// Note form2
		function countChars3(){
		
		var str = document.modulo_2.note.value;
		var strLong = document.modulo_2.note.value.length;
		if (strLong > 300) {
			alert('Il campo NOTE puo\' contenere un massimo di 300 caratteri');
			var strNew = str.substr(0,300);
			document.modulo_2.note.value = strNew;
			document.modulo_2.note.focus();
			return false;
			}
		}

		function VerificaCampo(campo)
		{
         
         var cifre= "0123456789.";
         var verifica= campo.value;
         var allValid = true;

         var allNum = "";
         for ( i = 0; i < verifica.length; i++ )
         {
                ch = verifica.charAt( i );
                for ( j = 0; j < cifre.length; j++ )
                 if ( ch == cifre.charAt( j ))
                        break;
                if ( j == cifre.length )
                {
                 allValid = false;
                 break;
                }
                allNum += ch;
         }
         if (!allValid)
         {
                alert( "Sono presenti dei caratteri alfanumerici, sono consentite solamente cifre numeriche" );
                campo.value='';
				campo.focus();
                return ( false );
         }
        return ( true );
		}
