function set_newsletter2(mail){

	StrMail=mail;
	if (StrMail.length>6){
	  var pos;
	  var dotpos;
	  pos = StrMail.indexOf("@");
	  if ( (pos >= (StrMail.length-3) ) || (pos < 2) )
		 {
		alert("Il campo email non è stato inserito nel formato corretto.");
		 document.getElementById('email').focus();
		 return (false);		 
		 }
	  pos=pos+1;
	  dotpos = StrMail.indexOf(".", pos);
	  if (dotpos > (StrMail.length-3) )
		 {
		alert("Il campo email non è stato inserito nel formato corretto.");
		 document.getElementById('email').focus();
		 return (false);
		 }
	  Strmail=StrMail.substr(pos);
	  if ( (StrMail.length < 5) || (dotpos <= 0) )
		 {			 
		 alert("Il campo email non è stato inserito nel formato corretto.");
		 document.getElementById('email').focus();
		 return (false);
		 }
	  }
   else
	  {
	  alert("Il campo email non è stato inserito nel formato corretto.");
	  document.getElementById('email').focus();
	  return (false);
	  }
document.getElementById('mail_status').value="ok";
}

function set_newsletter(mail){

	StrMail=mail;
	if (StrMail.length>6){
	  var pos;
	  var dotpos;
	  pos = StrMail.indexOf("@");
	  if ( (pos >= (StrMail.length-3) ) || (pos < 2) )
		 {
		alert("Il campo email non è stato inserito nel formato corretto.");
		 document.getElementById('email').focus();
		 return (false);		 
		 }
	  pos=pos+1;
	  dotpos = StrMail.indexOf(".", pos);
	  if (dotpos > (StrMail.length-3) )
		 {
		alert("Il campo email non è stato inserito nel formato corretto.");
		 document.getElementById('email').focus();
		 return (false);
		 }
	  Strmail=StrMail.substr(pos);
	  if ( (StrMail.length < 5) || (dotpos <= 0) )
		 {			 
		 alert("Il campo email non è stato inserito nel formato corretto.");
		 document.getElementById('email').focus();
		 return (false);
		 }
	  }
   else
	  {
	  alert("Il campo email non è stato inserito nel formato corretto.");
	  document.getElementById('email').focus();
	  return (false);
	  }

	 $.ajax({
	   type: "POST",
	   url: "newsletter_ajax.php",
	   data: "mail="+mail,
	   success: function(msg){
	   
		 alert("Richiesta di iscrizione elaborata con successo.");
		 /*
		 if(msg==1){
		 	alert("Iscrizione avvenuta con successo.");
		 	}
		 	else{
		 	alert("Indirizzo mail presente.");
		 	}
		 	*/
	   }
	 });

	 
}




	  
	  

function reset_inviato(marchio){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php?marchio="+marchio,
	   data: "id=1",
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 $("#result").html("<p>flag inviti azzerato</p>");
	   }
	 });
}

function can_iscritto(iscr){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=2&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 $(div).html("cancellato");
	   }
	 });
}

function can_mailing(iscr){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=3&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 $(div).html("cancellato");
	   }
	 });
}

function can_news(iscr){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=4&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 $(div).html("cancellato");
	   }
	 });
}

function can_link(iscr){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=11&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 $(div).html("cancellato");
	   }
	 });
}

function can_img(iscr){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=12&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 $(div).html("cancellato");
	   }
	 });
}
function can_prodotto(iscr){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=5&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 $(div).html("cancellato");
	   }
	 });
}

function can_nota(iscr){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=6&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 $(div).html("cancellato");
	   }
	 });
}

function can_allegato(iscr){
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=7&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 $(div).html("cancellato");
	   }
	 });
}

function mod_tasso(tasso,related){
	 tmin=$("#t_min_"+tasso+"_"+related).val();
	 tmax=$("#t_max_"+tasso+"_"+related).val();
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=8&tasso="+tasso+"&related="+related+"&tmin="+tmin+"&tmax="+tmax,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+tasso+"_"+related;
		 $(div).html("modificato");
	   }
	 });
}

