function isNumber(data)
{
	var numStr="0123456789";
	var thisChar;
	var ret=true;
	
	if(data.length==0) return ret;
	
	for(var i=0; i<data.length; i++)
	{
		thisChar=data.substring(i, i+1);
		if(numStr.indexOf(thisChar)==-1)
		{
			ret=false;
			break;
		}
	}
	
	return ret;
}

function isTestoValidoOld(nomeCampo, valore)
{
	var okChar="\\1234567890\'QWERTYUIOPASDFGHJKLZXCVBNMìqwertyuiopè+asdfghjklòàùzxcvbnm.-_|!\"£$%&/()=?^é*ç°§#,;: @";
	for(var j=0; j<valore.length; j++)
	{
		thisChar=valore.substring(j, j+1);
		if(okChar.indexOf(thisChar)==-1)
			return ("Il campo '" + nomeCampo + "' ha un contenuto non corretto: il carattere '"+thisChar+"' non è consentito");
	}		
	return "ok";
}

function isTestoValido(nomeCampo, valore)
{
	var errChar="<>";
	for(var j=0; j<valore.length; j++)
	{
		thisChar=valore.substring(j, j+1);
		if(errChar.indexOf(thisChar)!=-1)
			return ("Il campo '" + nomeCampo + "' ha un contenuto non corretto: il carattere '"+thisChar+"' non è consentito");
	}		
	return "ok";
}

function emailValida (mail)
	{
		var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
		return email_reg_exp.test(mail);
	}

function controllaMessaggio()
	{
		var retcn=true;
		if (document.forms[0].messaggio.value.length > 1500)
			{	
				alert("Il testo del messaggio non deve superare i 1500 caratteri");
				retcn=false;
			}
		return retcn;			
	}
	
function controllaMail()
	{
		var retcn=true;
		if (!emailValida(document.forms[0].email.value))
			{	
				alert("E-mail non valida");
				retcn=false;
			}
		return retcn;			
	}

function controllaNumerici()
{
	var retcn=true;
	if(document.forms[0].cap.value!="" && !isNumber(document.forms[0].cap.value))
	{
		alert("Il campo 'Cap' dev'essere numerico");
		retcn=false;
	}
	if(document.forms[0].tel.value!="" && !isNumber(document.forms[0].tel.value))
	{
		alert("Il campo 'Tel' dev'essere numerico");
		retcn=false;
	}
	if(document.forms[0].fax.value!="" && !isNumber(document.forms[0].fax.value))
	{
		alert("Il campo 'Fax' dev'essere numerico");
		retcn=false;
	}
	return retcn;
}

function controllaTesti()
{
	var retcn=true;
	var msg="";
	
	if((msg = isTestoValido("Azienda", document.forms[0].azienda.value))!="ok")
	{
		alert(msg);
		retcn=false;
	}
	else if((msg = isTestoValido("Nome", document.forms[0].nome.value))!="ok")
	{
		alert(msg);
		retcn=false;
	}
	else if((msg = isTestoValido("Cognome", document.forms[0].cognome.value))!="ok")
	{
		alert(msg);
		retcn=false;
	}
	else if((msg = isTestoValido("Indirizzo", document.forms[0].indirizzo.value))!="ok")	
	{
		alert(msg);
		retcn=false;
	}
	else if((msg = isTestoValido("Messaggio", document.forms[0].messaggio.value))!="ok")	
	{
		alert(msg);
		retcn=false;
	}	

	return retcn;
}

function controllaObbligatori()
{
	var retcl=true;
/*
	if(document.forms[0].nome.value.length==0)
	{
		alert("Il campo 'Nome' è obbligatorio");
		retcl=false;
	}
	else if(document.forms[0].cognome.value.length==0)
	{
		alert("Il campo 'Cognome' è obbligatorio");
		retcl=false;
	}
	else*/	
	if(document.forms[0].email.value.length==0)
	{
		alert("Il campo 'e-mail' è obbligatorio");
		retcl=false;
	}
	else if(document.forms[0].messaggio.value.length==0)
	{
		alert("Il campo 'Messaggio' è obbligatorio");
		retcl=false;
	}
	return retcl;
}

function Controlla()
{
	var retc=true;

	retc=controllaObbligatori()
	if(!retc) return false;
	
	retc=controllaTesti();
	if(!retc) return false;

	retc=controllaNumerici();
	if(!retc) return false;

	retc=controllaMail();
	if(!retc) return false;	

	retc=controllaMessaggio();
	if(!retc) return false;
	
	//alert(retc)
		
	document.forms[0].submit();
}