validazioniRicercaMOLCaricato = 1; //questa variabile serve per capire se questo file .js è stato caricato dal form.
numeroBanche = 40; //viene stampato dai formini
numeroMutui=400; //viene stampato dai formini
taegDa = "2,97%"; //viene stampato dai formini

function CheckCampiRicercaMOL(theForm) {
//se esistono i campi, li valida, eventualmente settando il focus sul campo se non passa la validazione.
//l'unica unità di misura utilizzata è l'euro, in cui le virgole non sono ammesse, e il punto è solo separatore delle migliaia.

	if(theForm.CodiceFinalita) {
		if(theForm.CodiceFinalita.options[theForm.CodiceFinalita.selectedIndex].value=='') {
			alert("Il campo \"Finalità del mutuo\" è obbligatorio ");
			theForm.CodiceFinalita.focus();
			return (false);
		}
	}

	if (theForm.CodiceTipoTasso) {
		if (theForm.CodiceTipoTasso.selectedIndex == 0) {
			alert("Il campo \"Tipo di tasso/prodotto\" è obbligatorio ");
			theForm.CodiceTipoTasso.focus();
			return (false);
		}
	}

	//validazione Valore Immobile
	if (theForm.Valore) {
		//verifico che non sia campo vuoto
		if (theForm.Valore.value=="")  {
			alert("Inserire il valore dell'immobile");
			theForm.Valore.focus();
			return (false);
		}
		//verifico che siano solo numeri o punti
		for (i = 0;  i < theForm.Valore.value.length;  i++) {
			if (!((theForm.Valore.value.charAt(i) >= '0' && theForm.Valore.value.charAt(i) <= '9') || theForm.Valore.value.charAt(i)=='.')) {
				alert('Attenzione! Inserire solo numeri interi, non usare caratteri nel campo "Valore Immobile"');
				theForm.Valore.focus();
				return (false);
			}
		}
		//verifico che non ci siano punti nei posti sbagliati (possono essere solo per separare le migliaia)
		j=1;
		lung = theForm.Valore.value.length;
		strTemp = "";
		for(i=lung-1;i>=0;i--) {
			if(theForm.Valore.value.charAt(i)=='.' && j%4!=0) {
				alert('Attenzione! Usare il punto solo per separare le migliaia, nel campo "Valore Immobile"');
				theForm.Valore.focus();
				return (false);
			}
			if(theForm.Valore.value.charAt(i)!='.') {
				strTemp = theForm.Valore.value.charAt(i) + strTemp;
			}
			j++;
		}
		//testo che il valore dell'immobile non sia inferiore a 3000 di euro.
		if (parseFloat(strTemp)<3000)  {
			alert("Attenzione! Il valore dell'immobile deve essere superiore a 3000 Euro.")
			theForm.Valore.focus();
			return (false);
		}
		//testo che il valore dell'immobile non sia superiore a 5 milioni di euro.
		if (parseFloat(strTemp)>5000000)  {
			alert("Attenzione! Il valore dell'immobile non può essere superiore a 5.000.000 di Euro.")
			theForm.Valore.focus();
			return (false);
		}
		//se ha passato tutte le validazioni, tolgo i punti perchè non servono.
		theForm.Valore.value = strTemp;		
	}

	//validazione Importo Mutuo
	if (theForm.ImportoMutuo) {
		//verifico che non sia campo vuoto
		if (theForm.ImportoMutuo.value=="")  {
			alert("Inserire l'importo del mutuo");
			theForm.ImportoMutuo.focus();
			return (false);
		}
		//verifico che siano solo numeri o punti
		for (i = 0;  i < theForm.ImportoMutuo.value.length;  i++) {
			if (!((theForm.ImportoMutuo.value.charAt(i) >= '0' && theForm.ImportoMutuo.value.charAt(i) <= '9') || theForm.ImportoMutuo.value.charAt(i)=='.')) {
				alert('Attenzione! Inserire solo numeri interi, non usare caratteri nel campo "Importo Mutuo"');
				theForm.ImportoMutuo.focus();
				return (false);
			}
		}
		//verifico che non ci siano punti nei posti sbagliati (possono essere solo per separare le migliaia)
		j=1;
		lung = theForm.ImportoMutuo.value.length;
		strTemp = "";
		for(i=lung-1;i>=0;i--) {
			if(theForm.ImportoMutuo.value.charAt(i)=='.' && j%4!=0) {
				alert('Attenzione! Usare il punto solo per separare le migliaia, nel campo "Importo Mutuo"');
				theForm.ImportoMutuo.focus();
				return (false);
			}
			if(theForm.ImportoMutuo.value.charAt(i)!='.') {
				strTemp = theForm.ImportoMutuo.value.charAt(i) + strTemp;
			}
			j++;
		}
		//testo che il ImportoMutuo dell'immobile non sia inferiore a 1000 euro.
		if (parseFloat(strTemp) < 1000)  {
			alert("Attenzione! Il valore l'importo del mutuo non può essere inferiore a 1.000 Euro.")
			theForm.ImportoMutuo.focus();
			return (false);
		}
		//testo che il ImportoMutuo dell'immobile non sia superiore a 3 milioni di euro.
		if (parseFloat(strTemp)>3000000)  {
			alert("Attenzione! Il valore l'importo del mutuo non può essere superiore a 3.000.000 di Euro.")
			theForm.ImportoMutuo.focus();
			return (false);
		}
		//se ha passato tutte le validazioni, tolgo i punti perchè non servono.
		theForm.ImportoMutuo.value = strTemp;		
	}

	if (theForm.ImportoMutuo && theForm.Valore) {
		if (parseFloat(theForm.Valore.value)<parseFloat(theForm.ImportoMutuo.value))  {
			alert("Attenzione! L'importo del mutuo non può essere superiore al valore dell'immobile");
			theForm.ImportoMutuo.focus();
			return (false);
		}
	}

	if (theForm.CodiceDurata) {
		if (theForm.CodiceDurata.selectedIndex == 0) {
			alert("Il campo \"Durata\" è obbligatorio ");
			theForm.CodiceDurata.focus();
			return (false);
		}
	}

	if (theForm.CodiceFrequenza) {
		if (theForm.CodiceFrequenza.selectedIndex == 0) {
			alert("Il campo \"Frequenza rate\" è obbligatorio ");
			theForm.CodiceFrequenza.focus();
			return (false);
		}
	}

	if (theForm.Eta) {
		//età composta solo da cifre
		for (i = 0;  i < theForm.Eta.value.length;  i++) {
			if (theForm.Eta.value.charAt(i) < '0' || theForm.Eta.value.charAt(i) > '9') {
				alert("Attenzione! L'età del richiedente non è stata inserita correttamente");
				theForm.Eta.focus();
				return (false);
			}
		}
		//da 18 a 75 anni ammessi.
		if ((theForm.Eta.value<18)||(theForm.Eta.value>75)) {
			alert("L'età del richiedente deve essere compresa tra 18 e 75 anni");
			theForm.Eta.focus();
			return (false);
		}
	}

	if (theForm.CodCategoriaReddituale) {
		if (theForm.CodCategoriaReddituale.selectedIndex == 0) {
			alert("Il campo \"Categoria Reddituale\" è obbligatorio ");
			theForm.CodCategoriaReddituale.focus();
			return (false);
		}
	}

	//validazione Reddito
	if (theForm.Reddito) {
		//verifico che non sia campo vuoto
		if (theForm.Reddito.value=="")  {
			alert("Inserire il reddito mensile netto totale dei richiedenti");
			theForm.Reddito.focus();
			return (false);
		}
		//verifico che siano solo numeri o punti
		for (i = 0;  i < theForm.Reddito.value.length;  i++) {
			if (!((theForm.Reddito.value.charAt(i) >= '0' && theForm.Reddito.value.charAt(i) <= '9') || theForm.Reddito.value.charAt(i)=='.')) {
				alert('Attenzione! Inserire solo numeri interi, non usare caratteri nel campo "Reddito"');
				theForm.Reddito.focus();
				return (false);
			}
		}
		//verifico che non ci siano punti nei posti sbagliati (possono essere solo per separare le migliaia)
		j=1;
		lung = theForm.Reddito.value.length;
		strTemp = "";
		for(i=lung-1;i>=0;i--) {
			if(theForm.Reddito.value.charAt(i)=='.' && j%4!=0) {
				alert('Attenzione! Usare il punto solo per separare le migliaia, nel campo "Reddito"');
				theForm.Reddito.focus();
				return (false);
			}
			if(theForm.Reddito.value.charAt(i)!='.') {
				strTemp = theForm.Reddito.value.charAt(i) + strTemp;
			}
			j++;
		}
		//se ha passato tutte le validazioni, tolgo i punti perchè non servono.
		theForm.Reddito.value = strTemp;		
	}

	if (theForm.CodiceResidenza) {
		if (theForm.CodiceResidenza.selectedIndex == 0) {
			alert("Il campo \"Provincia di residenza\" è obbligatorio ");
			theForm.CodiceResidenza.focus();
			return (false);
		}
	}

  	if (theForm.CodiceLocalita) {
		if (theForm.CodiceLocalita.selectedIndex == 0) {
			alert("Il campo \"Provincia dell'immobile\" è obbligatorio ");
			theForm.CodiceLocalita.focus();
			return (false);
		}
	}

	return (true);
}