function mod_all(all){
	 pesoall=$("#p_a_"+all).val();
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=10&all="+all+"&pesoall="+pesoall,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#mod_peso_"+all;
		 $(div).html("modificato");
	   }
	 });
}

function ante_mailing(iscr){	
	 $.ajax({
	   type: "POST",
	   url: "short_fun.php",
	   data: "id=9&id_iscr="+iscr,
	   success: function(msg){
		 //alert( "Data Saved: " + msg );
		 div="#"+iscr;
		 //$(div).html("cancellato");
	   }
	 });
}



function eseguiRichiesta(url,parameters,id) {

        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // Vedi note sotto
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Non riesco a creare una istanza XMLHTTP');
            return false;
        }
          http_request.onreadystatechange = function() { alertContents(http_request,id); };		  
          http_request.open('POST', url, true);
          http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		  http_request.setRequestHeader("Content-length", parameters.length);
		  http_request.setRequestHeader("Connection", "close");
		  http_request.send(parameters);

    }

    function alertContents(http_request,id) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
				result = http_request.responseText;
				switch(id) {	  
					case "AA":
						document.getElementById('rate_div').innerHTML = result;
						break;
					case "BB":
						document.getElementById('FIRST').innerHTML = result;
						break;
					case "CC":
						document.getElementById('SECOND').innerHTML = result;
						break;
					break;
					}	
            } else {
                alert('Si è verificato un problema con la richiesta');
            }
        }

    }

    
   function calcola_rata(prodotto,finalita,importo,durata,tasso,acconto,riscatto) {     
	 var post_z ='prodotto='+prodotto+'&finalita='+finalita+'&imp='+importo+'&durata='+durata+'&tasso='+tasso+'&acconto='+acconto+'&riscatto='+riscatto;	
	eseguiRichiesta('calcola_rata.php', post_z,'AA');
	 document.getElementById('rate_div').innerHTML = "<em>calcolo rata in corso...</em>";
   }
   
    
    
    
    function compila_rata(prodotto,finalita,tasso,step) {
    durata=$('#durata').val();
	importo=$('#importo').val(); 
	 var post_z ='prodotto='+prodotto+'&finalita='+finalita+'&step='+step+'&tasso='+tasso+'&durata='+durata+'&importo='+importo;	
	switch(step) {	  
		case "FIRST":{			
			eseguiRichiesta('compila_rata.php', post_z,'BB');
			document.getElementById('FIRST').innerHTML = "<em>selezione del tipo di tasso...</em>";
			}
		break;
		case "SECOND":{
			eseguiRichiesta('compila_rata.php', post_z,'CC');
			document.getElementById('SECOND').innerHTML = "<em>selezione della durata ...</em>";
			}
		break;
		}
	
   }
   

function controlloModulo_nl(){
	 
	 if($('#nome').val()==""){
		 alert("campo NOME mancante!");
		 $('#nome').focus();
		 return (false);
		 }
	if($('#cognome').val()==""){
		 alert("campo COGNOME mancante!");
		 $('#cognome').focus();
		 return (false);
		 }
	 if($('#mail').val()==""){
		 alert("campo E-MAIL mancante!");
		 $('#mail').focus();
		 return (false);
		 }	
	StrMail=$('#mail').val();
	   if (StrMail.length>6){
		  var pos;   
		  var dotpos;
		  pos = StrMail.indexOf("@");
		  if ( (pos >= (StrMail.length-3) ) || (pos < 2) )
			 {
			alert("Il campo email non è stato inserito nel formato corretto.");
			$('#mail').focus();
			 return (false);		 
			 }
		  pos=pos+1;
		  dotpos = StrMail.indexOf(".", pos);
		  if (dotpos > (StrMail.length-3) )
			 {
			alert("Il campo email non è stato inserito nel formato corretto.");
			 $('#mail').focus();
			 return (false);
			 }
		  Strmail=StrMail.substr(pos);
		  if ( (StrMail.length < 5) || (dotpos <= 0) )
			 {			 
			 alert("Il campo email non è stato inserito nel formato corretto.");
			 $('#mail').focus();
			 return (false);
			 }
		  }
	   else
		  {
		  alert("Il campo email non è stato inserito nel formato corretto.");
		  $('#mail').focus();
		  return (false);
		  }		
	 if($('#codice').val()==""){
		 alert("campo CODICE DI CONTROLLO mancante!");
		 $('#codice').focus();
		 return (false);
		 }	
	$('#action').val("invia");	
	return (true);
}

