var myreg_email=new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+[\.]([a-z0-9-\.-]+)*([a-z]{2,3})$");


function carica_loading(campo) {
	new Ajax.Updater(campo, "loader.php", {
			method:'get',
			parameters: ''
		});		
}

function carica_loading2(campo) {
	
	$.ajax({
		  type: "GET",
		  dataType: "html",
		  url: "loader2.php",
		  data: "",
		  success: function(dati) {
			   // Aggiorno il contenuto del DIV con i dati ricevuti
			   document.getElementById(campo).innerHTML=dati;
			   
		  },
		  error: function(richiesta,stato,errori) {
			   // Visualizzo un messaggio di errore in caso di chiamata fallita
			   alert('Errore nella chiamata AJAX:'+errori);
		  }
	 });		
	
}


function add_carrello(categoria, prodotto, qta) {

     if(qta=="" || isNaN(qta)!=false) {
          alert("La quantita' del prodotto non e' valida");
          return;
     }
	
	a=document.seleziona_prodotti;
	
	par="categoria="+categoria+"&prodotto="+prodotto+"&qta="+qta+"&modo=add";
	
	cont="cont_oscurato";
	document.getElementById('oscuratore').style.visibility="visible";
	
	new Ajax.Request("ajax_carrello.php", {
					
			method:"post",
			parameters:par,
			onLoading:carica_loading(cont),
			onComplete: function(transport) {
			
				result = transport.responseText || "no response text";
				
				if(result=="no response text") {
					alert("L'aggiunta del prodotto e' fallita.");
					document.getElementById('oscuratore').style.visibility="hidden";
					$(cont).innerHTML="";
					return;
				}
				
				testi=result.split(";");
				
				$('qta_carrello').innerHTML=testi[1];
				$(cont).innerHTML="<b>Operazione completata.</b>";
				
				if(testi[0]=="Esito=ok") {
					alert("Il prodotto e' stato aggiunto al carrello.");	
				} else {
					alert("L'aggiunta del prodotto e' fallita.");		
				}
				
				document.getElementById('oscuratore').style.visibility="hidden";
				$(cont).innerHTML="";
				
			}
					
		}
	);
	
	
}

function modifica_carrello(categoria, prodotto, qta) {

     if(qta=="" || isNaN(qta)!=false) {
          alert("La quantita' del prodotto non e' valida");
          return;
     }

     par="categoria="+categoria+"&prodotto="+prodotto+"&qta="+qta+"&modo=mod";

	cont="cont_oscurato";
	document.getElementById('oscuratore').style.visibility="visible";
	
	new Ajax.Request("ajax_carrello.php", {

			method:"post",
			parameters:par,
			onLoading:carica_loading(cont),
			onComplete: function(transport) {

				result = transport.responseText || "no response text";

				if(result=="no response text") {
					alert("L'aggiunta del prodotto e' fallita.");
					document.getElementById('oscuratore').style.visibility="hidden";
					$(cont).innerHTML="";
					return;
				}

				testi=result.split(";");


				$(cont).innerHTML="<b>Operazione completata.</b>";

				if(testi[0]=="Esito=ok") {
					alert("La quantita' del prodotto e' stata modificata.");
					location.href='invia_richiesta.php';
				} else {
					alert("Modifica fallita.");
				}

				document.getElementById('oscuratore').style.visibility="hidden";
				$(cont).innerHTML="";

			}

		}
	);

}

function elimina_carrello(categoria, prodotto) {

     par="categoria="+categoria+"&prodotto="+prodotto+"&qta=0&modo=del";

	cont="cont_oscurato";
	document.getElementById('oscuratore').style.visibility="visible";
	
	new Ajax.Request("ajax_carrello.php", {

			method:"post",
			parameters:par,
			onLoading:carica_loading(cont),
			onComplete: function(transport) {

				result = transport.responseText || "no response text";

				if(result=="no response text") {
					alert("L'aggiunta del prodotto e' fallita.");
					document.getElementById('oscuratore').style.visibility="hidden";
					$(cont).innerHTML="";
					return;
				}

				testi=result.split(";");


				$(cont).innerHTML="<b>Operazione completata.</b>";

				if(testi[0]=="Esito=ok") {
					alert("Il prodotto e' stato rimosso.");
					location.href='invia_richiesta.php';
				} else {
					alert("La rimozione del prodotto e' fallita.");
				}

				document.getElementById('oscuratore').style.visibility="hidden";
				$(cont).innerHTML="";

			}

		}
	);

}


function ctl_richiesta() {

	a=document.f_richiesta;
	
	if(a.email.value=="") {
	
		alert("L'email e' obbligatoria.");
		
	} else {
		res=myreg_email.test(a.email.value);
		
		if(!res) {
			alert("Attenzione, la casella email non e' corretta.");	
		} else {
			
			if(a.consenso.checked==false) {
				alert("Prestare consenso al trattamento dei dati personali");	
			} else {
				a.submit();
			}
		}
	}
	
}


function vis_dati_aziendali(mod) {
	/*
	if(mod=="on") {
		
		new Ajax.Updater("cont_oscurato", "dati_aziendali.php", {
						 
			method:'get',
			parameters: ''
			
			}
						 
		);
	
		document.getElementById('oscuratore').style.visibility='visible';
		
	} else {
		document.getElementById('oscuratore').style.visibility='hidden';	
	} */
	
	//parametro = document.getElementById('parametroDaPassare').value;
	 
	     // Effettuo la chiamata Ajax
		 
	if(mod=="on") {		 
	     $.ajax({
	          type: "GET",
	          dataType: "html",
	          url: "dati_aziendali.php",
	          data: "",
	          success: function(dati) {
	               // Aggiorno il contenuto del DIV con i dati ricevuti
	               document.getElementById("cont_oscurato").innerHTML=dati;
				   document.getElementById('oscuratore').style.visibility='visible';
			  },
	          error: function(richiesta,stato,errori) {
	               // Visualizzo un messaggio di errore in caso di chiamata fallita
	               alert('Errore nella chiamata AJAX:'+errori);
	          }
	     });
	} else {
		document.getElementById('oscuratore').style.visibility='hidden';	
	}
	
}

