$(function(){
	// START SLIDESHOW 
  	$('.animazione').cycle({fx:'fade', timeout:7000});
  	$('#animazioneTour').cycle({fx:'fade', timeout:6000});
	// END SLIDESHOW 
	
	
	// START DAILY TOURS
 	//$("a[rel='zoom']").colorbox();
 	$("a[title='googlemap']").colorbox({width:"80%", height:"80%", iframe:true});
    // END DAILY TOURS
	
	
	$('#messaggioGenerico').dialog({
		bgiframe:true, 
		autoOpen:false, 
		width:'800px', 
		modal:true, 
		title:'', 
		buttons:{
			'Chiudi': function(){ 
				$(this).dialog('close'); 
			} 
		}
	});
	
	$.validator.setDefaults({
		debug:true
	});
	
	
	// START gestione TOURS
	$('#dataTour').datepicker({
		changeMonth:true,
		changeYear:true, 
		showButtonPanel:true, 
		dateFormat:'dd-mm-yy'
	});
	
	$('#dataArrivo').datepicker({
		changeMonth:true,
		changeYear:true, 
		showButtonPanel:true, 
		dateFormat:'dd-mm-yy', 
		minDate:1, 
// 		onSelect: function(dateText, inst){
// 			$('#dataPartenza').attr('value',dateText);
// 			$('#dataPartenza').datepicker('option', 'minDate', dateText);
// 		}
	});
	
	$('#dataArrivo').change(function() {
		test = $(this).datepicker('getDate');
		testm = new Date(test.getTime());
		testm.setDate(testm.getDate());
		
		$('#dataPartenza').attr('value',test);
		$('#dataPartenza').datepicker('option', 'minDate', testm);
	});

	
	$('#dataPartenza').datepicker({
		changeMonth:true,
		changeYear:true, 
		showButtonPanel:true, 
		dateFormat:'dd-mm-yy', 
		minDate:1
	});
	
	var validaDatiOfferte = $('#offerteForm').validate({
		rules:{
			nome:{required:true},
			cognome:{required:true},
			email:{required:true, email:true},
			dataArrivo:{required:true},
			dataPartenza:{required:true},
			privacyOk:{required:true}, 
 			codiceVerifica:{required:true, equalTo:'#codiceVerificaControllo'}
		},
		messages:{
			nome:{required:'<br />'+CONTATTI_MSG_1_JS},
			cognome:{required:'<br />'+CONTATTI_MSG_2_JS},
			email:{required:'<br />'+CONTATTI_MSG_3_JS, email:'<br />'+CONTATTI_MSG_4_JS},
			privacyOk:{required:'<br />'+CONTATTI_MSG_6_JS}, 
			codiceVerifica:{required:'<br />'+CONTATTI_MSG_8_JS, equalTo:'<br />'+CONTATTI_MSG_7_JS}
		}
	});
	
	$('#offerteInvio').click(function(){
		if($('#offerteForm').validate().form()){
			var nome = $('#nome').val();
			var cognome = $('#cognome').val();
			var email = $('#email').val();
			var dataArrivo = $('#dataArrivo').val();
			var dataPartenza = $('#dataPartenza').val();
			var numeroPersone = $('#numeroPersone').val();
			var tipoStanza = $('#tipoStanza').val();
			var numeroStanze = $('#numeroStanze').val();
			var hotel = $('#hotel').val();
			var fileGestione = livelliPercorso+'moduli/offerte-invio-email.php';
			
			$.ajax(
				{
					type:'POST',
					url:fileGestione,
					data:{
						nome:nome, 
						cognome:cognome, 
						email:email, 
						dataArrivo:dataArrivo, 
						dataPartenza:dataPartenza, 
						numeroPersone:numeroPersone, 
						tipoStanza:tipoStanza, 
						numeroStanze:numeroStanze, 
						hotel:hotel, 
						lingua:lingua
					},
					dataType:'html',
					success:function(messaggio){
						if(messaggio.replace(/\s/g,"") != ""){ // il replace serve ad assicurarsi che il messaggio di ritorno non contenga spazi bianchi
							$('#messaggioGenerico').data('title.dialog', 'Errore su invio email');
							$('#messaggioGenerico').html(EMAIL_INVIO_NO_JS);
							$('#messaggioGenerico').dialog('open');
						}else{
							$('#messaggioGenerico').data('title.dialog', 'Conferma invio email'); 
							$('#messaggioGenerico').html(EMAIL_INVIO_OK_JS);
							$('#messaggioGenerico').dialog('open');
							$('#offerteAnnulla').click();
						}
					}
				}
			);
		}
	});
	
	$('#offerteAnnulla').click(function(){
  		validaDatiOfferte.resetForm();
	});
	// END gestione TOURS
	
	
	
	
	// START gestione EVENTI
	$('#condizioniUtlizzo').click(function(){
		$('#messaggioGenerico').data('title.dialog', EVENTI_CONDIZIONI_1_JS);
		$('#messaggioGenerico').html(EVENTI_CONDIZIONI_2_JS);
  		$('#messaggioGenerico').dialog('open');
	});
	
	$('#dataInizio').datepicker({
		changeMonth:true,
		changeYear:true, 
		showButtonPanel:true, 
		dateFormat:'dd-mm-yy', 
 		minDate:0
	});
	
	$('#dataFine').datepicker({
		changeMonth:true,
		changeYear:true, 
		showButtonPanel:true, 
		dateFormat:'dd-mm-yy'
	});
	// END gestione EVENTI
	
	
	
	// START gestione CONTATTI
	var validaDatiContatti = $('#contattiForm').validate({
		rules:{
			nome:{required:true},
			cognome:{required:true},
			email:{required:true, email:true},
			testo:{required:true}, 
			privacyOk:{required:true}, 
 			codiceVerifica:{required:true, equalTo:'#codiceVerificaControllo'}
		},
		messages:{
			nome:{required:'<br />'+CONTATTI_MSG_1_JS},
			cognome:{required:'<br />'+CONTATTI_MSG_2_JS},
			email:{required:'<br />'+CONTATTI_MSG_3_JS, email:'<br />'+CONTATTI_MSG_4_JS},
			testo:{required:'<br />'+CONTATTI_MSG_5_JS}, 
			privacyOk:{required:'<br />'+CONTATTI_MSG_6_JS}, 
			codiceVerifica:{required:'<br />'+CONTATTI_MSG_8_JS, equalTo:'<br />'+CONTATTI_MSG_7_JS}
		}
	});
	
	$('#contattiInvio').click(function(){
		if($('#contattiForm').validate().form()){
			var nome = $('#nome').val();
			var cognome = $('#cognome').val();
			var indirizzo = $('#indirizzo').val();
			var telefonoFax = $('#telefono').val();
			var email = $('#email').val();
			var messaggio = $('#testo').val();
			var fileGestione = livelliPercorso+'moduli/contatti-invio-email.php';
			
			$.ajax(
				{
					type:'POST',
					url:fileGestione,
					data:{
						nome:nome, 
						cognome:cognome, 
						indirizzo:indirizzo, 
						telefonoFax:telefonoFax, 
						email:email, 
						messaggio:messaggio, 
						lingua:lingua
					},
					dataType:'html',
					success:function(messaggio){
						if(messaggio.replace(/\s/g,"") != ""){ // il replace serve ad assicurarsi che il messaggio di ritorno non contenga spazi bianchi
							$('#messaggioGenerico').data('title.dialog', 'Errore su invio email');
							$('#messaggioGenerico').html(EMAIL_INVIO_NO_JS);
							$('#messaggioGenerico').dialog('open');
						}else{
							$('#messaggioGenerico').data('title.dialog', 'Conferma invio email'); 
							$('#messaggioGenerico').html(EMAIL_INVIO_OK_JS);
							$('#messaggioGenerico').dialog('open');
							$('#contattiAnnulla').click();
						}
					}
				}
			);
		}
	});
	
	$('#contattiAnnulla').click(function(){
  		validaDatiContatti.resetForm();
	});
	
	$('#contattiPrivacyLink').click(function(){
		$('#messaggioGenerico').data('title.dialog', CONTATTI_TESTO_2_JS);
		$('#messaggioGenerico').html(PRIVACY_JS);
  		$('#messaggioGenerico').dialog('open');
	});
	// END gestione CONTATTI
	
	
	// START gestione NEWSLETTER
	var validaDatiNewsletter = $('#newsletterForm').validate({
		rules:{
			nome:{required:true},
			cognome:{required:true},
			email:{required:true, email:true},
			privacyOk:{required:true}, 
 			codiceVerifica:{required:true, equalTo:'#codiceVerificaControllo'}
		},
		messages:{
			nome:{required:'<br />'+NEWSLETTER_MSG_1_JS},
			cognome:{required:'<br />'+NEWSLETTER_MSG_2_JS},
			email:{required:'<br />'+NEWSLETTER_MSG_3_JS, email:'<br />'+NEWSLETTER_MSG_4_JS},
			privacyOk:{required:'<br />'+NEWSLETTER_MSG_5_JS}, 
			codiceVerifica:{required:'<br />'+NEWSLETTER_MSG_7_JS, equalTo:'<br />'+NEWSLETTER_MSG_6_JS}
		}
	});
	
	$('#newsletterInvio').click(function(){
		if($('#newsletterForm').validate().form()){
			var nome = $('#nome').val();
			var cognome = $('#cognome').val();
			var email = $('#email').val();
			var fileGestione = livelliPercorso+'moduli/newsletter-invio-email.php';
			
			$.ajax(
				{
					type:'POST',
					url:fileGestione,
					data:{
						nome:nome, 
						cognome:cognome, 
						email:email, 
						lingua:lingua
					},
					dataType:'html',
					success:function(messaggio){
						if(messaggio.replace(/\s/g,"") != ""){ // il replace serve ad assicurarsi che il messaggio di ritorno non contenga spazi bianchi
							$('#messaggioGenerico').data('title.dialog', 'Errore su invio email');
// 							$('#messaggioGenerico').html(EMAIL_INVIO_NO_JS+'<br /><br/>'+messaggio);
							$('#messaggioGenerico').html(messaggio);
							$('#messaggioGenerico').dialog('open');
						}else{
							$('#messaggioGenerico').data('title.dialog', 'Conferma invio email'); 
							$('#messaggioGenerico').html(EMAIL_NEWSLETTER_INVIO_OK_JS);
							$('#messaggioGenerico').dialog('open');
							$('#newsletterAnnulla').click();
						}
					}
				}
			);
		}
	});
	
	$('#newsletterAnnulla').click(function(){
  		validaDatiNewsletter.resetForm();
	});
	// END gestione NEWSLETTER
	
	
	
	// START gestione CUSTOM EVENTS
	var validaDatiCustomEvents = $('#customEventsForm').validate({
		rules:{
			nome:{required:true},
			cognome:{required:true},
			email:{required:true, email:true},
			email_conferma:{required:true, equalTo:'#email'}, 
			messaggio:{required:true},
			privacyOk:{required:true}, 
 			codiceVerifica:{required:true, equalTo:'#codiceVerificaControllo'}
		},
		messages:{
			nome:{required:'<br />'+CUSTOM_EVENTS_MSG_1_JS},
			cognome:{required:'<br />'+CUSTOM_EVENTS_MSG_2_JS},
			email:{required:'<br />'+CUSTOM_EVENTS_MSG_3_JS, email:'<br />'+CUSTOM_EVENTS_MSG_4_JS},
			email_conferma:{required:'<br />'+CUSTOM_EVENTS_MSG_16_JS, equalTo:'<br />'+CUSTOM_EVENTS_MSG_15_JS},
			messaggio:{required:'<br />'+CUSTOM_EVENTS_MSG_14_JS},
			privacyOk:{required:'<br />'+CUSTOM_EVENTS_MSG_11_JS}, 
			codiceVerifica:{required:'<br />'+CUSTOM_EVENTS_MSG_13_JS, equalTo:'<br />'+CUSTOM_EVENTS_MSG_12_JS}
		}
	});
	
	$('#customEventsInvio').click(function(){
		if($('#customEventsForm').validate().form()){
			var nome = $('#nome').val();
			var cognome = $('#cognome').val();
			var email = $('#email').val();
			var messaggio = $('#messaggio').val();
			var fileGestione = livelliPercorso+'moduli/events-custom-invio-email.php';
			
			$.ajax(
				{
					type:'POST',
					url:fileGestione,
					data:{
						nome:nome, 
						cognome:cognome, 
						email:email, 
						messaggio:messaggio, 
						lingua:lingua
					},
					dataType:'html',
					success:function(messaggio){
						if(messaggio.replace(/\s/g,"") != ""){ // il replace serve ad assicurarsi che il messaggio di ritorno non contenga spazi bianchi
							$('#messaggioGenerico').data('title.dialog', 'Errore su invio email');
// 							$('#messaggioGenerico').html(EMAIL_INVIO_NO_JS+'<br /><br/>'+messaggio);
							$('#messaggioGenerico').html(messaggio);
							$('#messaggioGenerico').dialog('open');
						}else{
							$('#messaggioGenerico').data('title.dialog', 'Conferma invio email'); 
							$('#messaggioGenerico').html(EMAIL_CUSTOM_EVENTS_OK_JS);
							$('#messaggioGenerico').dialog('open');
							$('#customEventsAnnulla').click();
						}
					}
				}
			);
		}
	});
	
	$('#customEventsAnnulla').click(function(){
  		validaDatiCustomEvents.resetForm();
	});
	
// 	var validaDatiCustomEvents = $('#customEventsForm').validate({
// 		rules:{
// 			nome:{required:true},
// 			cognome:{required:true},
// 			email:{required:true, email:true},
// 			partecipanti:{required:true},
// 			durata:{required:true},
// 			alloggio:{required:true},
// 			luoghi:{required:true},
// 			visite:{required:true},
// 			trasporto:{required:true},
// 			privacyOk:{required:true}, 
//  			codiceVerifica:{required:true, equalTo:'#codiceVerificaControllo'}
// 		},
// 		messages:{
// 			nome:{required:'<br />'+CUSTOM_EVENTS_MSG_1_JS},
// 			cognome:{required:'<br />'+CUSTOM_EVENTS_MSG_2_JS},
// 			email:{required:'<br />'+CUSTOM_EVENTS_MSG_3_JS, email:'<br />'+CUSTOM_EVENTS_MSG_4_JS},
// 			partecipanti:{required:'<br />'+CUSTOM_EVENTS_MSG_5_JS},
// 			durata:{required:'<br />'+CUSTOM_EVENTS_MSG_6_JS},
// 			alloggio:{required:'<br />'+CUSTOM_EVENTS_MSG_7_JS},
// 			luoghi:{required:'<br />'+CUSTOM_EVENTS_MSG_8_JS},
// 			visite:{required:'<br />'+CUSTOM_EVENTS_MSG_9_JS},
// 			trasporto:{required:'<br />'+CUSTOM_EVENTS_MSG_10_JS},
// 			privacyOk:{required:'<br />'+CUSTOM_EVENTS_MSG_11_JS}, 
// 			codiceVerifica:{required:'<br />'+CUSTOM_EVENTS_MSG_13_JS, equalTo:'<br />'+CUSTOM_EVENTS_MSG_12_JS}
// 		}
// 	});
// 	
// 	$('#customEventsInvio').click(function(){
// 		if($('#customEventsForm').validate().form()){
// 			var nome = $('#nome').val();
// 			var cognome = $('#cognome').val();
// 			var email = $('#email').val();
// 			var partecipanti = $('#partecipanti').val();
// 			var durata = $('#durata').val();
// 			var alloggio = $('#alloggio').val();
// 			var luoghi = $('#luoghi').val();
// 			var visite = $('#visite').val();
// 			var trasporto = $('#trasporto').val();
// 			var fileGestione = livelliPercorso+'moduli/events-custom-invio-email.php';
// 			
// 			$.ajax(
// 				{
// 					type:'POST',
// 					url:fileGestione,
// 					data:{
// 						nome:nome, 
// 						cognome:cognome, 
// 						email:email, 
// 						partecipanti:partecipanti, 
// 						durata:durata, 
// 						alloggio:alloggio, 
// 						luoghi:luoghi, 
// 						visite:visite, 
// 						trasporto:trasporto, 
// 						lingua:lingua
// 					},
// 					dataType:'html',
// 					success:function(messaggio){
// 						if(messaggio.replace(/\s/g,"") != ""){ // il replace serve ad assicurarsi che il messaggio di ritorno non contenga spazi bianchi
// 							$('#messaggioGenerico').data('title.dialog', 'Errore su invio email');
// // 							$('#messaggioGenerico').html(EMAIL_INVIO_NO_JS+'<br /><br/>'+messaggio);
// 							$('#messaggioGenerico').html(messaggio);
// 							$('#messaggioGenerico').dialog('open');
// 						}else{
// 							$('#messaggioGenerico').data('title.dialog', 'Conferma invio email'); 
// 							$('#messaggioGenerico').html(EMAIL_CUSTOM_EVENTS_OK_JS);
// 							$('#messaggioGenerico').dialog('open');
// 							$('#customEventsAnnulla').click();
// 						}
// 					}
// 				}
// 			);
// 		}
// 	});
// 	
// 	$('#customEventsAnnulla').click(function(){
//   		validaDatiCustomEvents.resetForm();
// 	});
	// END gestione CUSTOM EVENTS
	
	
	
	// START gestione CARRELLO TOURS
	if($('input[name=os1]').attr('readonly') == false){
		$('input[name=os1]').datepicker({
			changeMonth:true,
			changeYear:true, 
			showButtonPanel:true, 
			dateFormat:'dd-mm-yy', 
			minDate:3
		});
	}
	
// 	function isValidEmailAddress(emailAddress){
// 		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
// 		return pattern.test(emailAddress);
// 	}
	
	$('input[name=submit]').click(function(){
		if($('input[name=os1]').val() == ''){
			$('input[name=os1]').focus();
			return false;
		}else{
			if($('input[name=os2]').val() == ''){
				$('input[name=os2]').focus();
				return false;
			}/*else{
				if(!isValidEmailAddress($('input[name=os2]').val())){
					$('input[name=os2]').focus();
					$('input[name=os2]').attr('value','');
					return false;
				}
			}*/
		}
	});
	// END gestione CARRELLO TOURS
	
});


