window.addEvent('domready',function(){
	
	var normalopts = {transition: Fx.Transitions.Quad.easeOut, duration: 1000, wheelStops: false, preventDefault:true};
	var smooth = new SmoothScrolling(normalopts);
	smooth.create();
	
	//create the tooltips
	var myTips = new Tips('.tips');
	
	$('finto-checkbox').addEvent('click', function(e) {
		e.stop();
		var myFx = new Fx.Tween(this);
		var valore = this.getChildren().get('text');
		
		if(valore == 0)
		{
			myFx.start('background-color', '#333');
			this.getChildren().set('text', 1);
		}	
		if(valore == 1)
		{
			myFx.start('background-color', '#fff');
			this.getChildren().set('text', 0);
		}
			
		 
	});
	
	var errori = new Array();
	var erroriIt = new Array ("Inserire un nominativo","Inserire un indirizzo valido","Inserire una richiesta","Inserire un numero valido","Inserire il risultato esatto","Accettare le condizioni");
	var erroriEn = new Array ("Insert a name","Inserirt a valid address","Insert a request","Insert a valid number","Insert the correct value","Accept the conditions");
	
	if(window.location.href.search('/en/') > 0)
		errori = erroriEn;
	else
		errori = erroriIt;

	function creaDiv(el, errore) {
		var div = new Element('div');
		var div = new Element('div', {'class' : 'errore-form', 'style' : 'opacity: 0;'}).set('html', errore);
		div.injectAfter(el);
		
		var myFx = new Fx.Tween(div);
		myFx.start('opacity', 0, 1);
	}

	$('invia-modulo').addEvent('click', function(e) {
		errore = 0;

		//PULIZIA
		$$('.errore-form').each(function(div) {
			e.stop();
			div.destroy();
		});
		
		//NOME
		if($('nome').value == '')
		{
			creaDiv($('nome'), errori[0]);
			e.stop();
			errore = 1;
		}
		
		//MAIL
		if(($('email').value == '' || !$('email').value.test(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) && errore == 0)
		{
			creaDiv($('email'), errori[1]);
			e.stop();
			errore = 1;
		}

		//RICHIESTA
		if($('richiesta').value == '' && errore == 0)
		{
			creaDiv($('richiesta'), errori[2]);
			e.stop();
			errore = 1;
		}

		//TELEFONO
		if(($('telefono').value != '') && errore == 0)
		{
			if($('telefono').value.test(/\D+/))
			{
				creaDiv($('telefono'), errori[3]);
				e.stop();
				errore = 1;
			}
		}

		//ACCETTA
		if($('finto-checkbox').getChildren().get('text') == 0 && errore == 0)
		{
			creaDiv($('finto-checkbox'), errori[5]);
			e.stop();
			errore = 1;
		}

		//CAPTCHA
		if(errore == 0)
		{
			if($('captcha').value == '' || $('captcha').value.test(/\D+/))
			{
				creaDiv($('captcha'), errori[4]);
				e.stop();
				errore = 1;
			}
			else
			{
				var myHTMLRequest = new Request({
					method: 'post',
					url: 'include/captcha/return-session-value.php',
					onSuccess: function(response){
						if($('captcha').value != response) {
							creaDiv($('captcha'), errori[4]);
							errore = 1;
						}
						else
							//ci pensiamo alla fine
							$('formContatto').submit();
					}
				}).send();
			}
			
		}
	
	});
	
	$$('.popup').each(function(popup) {
		popup.addEvent('click', function(e) {
			e.stop();
			var par = this.getProperty('href');
			window.open(par, '','toolbar=no,height=400,width=570,menubar=no,directories=no,location=no,scrollbars=yes,status=no,resizable=yes');
		});
	});
	
});