$(function() {
	if($('body').is('.template5'))
	{
		formValidator();
		datepicker();
		errorController();
	};
});

//By Chris Sheppard (Steria)
//Version 1.0
function errorController() {
	
	$("form :input").blur(function () {
		if($(this).parent().children("strong.error").attr("class")) {
			if($(this).val()) {
				$(this).parent().children("strong").css("display","none");
			} else {
				$(this).parent().children("strong").css("display","block");
			}
			
		}
		
   });
	
	$("form :radio").blur(function () {
		if($(this).parent().parent().parent().children("strong.error").attr("class")) {
			if($(this).val()) {
				$(this).parent().parent().next("strong").css("display","none");
			} else {
				$(this).parent().parent().next("strong").css("display","block");
			}
			
		}
		
   });

};


function formValidator()
{
	var showHints = true;
	
	// set validation defaults
	$.validator.setDefaults({
		wrapper: 'strong',
		onkeyup: false,
		onclick: false,
		errorPlacement: function(error, element) {
			element.next('.hint').hide();
			error.appendTo(element.parent()).hide().fadeIn();
		}
	});
	// add custom isDateValid method
	// validation occurs on day, and the 
	// method works out the month-year value
	$.validator.addMethod('valid-date', function(value, element) {
		var day	= value, // day
			monthYear = $(element).next('select').val(),
			year = monthYear.substr(0, 4), // year
			month = (monthYear.substr(4, 2)) - 1; //month
		return ((new Date(year, month, day)).getMonth() == month); // returns true if date 'exists'
	}, 'Please enter a valid date.');
	
	$('.submit input').click(function(){

		var emailaddress=document.getElementById("EMAIL_FIELD");
		if(document.getElementById("emailConfirm")) {
			var confirmEmailaddress=document.getElementById("emailConfirm");
			if(emailaddress.value!=confirmEmailaddress.value){
				document.getElementById("emailConfirm1").style.display="block";
				
				return false;
			} 
		}
		$('.hint').each(function(){
			$(this).hide();
			showHints = false;
		});
	});
	
	// autohide hints, and show them on focus
	$('.field').each(function(index, element) {
		
			var field = $(element),
			hint = field.find('.hint');			
			input = hint.prev(),
			// hide hint
			hint.hide();
			input.focus(function() {
				if(!input.hasClass('error') && showHints) {
					hint.fadeIn('fast');
				}
			}).blur(function() {
				if(showHints) hint.fadeOut('fast');
			})
	});
	
	/* BOOKING FORM --------------------------------- */
	$('#form-booking').validate({
		rules: {
			// add more rules here
		},
		messages: {
			'select-restaurant': 'Please select the restaurant you want to book a table at',
			'input-firstname': 'Please provide your first name so we can book you a table',
			'input-surname': 'Please provide your surname so we can book you a table',
			'input-postcode': 'Sorry, we didn’t recognise that postcode. Please enter a full postcode e.g. SE1 3XF',
			'input-mobile': 'Please enter your mobile number so we may get in touch and confirm your booking',
			'input-emailaddress': 'Please enter your email, we may need this to contact you and confirm your booking',
			'select-numberofadults': 'Please tell us how many adults are in your party so we can book the right table',
			'select-numberofchildren': 'Please tell us how many kids are in your party so we can book the right table',
			'select-preferreddateofbookingday':'Please let us know your preferred date of booking',
			'select-preferredtimeofbookingtime':'Please let us know your preferred time of booking'
		}		
	});
	
	/* SIGNUP FORM --------------------------------- */
	$('#form-signup').validate({
		rules: {
			// add more rules here
		},
		messages: {
			// add custom messages
		}
	});
	
	/* CONTACT FORM --------------------------------- */
	$('#form-contact').validate({
		rules: {
			// add more rules here
		},
		messages: {
			'input-restaurantvisited': 'This will help direct your comment effectively',
			'input-firstname': 'This is the name we use to address you in the response',
			'input-emailaddress': 'Please enter your email, we may need this to contact you and confirm your booking',
			'input-mobile': 'You must supply either a landline or a mobile number, so we can contact you',
			'input-natureofenquiry':'This will help us make improvements in the future'
		}
	});
	
	/*
	$('#form-contact .section-visitdetails').hide();
	$('#input-natureofenquiry').change(function() {
		if($(this).val() != "04") {
			$('#form-contact .section-visitdetails').show();
		}
		else {
			$('#form-contact .section-visitdetails').hide();
		}
	});
	$('input[name=input-responsechoice]').change(function() {
		if($('#input-wouldyoulikearesponseno').is(':checked')) {
			$('#form-contact .section-contactdetails').slideUp('fast');
		}
		else if($('#input-wouldyoulikearesponseyes').is(':checked')) {
			$('#form-contact .section-contactdetails').slideDown('fast');
		}
	}); */
};

function datepicker()
{
	var selectDay =   $('.select-day');
	var selectMonth = $('.select-monthyear');
	$('.datepicker').datepicker({
		dateFormat: 'd M, yy',
		minDate: '0M',
		maxDate: '+12M',
		buttonImage: '/etc/designs/brewersfayre/images/icons/icon_calendar.jpg',
		buttonImageOnly: true,
		showOn: 'button',
		onSelect: function(date) {
			var date = new Date(date);
			function pad(num) {
				return (num < 10) ? '0'+num : num;
			}				
			selectDay.val(pad(date.getDate()));
			selectMonth.val(date.getFullYear()+''+pad(date.getMonth()+1));
		},
		beforeShow: function() {
			var day       = selectDay.val(),
				monthYear = selectMonth.val(),
				year      = monthYear.substr(0, 4),
				month     = (monthYear.substr(4, 2) - 1);
			$(this).datepicker('setDate', new Date(year, month, day));	
			return {};
		}
	});
};


function datepickerBack()
{
	var selectDay =   $('.select-day');
	var selectMonth = $('.select-monthyear');
	$('.datepicker').datepicker({
		dateFormat: 'd M, yy',
		minDate: '-1M',
		maxDate: '0M',
		buttonImage: '/etc/designs/beefeater/images/functional/icon_calendar.gif',
		buttonImageOnly: true,
		showOn: 'button',
		onSelect: function(date) {
			var date = new Date(date);
			function pad(num) {
				return (num < 10) ? '0'+num : num;
			}				
			selectDay.val(pad(date.getDate()));
			selectMonth.val(date.getFullYear()+''+pad(date.getMonth()+1));
		},
		beforeShow: function() {
			var day       = selectDay.val(),
				monthYear = selectMonth.val(),
				year      = monthYear.substr(0, 4),
				month     = (monthYear.substr(4, 2));
			$(this).datepicker('setDate', new Date(year, month, day));	
			return {};
		}
	});
};
