function textCounter(field,cntfield,maxlimit) {
		if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit);
		else
		cntfield.value = maxlimit - field.value.length;
	}
/* Embed Flash 
========================================*/
$(function(){ 	
		
		$("#flashAdesioni").flashembed({ 
				src: "swf/AQF_Wall.swf",
				wmode: "transparent",
				version: [8, 0],
				expressInstall: "http://releases.flowplayer.org/swf/expressinstall.swf",
				onFail: function() { 
		        	$('.noFlash').show(); 
	    		}
	    	},
	    	{baseURL:'services/getAdesioni.php'}
	    );  
		
		// $("#sostenibilita #flashTest").flashembed({ 
				// src: "swf/AQF_test_sostenibilita.swf",
				// wmode: "transparent",
				// version: [8, 0],
				// expressInstall: "http://releases.flowplayer.org/swf/expressinstall.swf",
				// onFail: function() { 
		        	// $('.noFlash').show(); 
	    		// }
	    	// },
	    	// {baseURL:'services/getAdesioni.php'}
	    // );
		
		$("#chegocciasei #flashTest").flashembed({ 
				src: "swf/AQF_test_goccia.swf",
				wmode: "transparent",
				version: [8, 0],
				expressInstall: "http://releases.flowplayer.org/swf/expressinstall.swf",
				onFail: function() { 
		        	$('.noFlash').show(); 
	    		}
	    	},
	    	{baseURL:'services/getAdesioni.php'}
	    );
		
		
		
});

/*========================================*/		




function initFormConcorso(){
	var options = {
		dataType:'json',
		success: function(data){
			if (data.success){
				if ( data.response.vittoria == 'true') {
					$('#mostraConcorso').hide();
					$('#concorsoOK').show();
				
				}
				if ( data.response.vittoria == 'false') {
						$('#mostraConcorso').hide();
						$('#concorsoKO').show();
				}				
			}else{
				$('#captcha img').attr('src', 'img/captcha.php?'+Math.random());
				var validator = $("#frmConcorso").validate();
				for(var i=0; i<data.errors.length; i++){
					//controllo per mostrare l'errore di scontrino già usato
					//alert(data.errors[i].erroreGenerico);
					if (data.errors[i].erroreGenerico=='an error occurred') {
						$('#mostraConcorso').hide();
						$('#concorsoError').show();
					}
					if(data.errors[i].scontrino_usato!=undefined){
						$('#mostraConcorso').hide();
						$('#scontrinoKO').show();
					}
					validator.showErrors(data.errors[i]);
				}
			}
		}
	};
	$('#frmConcorso').ajaxForm(options);
	$('#captcha img').attr('src', 'img/captcha.php?'+Math.random());
};

// jquery onload -------------------				
$(function(){

	// popup
	$('a[rel="popup"]').live('click', function(){
		$(this).apriPopup({'width':'450', 'height':'540'});
		return false;
	});
	$('a[rel="credits"]').live('click', function(){
		$(this).apriPopup({'width':'300', 'height':'300'});
		return false;
	});		
	$('a#popDiritti').live('click', function(){
		$(this).apriPopup({'width':'450', 'height':'540'});
		return false;
	});
	$('a[rel="facebook"]').live('click', function(){
		$(this).apriPopup({'width':'500', 'height':'400'});
		return false;
	});
	// modale
	$('a[rel="modale"]').live('click', function(){
		$(this).apriModale({'color':'#3e3f3c','opacity':'0.1' });
		return false;
	});	
	
	// modale partecipazione al concorso
	$('a[rel="form-modale"]').click(function(){
		$(this).apriModale({'color':'#3e3f3c','opacity':'0.4',
			callback:initFormConcorso});
		return false;
	});
	
	// modale esempio scontrino
	$('#boxConcorso a#esempio').click(function(){
		$(this).apriModale({'color':'#3e3f3c','opacity':'0.1'});
		return false;
	});	
	
	// modale nonhaivinto
	$('#boxConcorso a#nonhaivinto').click(function(){
		$(this).apriModale({'color':'#3e3f3c','opacity':'0.1'});
		return false;
	});	
	
	
/* Firma la Goccia
========================================*/	
	

	$('#btn_preview').click(function(event) {
		var nome= $('#nome').val();
		var msg= $('#messaggio').val();
		if (nome =='' || msg==''){
			$('#previewGoccia').hide();
			$('#frmAdesione').submit();
		}else{
			flashembed("previewGoccia", { 
			src: "swf/AQF_Goccia_Crea.swf",
			flashvars: 'autore='+nome+'&messaggio='+msg,
			wmode: "transparent",
			version: [8, 0] ,
			onFail: function() { 
				$('.noFlash').show(); 
				}
			});
			$('#previewGoccia').show();
		}
	});
	
	$('#frmAdesione').ajaxForm({
		dataType:'json',
		success: function(data){
			if(!data.success){
				var validator = $("#frmAdesione").validate();
				for(var i=0; i<data.errors.length; i++){
					validator.showErrors(data.errors[i]);
				}
			}else{
				if(data.response.insert_complete){				
						$(this).apriModale({
						'target':'#adesione_ok',
						'color':'#3e3f3c',
						'opacity':'0.1'
						}),
						$('#frmAdesione').resetForm();
						$('#previewGoccia').hide();
				}else{
					$('#adesione_ko').show();
				}
			}
		}

	});
	

/*========================================*/	

});




	
