
var bookingType;
var pay = false;
var auto_schedule = false;

function setUp()
{
	
	if (bookingType == 'pay')
		{
			var method = $('input[name="method"]:checked').val();
			if (method)
			{
				if (method == 'paypal')
				{
					$('#clientDetails').hide();
					$('#payOptsButton').show();
				}
				else
				{
					$('#payOptsButton').hide();
					$('#clientDetails').show();
				}
			}
			else
			{
				$('#clientDetails').hide();
				$('#payOptsButton').show();
				
			}
			$('#payOpts').show();
			auto_schedule = $('select[name="flight_id"] option:selected').attr('rel');
			$('select[name="gift_flight_id"]').attr("disabled",true);
			$('select[name="flight_id"]').attr("disabled",false);
  		} 
  		else
  		{
			$('#payOpts').hide();
			$('#clientDetails').show();
	  		auto_schedule = $('select[name="gift_flight_id"] option:selected').attr('rel');
			$('select[name="flight_id"]').attr("disabled",true);
	  		$('select[name="gift_flight_id"]').attr("disabled",false);
		}
		showSchedule();

}

function showSchedule()
{
	if (auto_schedule) 
	{
		$('#noschedule').slideUp();
		$('#autoschedule').fadeIn('slow').slideDown("slow");
	} else
	{
		$('#autoschedule').slideUp("slow");
		$('#noschedule').fadeIn('slow').slideDown('slow');
	}
}
 
$(document).ready(function(){
	bookingType = ($('input[name="bookingType"]:checked').val() == 'pay')? 'pay' : 'gift';
	setUp();
	
	$(".zebra tr:nth-child(odd)").addClass("odd");
	$(".zebra tr:nth-child(even)").addClass("even");

	
	
	$('input[name="bookingType"]').click(function()
	{
		bookingType = $('input[name="bookingType"]:checked').val();
		setUp();
	});
	
	$('input[name="method"]').click(function()
	{
		if ($('input[name="method"]:checked').val() == 'paypal')
		{
			$('#clientDetails').hide();
			$('#payOptsButton').show();
		}
		else
		{
			$('#payOptsButton').hide();
			$('#clientDetails').show();
		}
	});
  
  $('select[name$="flight_id"]').change(function()
  {
  auto_schedule =  $('option:selected', this).attr('rel');
  showSchedule();
  
  });
  
$('#bookingForm').submit(function(){
	var formError = false;

	if (auto_schedule && !$('input[name="timeslot_id"]:checked').val())
	{
		$('#auto_scheduleError').fadeIn('slow').animate({opacity: 1.0}, 3000).fadeOut('slow');
		formError = true;
	}

	if (bookingType == 'pay')
	{
		if (!$('input[name="method"]:checked').val())
		{
			$('#methodError').fadeIn('slow').animate({opacity: 1.0}, 3000).fadeOut('slow');
			formError = true;
		}
	}
	
	if (bookingType == 'gift' || ($('input[name="method"]:checked').val() == 'call'))
	{
		var email_addressVal = $.trim($('#email_address').val());
		var nameVal = $.trim($('#first_name').val()) + $.trim($('#last_name').val());
		var phonesVal = $.trim($('#phone').val()) + $.trim($('#mobile').val());
		var emailRegex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		if(email_addressVal == '' || !emailRegex.test(email_addressVal))
		{
			$('#emailError').fadeIn('slow').animate({opacity: 1.0}, 3000).fadeOut('slow');
			formError = true;
		}
		
		if(nameVal == '')
		{
			$('#nameError').fadeIn('slow').animate({opacity: 1.0}, 3000).fadeOut('slow');
			formError = true;
		}
		if(phonesVal == '')
		{
			$('#phoneError').fadeIn('slow').animate({opacity: 1.0}, 3000).fadeOut('slow');
			formError = true;
		}
		if (!$('input[name="bookingType"]:checked').val())
		{
			$('#paymentError').fadeIn('slow').animate({opacity: 1.0}, 3000).fadeOut('slow');
			formError = true;
		}
	}
	return (formError == true)? false : true;
	});

 
})