//************************************
//      Controllo Partita IVA 
//************************************
function partitaIVA(sz_Codice)
   {
   var n_Val,n_Som1=0,n_Som2=0,lcv;
   if (sz_Codice.length!=11 || isNaN(parseFloat(sz_Codice)) || parseFloat(sz_Codice)<parseFloat(0))
      return false;
   
   for (lcv=0;lcv<9;lcv+=2)
   {
      n_Val=parseInt(sz_Codice.charAt(lcv));
      n_Som1+=n_Val;
      n_Val=parseInt(sz_Codice.charAt(lcv+1));
      n_Som1+=Math.floor(n_Val/5) + (n_Val<<1) % 10;
   }
   n_Som2 = 10 - (n_Som1 % 10);
   n_Val=parseInt(sz_Codice.charAt(10));
   if (n_Som2==n_Val)
      return true;
   return false;
   }
   
function isPIVA(what)
{
   if(what.length>0)
   {
	var i=new RegExp("[0-9]{11}");
	if(!i.test(what)) { return false; }   
   }
   return true;
}
function isCEEPIVA(what)
{
   if(what.length>0)
   {
	var i=new RegExp("([A-Z]{2})([A-Z0-9]{8,12})");
	if(!i.test(what)) { return false; }   
   }
   return true;
}

/**********************************************************************
   Validazione di una data passata in un unico campo 
***********************************************************************/
function isDateString(dateStr) {
// The function returns true if a valid date, false if not.
    //var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        //alert("Formati della data ammessi dd/mm/yyyy oppure dd-mm-yyyy.");
        //alert("Formato della data ammesso dd/mm/yyyy.");
        return false;
    }
	
// Vista la regExpr per accettare solamente mm/dd/yyyy oppure mm-dd-yyyy 
// basta invertire gli indici del giorno e del mese settati qui sotto
   day = matchArray[1]; // parse date into variables
   month = matchArray[3]; 
   year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Il Mese deve essere compreso fra 1 e 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Il Giorno deve essere compreso fra 1 e 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Il Mese "+month+" non ha 31 giorni!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("Febbraio " + year + " non ha " + day + " giorni!");
            return false;
        }
    }
    return true; // date is valid
}


/**********************************************************************
   Controllo se campo passato è un anno valido compreso fra 1900 e oggi
***********************************************************************/
function isYear (s) {
		var d;
		d = new Date();
		var yyyy = d.getFullYear()
		if ((notNull(s)) && (notBlank(s)) && (isSize(s, 4)) && (!isNaN(s)) && (isInRange(s, 1900, yyyy)) ) { 
			return true; 
		} else { 
			return false;
		}
}

// consente la cancellazione di un record 
function Elimina(nID, action){
	if(confirm("Sei sicuro di voler eliminare?")){
	window.location = '../admin/exec.asp?action='+action+'&id='+nID;
	}
}
// consente la cancellazione di un record Componente Curricula ver. 1.2
function Elimina1(nID, sSezione, sNome, sNomeCampo, action){
	if(confirm("Sei sicuro di voler eliminare?")){
	window.location = '../admin/exec.asp?action='+action+'&id='+nID+'&sezione='+sSezione+'&nome='+sNome+'&campo='+sNomeCampo;
	}
}

// Gestione Ins/Mod News
		function isValidForm( oForm ) {
			var sErr = '', sIntro = 'Errore di compilazione form:\n';
			if (!notBlank(oForm.descrizione_ENG.value)) sErr='Il campo Descrizione Inglese non può essere lasciato vuoto.';
			if (!notBlank(oForm.descrizione_ITA.value)) sErr='Il campo Descrizione Italiano non può essere lasciato vuoto.';
			if (!notBlank(oForm.titolo_ENG.value)) sErr='Il campo Titolo non può essere lasciato vuoto.';
			if (!notBlank(oForm.titolo_ITA.value)) sErr='Il campo Titolo Italiano non può essere lasciato vuoto.';
			if (sErr == '') 
				return true;
			else {
				alert(sIntro+sErr);
				return false;
			}
		}
