// JavaScript Document
/****************************************************************************************************/
/*                                   FUNZIONE DI CONTROLLO                                          */
/****************************************************************************************************/
//RICHIAMATA NELLA FORM DEL BUDGET, PAAF
function controlla(form) 
{  
	for(var i=0;i<form.elements.length;i++) 
	{	
		if(form[i].id=='YD_010_edizioneDI' || form[i].id=='YD_010_edizioneDF') continue;
        if (form[i].disabled == true) continue;
		//alert(form[i].id);
		//CAMPI OBBLIGATORI
		if(form[i].id.substr(0,1)=='Y'){
			//alert(form[i].id);
			//SE INPUT TEXT
			if(form[i].type=='text'){
				//SE CAMPO DATA
				if(form[i].id.substr(1,1)=='D'){
					if(form[i].id=='YD_010_edizioneDI') return true;
					if(form[i].id=='YD_010_edizioneDF') return true;
					if (!controllaData(form[i].id) || !validaData(form[i].id)) return false;
				}
				//SE CAMPO CARATTERE
				if(form[i].id.substr(1,1)=='C'){
					if(form[i].value=='') { 
						alert(" Attenzione!!! Questo campo è obbligatorio.");
						document.getElementById(form[i].id).focus();
						return false;
					}
					else {
						if(!controllaLunghezza(form[i].id,form[i].value)) return false;
					}
				}
				//SE CAMPO NUMERICO
				if(form[i].id.substr(1,1)=='N'){
					if(form[i].value=='') { 
						alert(" Attenzione!!! Questo campo è obbligatorio.");
						document.getElementById(form[i].id).focus();
						return false;
					}
					else {
						if(!isNumero(form[i].id)) return false;
					}
				}
				//SE CAMPO MAIL
				if(form[i].id.substr(1,1)=='M'){
					if(!checkMail(form[i].id)) return false;
				}		
			}
			//SELECT SINGOLA
			if(form[i].type=='select-one'){
				if(form[i].value=='default') continue;
				if(form[i].id=='YN_000_NazioneNascita' && form[i].value=='99999'){
				//if(document.getElementById('YN_000_NazioneNascita').value=='999100'){
					if(!ctrNasc()) return false;
				}
				//if(form[i].id=='YN_000_RegioneNascita' || form[i].id=='YN_000_ProvinciaNascita' || form[i].id=='YN_000_LuogoNascita')
				//	continue;
				//alert(form[i].value);				
				if(form[i].value==''){
					alert(" Attenzione!!! Questo campo è obbligatorio.");
					document.getElementById(form[i].id).focus();
					return false;
				}
			}
			//if(!validaFisc()) return false;
		}
		//CAMPI NON OBBLIGATORI
		if(form[i].id.substr(0,1)=='N'){
			//SE INPUT TEXT
			if(form[i].type=='text'){
				//CAMPO DIVERSO DA VUOTO
				if(form[i].value!=''){
					//SE CAMPO DATA
					if(form[i].id.substr(1,1)=='D'){
						if(form[i].id=='YD_010_edizioneDI') return true;
						if(form[i].id=='YD_010_edizioneDF') return true;
						if (!controllaData(form[i].id) || !validaData(form[i].id)) return false;
					}
					//SE CAMPO CARATTERE
					if(form[i].id.substr(1,1)=='C'){
						if(!controllaLunghezza(form[i].id,form[i].value)) return false;
					}
					//SE CAMPO NUMERICO
					if(form[i].id.substr(1,1)=='N'){
						if(!isNumero(form[i].id)) return false;
					}
					//SE CAMPO MAIL
					if(form[i].id.substr(1,1)=='M'){
						if(!checkMail(form[i].id)) return false;
					}				
				}
			}
		}
	}//FINE FOR
	//Controlla nel salvataggio totale la correttezza del periodo delle date
	if(form.name=='iniziativa'){
		if(document.getElementById('numEdi').value!='') {
			for(i=1;i<=document.getElementById('numEdi').value;i++){
				if (!confrontaDate(document.getElementById('YD_010_edizioneDI'+i),document.getElementById('YD_010_edizioneDF'+i))) return false; 
			}
		}
		else {
			alert(" Attenzione!!! Questo campo è obbligatorio.");
			document.getElementById('numEdi').focus();
			return false;
		}
	}
	return true;
}
function ctrNasc(){
	if(document.getElementById('YN_000_RegioneNascita').value==''){
		alert(" Attenzione!!! Questo campo è obbligatorio.");
		document.getElementById('YN_000_RegioneNascita').focus();
		return false;
	}
	if(document.getElementById('YN_000_ProvinciaNascita').value==''){
		alert(" Attenzione!!! Questo campo è obbligatorio.");
		document.getElementById('YN_000_ProvinciaNascita').focus();
		return false;
	}
	if(document.getElementById('YN_000_LuogoNascita').value==''){
		alert(" Attenzione!!! Questo campo è obbligatorio.");
		document.getElementById('YN_000_LuogoNascita').focus();
		return false;
	}
	return true;
}
/****************************************************************************************************/
/*                                FUNZIONE DI CONTROLLO SCHEDA AFR                                  */
/****************************************************************************************************/
function ctrCmpObbl(form) 
{ 
	//alert();
	for(var i=0;i<form.elements.length;i++) 
	{
		//se è una textbox
		if(form[i].type=='text')
		{
			//Campi obbligatori
			if(form[i].id.substr(0,1)=='Y')
			{
				if(form[i].value=='')
				{
					alert(" Attenzione!!! Questo campo è obbligatorio. ");
					document.getElementById(form[i].id).focus();
					return false;
				}
				else 
				{
					//Controllo sul campo data
					if(form[i].id.substr(1,1)=='D')
					{
						if (!controllaData(form[i].id,"Data di Programmazione") || 
							!validaData(form[i].id,"Data di Programmazione")) return false;
					}
					//Controllo che il campo sia numerico
					else if(form[i].id.substr(1,1)=='N') 
					{
						if(!isNumero(form[i].id)) return false;
						if(!controllaLunghezza(form[i].id,form[i].value)) return false;
					}
					else 
					{
						if(!controllaLunghezza(form[i].id,form[i].value)) return false;
					}
				}
			}
			//Campi NON obbligatori
			if(form[i].id.substr(0,1)=='N')
			{
				if(form[i].value!='')
				{
					//Controllo che il campo sia numerico
					if(form[i].id.substr(1,1)=='N')
					{
						if(!isNumero(form[i].id)) return false;
						if(!controllaLunghezza(form[i].id,form[i].value)) return false;
					}
					else 
					{
						if(!controllaLunghezza(form[i].id,form[i].value)) return false;
					}
				}
			}
		}

		//se è una select
		if(form[i].type=='select-one' && form[i].id!='YN000_Responsabile_paaf') 
		{
			//Campi obbligatori
			if(form[i].id.substr(0,1)=='Y')
			{
				if(form[i].value=='')
				{
					alert(" Attenzione!!! Questo campo è obbligatorio. ");
					document.getElementById(form[i].id).focus();
					return false;
				}
			}
		}
	}//FINE FOR
	return true;
}
//Funzione che permette la stampa dell'attestato sono nel caso in cui i primi tre caratteri del
//CodEvento = 104 (PRATO) e che TipoEvento=1 (Evento SST)
function ctrStampa() {
	if(document.getElementById('bottone').value=='stampa'){
	//	alert(document.getElementById('YC_016_CodEvento').value.substr(0,3)); return false;
		if (document.getElementById('YC_016_CodEvento').value.substr(0,3)!='104'){
			alert('Attenzione!!! Non è possibile effettuare la stampa');
			return false;
		}
		if (document.getElementById('Y_TipoEvento').value!=1){
			alert('Attenzione!!! Non è possibile effettuare la stampa');
			return false;
		}
	}
	return true;
}


