window.addEvent('domready', function() {
	
	//recupera html del form di ricerca per le integraizoni
    if($('ricerca_html')) {
		var req = new Request.HTML({url:'http://www.g40.it/ar/offerte.php',
			onSuccess: function(aa,bb,html) {if(html)$('ricerca_html').set('value',html);},
			onFailure: function(error) {}
		}).get({'act':'cerca','i':'1','a':'49','m':'p','l':'0','html':'1'});
	}


    var menu_aperto=false;
    var menu_p=false;
	//XXMOD menu principale a comaprsa
	$$('.menu_attivo').addEvent('mouseover',function() {
		var pos=this.getCoordinates('liv1');
		//alert(pos.left);
		//alert(pos.left);
		var ids=this.get('id').split('_');
		if(menu_aperto && menu_aperto!='s_'+ids[1])  {$(menu_aperto).setStyle('display','none');$(menu_p).removeClass('sel');}
		this.addClass('sel');

		if(ids[1]==7)$('s_'+ids[1]).setStyle('left',(pos.left-433));
		else if(ids[1]==8)$('s_'+ids[1]).setStyle('left',(pos.left-428));
		else $('s_'+ids[1]).setStyle('left',(pos.left-288));
		$('s_'+ids[1]).setStyle('display','block');
		menu_aperto='s_'+ids[1];
		menu_p='m_'+ids[1];
	});
	$$('#testata_in','#corpo','.menu_chiudi').addEvent('mouseover',function() {
		if(menu_aperto){
			$(menu_aperto).setStyle('display','none');
			$(menu_p).removeClass('sel');
			menu_aperto=false;
		}
	});



	if($('form_1')) {
	    setTimeout('document.form_1.submit()', 2000);
	    //$('form_1').addEvent('click',function() {alert('asd');});
	    //alert($('form_1').getProperty("action"));
	    //$('form_1').fireEvent('click');
	}

	function ControllaCF(cf) {
	    var validi, i, s, set1, set2, setpari, setdisp;
	    if( cf == '' )  return '';
	    cf = cf.toUpperCase();
	    if( cf.length != 16 ) return false;
	    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	    for( i = 0; i < 16; i++ ){
	        if( validi.indexOf( cf.charAt(i) ) == -1 )
	            return false;
	    }
	    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 false;
	    return true;
	}

	if($('multipla_inizio_1')){
		var multiple=new Array();
		for(var i=1;i<=25;i++) multiple[i]=$('multipla_inizio_'+i).get('value');
	}
	
			
	$$('.multipla_aggiungi').each(function (link) {
		link.addEvent('click',function(evento) {
			evento.preventDefault();
			var ids=this.get('id').split('_');
			var id=ids[2]+"_"+multiple[ids[2]];
			if($('multipla_'+id)){
				$('multipla_'+id).setStyle('opacity',0);
				$('multipla_'+id).setStyle('display','block');
				$('multipla_'+id).fade(1);
			}
			multiple[ids[2]]++;
		})
	});
	$$('.questionario_del').each(function (link) {
		link.addEvent('click',function(evento) {
			evento.preventDefault();
			var ids=this.get('id').split('_');
			var id=ids[1];
			$('tabella_'+id).fade(0);
			blocco_nascondi=function(){this.setStyles({'height':'0','margin':'0','display':'none'});};
			blocco_nascondi.delay(800, $('tabella_'+id));
		})
	});
	
	if($('bravissimo')) {
		$('bravissimo').addEvent('click',function(evento) {
	  		evento.preventDefault();
			var myRequest = new Request.HTML({url:'http://www.bookfly.it/bravonet/Sfoglia.aspx'}).get({'username': $('issimo_usr').get('value'),'password':$('issimo_pas').get('value')});
		});
	}
	
	if($('x_aggiorna')) {
		$('x_aggiorna').addEvent('click',function(evento) {
			//evento.preventDefault();
			var totale=0;
			var persone=0;
			
			$$('.x_sum').each(function(campo) {
				var xx=campo.get('value').toFloat();
				if(xx)totale=totale+(xx*1);
			});
			$$('.x_persona').each(function(campo) {
				if(campo.get('value')!="") persone=persone+1;				
			});
			
			
			if($('x_iscrizione')) {
				var isc=$('x_iscrizione2').get('value').toFloat();
				var tot_persone=persone*isc;
				$('x_iscrizione').set('value',tot_persone) ;
				totale=totale+tot_persone; 
			}
			if($('x_oneri')) {
				var one=$('x_oneri2').get('value').toFloat();
				var tot_persone=persone*one;
				$('x_oneri').set('value',tot_persone) ;
				totale=totale+tot_persone; 
			}
			if($('x_settimana')) {
				var isc=$('x_settimana').get('value').toFloat();
				if($('x_settimana_si').checked) {
					var tot_persone=persone*isc;
					totale=totale+tot_persone;
				}
				
			}
			$('x_totale').set('value',totale);
			
		});
		$('x_aggiorna').fireEvent('click');
	
	}
	if($("note_g40")){
		$("note_bravo").addEvent("click", function(evento) {
			evento.preventDefault();
			if ($("note_g402").getStyle("display") == "none") {
				$("note_g402").setStyle("display", "block");
			} else {
				$("note_g402").setStyle("display", "none");
			}
		});
	}
	
	if($('form_prenotazione')) {
		$('form_prenotazione').addEvent('submit',function(evento) {
			var vai=true;
			if($('aeroporto')) {
				if($('aeroporto').value=='no') {alert("E' necessario selezionare un aeroporto di partenza");vai=false;}
			}
			if($('prenotazione_email').get('value').search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/)== -1){vai=false;alert('Si prega di specificare un indirizzo email valido');}
			

			$$('.nato_il').each(function(nato_il) {
				var data_nascita=nato_il.get('value');
				var nome=nato_il.get('name');
				var id_riga=nome.charAt(2);
				if($('n_'+id_riga+'2').get('value')!="") {
				 if(data_nascita=="") {vai=false;alert('Si prega di inserire una data di nascita per il/i bambino/i.');} 
				}
			});
			
			if(!vai)evento.preventDefault(); 
		});
		
	}
	var vecchio=1;
	$$('.offerte_pag_blocco').each(function (blocco) {
		
		blocco.getElements('.offerta_pag_menu').addEvent('click',function(evento) {
			var num=this.get('html');
			if(num!=vecchio) {
				
				$$('.offerta_pag_menu_'+num).setStyles({'background':'#7FC6FF','font-weight':'bold'});
				$$('.offerta_pag_menu_'+vecchio).setStyles({'background':'#eaeaea','font-weight':'normal'});
				$('offerte_pag_'+vecchio).setStyle('display','none');
				$('offerte_pag_'+num).setStyle('display','block');
				vecchio=num;
			}
		});
	
	});
	//if($('.offerta_pag_menu'))$('.offerta_pag_menu').fireEvent('click');



	$$('.tooltip').each(function (item, index) {
		if(item) {
			var content = item.get('title').split('::');
			item.store('tip:title', content[0]);
			item.store('tip:text', content[1]);
		}
	});
	var tipz = new Tips('.tooltip');

	//tooltip piccolo
	var tipmini=$$('.tipmini');
	tipmini.each(function (item, index) {
		item.store('tip:title','' );
		item.store('tip:text', item.get('title'));
	});
	var tipz2=new Tips(tipmini,{className:'tip_mini',fixed:true,offsets:{'x': 75, 'y': 30}});



	var rainbow=false;
	var rainbow_id=1;
	$$('.colore').each(function(colore) {
		colore.addEvent('focus',function (evento) {
			evento.preventDefault();
			//rainbow=false;
			var id=this.get('id');
			var col=new Color(this.get('value'));

			//alert(id);
			rainbow=new MooRainbow(id, {
				'id': rainbow_id+'-pick-'+id,
				'startColor': col,
				'onChange': function(color) {
					$(id).value = color.hex;
				}
			});
			rainbow_id++;
		});
	});
	if($('integrazione')) {

	    $('integrazione').addEvent('submit',function(evento) {
	        //evento.preventDefault();
	        var successo=true;
			//numero offerte
			if(parseInt($('n_offerte').get('value'))<5 || parseInt($('n_offerte').get('value'))>15)  successo=false;
			//larghezza
			if(parseInt($('larghezza').get('value'))<550 || parseInt($('larghezza').get('value'))>1250)  successo=false;
			//larghezza cerca
			if(parseInt($('larghezza_cerca').get('value'))<170 || parseInt($('larghezza_cerca').get('value'))>400)  successo=false;
			//larghezza box
			if(parseInt($('larghezza_box').get('value'))<180 || parseInt($('larghezza_box').get('value'))>300)  successo=false;
	        //if($('n_offerte').get('value')<5 || $('n_offerte').get('value')>15)  successo=false;
	        if(!successo) alert('Attenzione, alcuni valori inseriti non soddisfano i criteri richiesti. Ti preghiamo di ricontrollare. Grazie.');
	        return successo;

	    });

	}



});