// -----------------------------------
function dimensione(campo, nome, dmin, dmax) {
	if (campo.value.length < dmin){
		alert("Inserire almeno "+dmin+" caratteri nel campo \""+nome+"\".");
		campo.focus();
		return (false);
	}
	if (campo.value.length > dmax) {
		alert("Inserire al massimo "+dmax+" caratteri nel campo \""+nome+"\".");
		campo.focus();
		return (false);
	}
	return (true);
}
// -----------------------------------
function intero(campo, nome) {
	if ( caratteri("0123456789",campo,nome) == "" ) {
		alert("Inserire solo cifre nel campo \""+nome+"\".");
		campo.focus();
		return (false);
	}
	return (true);
}
// -----------------------------------
function valuta(campo, nome) {
	var checkStr = campo.value;
	if (checkStr == "") return (true);
	if ( caratteri("0123456789,",campo,nome) == "" ) {
		alert("Inserire solo cifre e l'eventuale virgola decimale nel campo \""+nome+"\".");
		campo.focus();
		return (false);
	}
	var punti = 0;
	for (i = 0;  i < checkStr.length;  i++)
		if ( checkStr.charAt(i) == '.' ) {++punti;}
	if (punti > 1) {
		alert("Inserire un solo punto decimale nel campo \""+nome+"\".");
		campo.focus();
		return (false);
	}
	return (true);
}
// -----------------------------------
function data(campo, nome) {
	var sdata = caratteri("0123456789/",campo,nome);
  	var iGiorno;
  	var iMese;
  	var iAnno;
  	if ( sdata == "" ) return (true);
	if ( campo.value.charAt(2)!='/' || campo.value.charAt(5)!='/' ) {
		alert("Inserire nel campo \""+nome+"\" una data nel formato \"DD/MM/AAAA\".");
		campo.focus();
		return (false);
	}
	//alert("sdata="+sdata);
  	if (sdata.charAt(0)=="0")
  		iGiorno = parseInt(sdata.substring(1,3));
  	else
  		iGiorno = parseInt(sdata.substring(0,3));  	
  	if (sdata.charAt(3)=="0")
	  	iMese = parseInt(sdata.substring(4,5));
	else
	  	iMese = parseInt(sdata.substring(3,5));
  	iAnno = parseInt(sdata.substring(6,10));
	//alert("g="+iGiorno+",m="+iMese+",a="+iAnno);
	if (iGiorno<1 || iGiorno>31 || iMese<1 || iMese>12 || iAnno<1800 || iAnno>2100) {
		alert("Formato data non valido nel campo \""+nome+"\".");
		campo.focus();
		return (false);
	}
	return (true);
}
// -----------------------------------
function dataopz(campo,nome) {
	if ( !dimensione(campo,nome,0,10) ) return (false);
	if ( !data(campo,nome) ) return (false);
	return (true);
}
// -----------------------------------
function selezionelista(campo, nome, escludiprima) {
	if ( campo.selectedIndex < 0 || (escludiprima && campo.selectedIndex == 0) ) {
		alert("Selezionare una delle opzioni di \""+nome+"\".");
		campo.focus();
		return (false);
	}
	return (true);
}

// -----------------------------------
function NoChar(chBad, campo, nome) {
	var checkStr = campo.value;
	for (i = 0;  i < checkStr.length;  i++)
		if ( checkStr.charAt(i) == chBad ) {
			alert("Caratteri non ammessi in \""+nome+"\".");
			campo.focus();
			return (false);
		}
	return (true);
}
// -----------------------------------
function NoSpazi(campo, nome) {
	var checkStr = campo.value;
	for (i = 0;  i < checkStr.length;  i++)
		if ( checkStr.charAt(i) == ' ' ) {
			alert("Non sono ammessi spazi in \""+nome+"\".");
			campo.focus();
			return (false);
		}
	return (true);
}

// -----------------------------------
function caratteri(checkOK, campo, nome) {
	var carOK = "";
	var checkStr = campo.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j)) {
				break;
			}
		if (j == checkOK.length){
			allValid = false;
			carOK = "";
			break;
		}
		carOK += ch;
	}
	return (carOK);
}