/****************************************************************************************************/
/*                                      FUNZIONE GENERICHE                                          */
/****************************************************************************************************/
//CONTROLLA LA LUNGHEZZA DEL CAMPO
function controllaLunghezza(id,valore){
	var lung = 0;
	var lungMax = 0;
	var lungValore = 0;
	if(id.substr(3,1)!='0')	
		lung = id.substr(3,3);
	
	if(id.substr(3,1)=='0' && id.substr(4,1)!='0')
		lung = id.substr(4,2);
	
	if(id.substr(3,1)=='0' && id.substr(4,1)=='0')
		lung = id.substr(5,1);
	
	lungMax=parseInt(lung);
	lungValore = valore.length;
	if (lungValore>lungMax){
		alert(' Attenzione!!! La lunghezza massima di questo campo deve essere '+lungMax+'. ');
		document.getElementById(id).value=document.getElementById(id).value.substr(0,lungMax);
		document.getElementById(id).focus();
		return false;
	}
	return true;
}

//FUNZIONE CHE CONTROLLA LA VALIDITA' DELLA DATA INSERITA
function validaData(data){
	var d = document.getElementById(data).value;

	if((d=="00/00/0000")||(d=="31/12/9999"))
	{	
		alert(" ATTENZIONE!!! Inserisci un valore corretto. Formato gg/mm/aaaa. ");
		//alert(d);
		document.getElementById(data).focus();
		document.getElementById(data).select();
		return false;
	}	
	var months = new Array (31,28,31,30,31,30,31,31,30,31,30,31);
	var a=new Array();

	a=d.split('/');

	var day=a[0];
	var month=a[1];
	var year=a[2];
	if(((year%4 == 0)&&(year%100 != 0))||(year%400 == 0))
		months[1] = 29;
	if ((year>1900)&&(year<2100)&&(month>0)&&(month < 13)&&(day>0)&&(day<=months[month-1]) && year.length==4)
		return true;
	alert(" ATTENZIONE!!! Inserisci un valore corretto. Formato gg/mm/aaaa. ");

	document.getElementById(data).focus();
	document.getElementById(data).select();
	return false;
}