function controllaAddNl(){
	 
	if($('#titolo').val()==""){
		 alert("campo TITOLO mancante!");
		 $('#titolo').focus();
		 return (false);
		 }
	if($('#cappelletto').val()==""){
		 alert("campo CAPPELLETTO mancante!");
		 $('#cappelletto').focus();
		 return (false);
		 }
	 if($('#codice').val()==""){
		 alert("campo CODICE DI CONTROLLO mancante!");
		 $('#codice').focus();
		 return (false);
		 }	
	$('#todo').val("create");	
	return (true);	
}

function controllaAddNews(){
	 
	if($('#titolo').val()==""){
		 alert("campo TITOLO mancante!");
		 $('#titolo').focus();
		 return (false);
		 }
	if($('#cappelletto').val()==""){
		 alert("campo CAPPELLETTO mancante!");
		 $('#cappelletto').focus();
		 return (false);
		 }
	 if($('#codice').val()==""){
		 alert("campo CODICE DI CONTROLLO mancante!");
		 $('#codice').focus();
		 return (false);
		 }	
	$('#todo').val("create");	
	return (true);	
}

function controllaEditNl(){
	 
	if($('#titolo').val()==""){
		 alert("campo TITOLO mancante!");
		 $('#titolo').focus();
		 return (false);
		 }
	if($('#cappelletto').val()==""){
		 alert("campo CAPPELLETTO mancante!");
		 $('#cappelletto').focus();
		 return (false);
		 }
	 if($('#codice').val()==""){
		 alert("campo CODICE DI CONTROLLO mancante!");
		 $('#codice').focus();
		 return (false);
		 }	
	$('#todo').val("update");	
	return (true);	
}

function controllaEditNews(){
	 
	if($('#titolo').val()==""){
		 alert("campo TITOLO mancante!");
		 $('#titolo').focus();
		 return (false);
		 }
	if($('#cappelletto').val()==""){
		 alert("campo CAPPELLETTO mancante!");
		 $('#cappelletto').focus();
		 return (false);
		 }
	 if($('#codice').val()==""){
		 alert("campo CODICE DI CONTROLLO mancante!");
		 $('#codice').focus();
		 return (false);
		 }	
	$('#todo').val("update");	
	return (true);	
}


function controlloModulo_canNews(){
	 
	if($('#mail').val()==""){
		 alert("campo E-MAIL mancante!");
		 $('#mail').focus();
		 return (false);
		 }	
	StrMail=$('#mail').val();
	   if (StrMail.length>6){
		  var pos;   
		  var dotpos;
		  pos = StrMail.indexOf("@");
		  if ( (pos >= (StrMail.length-3) ) || (pos < 2) )
			 {
			alert("Il campo email non è stato inserito nel formato corretto.");
			$('#mail').focus();
			 return (false);		 
			 }
		  pos=pos+1;
		  dotpos = StrMail.indexOf(".", pos);
		  if (dotpos > (StrMail.length-3) )
			 {
			alert("Il campo email non è stato inserito nel formato corretto.");
			 $('#mail').focus();
			 return (false);
			 }
		  Strmail=StrMail.substr(pos);
		  if ( (StrMail.length < 5) || (dotpos <= 0) )
			 {			 
			 alert("Il campo email non è stato inserito nel formato corretto.");
			 $('#mail').focus();
			 return (false);
			 }
		  }
	   else
		  {
		  alert("Il campo email non è stato inserito nel formato corretto.");
		  $('#mail').focus();
		  return (false);
		  }		
	 if($('#codice').val()==""){
		 alert("campo CODICE DI CONTROLLO mancante!");
		 $('#codice').focus();
		 return (false);
		 }	
	$('#action').val("invia");	
	return (true);	
}


function evidenzia(elemento){
	elemento.style.border = "1px solid #F65F13";	
	elemento.style.color = "#F65F13";
}

