Reservation = {
	form: 'reservationForm', // id of form

	// Getting values from fields using field name
	value: function(field){
		var form = $('#' + this.form);
		return form.find('*[name=' + field + ']').val();
	},

	// Checking childrens, rooms and changes
	check: {
		children: function(num, obj){
			var obj = $(obj).parent().parent();
			if (Reservation.value('children-' + num) > 0) {
				obj.find('.children-section').show();
				i = 0;
				obj.find('.children-section select').each(function(){
					i++;
					if (i <= Reservation.value('children-' + num)) $(this).show(); else $(this).hide();
				});
			} else obj.find('.children-section').hide();
		},
		room: function(){
			add = function(num){
				var additional = '\
					<ul class="room-section room-additional"><li class="room-place"><label>' + num + '</label></li><li><label for="adults-amount-' + num + '">' + term_adults + '</label>\
					<select id="adults-amount-' + num + '" class="adults-selector" name="adults-' + num + '"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></li><li>\
					<label for="children-amount-' + num + '">' + term_children + '</label>&nbsp;<select id="children-amount-' + num + '" class="children-selector" name="children-' + num + '"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></li><li class="children-section"><label>' + term_age + '</label>\
					<select class="children-age-selector" name="children-age-1-' + num + '"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select>\
					<select class="children-age-selector" name="children-age-2-' + num + '"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select>\
					<select class="children-age-selector" name="children-age-3-' + num + '"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select></li></ul>';
				return additional;
			}
			var form = $('#' + Reservation.form);

			rooms = form.find('.room-section').size();
			if (rooms < Reservation.value('rooms')) {
				while (rooms < Reservation.value('rooms')) {
					rooms++;
					$(add(rooms)).insertAfter(form.find('.room-section:last'));
				}
			} else {
				while (rooms > Reservation.value('rooms')) {
					rooms--;
					form.find('.room-section:last').remove();
				}
			}
			this.change();
		},
		change: function(){
			var self = this;
			var form = $('#' + Reservation.form);

			form.find('.children-selector').change(function(){
				var id = $(this).attr('name');
				id = id.split('-')[1];
				self.children(id, this);
			});
		}
	},

	// Initilizing reservation form
	init: function(){
		var self = this;
		var form = $('#' + this.form);

		this.check.room();
		this.check.change();
		form.find('.children-selector').each(function(){
			var id = $(this).attr('name');
			id = id.split('-')[1];
			self.check.children(id, this);
		});

		var date = new Date();
		form.find('select[name=day-in]').val(date.getDate() + 1);
		form.find('select[name=month-in], select[name=month-out]').val(date.getMonth() + 1);
		form.find('select[name=year-in], select[name=year-out]').val(date.getYear());
		form.find('select[name=day-out]').val(date.getDate() + 2);

		form.submit(function(){
			var url = {
				datefrom: '/datefrom,' + self.value('year-in') + '-' + self.value('month-in') + '-' + self.value('day-in'),
				dateto: '/dateto,' + self.value('year-out') + '-' + self.value('month-out') + '-' + self.value('day-out'),
				room: '/pr,'
			};

			form.find('.room-section').each(function(i){
				i++;
				if (i != 1) url.room += 'r';
				children = '';

				$(this).find('.children-section select').each(function(t){
					t++;
					if (t <= Reservation.value('children-' + i)) {
						if (t != 1) children += 'c';
						else children += 'a';
						children += $(this).val();
					}
				});

				url.room += Reservation.value('adults-' + i) + children;
			});

			datefromcheck = new Date(Reservation.value('year-in'), Reservation.value('month-in') - 1, Reservation.value('day-in'));
			datetocheck = new Date(Reservation.value('year-out'), Reservation.value('month-out') - 1, Reservation.value('day-out'));
			if (date.getTime() - 86400000 > datefromcheck.getTime() || datetocheck.getTime() <= datefromcheck.getTime())
				alert(term_warning);
			else {
				var src = url.datefrom + url.dateto + url.room;
				document.location.href = self.action + src;
			}
			return false;
		});
		$('#rooms-amount').change(function(){
			self.check.room();
		});
		$('#day-in, #month-in, #year-in').change(function(){
			var cin = new Date(self.value('year-in'), Number(self.value('month-in')) - 1, Number(self.value('day-in')) + 1);
			var cout = new Date(self.value('year-out'), Number(self.value('month-out')) - 1, Number(self.value('day-out')) + 1);

			if (cin.getTime() > cout.getTime()) {
				document.getElementById('year-out').value = cin.getFullYear();
				document.getElementById('month-out').value = cin.getMonth() + 1;
				document.getElementById('day-out').value = cin.getDate();
			}
		});
	}
}