// Gestione Ins Modulo Collaborazione
		function isValidCollaboraForm(sLingua, oForm ) {
			var sErr = '', sIntro = '', sMsg = '';
			if (sLingua == 'Ita') {
				sIntro = 'Errore di compilazione form:\n';
			} else if (sLingua=='Eng') {
				sIntro = 'Pls check the following fields:\n';
			}
			if (!notBlank(oForm.proposta.value)) {
						if (sLingua == 'Ita') {
							sErr='Il campo Proposta ad AG-Cerec\nnon può essere lasciato vuoto.';
						} else if (sLingua == 'Eng') {
							sErr='Pls specify yr Proposal to AG-Cerec.';
						}
				 }
			if (!notBlank(oForm.attivita.value)) {
						if (sLingua == 'Ita') {
			 				sErr='Il campo Attività della Vostra Ditta\nnon può essere lasciato vuoto.';
						} else if (sLingua == 'Eng') {
							sErr='Pls specify yr Company Activity.';
						}
				 }
			if (notBlank(oForm.url.value)) { 
				 if (!isURL(oForm.url.value)){
						if (sLingua == 'Ita') {
				  			sErr='Il campo Sito WEB non risulta corretto.';
						} else if (sLingua == 'Eng') {
							sErr='Pls check yr WEB site URL.';
						}
				 }
			}
			if (notBlank(oForm.mail.value)) { 
				 if (!isEmail(oForm.mail.value)) {
						if (sLingua == 'Ita') {
							sErr='Il campo E-Mail non risulta corretto.';
						} else if (sLingua == 'Eng') {
							sErr='Pls check yr E-Mail Address.';
						}
				 }
			}
			if (!notBlank(oForm.mail.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo E-Mail non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls Enter your E-mail Address.';
					}
			}
			if (notBlank(oForm.fax.value)) { 
				 if (!isPhone(oForm.fax.value)){
						if (sLingua == 'Ita') {
				  			sErr='Il campo Fax non risulta corretto.';
						} else if (sLingua == 'Eng') {
							sErr='Pls check yr Fax Number.';
						}
				 }
			}
			if (notBlank(oForm.tel.value)) { 
				 if (!isPhone(oForm.tel.value)) {
						if (sLingua == 'Ita') {
							sErr='Il campo Telefono non risulta corretto.';
						} else if (sLingua == 'Eng') {
							sErr='Pls check yr Telephone Number.';
						}
				 }
			}
			if (!notBlank(oForm.tel.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo Telefono non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls Enter ys Telephone Number.';
					}
			}
			if (notBlank(oForm.iva.value)) { 
					if (sLingua == 'Ita') {
						 if (!isCEEPIVA(oForm.iva.value)) sErr='Il campo Partita IVA non risulta corretto.\nPregasi specificare il Codice Iva in formato CEE';
					} else if (sLingua == 'Eng') {
						 if (!isCEEPIVA(oForm.iva.value)) sErr='Pls check yr VAT Number.\nFormat accepted is CEE Format:\nnr. 2 Alphabetic character [A-Z] + Max 12 Alphanumeric characters[0-9A-Z]';
					}
			}
			if (oForm.naz[0].selected) {
					if (sLingua == 'Eng') {
						 sErr='Pls Select your Country.';
					} else if (sLingua == 'Ita') {
						 sErr='Pregasi selezionare la propria Nazionalità.';
					}
			}
			if (!notBlank(oForm.indirizzo.value)) {
					if (sLingua == 'Ita') {
					    sErr='Il campo Indirizzo non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls specify yr Address.';
					}
			}
			if (!notBlank(oForm.ditta.value) && !notBlank(oForm.rappr.value)){
					if (sLingua == 'Ita') {
						sErr='Il campo Nome Ditta o il Campo Rappresentante non possono essere lasciati vuoti.';
					} else if (sLingua == 'Eng') {
						sErr='Pls specify yr Company Name or Your Name as Dealer.';
					}
			}
			if (sErr == '') 
				return true;
			else {
				alert(sIntro+sErr);
				return false;
			}
		}