//FUNZIONE CHE CONTROLLA IL FORMATO DELLA DATA CON L'ESPRESSIONI REGOLARI
function controllaData(data) {
	var dataReg=/^\d{2}\/\d{2}\/\d{4}$/;
	if(!dataReg.test(document.getElementById(data).value)){
		alert(" ATTENZIONE!!! Inserisci un valore corretto. Formato gg/mm/aaaa. ");
		document.getElementById(data).focus();
		document.getElementById(data).select();
		return false;
	}
	return true;
}

//FUNZIONE CHE CONFRONTA DUE DATE
function confrontaDate(data_da,data_a) {
	var p1 = new Array();
	var p2 = new Array();
	p1=data_da.value.split('/');
	p2=data_a.value.split('/');
	var gg1 = p1[0];
	var mm1 = p1[1];
	var aa1 = p1[2];
	var gg2 = p2[0];
	var mm2 = p2[1];
	var aa2 = p2[2];
	var data1=aa1+mm1+gg1;
	var data2=aa2+mm2+gg2;
	if (data2>=data1)
		return true;
	else {
		alert(" ATTENZIONE!!! La data del secondo campo è antecedente alla data del primo campo. ");
		data_da.focus();		
		return false;		
	}
}

//Controllo se il valore inserito è numerico
function isNumero(id)
{
	//alert(id); return;
	var valore = document.getElementById(id).value;
	if(valore!='' && isNaN(valore))
	{
		alert("Inserire un valore numerico in questo campo!");
		document.getElementById(id).focus();
		document.getElementById(id).select();
		return false;
	}
	return true;
}

//FUNZIONE CHE CONTROLLA LA VALIDITA' DELLA MAIL INSERITA
function checkMail(campo){
	var MailRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if(document.getElementById(campo).value!='' && MailRegExp.test(document.getElementById(campo).value))
		return true;
	else {
		alert(" ATTENZIONE!!! Inserire un indirizzo e-mail valido. ");
		document.getElementById(campo).focus()
		document.getElementById(campo).select()
		return false;
	}
}
//SVUOTA COMBO
function svuotaCB(nomeCB) {
	var combo = document.getElementById(nomeCB);
	for(var i=combo.length-1;i>=0;i--)
		if(combo.value!=''){
			//combo.remove(i);
			combo.options[i]=null;
		}
}
//RESETTA FORM
function resetta(form)
{
	for(var i=0;i<form.elements.length;i++){
		//if(document.getElementById(form[i].id).readOnly==true && form[i].id=='CapResid') 
		//	form[i].value = '';
		//else continue;
		if(form[i].type!='hidden' && form[i].type!='button' && form[i].type!='submit')
			form[i].value = '';
		if(form[i].type=='checkbox')
			form[i].checked=false;
		if(form[i].type=='select-one')
			if(form[i].id=='YN_000_ProvinciaResid' || form[i].id=='YN_000_LuogoResid')
				svuotaCB(form[i].id);
	}
}
/****************************************************************************************************/
/*                   FUNZIONE DI CONTROLLO PER ESTRAZIONE E STAMPA FLUSSO AFR                       */
/****************************************************************************************************/
function ctrEstrazione() {
	if (!controllaData("DaData","Da Data") || 
		!validaData("DaData","Da Data")) return false;
	if (!controllaData("AData","A Data") || 
		!validaData("AData","A Data")) return false;
	if (!confrontaDate(document.getElementById('DaData'),document.getElementById('AData'))) return false;
	return true;
}
function ctrStampaElenco() {
	if(document.getElementById('DaData').value!='') {
		if (!controllaData("DaData","Da Data") || 
			!validaData("DaData","Da Data")) return false;
	}
	if(document.getElementById('AData').value!='') {
		if (!controllaData("AData","A Data") || 
			!validaData("AData","A Data")) return false;
	}
	if(document.getElementById('DaData').value!='' && document.getElementById('AData').value!=''){
		if (!confrontaDate(document.getElementById('DaData'),document.getElementById('AData'))) return false;
	}
	return true;
}
/****************************************************************************************************/
/*                                 FUNZIONE DI CONTROLLO PER INIZIATIVE                             */
/****************************************************************************************************/
function controllaModificaSalva(DaData,AData){
	if (!controllaData(DaData,"Data Iniziale") || !validaData(DaData,"Data Iniziale")) return false;
	if (!controllaData(AData,"Data Finale") || !validaData(AData,"Data Finale")) return false;
	if (!confrontaDate(document.getElementById(DaData),document.getElementById(AData))) return false; 
	return true;
}




