$(function(){
	var ContactForm = {
		
		datepickerParams : {
			'constrainInput': true,
			'dateFormat': 'yy-mm-dd',
			'minDate': '0',
			'defaultDate': '0'
		},
		
		numOfNights: false,
		
		init: function() {
			this.form = $('#reservationForm');
			numOfNights = parseInt(this.form.find(':input[name=num_nights]').val());
			ContactForm.numOfNights = numOfNights? numOfNights : false;
			
			var dashRE = new RegExp('-', 'gi');
			
			var minDate = this.form.find(':input[name=akcio_start]').val();
			
			if(minDate) {
				ContactForm.minDate = new Date(minDate.replace(dashRE, '/'));
				ContactForm.datepickerParams.minDate = ContactForm.minDate;
			}

			var maxDate = this.form.find(':input[name=akcio_end]').val();
			
			
			if(maxDate) {
				ContactForm.maxDate = new Date(maxDate.replace(dashRE, '/'));
				if(numOfNights) {
					ContactForm.maxDate.setDate(ContactForm.maxDate.getDate() - numOfNights);
				}
				ContactForm.datepickerParams.maxDate = ContactForm.maxDate;
				
			}
			/*			
			var message = ContactForm.form.find(':input[name="message"]').val();
			if(message) {
				ContactForm.form.find(':input[name="message"]').parent().html('<input type="hidden" name="message" value="' + message + '" />');
			}
			*/
			
			var room_type = ContactForm.form.find('select[name="fields[room_type]"]').val();
			if(room_type) {
				var room_type_text = ContactForm.form.find('select[name="fields[room_type]"] option[value=' + room_type + ']').text();
				ContactForm.form.find('select[name="fields[room_type]"]').replaceWith('<input class="szf_text" type="text" readonly="readonly" value="' + room_type_text + '" />');
			}
			
			if(ContactForm.form.find(':input[name="fields[num_children]"]').val()) {
				ContactForm.form.find(':input[name="fields[num_children]"]').attr('readonly', true);
			}
			
			if(ContactForm.form.find(':input[name="fields[num_adults]"]').val()) {
				ContactForm.form.find(':input[name="fields[num_adults]"]').attr('readonly', true);
			}
			
			if(ContactForm.form.find(':input[name="subject"]:checked').val() == 'foglalas') {
				ContactForm.form.find(':input[name="subject"]').parent().html('<input type="hidden" name="subject" value="foglalas" />');
			}
			
		},		
				
		bindCalendar: function() {
			$(':input[name="fields[start_date]"]').datepicker(this.datepickerParams)
				.keydown(function(){ return false; });
			if(ContactForm.numOfNights) {
				ContactForm.form.find(':input[name="fields[end_date]"]').attr('readonly', true);
				ContactForm.form.find(':input[name="fields[start_date]"]').change(function() {
					var startDate = $(this).val().replace(/-/gi, '/');
					var endDate = new Date(startDate);
					var dd = endDate.getDate();
					endDate.setDate(dd + ContactForm.numOfNights);
					var month = endDate.getMonth() + 1;
					if(month < 10) {
						month = '0' + month;
					}
					endDate = endDate.getFullYear() + '-' + month + '-' + endDate.getDate();
					ContactForm.form.find(':input[name="fields[end_date]"]').val(endDate);
				})
			} else {
				$(':input[name="fields[end_date]"]').datepicker(this.datepickerParams)
				.keydown(function(){ return false; });
			}
		},
		
		bindInputFilters: function() {
			$(':input.onlyDigit').keyup(function(){
				var $el = $(this);
				var val = $el.val();
				val = val.replace(/[^0-9]/g, '');
				$el.val(val);	
			});
			$(':input.phone').keyup(function() {
				var $el = $(this);
				var val = $el.val();
				val = val.replace(/[^0-9\-\/ ()+]/g, '');
				$el.val(val);		
			});
		},
		
		counter: 1
	}
	ContactForm.init();
	ContactForm.bindCalendar()
	ContactForm.bindInputFilters();
	
	$('#reservationForm').validate();
	

	
});