function vis_legenda(x) {
	
	par="tipo="+x;
	new Ajax.Updater("cont_oscurato", "legende.php", {
						 
			method:'get',
			parameters: par,
			onComplete: document.getElementById('oscuratore').style.visibility='visible'
			
			}
						 
		);
	
		document.getElementById('oscuratore').style.visibility='visible';	
	
}

function vis_verde_fedelta() {
	$.ajax({
		  type: "GET",
		  dataType: "html",
		  url: "verde_fedelta.php",
		  data: "",
		  success: function(dati) {
			   // Aggiorno il contenuto del DIV con i dati ricevuti
			   document.getElementById("cont_oscurato").innerHTML=dati;
			   document.getElementById('oscuratore').style.visibility='visible';
		  },
		  error: function(richiesta,stato,errori) {
			   // Visualizzo un messaggio di errore in caso di chiamata fallita
			   alert('Errore nella chiamata AJAX:'+errori);
		  }
	 });	
	
}


function ctl_letter() {
	
	a=document.f_registrazione;
	obblighi=0;
	tipo=0;
	
	errori= new Array();
	
	for(i=0; i<a.professione.length; i++) {
	
		if(a.professione[i].checked==false) {
			obblighi++;
			
		} else {
			
			if(i==3) {
				tipo=2;	
			} else {
				tipo=1;	
			}	
		}
		
	}
	if(obblighi>3) {
		alert("Attenzione indica con che tipo di utente vuoi registrarti");
		return;
	} else {
		obblighi=0;
		
		if(tipo==1) {
			
			if(a.sesso[0].checked==false &&	a.sesso[1].checked==false) {
				obblighi++;	
			}
		}
		
		if(a.provincia.value=="") {
			obblighi++;	
		}
		
		if(a.email.value=="") {
			obblighi++;	
		}
		
		if(obblighi>0) {
			stringa="Uno o piu' dei seguenti campi obbligatori sono rimasti vuoti\n\r";
			if(tipo==1) {
				stringa=stringa+"sesso, provincia, email";	
			} 
			if(tipo==2) {
				stringa=stringa+"provincia, email";	
			}
			alert(stringa);
			return;
		}
		
		res=myreg_email.test(a.email.value);
		if(!res) {
			alert("La casella email non e' scritta in modo corretto.");
			return;
		}
		
	}
	
	if(a.consenso.checked==true) {
		 a.submit();
		//alert("Ciao");
	} else {
		alert("E' necessario prestare il consenso al trattamento dei dati personali");	
	}
}


function ctl_preventivo() {

	a=document.f_richiesta;
	
	if(a.email.value=="" || a.comune.value=="" || a.telefono.value=="" || a.testo.value=="") {
		
		alert("Attenzione. I campi in rosso sono tutti obbligatori.");
		return;
		
	}
	
	res=myreg_email.test(a.email.value);
	if(!res) {
	
		alert("L'email non e' scritta correttamente");
		return;
	}
	
	if(a.somma.value=="") {
		alert("Inserire la somma dei due numeri in rosso.");
		return;
	}
	
	if(a.somma.value!=a.res_somma.value) {
		alert("La somma dei due numeri e' errata.");
		return;
	}
	
	if(a.consenso.checked==true) {
		 a.submit();
		//alert("Ciao");
	} else {
		alert("E' necessario prestare il consenso al trattamento dei dati personali");	
	}
	
}


function ctl_descrizione(testo) {
	
	alert(testo);

	document.getElementById("cont_descrizione").innerHTML=testo;
	
}

function foto_galleria(foto) {

	//carica_loading2('cont_dettaglio');
	
	$.ajax({
		  type: "POST",
		  dataType: "html",
		  url: "foto_galleria.php",
		  data: "foto="+foto,
		  success: function(dati) {
			   // Aggiorno il contenuto del DIV con i dati ricevuti
			   document.getElementById('cont_dettaglio').innerHTML=dati;
			   
		  },
		  error: function(richiesta,stato,errori) {
			   // Visualizzo un messaggio di errore in caso di chiamata fallita
			   alert("Errore caricamento foto");
		  }
	 });
	
	
}

function agg_didascalia(id) {

	
	$.ajax({
		  type: "GET",
		  dataType: "html",
		  url: "didascalia_gallery.php",
		  data: "id="+id,
		  success: function(dati) {
			   // Aggiorno il contenuto del DIV con i dati ricevuti
			   document.getElementById('cont_descrizione').innerHTML=dati;
			   
		  },
		  error: function(richiesta,stato,errori) {
			   // Visualizzo un messaggio di errore in caso di chiamata fallita
			   alert("Errore caricamento descrizione");
		  }
	 });
	
}

var s_lampeggio=1;

function lamp_news() {
    if(s_lampeggio==1) {
        document.getElementById('link_news').style.display='none';
        s_lampeggio=0;
    } else {
        document.getElementById('link_news').style.display='block';
        s_lampeggio=1;
    }
    
    setTimeout("lamp_news()", 0800);
    
}


/*****   GOOGLE ANALYTICS **********/

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-23875351-4']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  
/******   FINE GOOGLE ANALYTICS ***********/