function non_evidenzia(elemento){
	elemento.style.border = "1px solid #CCCCCC";	
	elemento.style.color = "#000000";
}

function cambia_azione(azione, id_socio){	
	document.getElementById('action').value=azione;
	document.getElementById('id_socio').value=id_socio;
	}

function controlloModulo_addinfo(id_socio, azione){
	StrMail=document.getElementById('email_a').value;
	if(StrMail.length>1){
	   if (StrMail.length>6){
		  var pos;   
		  var dotpos;
		  pos = StrMail.indexOf("@");
		  if ( (pos >= (StrMail.length-3) ) || (pos < 2) )
			 {
			alert("Il campo email non è stato inserito nel formato corretto.");
			 document.getElementById('email_a').focus();
			 return (false);		 
			 }
		  pos=pos+1;
		  dotpos = StrMail.indexOf(".", pos);
		  if (dotpos > (StrMail.length-3) )
			 {
			alert("Il campo email non è stato inserito nel formato corretto.");
			 document.getElementById('email_a').focus();
			 return (false);
			 }
		  Strmail=StrMail.substr(pos);
		  if ( (StrMail.length < 5) || (dotpos <= 0) )
			 {			 
			 alert("Il campo email non è stato inserito nel formato corretto.");
			 document.getElementById('email_a').focus();
			 return (false);
			 }
		  }
	   else
		  {
		  alert("Il campo email non è stato inserito nel formato corretto.");
		  document.getElementById('email_a').focus();
		  return (false);
		  }	 
	}
	if(document.getElementById('codice_a').value==""){
		 alert("campo CODICE DI CONTROLLO mancante!");
		 document.getElementById('codice_a').focus();
		 return (false);
		 }
	 if(document.getElementById('privacy_a').checked==0){
		 alert("informativa sulla prinacy non accettata!");		 
		 return (false);
		 }	
	document.getElementById('action').value=azione;	
	document.getElementById('id_socio').value=id_socio;
	return (true);
	

}
function controlloModulo_socio(){
	 
	 if(document.getElementById('nome').value==""){
		 alert("campo NOME mancante!");
		 document.getElementById('nome').focus();
		 return (false);
		 }
	if(document.getElementById('cognome').value==""){
		 alert("campo COGNOME mancante!");
		 document.getElementById('cognome').focus();
		 return (false);
		 }
	if(document.getElementById('nato_a').value==""){
		 alert("campo NATO A mancante!");
		 document.getElementById('nato_a').focus();
		 return (false);
		 }
	if(document.getElementById('nato_il').value==""){
		 alert("campo NATO IL mancante!");
		 document.getElementById('nato_il').focus();
		 return (false);
		 }
	if(document.getElementById('via').value==""){
		 alert("campo INDIRIZZO mancante!");
		 document.getElementById('via').focus();
		 return (false);
		 }
	if(document.getElementById('citta').value==""){
		 alert("campo CITTA mancante!");
		 document.getElementById('citta').focus();
		 return (false);
		 }
	if(document.getElementById('cap').value==""){
		 alert("campo CAP mancante!");
		 document.getElementById('cap').focus();
		 return (false);
		 }
	if(document.getElementById('provincia').value==""){
		 alert("campo PROVINCIA mancante!");
		 document.getElementById('provincia').focus();
		 return (false);
		 }
	if(document.getElementById('tel').value==""){
		 alert("campo TELEFONO mancante!");
		 document.getElementById('tel').focus();
		 return (false);
		 }
	 if(document.getElementById('mail').value==""){
		 alert("campo E-MAIL mancante!");
		 document.getElementById('mail').focus();
		 return (false);
		 }
	StrMail=document.getElementById('mail').value;
	   if (StrMail.length>6){
		  var pos;   
		  var dotpos;
		  pos = StrMail.indexOf("@");
		  if ( (pos >= (StrMail.length-3) ) || (pos < 2) )
			 {
			alert("Il campo email non è stato inserito nel formato corretto.");
			 document.getElementById('mail').focus();
			 return (false);		 
			 }
		  pos=pos+1;
		  dotpos = StrMail.indexOf(".", pos);
		  if (dotpos > (StrMail.length-3) )
			 {
			alert("Il campo email non è stato inserito nel formato corretto.");
			 document.getElementById('mail').focus();
			 return (false);
			 }
		  Strmail=StrMail.substr(pos);
		  if ( (StrMail.length < 5) || (dotpos <= 0) )
			 {			 
			 alert("Il campo email non è stato inserito nel formato corretto.");
			 document.getElementById('mail').focus();
			 return (false);
			 }
		  }
	   else
		  {
		  alert("Il campo email non è stato inserito nel formato corretto.");
		  document.getElementById('mail').focus();
		  return (false);
		  }
	// if(document.getElementById('codfisc').value==""){
	//	 alert("campo CODICE FISCALE mancante!");
	//	 document.getElementById('codfisc').focus();
	//	 return (false);
	//	 }
	//	 else{
	//	var test=ControllaCF(document.getElementById('codfisc').value);
	//	if(test!="") {
	//		alert(test);
	//		document.getElementById('codfisc').focus();
	//		return(false);
	//		}
	//	 }
	if(document.getElementById('abilito').checked==0){
		 alert("accettazione dello statuto non sottoscritta!");		 
		 return (false);
		 }
	 if(document.getElementById('codice').value==""){
		 alert("campo CODICE DI CONTROLLO mancante!");
		 document.getElementById('codice').focus();
		 return (false);
		 }
	 if(document.getElementById('privacy').checked==0){
		 alert("informativa sulla prinacy non accettata!");		 
		 return (false);
		 }
	document.getElementById('action').value="invia";
	return (true);
	//window.location='index3.php?idstruttura='+idstr+'&action=invia';
}

	
	function cancella_mail(campo){
		if (campo.value=="inserisci la tua e-mail"){
			campo.value="";
		}
		else if (campo.value==""){
			campo.value="inserisci la tua e-mail";
		}
	}
	
	


