/*
includere nel tag head
<script language="JavaScript" src="tools/valida.js"></script>
*/

function isBlank(s) {
	var c;
	for(var i = 0; i < s.length; i++) {
		c = s.charAt(i);
		if ((c != " ") && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function validaText(campo, nomeCampo, valoreCampo) {
	//alert(nomeCampo + " = " +  valoreCampo);
	if ((valoreCampo == null) || (valoreCampo == "") || isBlank(valoreCampo)) {
		alert("Il campo '" + nomeCampo  + "'\nnon puo' essere vuoto.");
		campo.focus();
		return false;
	} else {
		return true;
	}
}

function validaNumeroFloatRegExp(campo, nomeCampo, valoreCampo) {
	var regexprNumero = /[^0-9,\.]/;
	if ((isBlank(valoreCampo)) || (regexprNumero.test(valoreCampo))) {
		alert("Il campo '" + nomeCampo  + "'\ndeve essere un numero senza spazi.\nL'eventuale separatore decimale è il carattere ',' o '.'");
		campo.focus();
		return false;
	} else {
		return true;
	}
}

//formato data gg/mm/aaaa
function validaData(campo, nomeCampo, valoreCampo) {
	//alert(nomeCampo + " = " +  valoreCampo);
	var err = 0
	string = valoreCampo
	var valid = "0123456789/"
	var ok = "yes";
	var temp;
	//controllo che tutti i caratteri siano validi
	for (var i=0; i< string.length; i++) {
		temp = "" + string.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") err = 1;
	}
	//controllo la lunghezza della stringa (10=gg/mm/aaaa)
	if (string.length != 10) err=1
	giorno = string.substring(0, 2) // giorno
	c = string.substring(2, 3)// '/'
	mese = string.substring(3, 5) // mese
	e = string.substring(5, 6)// '/'
	anno = string.substring(6, 10) // anno
	if (mese<1 || mese>12) err = 1
	if (c != '/') err = 1
	if (giorno<1 || giorno>31) err = 1
	if (e != '/') err = 1
	//if (anno<0 || anno>99) err = 1
	if (mese==4 || mese==6 || mese==9 || mese==11){
		if (giorno==31) err=1
	}
	if (mese==2){
	var g=parseInt(anno/4)
	if (isNaN(g)) {
		err=1
	}
	if (giorno>29) err=1
	if (giorno==29 && ((anno/4)!=parseInt(anno/4))) err=1
	}
	if (err==1) {
		alert("il campo '" + nomeCampo  + "'\ndeve essere una data nel formato gg/mm/aaaa");
		campo.value = valoreCampo; 
		campo.focus();
		return false;
	}
	else {
		return true;
   }
}

function validaCheckbox(campo, nomeCampo, valoreCampo) {
	//alert(nomeCampo + " = " +  valoreCampo);
	if (campo.checked) {
		return true;
	} else {
		alert("Il campo '" + nomeCampo  + "'\ndeve essere un spuntato.");
		campo.focus();
		return false;
	}
}

//non funziona!! (gli oggetti passati da form sono indefiniti...bisogna passarli dal controllo stesso)
function validaRadio(campo, nomeCampo, valoreCampo) {
	//alert(campo);
	//alert(nomeCampo + " = " +  valoreCampo);
	if ((valoreCampo == null) || (valoreCampo == "")) {
		alert("Valore errato : \nIndicare il sesso.");
		return false;
	} else {
		return true;
	}
}

//non funziona!! (per valori come '3 ' o '3a')
function validaNumero(campo, nomeCampo, valoreCampo) {
	if ((isBlank(valoreCampo)) || (isNaN(parseInt(valoreCampo)))) {
		alert("Il campo '" + nomeCampo  + "'\ndeve essere un numero.");
		campo.focus();
		return false;
	} else {
		return true;
	}
}