/****************************************/
/** funzioni per iscrizione scolastica **/
/****************************************/



function validate() {

	// controllo del NOME
	var givenname = document.getElementById("inNome");
	if (givenname.value == "") {
		alert('Devi inserire il nome');
		givenname.focus();
		return false;
	} else {
		if (givenname.value.length < 2)	{
			alert('Il nome deve contenere almeno 2 caratteri');
			givenname.focus();
			return false;
		}
	}
	if (! isalpha(givenname.value)) {
		alert('Il nome puo\' contenere solo lettere non accentate');
		givenname.focus();
		return false;
	}

	// controllo del COGNOME
	var surname = document.getElementById("inCognome");
	if (surname.value == "") {
		alert('Devi inserire il cognome');
		surname.focus();
		return false;
	} else {
		if (surname.value.length < 2) {
		alert('Il cognome deve contenere almeno 2 caratteri');
		surname.focus();
		return false;
		}
	}

	if (! isalpha(surname.value)) {
		alert('Il cognome puo\' contenere solo lettere non accentate');
		surname.focus();
		return false;
	}

	// controllo della DATA DI NASCITA
	var datanasc = document.getElementById("inDataNascita");
	if (datanasc.value == "") {
		alert('Devi inserire la data di nascita');
		datanasc.focus();
		return false;
	} else {
		if (datanasc.value.length != 10) {
			alert('La data di nascita deve essere nel formato gg\/mm\/aaaa');
			datanasc.focus();
			return false;
		}
	}

	if (! checkDate(datanasc.value, '\/')) {
		alert('La data di nascita deve essere nel formato gg\/mm\/aaaa');
		datanasc.focus();
		return false;
	}

	// controllo dell'INDIRIZZO
	var surname = document.getElementById("inIndirizzo");
	if (surname.value == "") {
		alert('Devi inserire l\'indirizzo');
		surname.focus();
		return false;
	} else {
		if (surname.value.length < 2) {
			alert('L\'indirizzo deve contenere almeno 2 caratteri');
			surname.focus();
			return false;
		}
	}

	// controllo dello STATO
	var stato = document.getElementById("instato");
	if (stato.value == "") {
		alert('Devi inserire lo Stato');
		stato.focus();
		return false;
	} else {
		if (stato.value.length < 2) {
			alert('lo Stato deve contenere almeno 2 caratteri');
			stato.focus();
			return false;
		}
	}
	
	// controllo del COMUNE DI RESIDENZA
	if (stato.value.toUpperCase()=="ITALIA") {
		var comune = document.getElementById("inComuneResidenza");
		if (comune.value == "") {
			alert('Devi inserire il Comune di Residenza');
			comune.focus();
			return false;
		} else {
			if (comune.value.length < 2) {
				alert('Il Comune di Residenza deve contenere almeno 2 caratteri');
				comune.focus();
				return false;
			}
		}
	
		if (! isalpha(comune.value)) {
			alert('Il comune puo\' contenere solo lettere non accentate');
			comune.focus();
			return false;
		}
	
		// controllo della Provincia
		var prov = document.getElementById("inProvincia");
		if (prov.value == "") {
			alert('Devi inserire la Provincia');
			prov.focus();
			return false;
		} else {
			if (prov.value.length < 2) {
				alert('la Provincia deve contenere almeno 2 caratteri');
				prov.focus();
				return false;
			}
		}
	
		if (! isalpha(prov.value)) {
			alert('La Provincia puo\' contenere solo lettere non accentate');
			prov.focus();
			return false;
		}
	}

	// controllo dell'email
	var mail = document.getElementById("inEmail");
	if (mail.value == "") {
		alert('Devi inserire l\'e-mail');
		mail.focus();
		return false;
	} else {
		if (!emailvalidation(mail.value)) {
			alert('Indirizzo email non valido');
			mail.focus();
			return false;
		}
	}

	// controllo del codice Fiscale
	var codfisc = document.getElementById("inCodiceFiscale");
	if (codfisc.value == "") {
		alert('Devi inserire il Codice Fiscale');
		codfisc.focus();
		return false;
	} else {
		if (codfisc.value.length != 16) {
			alert('Il Codice Fiscale deve contenere 16 caratteri');
			codfisc.focus();
			return false;
		}
	}

	if (! isalphanumeric(codfisc.value)) {
		alert('La Provincia puo\' contenere solo lettere non accentate e numeri');
		prov.focus();
		return false;
	}

	// controllo del nome e cognome del figlio
	var figlio = document.getElementById("inNomeFiglio");
	if (figlio.value == "") {
		alert('Devi inserire il nome e cognome del figlio');
		figlio.focus();
		return false;
	} else {
		if (figlio.value.length < 2) {
			alert('Il nome e cognome del figlio deve contenere almeno 2 caratteri');
			figlio.focus();
			return false;
		}
	}

	if (! isalpha(figlio.value)) {
		alert('Il nome del figlio puo\' contenere solo lettere non accentate');
		figlio.focus();
		return false;
	}

	// controllo della DATA DI NASCITA del FIGLIO!!
	var datanascf = document.getElementById("inDataNascitaFiglio");
	if (datanascf.value == "") {
		alert('Devi inserire la data di nascita del figlio');
		datanascf.focus();
		return false;
	} else {
		if (datanascf.value.length != 10) {
			alert('La data di nascita del figlio deve essere nel formato gg\/mm\/aaaa');
			datanascf.focus();
			return false;
		}
	}

	if (! checkDate(datanascf.value, '\/')) {
		alert('La data di nascita del figlio deve essere nel formato gg\/mm\/aaaa');
		datanascf.focus();
		return false;
	}
	
	// controllo del comune selezionato!!
	var comuneselez = document.getElementById("inComuneSelezionato");
	if (comuneselez.value == "") {
		alert('Selezonare un comune');
		comuneselez.focus();
		return false;
	}

	// controllo del servizio selezionato!!
	var servizioselez = document.getElementById("inServizioComunale");
	if (servizioselez.value == "") {
		alert('Selezonare un Servizio Comunale');
		servizioselez.focus();
		return false;
	}
	
	// controllo del plesso o scuola di preferenza!!
	var plesso = document.getElementById("inPlesso");
	if (plesso.options!=null) {
		if (plesso.options.length > 0) {
			if (plesso.value == "") {
				alert('Selezonare un Plesso o una Scuola di preferenza');
				plesso.focus();
				return false;
			}
		}
	}
	var plesso = document.getElementById("inaccettazione");
	if (!plesso.checked) {
		alert("E' necessario accettare l'informativa sulla privacy per proseguire");
		return false;
	}
	
	return true;

}

