//VERIFICA CHE SIA INSERITO IL NUMERO DI CARATTERI RICHIESTO PER LA CASELLA DI TESTO
//ACCETTA IL PARAMETRO "numeroCaratteri" CHE DEFINISCE IL NUMERO DI CARATTERI DELL'AREA
//E "idTextArea", CHE E'L'DENTIFICATIVO PER EFFETTUARE LE OPERAZIONI DI VERIFICA
function verificaLunghezzaTextArea(numeroCaratteri, idTextArea)
	{
	contenutoTextArea = document.getElementById(idTextArea).value
	lunghezzaTextArea = document.getElementById(idTextArea).value.length
		if(lunghezzaTextArea >= numeroCaratteri - 1)
		{
			alert("Attenzione\nE'possibile inserire unicamente " + numeroCaratteri + " per la descrizione")
			document.getElementById(idTextArea).value = contenutoTextArea.substr(0, numeroCaratteri)
		}
	}
//


function verificaCheckbox(messaggioAlert)
{
	var almenoUnaCheck
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		typeField = document.forms[0].elements[i].type;
		if(typeField == "checkbox")
		{
			almenoUnaCheck = almenoUnaCheck || document.forms[0].elements[i].checked
		}
	}
		if (almenoUnaCheck == false)
		{
			alert(messaggioAlert) 
			return false;
		}
		else
		{
			return checkForm();
		}
}



function checkPhoneFax(phonePhaxNumber,i)
{
	if(!isNaN(phonePhaxNumber) == false)
	{
		alert("Attenzione, inserire un valore numerico per il telefono-fax\nPremendo `OK`, sarà evidenziato il campo contenente l'errore.")
		document.forms[0].elements[i].focus();
		return "error";
	}
}

function checkEmail(emailAddress,i)
{
	validRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,3}))$/
	strEmail = emailAddress;
	if (strEmail.search(validRegExp) == -1) 
	{
		alert("Attenzione, inserire un indirizzo eMail corretto\nPremendo `OK`, sarà evidenziato il campo contenente l`errore.");
		document.forms[0].elements[i].focus();
		return "error";
	} 
}

function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return "La lunghezza del codice fiscale non è\n"
		+"corretta: il codice fiscale dovrebbe essere lungo\n"
		+"esattamente 16 caratteri.\n";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.\nI caratteri validi sono le lettere e le cifre.\n";
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return "Il codice fiscale non è corretto:\n"+
			"il codice di controllo non corrisponde.\n";
	return "";
}


function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non è\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return "La partita IVA contiene un carattere non valido `" +
				pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return "La partita IVA non è valida:\n" +
			"il codice di controllo non corrisponde.\n";
	return '';
}   
   
function checkForm()
	{
	var i;
	var typeField;
	var dataName;
	var dataValue;
	var dataFocus;
	for(i = 0; i < document.forms[0].elements.length; i++){
			typeField = document.forms[0].elements[i].type;
			idField = document.forms[0].elements[i].id;
			dataName = document.forms[0].elements[i].name;
			dataValue = document.forms[0].elements[i].value;
			
			/*CANCELLAZIONE DEGLI SPAZI INUTILI NEI CAMPI*/
			if(dataName == "txtPrezzoAffittoSettimana")
			{
			document.forms[0].elements[i].value = document.forms[0].elements[i].value.replace(/\s+/g,"");  
			}
			/*FINE CANCELLAZIONE DEGLI SPAZI INUTILI NEI CAMPI*/

			
			if(typeField != 'hidden')
			{
				if(document.forms[0].elements[i].disabled != true)
				{
					dataFocus = document.forms[0].elements[i].focus();
				}
			}
			
			if(idField == "price" || idField == "noControl" || idField.substr(0,6) == "idData" || idField == "email partner")
			{
				if(idField == "price")
				{
					if(dataValue != "")
					{
						if(isNaN(dataValue))
						{
							document.forms[0].elements[i].style.background = "#66CCFF"
							alert("Attenzione, inserire un valore numerico\nPremendo 'OK', sarà evidenziato il campo contenente l'errore.")
							dataFocus;
							return false;
						}
					}
				}
							
							
				if(idField == "email partner" || idField == "email utente")
				{
					if(dataValue != "")
					{
						if(checkEmail(dataValue,i) == "error") return false;
					}
				}
			
				if(dataName == "txtFaxAzienda" && dataValue != "")
				{
					if(checkPhoneFax(dataValue,i) == "error") return false;
				}
				
				if(dataName == "txtCFPI" && dataValue != "")
				{
					if( dataValue.length == 16 )
						err = ControllaCF(dataValue);
					else if( dataValue.length == 11 )
						err = ControllaPIVA(dataValue);
					else
						err = "Il codice introdotto non è valido:\n\n" +
							"  - un codice fiscale deve essere lungo 16 caratteri;\n\n" +
							"  - una partita IVA deve essere lunga 11 caratteri.\n";
				
					if( err > '' )
					{
						alert("VALORE ERRATO\n\n" + err + "\nCorreggi e riprova!");
						return false;
					}
				}				
			}
			
			else{

				switch (typeField){
					
					case 'select-one':
						if(dataValue == ""){
							if(document.forms[0].elements[i].disabled != true)
							{
								alert("Selezionare un valore per il campo " + document.forms[0].elements[i].id)
								document.forms[0].elements[i].style.background = "#66CCFF"
								document.forms[0].elements[i].focus()
								return false;
							}
						}
					break;
					
					case 'text':
					case 'password':
					case 'textarea':
						dataValue = document.forms[0].elements[i].value;
						//VERIFICA DELLA PRESENZA DEI CAMPI OBBLIGATORI
						if(dataValue == "" || dataValue == " "){
							if(dataName == "dd1" || dataName == "mm1" || dataName == "yy1")
							{
								alert("Inserire un valore per il campo data arrivo")
								return false;
							}
							if(dataName == "dd2" || dataName == "mm2" || dataName == "yy2")
							{
								alert("Inserire un valore per il campo data partenza")
								return false;
							}
							alert("Inserire un valore per il campo " + document.forms[0].elements[i].id)
							document.forms[0].elements[i].style.background = "#66CCFF"
							document.forms[0].elements[i].value = ""
							document.forms[0].elements[i].focus()
							return false;
						}
						else
						{
							//VERIFICA DEI VALORI DEI CAMPI TESTO IN BASE ALLA TIPOLOGIA NEI CAMPI OBBLIGATORI								
							if(dataName == "txtBudgetAcquisto")
							{
								if(isNaN(dataValue)){
									alert("Attenzione, inserire un valore numerico\nPremendo 'OK', sarà evidenziato il campo contenente l'errore.")
									dataFocus;
									return false;
								}
							}
								
							if(dataName == "txtTelefonoAzienda")
							{
								if(checkPhoneFax(dataValue,i) == "error") return false;
							}
							if(dataName == "txtEmail" || dataName == "txtEmailAmministratore" || dataName == "txtEmailMittente")
							{
								if(checkEmail(dataValue,i) == "error") return false;
							}
													
							if(dataName == "txtCap")
							{
								if(dataValue.length < 5 || isNaN(dataValue))
								{
									alert("Attenzione, il CAP deve essere composto\nda 5 caratteri numerici.\nPremendo 'OK', sarà evidenziato il campo contenente l'errore.");
									document.forms[0].elements[i].focus();
									return false;
								}
							}						
						}
					break;
				}
			}
			i = i++
		}
}