
if ( window.jQuery ) {

	jQuery.fn.defaultText = function( text ) {
		return jQuery(this).focus(function(){
			if ( text == jQuery(this).val() )
				jQuery(this).val('');
		}).blur(function(){
			if ( '' == jQuery(this).val() )
				jQuery(this).val(text);
		});
	};

	jQuery.fn.isDate = function() {
		flag = true;
		this.each(function(){
			if ( !jQuery(this).val().match(/^[0-9]{1,4}$/) )
				flag = false;
		});
		return flag;
	};

	jQuery(document).ready( function( $ ) {

		if ( !$('#book_accom_req').attr('checked') ) {
			$('#accom_details').hide();
			$('#book_accom_type').removeClass('input-required');
		}

		if ( !$('#book_arrival_details').attr('checked') ) {
			$('#arrival_details').hide();
		}

		if ( !$('#book_departure_details').attr('checked') ) {
			$('#departure_details').hide();
		}

		$('#bookform').submit( function() {

			var lastfield = null;
			$(".input-required").each(function()
			{
				if(!this.value.length)
				{
					if(!lastfield)
					{
						lastfield = this;
						$(this).focus().end();
					}

					$(this).parent().addClass('input-invalid').end().change(function(e)
					{
						$(this).parent().removeClass('input-invalid');
					});
				}
			});

			if(lastfield)
			{
				alert( bookingsL10n.missingInfo );
				return false;
			} else if ( !$('#book_client_dobday,#book_client_dobmonth,#book_client_dobyear').isDate() ) {
				$('#book_client_dobday,#book_client_dobmonth,#book_client_dobyear').parent().addClass('input-invalid').end().change( function(e) {
					$(this).parent().removeClass('input-invalid');
				} ).eq(0).focus();
				alert( bookingsL10n.invalidDOB );
				return false;
			} else {
				$('#submit').attr('disabled',true).val( bookingsL10n.pleaseWait );
			}

		} );

		$('#book_accom_req').click( function() {

			if ( $(this).attr('checked') ) {
				$('#book_accom_type').addClass('input-required');
				$('#accom_details').show();
			} else {
				$('#book_accom_type').removeClass('input-required');
				$('#accom_details').hide();
			}

		} );

		$('#book_arrival_details').click( function() {

			if ( $(this).attr('checked') ) {
				$('#arrival_details').show();
			} else {
				$('#arrival_details').hide();
			}

		} );

		$('#book_departure_details').click( function() {

			if ( $(this).attr('checked') ) {
				$('#departure_details').show();
			} else {
				$('#departure_details').hide();
			}

		} );

		$('#bookform .help').css({
			padding  : 0,
			border   : 0,
			margin   : 0
		}).each(function(i){
			$(this).before('<div class="helpicon" id="help_' + i + '"></div>').html('<div class="helptext" id="help_' + i + '_text">' + $(this).html() + '</div>');
		});
		$('#bookform .helptext').css({
			display  : 'none',
			position : 'absolute'
		}).addClass('help');
		$('#bookform .helpicon').hover(function(i){
			var pos = $(this).offset();
			$('#bookform #' + $(this).attr('id') + '_text').css({
				top      : pos.top + ( $(this).width() ),
				left     : pos.left - ( $('#bookform #' + $(this).attr('id') + '_text').width() / 2 )
			}).show();
		},function(i){
			$('#bookform #' + $(this).attr('id') + '_text').fadeOut();
		});

		$('#book_client_dobday').defaultText('dd');
		$('#book_client_dobmonth').defaultText('mm');
		$('#book_client_dobyear').defaultText('yyyy');

	} );

}
