/*
Checkfields.js

Roberto Ianniello
ver 1.2, 10/2/2000

*/

function checkNotEmpty(fvalue) {
notempty=0;
if (fvalue) {
	for (idx=0; idx<fvalue.length; idx++) {
		if (fvalue.charAt(idx)!=" ") {
			notempty=1;
			break;
		}
	}
}
return notempty;
}


function checkIsNumberInt(fvalue) {
isnum=1;
if (fvalue) {
	for (idx=0; idx<fvalue.length; idx++) {
		car=fvalue.charAt(idx);
		if ((car!=" ") && ((car<"0") || (car>"9"))) {
			isnum=0;
			break;
		}
	}
} else {
	isnum=0;
}
return isnum;
}

function checkIsEmail(fvalue) {
  isnum=1;
  if (fvalue) { 
      	idx=fvalue.indexOf('@', 0);
	idx2= (idx >0) ? fvalue.indexOf('@', idx+1) : -1;
	isnum = ! ( (idx <= 0) || (idx >= fvalue.length-1) || (idx2 != -1));
  } else {
	isnum=0;
  }
return isnum;
}




function checkGuestBookForm() {
errors='';
nomevuoto = "Il campo nome è vuoto\n";
cognomevuoto = "Il campo cognome è vuoto\n";
telefonovuoto = "Il campo telefono è vuoto\n";
telefonoerrato = "Il campo telefono non è numerico\n";
attenzione = "Attenzione:\n";

isvalid=true;
if (!checkNotEmpty(document.librovisitatori.nome.value)) {
	errors=errors + nomevuoto;
	isvalid=false;
}
if (!checkNotEmpty(document.librovisitatori.cognome.value)) {
	errors=errors + cognomevuoto;
	isvalid=false;
}
/*
if (!checkNotEmpty(document.librovisitatori.telefono.value)) {
	errors=errors + telefonovuoto;
	isvalid=false;
} else {
	if (!checkIsNumberInt(document.librovisitatori.telefono.value)) {
		errors=errors + telefonoerrato;
		isvalid=0;
	}
}
*/
if (!isvalid) {
	alert(attenzione+errors);
}
 return isvalid;
}




function checkInfoForm() {
errors='';
nomevuoto = "Il campo nome è vuoto\n";
cognomevuoto = "Il campo cognome è vuoto\n";
messaggiovuoto = "\nLa richiesta non è stata scritta.\n";
telefonovuoto = "Deve essere indicato almeno un recapito (telefono, fax o email)\n";
telefonoerrato = "Il campo telefono non è numerico\n";
faxerrato = "Il campo fax non è numerico\n";
emailerrata="Indirizzo e-mail errato\n";
attenzione = "Attenzione:\n";

isvalid=true;
istelefono=false;
isfax=false;
isemail=false;

if (!checkNotEmpty(document.info.nome.value)) {
	errors=errors + nomevuoto;
	isvalid=false;
}
if (!checkNotEmpty(document.info.cognome.value)) {
	errors=errors + cognomevuoto;
	isvalid=false;
}


if (!checkNotEmpty(document.info.telefono.value)) {
	istelefono=false;
} else {
	if (!checkIsNumberInt(document.info.telefono.value)) {
		errors=errors + telefonoerrato;
		isvalid=false;
	}
	istelefono=true;
}


if (!checkNotEmpty(document.info.fax.value)) {
	isfax=false;
} else {
	if (!checkIsNumberInt(document.info.fax.value)) {
		errors=errors + faxerrato;
		isvalid=false;
	}
	isfax=true;
}


if (!checkNotEmpty(document.info.email.value)) {
	isemail=0;
} else {
	if (!checkIsEmail(document.info.email.value)) {
		errors=errors + emailerrata;
		isvalid=false;
	}
	isemail=1;
}

if (!istelefono && !isfax && !isemail) {
	errors=errors + telefonovuoto;
	isvalid=false;
}
if (!checkNotEmpty(document.info.richiesta.value)) {
	errors=errors + messaggiovuoto;
	isvalid=false;
}
if (!isvalid) {
	alert(attenzione+errors);
}
 return isvalid;
}