/****************************************/
/************** funzioni varie **********/
/****************************************/



function emailvalidation(entered) {
	apos=entered.indexOf("@");
	dotpos=entered.lastIndexOf(".");
	lastpos=entered.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
		return false;
	} else {
		return true;
	}
}

function trim (stringa) {
	var inizio =0;
	var tmp = stringa;
	
	for(var i=0; i<tmp.length; i++){
		if(tmp.charCodeAt(i)==13 || tmp.charCodeAt(i)==10 || tmp.charCodeAt(i)==32) inizio++;
		else break;
	}
	tmp = tmp.substr(inizio);

	for(var i=tmp.length-1; i>0; i--){
		inizio=i;
		if(tmp.charCodeAt(i)!=13 && tmp.charCodeAt(i)!=10 && tmp.charCodeAt(i)!=32) break;
	}
	tmp = tmp.substr(0,inizio+1);
	return tmp;
}

function isalpha(stringa) {
// NOTA: accetta lettere, numeri, spazi e accenti
	var retval=true;
	for (var i=0;i<stringa.length; ++i) {
		if (((stringa.charAt(i) < "a") || (stringa.charAt(i) > "z")) && ((stringa.charAt(i) != " ")) &&
				((stringa.charAt(i) != "'")) && ((stringa.charAt(i) != "_")) && ((stringa.charAt(i) != "-")) &&
				((stringa.charAt(i) != "&")) && ((stringa.charAt(i) != ".")) && ((stringa.charAt(i) != ",")) &&
				((stringa.charAt(i) < "0") || (stringa.charAt(i) > "9")) && ((stringa.charAt(i) < "A") || (stringa.charAt(i) > "Z")))
			retval = false
	}
	return retval;
}

function isalphanumeric(stringa) {
// NOTA: accetta lettere e numeri
	var retval=true;
	for (var i=0;i<stringa.length; ++i) {
		if (((stringa.charAt(i) < "0") || (stringa.charAt(i) > "9")) && ((stringa.charAt(i) < "a") || (stringa.charAt(i) > "z")) &&
				((stringa.charAt(i) < "A") || (stringa.charAt(i) > "Z")) && ((stringa.charAt(i) != "_")) &&
				((stringa.charAt(i) != "-")) && ((stringa.charAt(i) != ".")))
			retval = false
	}
	return retval;
}

