/**
* A nyito oldali hotel kereso
*/
var CHotelNyitoKereso = new Class({
	initialize: function(){
		this.layer_input = new CmooLayer_input();
		this.errorlayer = new CmooErrorLayer();
		this.oLocation = new Clocation('hotel_orszag','hotel_varos', null);
		this.checkindate = new Cnaptar('checkindate',$('checkindate_evho'),$('checkindate_nap'),$('checkindate_calendar'), datum.shiftDate(_currentdate,3), datum.shiftDate(_currentdate,365), (function(){ this.checkinChange(); }).bind(this));
		this.checkoutdate = new Cnaptar('checkoutdate',$('checkoutdate_evho'),$('checkoutdate_nap'),$('checkoutdate_calendar'), datum.shiftDate(_currentdate,7), datum.shiftDate(_currentdate,34), (function(){ this.checkoutChange(); }).bind(this));
		this.checknights = $('checknights');
		this.checknights.addEvent('change',this.checknightsChange.bind(this));
		this.checknights.empty();
		for (i=1; i<=30; i++) new Element('option',{'html':i, 'value':i, 'selected':(i == 7)}).inject(this.checknights);
		this.checkinChange();
		this.roomcount = new Croomcount('roomcount','roominput','felnottek','gyerekek', {
			roomcount: 1,
			adults: [1,1,1,1,1],
			children: [0,0,0,0,0]
		});
		$('hotel_neve').addEvent('focus', function(){
			$('hotel_neve').set('value', '');
			$('hotel_neve').removeEvents('focus');
		});
		$('hotelkereso_submit').addEvent('click',(this.onSubmitClick).bind(this));
	},
	checkinChange: function(){
		this.checkoutdate.setNewRange(datum.shiftDate($A(this.checkindate.getDate()),1),this.checknights.get('value'));
	},
	checkoutChange: function(){
		this.checknights.set('value',datum.daysBetweenDates(this.checkoutdate.getDate(),this.checkindate.getDate()));
	},
	checknightsChange: function(){
		this.checkoutdate.setNewRange(datum.shiftDate($A(this.checkindate.getDate()),1),this.checknights.get('value'));
	},
	onSubmitClick: function(){
		
		
		
		if ($('hotel_orszag').get('value')=='0'){
			this.errorlayer.pop('A kereséshez választania kell egy országot.');
			return false;
		}
		if ($('hotel_varos').get('value')=='0'){
			this.errorlayer.pop('A kereséshez választania kell egy várost.');
			return false;
		}
		var roomcount = $('roomcount').get('value');
		this.totalchilds = 0;
		this.currentchild = 1;
		for (i=1; i<=roomcount; i++){
			var childcount = $('gyerekek_'+i).get('value');
			for (j=1; j<=childcount; j++){
				this.totalchilds++;
			}
		}
		if ( this.totalchilds>0 ) {
			this.popOne();
			return false;
		}
	},
	popOne: function(){
		this.layer_input.pop('Adja meg a gyermekek életkorát (0 - 12)','<label class="label_130">'+this.currentchild+'.gyermek:</label> <input class="input_210 input_border" id="childage" />',(this.onPopClose).bind(this));
		$('childage').addEvent('keypress', function(event){
			if (event.key.match('backspace|delete|enter|left|right')) return true;
			if (!event.key.match('[0-9]')) return false;
			if (event.target.get('value').length == 2) return false;
		});
	},
	onPopClose: function(){
		if (($('childage').get('value').match("^[0-9]{1,2}$")) && ($('childage').get('value') <= 12)) {
			new Element('input',{'type':'hidden','name':'childages['+this.currentchild+']', 'value': $('childage').get('value')}).inject($('belsoform'))
			this.currentchild++;
			if (this.currentchild <= this.totalchilds){
				this.popOne();
			}else{
				$('belsoform').submit();
			}
		}else{
			this.popOne();
		}
	}
});

document.addEvent('domready', function(){
	new CHotelNyitoKereso();
});
