$(document).ready(
	function()
	{
		var subForm = function()
		{
			var ajaxLoader = document.createElement( 'img' );
			ajaxLoader.setAttribute( 'src', '/a/themes/harley/img/global/ajax-loader.gif' );
			ajaxLoader.setAttribute( 'width', 16 );
			ajaxLoader.setAttribute( 'height', 16 );
			ajaxLoader.setAttribute( 'alt', '' );
			ajaxLoader.setAttribute( 'class', 'ajaxLoader' );
			$(this).parent().append( ajaxLoader );

			var has_bike = $('input[name=has_bike]:checked').val();
			var current_bike_make = $('select[name=current_bike_make]').val();
			var current_bike_model = $('select[name=current_bike_model]').val();
			var current_bike_cc = $('select[name=current_bike_cc]').val();
			var postData = 'has_bike=' + has_bike + '&current_bike_make=' + current_bike_make +
				'&current_bike_model=' + current_bike_model + '&current_bike_cc=' + current_bike_cc;

			$.ajaxSetup({
					  scriptCharset: "utf-8" ,
					  contentType: "application/x-www-form-urlencoded; charset=utf-8"
			});


			$.ajax(
				{
					data: postData,
					url: '/services/widgets/currentBike/',
					type: 'POST',
					dataType: 'html',

					error: function( request, textStatus, errorThrown )
					{
						var dnv = document.createElement( 'input' );
						dnv.setAttribute( 'name', 'dnv' );
						dnv.setAttribute( 'type', 'hidden' );
						var form = document.getElementById( 'bookingForm' );
						form.appendChild( dnv );
						form.setAttribute( 'action', form.getAttribute( 'action' ) + '#ownBike' );

						form.submit();

						return true;
					},

					success: function( data )
					{
						$('#ownBike').replaceWith( data );
						$('.noJs').hide();
						$('input[name=has_bike]').change( subForm );
						$('input[name=has_bike]').click( subForm );
						$('select[name=current_bike_make]').change( subForm );
						$('.ajaxLoader').remove();
						$('input[name=has_bike][value=' + has_bike + ']')[0].checked = true;
					}
				}
			);

			return false;

		}

		$('input[name=has_bike]').change( subForm );
		$('input[name=has_bike]').click( subForm );
		$('select[name=current_bike_make]').change( subForm );
	}
);
