$(function() {
	//form check
	$('.required').parent().children('label').append(' <em class="orange">*</em>');
	$('.error').live('change', function() {
		$(this).removeClass('error');
	});

	$('.form').submit(function() {
		
		if ($(this).hasClass('booking')){
			b = '<strong>Richiesta informazioni dal sito web</strong><hr> <strong>Nome e cognome:</strong> ' + $('input[name="nome"]').val()+
			'<br> <strong>Email:</strong> ' + $('.email').val() + 
				'<br> <strong>Numero Persone:</strong> ' + $('input[name="numb"]').val() +
				'<br> <strong>Data:</strong> ' + $('input[name="date"]').val() + 
				'<br> <strong>Orario:</strong> ' + $('select[name="orario"]').val();
		} else {
			b = '<strong>Richiesta informazioni dal sito web</strong><hr> <strong>Nome e cognome:</strong> ' + $('input[name="nome"]').val()+
			'<br> <strong>Email:</strong> ' + $('.email').val() + 
			'<br> <hr> <strong>Telefono:</strong><br>' + $('input[name="telefono"]').val()+
			'<br> <hr> <strong>Informazioni:</strong><br>' + $('textarea[name="body"]').val();
		}
		$('textarea[name="body"]').val(b);
		
							   
		valid = true;
		$('.required').each(function() {
			$t = $(this);
			val = $(this).val();
			if (val == '') {
				$t.addClass('error');
				valid = false;
			}
		});
		
		if (!valid) {
			alert('Riempire i campi contrassegnati.');
		}
		
		if (valid && $('input.email').length) {
			$mail = $('.email');
			if (!validmail($mail.val())) {
				valid = false;
				$mail.addClass('error');
				alert("L'email inserita non risulta valida, si prega di controllare.");
			}
		}

		if (valid && $('input.privacy').length) {
			$p = $('input.privacy');
			if (!$p.attr('checked')) {
				valid = false;
				$p.parent().addClass('error');
				alert("Si prega di accettare la normativa sulla privacy.");
			}
		}
		
		if (valid && $('input.password2').length) {
			if ($('.password2').val() != $('input[name="password"]').val()) {
				valid = false;
				alert("Le password inserite non coincidono.");
			}
		}
		/*
		if (valid && false)  {
			post = $(this).serialize();
			alert(post);
			$.post('http://www.wsc-dev.com/cdn/core/core.php?class=mailer&method=sendpost', post, function(data) {
				alert('sent');
				alert(data);
			});
		}
		*/
			
		return valid;
	});
});

