function somma(form,id,target)
{
var s=0;
for (i=0;i<form.elements.length;i++)
 {if (form.elements[i].id.match(id))
   s=s+Number(form.elements[i].value);

 }
target.value=s;

//chiama la routine specifica 
aggiorna();
}



function controlla(o)
{

for (i=0;i<o.elements.length;i++)
{
 if (o.elements[i].id.match("obbligo") && o.elements[i].value=="")
   {alert("Attenzione il campo "+o.elements[i].name+" è obbligatorio");
	     o.elements[i].focus();
	     o.elements[i].select();
		return false;
   
   }


//if (o.elements[i].id.match("numero")) alert(o.elements[i].id.search("numero"))
//controlla che ci siano n numeri
 if (o.elements[i].id.match("numero") && o.elements[i].value!="")
 {
 x=o.elements[i].id.substr(o.elements[i].id.search("numero")+6,2)
 n=Number(x)
 re=/\d/g;
if (n!="" && o.elements[i].value.search(re)!=-1)
 {
  if (o.elements[i].value.match(re).length!=n) 
  	{ alert("Hai commesso un errore nel digitare il campo "+o.elements[i].name+"\nnumero caratteri non valido");
	     o.elements[i].focus();
	     o.elements[i].select();
		return false;
	}
		
  }
  //controlla che siano tutti numeri
re=/\D/;
  if (re.exec(o.elements[i].value)) 
  	{ alert("Hai commesso un errore nel digitare il campo "+o.elements[i].name+"\nnon sono ammesse lettere");
	     o.elements[i].focus();
	     o.elements[i].select();
		return false;
		}
}


//controlla formato email
 if (o.elements[i].id.match("email") && o.elements[i].value!="")
 { re=/\w{1,}\@\w{1,}\.\w{2,}/;
  if (!re.exec(o.elements[i].value)) 
  	{ alert("Hai commesso un errore nel digitare l'email nel campo "+o.elements[i].name);
	     o.elements[i].focus();
	     o.elements[i].select();
		return false;
		}
 



 }

//controlla codice fiscale o nullo
 if (o.elements[i].id.match("cf"))
	{re=/\D\D\D\D\D\D\d\d\D\d\d\D\d\d\d\D/;
	  if (!re.exec(o.elements[i].value) && o.elements[i].value!="") 
	  	{ alert("Hai commesso un errore nel digitare il codice fiscale nel campo "+o.elements[i].name);
	     o.elements[i].focus();
	     o.elements[i].select();
		return false;
		}
	}


//controlla data in formato dd/dd/dd o nulla

 if (o.elements[i].id.match("data"))
  {re=/\d\d[/]\d\d[/]\d\d/;
   if (!re.exec(o.elements[i].value) && o.elements[i].value!="") 
      {alert("Hai commesso un errore nel digitare la data nel campo "+o.elements[i].name);
      o.elements[i].focus();
	  o.elements[i].select();
	  return false;
	  }
  
  
  }
 }


} 

function conteggia(obj,maxInput){
	
	if (obj.value.length > maxInput){
	    alert ("Attenzione: i caratteri a disposizione sono " + maxInput);
	    
		obj.value= obj.value.substring(0,maxInput);
		Disp = 0;
	}
	else{
		Disp = maxInput - obj.value.length;
	}
		
	document.form1 [obj.name+"contatore"].value = Disp;
}