function espandi(elemento){
	
	//dipendenze con funzione freccia
	elem_freccia = document.getElementById("contenitore"+elemento);
	if(elem_freccia){
		if(elem_freccia.style.zIndex>=3){
				elem_freccia.style.zIndex=2;
		}
		else elem_freccia.style.zIndex=3;
	}
	
	if(document.getElementById("elemento"+elemento).style.display=="none"){
			$("#elemento"+elemento).slideDown();
		}
		else
			$("#elemento"+elemento).slideUp();
		}
	
	function espandi_slider(){
		if(document.getElementById("slider").style.display=="none"){
			document.getElementById("slider").style.display="";
		}
		else
			document.getElementById("slider").style.display="none";
	}

 

    

   
   function freccetta(contenitore){
	  	//questa funzione ha dipendeze con esapndi()
		elem_freccia = document.getElementById("contenitore"+contenitore);
		elem_background = elem_freccia.style.background;
		elem_z_index = elem_freccia.style.zIndex;
		//uso zindex come flag stato
		if(elem_z_index == 1 || elem_z_index == ""){
			elem_freccia.style.background = "#FFFFFF url('images/pulsanti-dw.gif') no-repeat top left";
			elem_freccia.style.zIndex=2;			
		}	
		else if (elem_z_index == 2){
			 elem_freccia.style.background = "#FFFFFF url('images/pulsanti-sx.gif') no-repeat   top left";
			 elem_freccia.style.zIndex=1;
   		}
		else if (elem_z_index == 3){
			 elem_freccia.style.background = " #FFFFFF url('images/pulsanti-x-gray.gif') no-repeat   top left";
			 elem_freccia.style.zIndex=4;
   		}
		else if(elem_z_index == 4){
			 elem_freccia.style.background = " #FFFFFF url('images/pulsanti-x-orange.gif') no-repeat  top left";
			 elem_freccia.style.zIndex=3;
   		}
   }
   

/**************************************
    Controllo del Codice Fiscale
    Linguaggio: JavaScript
***************************************/

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 genera_pw(plength){
	var keylist = "abcdefghijklmnopqrstuvwxyz1234567890";
	temp = '';
	for (i=0; i<plength; i++)
		temp += keylist.charAt(Math.floor(Math.random()*keylist.length));
	return temp;
}


