function ContaQuanti(pos,str,car,q) {
	// funzione ricorsiva che conta quante sottostringhe
	// sono contenute all'interno di una stringa
	// es1. ContaQuanti(0,"pippo","p",0) ritorna 3
	// es2. ContaQuanti(0,"pippo","z",0) ritorna 0
	// es3. ContaQuanti(0,"voglio l'erba voglio","voglio",0) ritorna 2
	// es4. ContaQuanti(0,"voglio l'erba voglio","ogli",0) ritorna 2
	var CQ = str.indexOf(car,pos);
	if (CQ == -1) {
		return(q);
	} else {
		return(ContaQuanti(CQ+1,str,car,q+1));
	}
}

function IsEmpty(field) {
	var OK = true;
	if (field.length > 0) {
		var i = 0;
		for (i=0; i<field.length; i++) {
			if (field.charAt(i) != " ") {
				OK = false;
				break;
			}
		}
	}
	return(OK);
}

function EmailOK(email) {
	var OK = true;
	if (IsEmpty(email)) {
		OK = false;
	} else {
		if (ContaQuanti(0,email,"@",0) == 1) {
			var pAt = email.indexOf("@");
			var pP = email.indexOf(".");
			if ((pP > 0) && (pP < (email.length - 2))) {
				var pPprec = -1;
				pP = 0;
				do
				{
					pP = email.indexOf(".",pP);
					if (pP == (-1)) {
						break;
					} else {
						if ((pP == pAt+1) || (pP == pAt-1) || (pPprec == (pP-1) || (pP > (email.length - 3)))) {
							OK = false;
							break;
						} else {
							pPprec = pP;
							pP += 1;
						}
					}
				}
				while (pP < email.length);
			} else {
				OK = false;
			}
		} else {
			OK = false;
		}
	}
	return(OK);
}

function check() {
	var conta;
	strErr = '';
	strErr2 = '';
	conta = 0;
	if (IsEmpty(document.InviaArticolo.Demail.value)) {
		strErr = strErr + '- e-mail del destinatario -\n';
		conta += 1;
	}
	if (IsEmpty(document.InviaArticolo.Mnome.value)) {
		strErr = strErr + '- Nome del mittente -\n';
		conta += 1;
	}
	if (IsEmpty(document.InviaArticolo.Memail.value)) {
		strErr = strErr + '- e-mail del mittente -\n';
		conta += 1;
	}
	if (conta > 1) {
		strErr = 'Compilare i campi: \n' + strErr;
	} else if (conta == 1) {
		strErr = 'Compilare il campo \n' + strErr;
	}

	if (strErr == '') {
		conta = 0;
		if (!EmailOK(document.InviaArticolo.Demail.value)) {
			strErr2 = strErr2 + '- e-mail del destinatario -\n';
			conta += 1;
		}
		if (!EmailOK(document.InviaArticolo.Memail.value)) {
			strErr2 = strErr2 + '- e-mail del mittente -\n';
			conta += 1;
		}
		if (conta > 1) {
			strErr2 = 'I campi: \n' + strErr2 + ' non sembrano essere correttamente compilati';
		} else if (conta == 1) {
			strErr2 = 'Il campo: \n' + strErr2 + ' non sembra essere correttamente compilato';
		}
	}
	strErr = strErr + strErr2;
	if (strErr != '') {
		alert(strErr);
		return(false);
	} else {
		return(true);
	}
}


