$(document).ready(function() {

	function age_switch(element) {
		return;
		age = element.val();
		element.parent().parent().find('.extra_ticket_type option').each(function() {
			if($(this).text().match(/7.and.under/i)) {
				if(age < 8)
					$(this).attr('selected', 'selected');
				else
					$(this).attr('selected', false);
			} else {
				if(age < 8)
					$(this).attr('selected', false);
				else
					$(this).attr('selected', 'selected');
			}
		});
	}

	$('#add_button').click(function() {
		last = $('.extra_ticket:last');
		copy = last.clone();
		count = (copy.find('.extra_ticket_counter').attr('value') * 1) + 1; // Multiply by one to cast the string to an int

		copy.find('h4 span.member').text(' / Ticket ' + (count + 1));
		copy.find('.extra_ticket_name').attr('id', 'Ticket_' + count + '_customer_full_name');
		copy.find('.extra_ticket_name').attr('name', 'Ticket[' + count + '][customer_full_name]');
		copy.find('.extra_ticket_name').attr('value', '');
		copy.find('.extra_ticket_age').attr('id', 'Ticket_' + count + '_customer_age');
		copy.find('.extra_ticket_age').attr('name', 'Ticket[' + count + '][customer_age]');
		copy.find('.extra_ticket_gender').attr('id', 'Ticket_' + count + '_customer_gender');
		copy.find('.extra_ticket_gender').attr('name', 'Ticket[' + count + '][customer_gender]');
		copy.find('.extra_ticket_type').attr('id', 'Ticket_' + count + '_ticket_type_id');
		copy.find('.extra_ticket_type').attr('name', 'Ticket[' + count + '][ticket_type_id]');
		copy.find('.extra_ticket_counter').attr('value', count);

		copy.find('.extra_ticket_age').change(function() {
			age_switch($(this));
		});
		
		last.after(copy);

		if(count == 29)
			$(this).hide();

		if(count > 0)
			$('#remove_button').show();

		return false;
	});

	$('#remove_button').click(function() {
		last = $('.extra_ticket:last');
		count = (last.find('.extra_ticket_counter').attr('value') * 1) - 1; // Multiply by one to cast the string to an int
		last.remove();

		if(count < 1)
			$(this).hide();

		if(count < 29)
			$('#add_button').show();

		return false;
	});

	$('input').keyup(function(e) {
		e.preventDefault();
		if(e.keyCode == 13) {
			e.stopPropagation();
			$('#go').click();
			return false;
		}
	});

	$('.extra_ticket_age').change(function() {
		age_switch($(this));

	});

	$('table.paypal_submit').click(function(e){
		$(this).parents('form:first').submit();
	});

	$('form').validate();
});

