function ControllaPIVA(iva)
{
	if( iva == '' )  return 'Il campo PARTITA IVA è obbligatorio';
	if( iva.length != 11 )
		return "La lunghezza della partita IVA non è\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( iva.charAt(i) ) == -1 )
			return "La partita IVA contiene un carattere non valido `" +
				iva.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += iva.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( iva.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != iva.charCodeAt(10) - '0'.charCodeAt(0) )
		return "La partita IVA non è valida:\n" +
			"il codice di controllo non corrisponde.\n";
	return '';
}

function conta() {
var contaSelezionati = 0;
var x=document.getElementById("localita");
for (indice=0;indice<x.length;indice++)
{
if (x.options[indice].selected) contaSelezionati++;
}
return contaSelezionati;
}

function conts() {
var contaSelezionati = 0;
var x=document.getElementById("tipologie");
for (indice=0;indice<x.length;indice++)
{
if (x.options[indice].selected) contaSelezionati++;
}
return contaSelezionati;
}

function contb() {
			var blnChecked = false;
			var myImages = document.modulo.elements['lingue'];
			var error = 0;
			
				//alert(myImages.length);
				for (intCounter = 0; intCounter < document.forms['modulo'].elements['lingue'].length; intCounter++) {
					//alert(document.modulo.elements['lingue[]'][intCounter].value);
					if(document.modulo.elements['lingue'][intCounter].checked) {
						error++;
					}
				}
					return error;
			}


function conf_invio() {
		
     var user  	   = document.modulo.user.value;
     var nome      = document.modulo.nome.value;
     var iva 	   = document.modulo.iva.value;
     var indirizzo = document.modulo.indirizzo.value;
     var telefono  = document.modulo.telefono.value;
     var fax	   = document.modulo.fax.value;
     var email     = document.modulo.email.value;
     var ubicazione= document.modulo.ubicazione.value;
     
     var mail_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
     err = ControllaPIVA(iva);
     num_lin = contb();
     num_loc = conta();
     num_tip = conts();

   if ((user == "") || (user == "undefined") || (user.length<"6")) {
           alert("Il campo USER deve essere almeno di 6 caratteri.");
           document.modulo.user.focus();
           return false;        }
     
	else if ((nome == "") || (nome == "undefined")) {
           alert("Il campo RAGIONE SOCIALE è obbligatorio.");
           document.modulo.nome.focus();           
           return false;
        }
	else if ( err > '' ) {
	   alert(err);
	   document.modulo.iva.focus();
	   return false;
	}
        else if ((indirizzo == "") || (indirizzo == "undefined")) {
           alert("Il campo INDIRIZZO è obbligatorio.");
           document.modulo.indirizzo.focus();           
           return false;
        }
        else if ((telefono == "") || (telefono == "undefined")) {
           alert("Il campo TELEFONO è obbligatorio.");
           document.modulo.telefono.focus();           
           return false;
        }
        else if ((fax == "") || (fax == "undefined")) {
           alert("Il campo FAX è obbligatorio.");
           document.modulo.fax.focus();           
           return false;
        }
        else if (!mail_reg_exp.test(email) || (email == "") || (email == "undefined")) {
           alert("Inserire un indirizzo E-mail corretto.");
           document.modulo.email.focus();           
           return false;
        }
        else if ((ubicazione == "") || (ubicazione == "undefined")) {
           alert("Il campo UBICAZIONE è obbligatorio.");
           document.modulo.ubicazione.focus();           
           return false;
        }
        else if (num_lin == "0") {
           alert("Selezionare almeno 1 lingua.");   
           return false;
        } 
        else if (num_loc == "0") {
          alert("Selezionare almeno 1 localita.");        
           return false;
        }
        else if (num_tip == "0") {
          alert("Selezionare almeno 1 tipologia.");         
           return false;
        }
        //INVIA IL MODULO
        else {
           document.modulo.submit();
        }
     
  }