function checkDate(str, separet) {
	var strTmp = "^[0-9]{2}" + separet + "[0-9]{2}" + separet + "[0-9]{4}$";
	var objRegExp = new RegExp(strTmp, "g");
	if (!objRegExp.test(str)) return false;

	var ss;
	if (separet != "") {
		ss = str.split(separet);
	} else {
		ss[0] = str.substr(0,2);
		ss[1] = str.substr(2,2);
		ss[2] = str.substr(4,4);
	}

	if ((ss[1] < 1) || (ss[1] > 12)) return false;

	var objDate = new Date(ss[2], ss[1], 1 - 1);
	if ((ss[0] < 1) || (ss[0] > objDate.getDate())) return false;

	return true;
}

/****************************************/
/** funzione per controllo campi info ***/
/****************************************/

function controllainfo(){
	var valore = trim(document.getElementById('infrom').value);
	if(valore == null || valore.length==0) {
		alert("il campo mittente deve essere valorizzato");
		return false
	} else {
		if (!emailvalidation(valore)) {
			alert('Indirizzo email non valido');
			return false;
		}
	}

	valore = trim(document.getElementById('inoggetto').value);
	if(valore == null || valore.length==0) {
		alert("il campo oggetto deve essere valorizzato");
		return false
	}
	
	valore = trim(document.getElementById('intesto').value);
	if(valore == null || valore.length==0) {
		alert("il campo testo deve essere valorizzato");
		return false
	}
	return true;
}

/**************************************************/
/** funzione per controllo campi post dei forum ***/
/**************************************************/


function controllacampipost(){
	var valore = trim(document.getElementById('inoggetto').value);
	if(valore == null || valore.length==0) {
		alert("il campo oggetto deve essere valorizzato");
		return false
	}

	valore = trim(document.getElementById('intesto').value);
	if(valore == null || valore.length==0) {
		alert("il campo testo deve essere valorizzato");
		return false
	}
	return true;
}

/*******************************/
/** funzioni per accesso atti **/
/*******************************/

function validateAtti() {

	// controllo del NOME
	var givenname = document.getElementById("inNome");
	givenname.value = trim(givenname.value);
	if (givenname.value == "") {
		alert('Devi inserire il nome');
		givenname.focus();
		return false;
	} else {
		if (givenname.value.length < 2) {
			alert('Il nome deve contenere almeno 2 caratteri');
			givenname.focus();
			return false;
		}
	}
	if (! isalpha(givenname.value)) {
		alert('Il nome puo\' contenere solo lettere non accentate');
		givenname.focus();
		return false;
	}

	// controllo del COGNOME
	var surname = document.getElementById("inCognome");
	surname.value=trim(surname.value);
	if (surname.value == "") {
		alert('Devi inserire il cognome');
		surname.focus();
		return false;
	} else {
		if (surname.value.length < 2) {
			alert('Il cognome deve contenere almeno 2 caratteri');
			surname.focus();
			return false;
		}
	}
	if (! isalpha(surname.value)) {
		alert('Il cognome puo\' contenere solo lettere non accentate');
		surname.focus();
		return false;
	}

	// controllo dell'INDIRIZZO
	var surname = document.getElementById("inIndirizzo");
	surname.value=trim(surname.value);
	if (surname.value == "") {
		alert('Devi inserire l\'indirizzo');
		surname.focus();
		return false;
	} else {
		if (surname.value.length < 2) {
			alert('L\'indirizzo deve contenere almeno 2 caratteri');
			surname.focus();
			return false;
		}
	}

	// controllo del COMUNE DI RESIDENZA
	var comune = document.getElementById("inComuneResidenza");
	comune.value=trim(comune.value);
	if (comune.value == "") {
		alert('Devi inserire il Comune di Residenza');
		comune.focus();
		return false;
	} else {
		if (comune.value.length < 2) {
			alert('Il Comune di Residenza deve contenere almeno 2 caratteri');
			comune.focus();
			return false;
		}
	}
	if (! isalpha(comune.value)) {
		alert('Il comune puo\' contenere solo lettere non accentate');
		comune.focus();
		return false;
	}

	// controllo della Provincia
	var prov = document.getElementById("inProvincia");
	prov.value = trim(prov.value);
	if (prov.value == "") {
		alert('Devi inserire la Provincia');
		prov.focus();
		return false;
	} else {
		if (prov.value.length < 2) {
			alert('la Provincia deve contenere almeno 2 caratteri');
			prov.focus();
			return false;
		}
	}
	if (! isalpha(prov.value)) {
		alert('La Provincia puo\' contenere solo lettere non accentate');
		prov.focus();
		return false;
	}

	// controllo dell'email
	var mail = document.getElementById("inEmail");
	mail.value = trim(mail.value);
	if (mail.value == "") {
		alert('Devi inserire l\'e-mail');
		mail.focus();
		return false;
	} else {
		if (!emailvalidation(mail.value)) {
			alert('Indirizzo email non valido');
			mail.focus();
			return false;
		}
	}

	// controllo Motivazioni
	var tipoatto = document.getElementById("inTipoAtto");
	tipoatto.value = trim(tipoatto.value);
	if (tipoatto.value == "") {
		alert('Devi inserire il tipo di atto richiesto');
		tipoatto.focus();
		return false;
	}

	var plesso = document.getElementById("inaccettazione");
	if (!plesso.checked) {
		alert("E' necessario accettare l'informativa sulla privacy per proseguire");
		return false;
	}
	
	return true;

}


