//=====================================================
//  FUNZIONE CHE CONTROLLA IL CAMPO EMAIL DI UN FORM
//=====================================================			
function isEmailOk(data) {
  if (data.indexOf(".") != -1) {
      if (data.indexOf("@") != -1)  { 
	      return true;
		  }
		}
	else return false;	  
}

//=============================================================
//  FUNZIONE CHE CONTROLLA IL RIEMPIMENTO DEL CAMPO DI UN FORM
//=============================================================		
function isSpace(data) {
	var contr2=0;
   	for(var i=0; i < data.length; i++){
		if(data.substring(i, i+1) != " ")
	   		contr2=1; 
	}   
	if  (contr2==1)   
		return(false);  
return(true);
}

//=============================================================
//  FUNZIONE CHE CONTROLLA IL CODICE FISCALE
//=============================================================	
function codiceFISCALE(cfins)
   {
   var cf = cfins.toUpperCase();
   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
   if (!cfReg.test(cf))
      return false;
   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   var 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 false;
   return true;
   }

//======================================
//  FUNZIONE CHE APRE UNA FINESTRA 
//======================================
function apripagina(pagina,dx,dy,rsz,scrl,pos,menub){
	
	var settings, TopPosition, LeftPosition;

	//	screen.width    DIMENSIONE DELLO SCHERMO IN LARGHEZZA	
	//	screen.height  	DIMENSIONE DELLO SCHERMO IN ALTEZZA
	//	dx				DIMENSIONE DELLA FINESTRA IN LARGHEZZA
	//	dy				DIMENSIONE DELLA FINESTRA IN ALTEZZA
    if (pos==""){
		pos = "center";
	}
	if (pos=="center"){
		// allineamento della finestra in mezzo
		TopPosition = ((screen.height) / 2) - (dy/2);
		// allineamento della finestra in mezzo
		LeftPosition = ((screen.width) / 2) - (dx/2);
	}
	if (pos=="right"){
		// allineamento della finestra in alto a destra
		TopPosition = 0;
		// allineamento della finestra a destra
		LeftPosition = (screen.width) - dx -10;
	}
	if (pos=="left"){
		// allineamento della finestra in alto a sinistra
		TopPosition = 0;
		// allineamento della finestra a destra
		LeftPosition = 0;
	}
	
	if (menub==""){
		menub = "yes";
	}
		
	// settaggio delle caratteristiche della finestra da aprire
	settings ='height='+dy+',width='+dx+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrl+',resizable='+rsz+',titlebar=yes,status=yes,menubar='+menub;
	
	// se esite gia' una finestra aperta allora la chiudo e la distruggo;
	//if (win != null){
	//	win.close();
	//	win = null;
	//}
	
	// apro una nuova finestra e le assegno il focus il nome della nuova finestra
	// (secondo campo nell'elenco parametri) non deve contenere nulla ;
	// se il campo nomepagina non è vuoto apro sempre nella stessa finestra che 
	// ha sempre lo stesso nome 
	
	//win = window.open(pagina,'',settings)
	
	//win = window.open(pagina,'ciccio',settings)
	win = window.open(pagina,'ciccio')
	win.window.focus();
}
   
//=============================================================
//  AJAX: CONTROLLA SE UN VALORE E' GIA' INSERITO NEL DB
//=============================================================
function ckeck_campo(valore_campo_controllare, nome_form, nome_campo_messaggio, pagina_check, messaggio_di_errore){
	// si intercetta il browser utilizzato dall'utente
	if (window.XMLHttpRequest) {
		// mozilla, safari, ...
  		http_request = new XMLHttpRequest();
	}
	else {
		 // internet explorer
  		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	// si espone ciò che succederà quando arriverà la risposta dal server
	http_request.onreadystatechange = function(){
		if ( (http_request.readyState == 4) && (http_request.status == 200) ) {
			if (http_request.responseText==messaggio_di_errore){
				alert("ATTENZIONE: il nome utente è stato già scelto!");
				//document.getElementById(nome_campo_messaggio).style.visibility = 'visible';
				//document.getElementById(nome_campo_messaggio).style.display = 'block';
			} else {
				esegui_form = "document."+nome_form+".submit();";
				eval(esegui_form);
			}
		}
	}
	// si formula la richiesta utilizzando i due metodi "open()" e "send()" della classe di richiesta HTTP
	http_request.open("GET",pagina_check+document.getElementById(valore_campo_controllare).value);
 	http_request.send(null);
}

//======================================
//  FUNZIONE CHE APRE UNA FINESTRA 
//======================================
function apripagina2(pagina,dx,dy,rsz,scrl,pos,menub){
	
	var settings, TopPosition, LeftPosition;

	//	screen.width    DIMENSIONE DELLO SCHERMO IN LARGHEZZA	
	//	screen.height  	DIMENSIONE DELLO SCHERMO IN ALTEZZA
	//	dx				DIMENSIONE DELLA FINESTRA IN LARGHEZZA
	//	dy				DIMENSIONE DELLA FINESTRA IN ALTEZZA
    if (pos==""){
		pos = "center";
	}
	if (pos=="center"){
		// allineamento della finestra in mezzo
		TopPosition = ((screen.height) / 2) - (dy/2);
		// allineamento della finestra in mezzo
		LeftPosition = ((screen.width) / 2) - (dx/2);
	}
	if (pos=="right"){
		// allineamento della finestra in alto a destra
		TopPosition = 0;
		// allineamento della finestra a destra
		LeftPosition = (screen.width) - dx -10;
	}
	if (pos=="left"){
		// allineamento della finestra in alto a sinistra
		TopPosition = 0;
		// allineamento della finestra a destra
		LeftPosition = 0;
	}
	
	if (menub==""){
		menub = "yes";
	}
		
	// settaggio delle caratteristiche della finestra da aprire
	//settings ='height='+dy+',width='+dx+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrl+',resizable='+rsz+',titlebar=yes,status=yes,menubar='+menub;
	settings ='height='+dy+',width='+dx+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrl+',resizable='+rsz+',titlebar=no,status=no,menubar=no';
	
	// se esite gia' una finestra aperta allora la chiudo e la distruggo;
	//if (win != null){
	//	win.close();
	//	win = null;
	//}
	
	// apro una nuova finestra e le assegno il focus il nome della nuova finestra
	// (secondo campo nell'elenco parametri) non deve contenere nulla ;
	// se il campo nomepagina non è vuoto apro sempre nella stessa finestra che 
	// ha sempre lo stesso nome 
	
	//win = window.open(pagina,'',settings)
	
	//win = window.open(pagina,'ciccio',settings)
	win = window.open(pagina,'ciccio',settings)
	win.window.focus();
}

//=================================================
//  PERMETTE DI INSERIRE SOLO DETERMINATI CARATTERI
//=================================================
// si possono inserire caratteri che contengono solo lettere, numeri e underscore
function check_caretteri(campo_input){
	path_array = campo_input.split("\\");
	var filtro_caratteri=/^[a-zA-Z0-9\.\_\-]+$/;
	if (!filtro_caratteri.test(path_array[path_array.length-1])) {
		return false;
	}
	return true;
}

//=============================================================
//  CHIEDE CONFERMA PRIMA DI ELIMINARE
//=============================================================
function elimina(){
	if (!confirm("Confermi l'eliminazione?")){
		return false;
	}
	else
		return true;
}
//=============================================================
//  Controlla la validità dei form per la gestione dei gruppi
//=============================================================
function controllaForm(form) {
	var ret = Boolean(form.gruppo.value);
	if (ret) {
		return true;
	} else {
		alert("Attenzione: compilare il modulo");
		return false;
	}
}