// Gestione Ins Modulo Curricula
		function isValidCurriculaForm(sLingua, oForm ) {
			var sErr = '', sIntro = '', sMsg = '';
			if (sLingua == 'Ita') {
				sIntro = 'Errore di compilazione form:\n';
			} else if (sLingua=='Eng') {
				sIntro = 'Pls check the following fields:\n';
			}
			if (!notBlank(oForm.conoscenze.value)){
					if (sLingua == 'Ita') {
							sErr='Il campo Conoscenze non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
							sErr='Psl specify your knowledge.';
					}
			}
			if (oForm.ufficio[0].selected) {
					if (sLingua == 'Eng') {
						 sErr='Pls select your Field of Interest.';
					} else if (sLingua == 'Ita') {
						 sErr='Pregasi selezionare il Settore di Interesse.';
					}
			}
			if (!notBlank(oForm.titstudio.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo Titolo di Studio non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls Enter your Level of Study.';
					}
			}
			if (notBlank(oForm.mail.value)) { 
				 if (!isEmail(oForm.mail.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo E-Mail non risulta corretto.';
					} else if (sLingua == 'Eng') {
						sErr='Invalid Mail format.';
					}
				 }
			}
			if (!notBlank(oForm.mail.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo E-Mail non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls Enter your E-mail Address.';
					}
			}
			
			if (notBlank(oForm.tel.value)) { 
				 if (!isPhone(oForm.tel.value)) {
						if (sLingua == 'Ita') {
							sErr='Il campo Telefono non risulta corretto.';
						} else if (sLingua == 'Eng') {
							sErr='Invalid Telephone Number format.';
						}
				 }
			}
			if (!notBlank(oForm.tel.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo Telefono non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls Enter your Phone Number.';
					}
				}
			
			if (oForm.leva[0].selected) {
					if (sLingua == 'Eng') {
						 sErr='Pls specify your military position.';
					} else if (sLingua == 'Ita') {
						 sErr='Pregasi la propria posizione rispetto\nagli obblighi militari.';
					}
			}
			if (oForm.stato_civ[0].selected) {
					if (sLingua == 'Eng') {
						 sErr='Pls select your Status.';
					} else if (sLingua == 'Ita') {
						 sErr='Pregasi selezionare il proprio Stato Civile.';
					}
			}
			if (oForm.naz[0].selected) {
					if (sLingua == 'Eng') {
						 sErr='Pls Select your Country.';
					} else if (sLingua == 'Ita') {
						 sErr='Pregasi selezionare la propria Nazionalità.';
					}
			}
			if (!notBlank(oForm.localita.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo Località non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls Enter the name of your Town.';
					}
				}
			if (notBlank(oForm.cap.value)) {
					if (sLingua == 'Ita') {
						if (!isCAP(oForm.cap.value)) sErr='Campo CAP non corretto.';
					}
			}
			if (!notBlank(oForm.cap.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo Cap non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls Enter your Postal Code.';
					}
				}
			if (!notBlank(oForm.residenza.value)) {
					if (sLingua == 'Ita') {
						sErr='Il campo Residenza non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
						sErr='Pls Enter your Address.';
					}
				}
			if (notBlank(oForm.birth.value)) {
				if (!isDateString(oForm.birth.value)) {
					if (sLingua == 'Ita') {
						sErr='Formato Data di nascita non Corretto: ' + oForm.birth.value + '.\nFormato accettato dd/mm/yyyy.';
					} else if (sLingua == 'Eng') {
						sErr='Invalid Birth Date format: ' + oForm.birth.value + '.\nFormat accepted is dd/mm/yyyy.';
					}
				}
			}
			if (!notBlank(oForm.birth.value)) {
					if (sLingua == 'Ita') {
							sErr='Il campo Data di Nascita non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
							sErr='Psl Enter your Birth Date';
					}
			}
			if (!notBlank(oForm.natoa.value)) {
					if (sLingua == 'Ita') {
							sErr='Il campo Nato A non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
							sErr='Psl Enter your Place of Birth';
					}
			}
			if (!notBlank(oForm.cognome.value)) {
					if (sLingua == 'Ita') {
							sErr='Il campo Cognome non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
							sErr='Psl Enter your Surname';
					}
			}
			if (!notBlank(oForm.nome.value)){
					if (sLingua == 'Ita') {
							sErr='Il campo Nome non può essere lasciato vuoto.';
					} else if (sLingua == 'Eng') {
							sErr='Psl Enter your Name';
					}
			}
			if (sErr == '') 
				return true;
			else {
				alert(sIntro+sErr);
				return false;
			}
		}