function cambiacomuneatti(slct) {
	// recupera il comune selezionato
	comuneSel = slct.selectedIndex;
	var temp2 = document.getElementById('inserviziocomunale');
	temp2.value = comuni[comuneSel][0][0];

}



function caricaatti(etichetta) {
	var slct= document.getElementById(etichetta);
	cambiacomuneatti(slct);
}

/*******************************/
/** funzioni per atti on line **/
/*******************************/

function caricaaol() {
	var slct= document.getElementById('inComuneSelezionato');
	cambiacomuneaol(slct);
}

function cambiacomuneaol(slct) {

	comuneSel = slct.selectedIndex;

	var slct2= document.getElementById('inserviziocomunale');
	slct2.value = comuni[comuneSel][0][0][0];
	cambiaservizioaol(slct2);
}

function cambiaservizioaol(slct) {

	servizioSel = 0;
	var option;
	var slct3= document.getElementById('inTipoAtto');
	if (slct3.options!=null) {
		for (var count = slct3.options.length - 1; count >= 0; count--) {
			slct3.options[count] = null;
		}
	}
	if (servizioSel>-1) {
		var temp = slct3;
		option = document.createElement("OPTION");
		temp.options[0]= option;
		option.value = "-1";
		option.text  = "Tutti";

		temp.size= comuni[comuneSel][servizioSel][1].length +1;
		for (var count = 0; count < comuni[comuneSel][servizioSel][1].length ; count++) {
			option = document.createElement("OPTION");
			temp.options[count+1]= option;
			option.value = comuni[comuneSel][servizioSel][1][count][0];
			option.text  = comuni[comuneSel][servizioSel][1][count][1];
		}
	}
	slct3.selectedIndex=0;
}



function checkstring(stringa){
	if(stringa!=null && stringa.length>0) {
		return true;
	} else {
		return false;
	}
}

function controlladata(stringa) {
	/*
		return 0 è nulla;
		return 1 data non valida;
		return 2 data valida;
	*/
	var avvertimento = 'La data deve essere nel formato gg\/mm\/aaaa oppure gg-mm-aaaa';
	if(checkstring(stringa)) {
		if(stringa.length==10) {
			if (checkDate(stringa, '\/')) {
				return 2;
			} else if (checkDate(stringa, '-')) {
				return 2;
			} else {
				alert(avvertimento);
				return 1;
			}
		}
		alert(avvertimento);
		return 1;
	}
	return 0;
}


function validaattionline(){
	var tmp = document.getElementById('inATTI');
	var option=document.getElementById('inTipoAtto').options;
	for(var i=0; i<option.length;i++) {
		if(option[i].selected) tmp.value+=option[i].value+",";
	}

	var ck1 = controlladata(document.getElementById('inDataD').value);
	var ck2 = controlladata(document.getElementById('inDataA').value);
	if(ck1==1 || ck2==1) {
		return false;
	} else if(ck1==2 && ck2==0) {
		alert('Deve essere inserita anche la data di fine del periodo di ricerca');
		document.getElementById('inDataA').focus();
		return false;
	} else if(ck1==0 && ck2==2) {
		alert('Deve essere inserita anche la data di inizio del periodo di ricerca');
		document.getElementById('inDataD').focus();
		return false;
	}
	var na1 = trim(document.getElementById('inNumeroD').value);
	var na2 = trim(document.getElementById('inNumeroA').value);
	var naA = trim(document.getElementById('inAnno').value);
	if(checkstring(na1) || checkstring(na2)){
		if(!checkstring(naA)){
			alert("e' necassario inserire l'anno");
			document.getElementById('inAnno').focus();
			return false;
		}
	}
	if(checkstring(naA)){
		if(!checkstring(na1) && !checkstring(na2)){
			alert("e' necassario inserire almeno uno dei campi numero atto");
			document.getElementById('inNumeroD').focus();
			return false;
		}
	}
	return true;
}

