function adjustTravelers(checkbox)
{
	if(checkbox.checked) {
		if(checkbox.value == '1') {
			$('#quote_form_birth_second').attr('disabled', 'disabled').attr('readonly', 'readonly').removeClass('validator_error').val('');
		}
		else if(checkbox.value == '2') {
			$('#quote_form_birth_second').removeAttr('disabled').removeAttr('readonly').removeClass('validator_error').val('');
		}
	}
}

function submitQuote()
{
	var $fields = $('.quote_form_field').not('[disabled]');
	var post = {
		action: 'submit',
		agent_id: 38533,
		module_id: 1,
		destination: 2,
		province: 'ON'
	};

	// wait button
	showWaitButton();

	// hide previous status bar
	$('#quote_form_status').hide();

	// remove validator errors
	$fields.removeClass('validator_error');

	// validate form
	if(!$fields.validator('validate')) {
		$('#quote_form_status').text('Some fields have errors. Please look for the fields marked in red.').slideDown();
		hideWaitButton();

		return;
	}

	// parse form data
	$fields.each(function() {
		if(this.type == undefined || this.name == '') return true;

		if(this.name.indexOf('[]') >= 0) {
			var name = this.name.replace('[]', '');

			if(post[name] == undefined) {
				post[name] = [];
			}

			post[name].push(this.value);
		}
		else if(this.type == 'radio') {
			if(this.checked) {
				post[this.name] = this.value;
			}
		}
		else {
			post[this.name] = this.value;
		}
	});

	// set loading status
	var loadingAnimation = true;

	$.tb_show({
		showTitle: false,
		modal: true,
		height: 19,
		width: 220,
		contentID: 'quote_form_loading'
	});

	setTimeout(function() {
		loadingAnimation = false;
	}, 1500);

	$.ajax({
		url: '/service/quote_service',
		type: 'POST',
		cache: false,
		data: {post: JSON.stringify(post)},
		dataType: 'json',
		success: function(data) {
			if(data.success) {
				if(!data.quoteID || !data.userHash || !data.userID) return;

				// keep animation until redirect
				loadingAnimation = true;

				window.location.href = 'http://www.travelinsurancequotes.ca/client/' + data.userID +
						'?quote=' + data.quoteID + '&hash=' + data.userHash;
			}
			else {
				$('#quote_form_status').text(data.error || 'error').slideDown();
			}
		},
		error: function() {
			$('#quote_form_status').text('Unable to perform quote request. Please try again later.').slideDown();
		},
		complete: function() {
			hideWaitButton();

			var interval = setInterval(function() {
				if(loadingAnimation) return;

				clearInterval(interval);

				$.tb_remove();
			}, 100);
		}
	});
}

function showWaitButton()
{
	$('#quote_form_button_submit').addClass('quote_form_button_disabled').blur();
}

function hideWaitButton()
{
	$('#quote_form_button_submit').removeClass('quote_form_button_disabled');
}

$(function() {
	$('#quote_form_status').css({opacity: 0.7});

	// setup datepickers
	$('#quote_form_departure').datepicker({
		showAnim: 'slideDown',
		onChangeMonthYear: function(year, month, dp) {
			var date = new Date();
			date.setFullYear(year * 1);
			date.setMonth(month * 1 - 1, dp.selectedDay * 1);

			$(this).datepicker('setDate', date);

			$(this).triggerHandler('change');
		},
		onClose: function() {
			$(this).validator('validate');
		},
		onSelect: function(date, dp) {
			var dateDeparture = $(this).datepicker('getDate');
			var dateReturn = $('#quote_form_return').datepicker('getDate');

			if(!dateReturn || dateDeparture > dateReturn) {
				$('#quote_form_return').datepicker('setDate', dateDeparture).validator('validate');
			}

			$(this).triggerHandler('change');
		},
		beforeShow: function(input) {
			return {
				minDate: new Date()
			};
		}
	});

	$('#quote_form_return').datepicker({
		showAnim: 'slideDown',
		onChangeMonthYear: function(year, month, dp) {
			var date = new Date();
			date.setFullYear(year * 1);
			date.setMonth(month * 1 - 1, dp.selectedDay * 1);

			$(this).datepicker('setDate', date);

			$(this).triggerHandler('change');
		},
		onClose: function() {
			$(this).validator('validate');
		},
		onSelect: function(date, dp) {
			$(this).triggerHandler('change');
		},
		beforeShow: function(input) {
			return {
				minDate: (($('#quote_form_departure').length && $('#quote_form_departure').datepicker('getDate')) || new Date())
			};
		}
	});

	$('#quote_form_birth_first, #quote_form_birth_second').datepicker({
		changeMonth: true,
		changeYear: true,
		showAnim: 'slideDown',
		defaultDate: '-20y',
		yearRange: '1910:2010',
		onChangeMonthYear: function(year, month, dp) {
			var date = new Date();
			date.setFullYear(year * 1);
			date.setMonth(month * 1 - 1, dp.selectedDay * 1);

			$(this).datepicker('setDate', date);
		},
		onClose: function() {
			$(this).validator('validate');
		}
	});


	$('.quote_form_field').validator({
		invalidEmpty: true
	});

	$('#quote_form_email')
		.validator({
			invalidEmpty: true,
			format: 'email'
		})
		.blur(function(e) {
			$(this).validator('validate');
		});
});

