var FrmST='';
var hide_wiz_advanced=false;
var oTC=null;
var HPW=null;

function oSearchWizard(type) {
	this.langset=uGLOBALS.msg.search_wiz;

	this.meta= {
		// hotel
		H: {
			all:	{
				io_owrt:'display:none',
				io_transfer_owrt:'display:none',
				io_orig:'display:none',
				io_dest:'display:',
				io_dest_find:'display:',
				io_btn_orig_sel:'display:',
				io_btn_dest_sel:'display:',
				io_dest_find_transfer:'display:none',
				io_orig_txt:'innerHTML='+this.langset.H.io_orig_txt,
				io_dest_txt:'innerHTML='+this.langset.H.io_dest_txt,
				io_since_txt:'innerHTML='+this.langset.H.io_since_txt,
				io_till_txt:'innerHTML='+this.langset.H.io_till_txt,
				io_car_pdtime:'display:none',
				io_wkd_spacer:'display:none',
				io_dates:'display:',
				io_dates_wkd:'display:none',
				io_rooms_lst:'display:',
				io_guest_lst:'display:',
				io_avia_cabin:'display:none',
				io_avia_cabin_hsep:'display:none',
				io_dest_block:'display:',
				rplus_img:'display:none',
				wiz_transfer_o48:'display:none'
			},
			left:	{},
			center:	{},
			ctrl:	'r1',
			cache_city: []
		},
		// hotel allinc
		HI: {
			all:	{
				io_owrt:'display:none',
				io_transfer_owrt:'display:none',
				io_orig:'display:none',
				io_dest:'display:',
				io_dest_find:'display:',
				io_btn_orig_sel:'display:',
				io_btn_dest_sel:'display:',
				io_dest_find_transfer:'display:none',
				io_orig_txt:'innerHTML='+this.langset.H.io_orig_txt,
				io_dest_txt:'innerHTML='+this.langset.H.io_dest_txt,
				io_since_txt:'innerHTML='+this.langset.H.io_since_txt,
				io_till_txt:'innerHTML='+this.langset.H.io_till_txt,
				io_car_pdtime:'display:none',
				io_wkd_spacer:'display:none',
				io_dates:'display:',
				io_dates_wkd:'display:none',
				io_rooms_lst:'display:',
				io_guest_lst:'display:',
				io_avia_cabin:'display:none',
				io_avia_cabin_hsep:'display:none',
				io_dest_block:'display:',
				rplus_img:'display:none',
				wiz_transfer_o48:'display:none'
			},
			left:	{},
			center:	{},
			ctrl:	'r9',
			cache_city: []
		},

		// avia
		A: {
			all:	{
				io_owrt:'display:',
				io_transfer_owrt:'display:none',
				io_orig:'display:',
				io_dest:'display:',
				io_dest_find:'display:',
				io_btn_orig_sel:'display:',
				io_btn_dest_sel:'display:',
				io_dest_find_transfer:'display:none',
				io_orig_txt:'innerHTML='+this.langset.A.io_orig_txt,
				io_dest_txt:'innerHTML='+this.langset.A.io_dest_txt,
				io_since_txt:'innerHTML='+this.langset.A.io_since_txt,
				io_till_txt:'innerHTML='+this.langset.A.io_till_txt,
				io_car_pdtime:'display:none',
				io_wkd_spacer:'display:none',
				io_dates:'display:',
				io_dates_wkd:'display:none',
				io_rooms_lst:'display:none',
				io_guest_lst:'display:',
				io_avia_cabin:'display:',
				io_avia_cabin_hsep:'display:',
				rplus_img:'display:none',
				wiz_transfer_o48:'display:none'
			},
			left:	{},
			center:	{},
			ctrl:	'r2',
			cache_city: []
		},

		// avia
		AB: {
			all:	{
				io_owrt:'display:',
				io_transfer_owrt:'display:none',
				io_orig:'display:',
				io_dest:'display:',
				io_dest_find:'display:',
				io_btn_orig_sel:'display:',
				io_btn_dest_sel:'display:',
				io_dest_find_transfer:'display:none',
				io_orig_txt:'innerHTML='+this.langset.A.io_orig_txt,
				io_dest_txt:'innerHTML='+this.langset.A.io_dest_txt,
				io_since_txt:'innerHTML='+this.langset.A.io_since_txt,
				io_till_txt:'innerHTML='+this.langset.A.io_till_txt,
				io_car_pdtime:'display:none',
				io_wkd_spacer:'display:none',
				io_dates:'display:',
				io_dates_wkd:'display:none',
				io_rooms_lst:'display:none',
				io_guest_lst:'display:',
				io_avia_cabin:'display:',
				io_avia_cabin_hsep:'display:',
				rplus_img:'display:none',
				wiz_transfer_o48:'display:none'
			},
			left:	{},
			center:	{},
			ctrl:	'r20',
			cache_city: []
		},

		// hotel + avia
		HA: {
			all:	{
				io_owrt:'display:',
				io_transfer_owrt:'display:none',
				io_orig:'display:',
				io_dest:'display:',
				io_dest_find:'display:',
				io_btn_orig_sel:'display:',
				io_btn_dest_sel:'display:',
				io_dest_find_transfer:'display:none',
				io_orig_txt:'innerHTML='+this.langset.HA.io_orig_txt,
				io_dest_txt:'innerHTML='+this.langset.HA.io_dest_txt,
				io_since_txt:'innerHTML='+this.langset.HA.io_since_txt,
				io_till_txt:'innerHTML='+this.langset.HA.io_till_txt,
				io_car_pdtime:'display:none',
				io_wkd_spacer:'display:none',
				wkd_spacer:'display:none',
				io_dates:'display:',
				io_dates_wkd:'display:none',
				io_rooms_lst:'display:',
				io_guest_lst:'display:',
				io_avia_cabin:'display:',
				io_avia_cabin_hsep:'display:none',
				io_dest_block:'display:',
				rplus_img:'display:',
				wiz_transfer_o48:'display:none'
			},
			left:	{},
			center:	{},
			ctrl:	'r3',
			cache_city: []
		},

		// car
		C: {
			all:	{
				io_owrt:'display:none',
				io_transfer_owrt:'display:none',
				io_orig:'display:',
				io_dest:'display:none',
				io_dest_find:'display:',
				io_btn_orig_sel:'display:',
				io_btn_dest_sel:'display:none',
				io_dest_find_transfer:'display:none',
				io_orig_txt:'innerHTML='+this.langset.C.io_orig_txt,
				io_dest_txt:'innerHTML='+this.langset.C.io_dest_txt,
				io_since_txt:'innerHTML='+this.langset.C.io_since_txt,
				io_till_txt:'innerHTML='+this.langset.C.io_till_txt,
				io_car_pdtime:'display:',
				io_wkd_spacer:'display:none',
				io_dates:'display:',
				io_dates_wkd:'display:none',
				io_guest_lst:'display:none',
				io_avia_cabin:'display:none',
				io_avia_cabin_hsep:'display:none',
				io_dest_block:'display:',
				rplus_img:'display:none',
				wiz_transfer_o48:'display:none'
			},
			left:	{},
			center:	{},
			ctrl:	'r5',
			cache_city: []
		},

		// transfer
		T: {
			all:	{
				io_owrt:'display:none',
				io_transfer_owrt:'display:',
				io_orig:'display:none',
				io_dest_find:'display:none',
				io_orig:'display:none',
				io_dest_find_transfer:'display:',

				io_since_txt:'innerHTML='+this.langset.H.io_since_txt,
				io_till_txt:'innerHTML='+this.langset.H.io_till_txt,

				io_dest_txt:'innerHTML='+this.langset.T.io_dest_txt,
				io_since_txt:'innerHTML='+this.langset.T.io_since_txt,
				io_car_pdtime:'display:none',
				io_wkd_spacer:'display:none',
				io_dates:'display:',
				io_dates_wkd:'display:none',
				io_guest_lst:'display:none',
				io_avia_cabin:'display:none',
				io_avia_cabin_hsep:'display:none',
				rplus_img:'display:none',
				wiz_transfer_o48:'display:'
			},
			left:	{},
			center:	{},
			ctrl:	'r10',
			cache_city: []
		},

		// rzd
		ZD: {
			all:	{
				io_owrt:'display:none',
				io_transfer_owrt:'display:none',
				io_orig:'display:',
				io_dest:'display:',
				io_dest_find:'display:',
				io_btn_orig_sel:'display:none',
				io_btn_dest_sel:'display:none',
				io_dest_find_transfer:'display:none',
				io_orig_txt:'innerHTML='+this.langset.ZD.io_orig_txt,
				io_dest_txt:'innerHTML='+this.langset.ZD.io_dest_txt,
				io_since_txt:'innerHTML='+this.langset.ZD.io_since_txt,
				io_car_pdtime:'display:none',
				io_wkd_spacer:'display:none',
				io_dates:'display:',
				io_dates_wkd:'display:none',
				io_guest_lst:'display:none',
				io_avia_cabin:'display:none',
				io_avia_cabin_hsep:'display:none',
				io_dest_block:'display:none',
				rplus_img:'display:none',
				wiz_transfer_o48:'display:none'
			},
			left:	{},
			center:	{},
			ctrl:	'r20',
			cache_city: {
				io_original:'Москва',
				orig:'Москва',
				io_destination:'Санкт-Петербург',
				dest:'Санкт-Петербург'
			}
		},

		P: {
			all:	{
				io_rooms_lst:'display:none'
			},
			left:	{},
			center:	{},
			ctrl:	false,
			cache_city: []
		}
	};

	this.frm_id;
	this.type=type;
	this.mode='';
	this.prev_mode='';

	var orig_change=0;
	var dest_change=0;

	this.adultsPerRoom=new Array();
	this.childrenPerRoom=new Array();
	this.childAgesPerRoom=new Array();
	this.childs_tpl=new Array();
	this.rooms_tpl=new Array();

	this.init = function(){
		oTC=new oTransferCity();
		this.check_currency();
	};

	this._setMode = function(modeMeta) {
		var tmp, elm;
		if(modeMeta=='') return;
		for (i in modeMeta) {
			elm=u('#'+i);

			if(modeMeta[i].indexOf('=')!=-1){
				tmp1=modeMeta[i].substring(0, modeMeta[i].indexOf('='));
				tmp2=modeMeta[i].substring(modeMeta[i].indexOf('=')+1);
				if (elm) elm[tmp1]=tmp2;
			}else if(modeMeta[i].indexOf(':')!=-1){
				tmp=modeMeta[i].split(':');
				if (elm) elm.style[tmp[0]]=tmp[1];
			}
		}
	};

	this.reset=function(){
		document.getElementById('io_destination').value='';
		document.getElementById('dest').value='';
		document.getElementById('dest1').value='';
	};

	this.collapse=function(ok){
		if(ok){
			u('#wizard_collapsed').show();
			u('#wizard_frm').hide();
		}else{
			u('#wizard_collapsed').hide();
			u('#wizard_frm').show();
		}
	};

	this.setType=function(mode) {
		var elm, i;

		this.prev_mode=this.mode;

		this.cache_mode();

		// set mode
		if (mode && this.meta[mode] && (!this.meta[mode].ctrl || u('#'+this.meta[mode].ctrl))) {
			if(document.getElementById(this.meta[mode].ctrl)) document.getElementById(this.meta[mode].ctrl).checked=true;
			this.mode=mode;
		} else {
			for (i in this.meta) {
				if(elm=document.getElementById(this.meta[i].ctrl))
					if (elm && elm.checked) {this.mode=i; break;}
			}
		}

		// Set default mode
		if(this.mode==''){
			for (i in this.meta) {
				this.mode=i;
				if(document.getElementById(this.meta[i].ctrl)) document.getElementById(this.meta[i].ctrl).checked=true;
				break;
			}
		}

		this.check_currency();

		FrmST=this.mode;

		if(u('#mode_single')) u('#mode_single').value=this.mode;

		this.make_sel();
	};

	this.check_currency_prev=false;
	this.check_currency=function(){
		if(u('#set_currency')===false) return true;
		if(this.check_currency_prev!==false && u('#master_paytypes_alert_'+this.check_currency_prev)!==false) u('#master_paytypes_alert_'+this.check_currency_prev).hide();
		if(u('#master_paytypes_info')!==false) u('#master_paytypes_info').show();
		if(this.mode!='A' && this.mode!='AB' && this.mode!='HA') return true;

		this.check_currency_prev=u('#set_currency').value;
		if(u('#master_paytypes_alert_'+this.check_currency_prev)===false) return true;
		u('#master_paytypes_alert_'+this.check_currency_prev).show();
		if(u('#master_paytypes_info')!==false) u('#master_paytypes_info').hide();

		return true;
	}

	this.make_sel=function(){
		for(var i in this.meta)
			if(u('#'+this.meta[i].ctrl+'_sel')){
				u('#'+this.meta[i].ctrl+'_sel').killClass('root-master-mode-selected');
			}
		if(u('#'+this.meta[this.mode].ctrl+'_sel'))
			u('#'+this.meta[this.mode].ctrl+'_sel').addClass('root-master-mode-selected');
	};

	this.set_mode=function(m){
		this.setType(m);
		this.change_mode_val();
	};

	this.change_mode=function(){
		tmp=this.mode;
		this.setType();
		if(tmp==this.mode) return;

		this.change_mode_val();
	};

	this.cache_mode=function(){
		if(this.mode=='') return;

		this.meta[this.mode].cache_city['io_original']=document.getElementById('io_original').value;
		this.meta[this.mode].cache_city['orig']=document.getElementById('orig').value;
		this.meta[this.mode].cache_city['orig1']=document.getElementById('orig1').value;
		if(this.mode=='ZD'){
			this.meta[this.mode].cache_city['io_destination']=document.getElementById('io_destination').value;
			this.meta[this.mode].cache_city['dest']=document.getElementById('dest').value;
			this.meta[this.mode].cache_city['dest1']=document.getElementById('dest1').value;
		}
	};

	this.restore_mode=function(){
		if(this.mode=='C') document.getElementById('io_original').value='';
		else{
			//document.getElementById('io_original').value=uGLOBALS.msg.search_wiz.io_original;
			//document.getElementById('orig').value=uGLOBALS.msg.search_wiz.io_original_id;
			//document.getElementById('orig1').value=uGLOBALS.msg.search_wiz.io_original_id1;
		}

		if(document.getElementById('dest_tmp_name').value!=''){
			if(this.mode=='C' && document.getElementById('dest_tmp_ap').value!=''){
				/*
				document.getElementById('io_original').value=document.getElementById('dest_tmp_name').value+' ('+document.getElementById('dest_tmp_ap').value.substring(1)+')'+', '+document.getElementById('dest1_tmp_name').value;
				document.getElementById('orig').value=document.getElementById('dest_tmp_ap').value;
				document.getElementById('orig1').value=document.getElementById('dest1_tmp').value;
				document.getElementById('io_destination').value=document.getElementById('io_original').value;
				document.getElementById('dest').value=document.getElementById('orig').value;
				document.getElementById('dest1').value=document.getElementById('orig1').value;
				*/
			}else{
				if(this.prev_mode=='ZD' || this.mode=='ZD' || ((this.mode=='C' || this.mode=='A' || this.mode=='HA') && (document.getElementById('orig').value==document.getElementById('dest_tmp').value || document.getElementById('dest_tmp_ap').value==''))){
					document.getElementById('io_destination').value='';
					document.getElementById('dest').value='';
					document.getElementById('dest1').value='';
				}else{
					document.getElementById('io_destination').value=document.getElementById('dest_tmp_name').value+', '+document.getElementById('dest1_tmp_name').value;
					document.getElementById('dest').value=document.getElementById('dest_tmp').value;
					document.getElementById('dest1').value=document.getElementById('dest1_tmp').value;
				}
			}
		}
		if(this.mode=='ZD'){
			document.getElementById('io_original').value='';
			document.getElementById('orig').value='';
			document.getElementById('orig1').value='';
		}

		for(i in this.meta[this.mode].cache_city) document.getElementById(i).value=this.meta[this.mode].cache_city[i];

		if(this.mode=='A' || this.mode=='AB' || this.mode=='HA'){
			if(u('#orig1').value==u('#dest1').value){
				u('#io_original').value=uGLOBALS.msg.search_wiz.io_original;
				u('#orig').value=uGLOBALS.msg.search_wiz.io_original_id;
				u('#orig1').value=uGLOBALS.msg.search_wiz.io_original_id1;
			}
		}

		if(this.mode=='A' || this.mode=='AB' || this.mode=='HA' || this.mode=='T') this.change_owrt();
	};

	this.change_mode_val=function(){
		this.build();
		//this.reset();

		this.restore_mode();

		if(this.mode=='H') this.disp_advanced(true, false);
		else this.disp_advanced(false, false);
	};

	this.disp_advanced_n=false;
	this.disp_advanced=function(v, n){
		if(hide_wiz_advanced) return;

		if(n) this.disp_advanced_n=true;
		if(this.disp_advanced_n) v=false;

		if(el=document.getElementById('io_wiz_advanced')){
			if(v && (this.mode=='H' || this.mode=='HA')) el.style.display='';
			else el.style.display='none';
		}
	};

	this.init_dest='';
	this.init_validator=function(frm_id){
		if(curPage!='/search') this.disp_advanced(false, true);

		this.frm_id=frm_id;

		this.oVal=new oValid();
		this.oVal.frm_id=this.frm_id;
		this.oVal.parse_frm();

		this.init_dest=document.getElementById('dest').value;
	};

	this.oVal=null;
	this.build=function(){
		this.rooms_cnt();
		// build search form
		var len=this.mode.length, cm='', lim=25, pos=0;
		while (lim-- && pos<len) {

			cm=this.mode.substr(pos,len);

			if (!this.meta[cm]) {
				if (--len<0) {ShowAlert('Wrong mode!'); return;}
				continue;
			}

			pos+=cm.length;

			// set modes
			this._setMode(this.meta[cm]['all']);
			if (this.meta[cm][this.type]) this._setMode(this.meta[cm][this.type]);
		}
		document.getElementById('cur_prov').value='';

		if(uGLOBALS['SW_sel']!=undefined && this.mode=='H'){
			document.getElementById('io_dest_sel').style.display='';
			document.getElementById('io_dest_find').style.display='none';
			var dest=u('#dest').value, dest1=u('#dest1').value;
			this.build_dest_sel();
			if(dest1!=''){
				u('#io_choose_country').value=dest1;
				this.dest_sel(false);
				u('#io_choose_city').value=dest;
				this.dest_sel(true);
			}
		}else if(this.mode!='T'){
			if(document.getElementById('io_dest_sel')){
				document.getElementById('io_dest_sel').style.display='none';
				document.getElementById('io_dest_find').style.display='';
			}
		}

		this.set_bold.init();

		//if(uGLOBALS['url']!='/') return;
		if (!u('#io_dest_find_sep').show) return;

		if(u('#io_dest_find').visible() && u('#io_orig').visible() && u('#io_dest').visible()){
			u('#io_dest_find_sep').show();
			u('#io_orig').width='46%';
		}else{
			if(u('#io_dest_find_sep')!==false) u('#io_dest_find_sep').hide();
			u('#io_orig').width='';
		}
		this.bottom_text_pos();
	};

	this.build_dest_sel=function(){
		if(uGLOBALS['SW_dest_sel_init']!=undefined) return false;
		el=u('#io_choose_country');
		for(var i in uGLOBALS['SW_dest_sel']){
			el.add_opt(i, uGLOBALS['SW_dest_sel'][i][0]);
		}
		uGLOBALS['SW_dest_sel_init']=true;

		this.dest_sel(false);
	};

	this.dest_sel=function(is_city){
		if(!is_city){
			gco=u('#io_choose_country').value;
			el=u('#io_choose_city');
			el.clear();
			if(uGLOBALS['SW_dest_sel'][gco]==undefined) return false;
			for(var n in uGLOBALS['SW_dest_sel'][gco][1])
				el.add_opt(n, uGLOBALS['SW_dest_sel'][gco][1][n]);
		}
		u('#dest1').value=gco;
		u('#dest').value=u('#io_choose_city').value;

		if(u('#io_choose_country').selectedIndex==-1) return false;

		c1=u('#io_choose_country').options[ u('#io_choose_country').selectedIndex ].text;
		c2=u('#io_choose_city').options[ u('#io_choose_city').selectedIndex ].text;
		u('#io_destination').value=c2+', '+c1;
	};

	this.validate = function (CheckCountry) {
		if(HPW!=null) HPW.update();

		if(this.mode=='ZD'){
			document.location.href='http://rzd.iglobe.ru/index.php?sub_id=&from='+u('#io_original').value+'&to='+u('#io_destination').value+'&departure_date='+u('#in_date').value+'&charset=windows-1251';
			return false;
		}

		if(this.mode=='T'){
			var ok=false, now=new Date(), date1=make_date(u('#in_date').value, '.'), date2=make_date(u('#out_date').value, '.'), limit=2;

			date1.setDate(date1.getDate()-limit);
			date2.setDate(date2.getDate()-limit);
			if(((this.owrt_mode=='RT' || this.owrt_mode=='OW1') && date1>now) || (this.owrt_mode=='OW2' && date2>now)) ok=true;

			if(!ok){
				alert(this.langset.T.alert_limit);
				return false;
			}

			document.getElementById(this.frm_id).submit();
			setTimeout('show_wait(0)', 1000);
			return true;
		}

		if(this.mode!='H'){
			document.getElementById('cur_sfb').value='';
			document.getElementById('cur_hid').value='';
		}

		this.clear_err('err_orig');
		this.clear_err('err_dest');

		if(this.mode!='P' && document.getElementById('io_destination').value==''){
			this.set_err('err_dest', uGLOBALS.msg.search_wiz.select_city);
			err=true;
			return false;
		}
		this.set_err('err_dest', false);

		// check original city
		if(document.getElementById('io_original').value=='' && (this.orig_change || CheckCountry) && (this.mode=='A' || this.mode=='AB' || this.mode=='HA' || this.mode=='C')){
			this.set_err('err_orig', uGLOBALS.msg.search_wiz.select_city);
			this.show_suggest(1, 'io_original');
			return false;
		}

		// check desination city
		if(this.dest_change && CheckCountry){
			this.set_err('err_dest', uGLOBALS.msg.search_wiz.select_city);
			this.show_suggest(1, 'io_destination');
			return false;
		}

		if((this.mode=='A' || this.mode=='AB' || this.mode=='HA') && document.getElementById('orig1').value=='STUS' && document.getElementById('orig1').value==document.getElementById('dest1').value){
			this.set_err('err_dest_find', uGLOBALS.msg.search_wiz.usa_noavi);
			err=true;
			return false;
		}
		this.set_err('err_dest_find', false);

		if(this.mode=='A' || this.mode=='AB' || this.mode=='HA' || this.mode=='C') {
			if (document.getElementById('io_original').value==''){
				this.set_err('err_orig', uGLOBALS.msg.search_wiz.select_city);
				err=true;
			}
		}
		this.set_err('err_orig', false);
		this.set_err('err_dest', false);

		var err=false;
		if(this.mode=='') err=true;

		var check_date=false;
		if(this.mode=='A' || this.mode=='AB' || this.mode=='HA' || this.mode=='C' || this.mode=='P') check_date=true;

		tmp_el=document.getElementById('in_date');
		if(check_date && !this.oVal.validate('in_date')){
			this.set_err('err_in_date', uGLOBALS.msg.search_wiz.bad_date);
			err=true;
			return false;
		}else{
			this.clear_err('err_in_date');
			if(tmp_el.value==uTypes['date']['mask']) tmp_el.value='';
		}

		tmp_el=document.getElementById('out_date');
		if(check_date && !this.oVal.validate('out_date')){
			this.set_err('err_out_date', uGLOBALS.msg.search_wiz.bad_date);
			err=true;
		}else{
			this.clear_err('err_out_date');
			if(tmp_el.value==uTypes['date']['mask']) tmp_el.value='';
		}
		this.set_err('err_in_date', false);
		this.set_err('err_out_date', false);

		if(err) return false;
		else{
			document.getElementById(this.frm_id).submit();
			setTimeout('show_wait(0)', 200);
		}
	};

	this.fhtl=function(){
		u('#cur_hid').value='';
		u('#cur_sfb').value='';
		this.validate();
	};

	this.bottom_text_pos=function(){
		if(u('#root-master-bottom-text')===false) return false;
		var m=u('#root-master-in').offsetHeight-u('#root-master-top').offsetHeight;
		if(m<100){
			u('#root-master-bottom-text').hide();
			return false;
		}
		m+=50;

		u('#root-master-bottom-text').show();
		if(m<100) m=0;
		else m=m/2-u('#root-master-bottom-text').offsetHeight+10;
		//u('#root-master-bottom-text').show();
		u('#root-master-bottom-text').style.marginTop=parseInt(m)+'px';
	};

	this.rooms_cnt=function(redraw_rooms){
		var rooms_num=u('#rooms_cnt').value;
		if(redraw_rooms==undefined) redraw_rooms=true;

		if(this.mode!='H' && this.mode!='HA' && this.mode!='W') rooms_num=1;

		// DRAW ROOMS
		if(redraw_rooms){
			var html=this.rooms_tpl['body'], i=0, tmp=[], tmp2=[], a='';
			if(this.mode=='H' || this.mode=='HA') tmp.push(this.rooms_tpl['title_H']);
			tmp.push(this.rooms_tpl['title_ADT']);
			tmp.push(this.rooms_tpl['title_CHD']);
			html=html.replace('{title}', implode(this.rooms_tpl['sep'], tmp));
			tmp=[];
			var min_adt=1;
			if(this.mode=='P') min_adt=1;
			for(i=0; i<rooms_num; i++){
				tmp2=[];

				if(this.mode=='H' || this.mode=='HA'){
					if(this.rooms_tpl['rm_H:'+rooms_num]!=undefined) a=this.rooms_tpl['rm_H:'+rooms_num].replace(new RegExp('{x}', 'g'), i+1);
					else a=this.rooms_tpl['rm_H'].replace(new RegExp('{x}', 'g'), i+1);
					tmp2.push(a);
				}

				a=this.rooms_tpl['rm_ADT'].replace(new RegExp('{i}', 'g'), i);
				a=a.replace('{opts}', buildSelect(false, false, min_adt, 4, this.adultsPerRoom[i]));
				tmp2.push(a);

				a=this.rooms_tpl['rm_CHD'].replace(new RegExp('{i}', 'g'), i);
				a=a.replace('{opts}', buildSelect(false, false, 1, 3, this.childrenPerRoom[i], false, true));
				tmp2.push(a);
				tmp.push(this.rooms_tpl['rm'].replace('{list}', implode(this.rooms_tpl['sep'], tmp2)));
			}
			html=html.replace('{rms}', implode(this.rooms_tpl['sep_row'].replace('{n}', tmp2.length+2), tmp));
			u('#io_guest_info').innerHTML=html;
		}

		this.bottom_text_pos();
		if(this.mode!='H' && this.mode!='A' && this.mode!='AB' && this.mode!='P' && this.mode!='HA' && this.mode!='W') return;

		var max_chd=0;
		for(i=0; i<rooms_num; i++){
			v=document.getElementById('room_CHD['+i+']').value;
			if(this.childrenPerRoom[i]==null && v) this.childrenPerRoom[i]=v;
			else if(this.childrenPerRoom[i]==null) this.childrenPerRoom[i]=0;
		}
		for(i=0; i<rooms_num; i++) if(this.childrenPerRoom[i]>max_chd) max_chd=this.childrenPerRoom[i];

		// no chd
		if(!max_chd){ u('#io_childs_list').hide(); this.bottom_text_pos(); return; }

		u('#io_childs_list').show();

		var html='', a='', x=0, tmp=[], tmp_title=[], tmp2=[];
		for(x=0; x<max_chd; x++) tmp2.push(this.childs_tpl['title'].replace('{x}', (x+1)));
		tmp_title.push(implode(this.childs_tpl['sep'], tmp2));

		for(i=0; i<rooms_num; i++){
			tmp2=[];
			if(this.childrenPerRoom[i]>0){
				for(x=0; x<this.childrenPerRoom[i]; x++){
					if(this.childAgesPerRoom[i]==null) age=0;
					else age=this.childAgesPerRoom[i][x];
					var age_max=13;
					if(this.mode=='A' || this.mode=='AB' || this.mode=='P') age_max=11;
					a=this.childs_tpl['td'].replace('{sel}', buildSelect('room_CHD_ages['+i+']['+x+']', 'SW.setCHDAge('+i+', '+x+', this.value);', 1, age_max, age, 1));
					tmp2.push(a);
				}
				if (this.mode=='A' || this.mode=='AB' || this.mode=='P')
					var b=this.childs_tpl['row_A'].replace('{x}', parseInt(i)+1);
				else
					var b=this.childs_tpl['row'].replace('{x}', parseInt(i)+1);
				b=b.replace('{body}', implode(this.childs_tpl['sep'], tmp2));
				tmp.push(b);
			}
		}
		if(tmp.length){
			a=this.childs_tpl['body'].replace('{title}', implode('', tmp_title));
			a=a.replace('{body}', implode(this.childs_tpl['sep_row'], tmp));
			u('#io_childs_sel').innerHTML=a;
		}
		this.bottom_text_pos();
	};

	this.setCHDAge=function(room, child, age){
		if(this.childAgesPerRoom[room]==null) this.childAgesPerRoom[room]=new Array();
		this.childAgesPerRoom[room][child]=age;
	};

	this.setADTNum=function(room, num){
		this.adultsPerRoom[room]=num;
	};

	this.setCHDNum=function(room, num){
		this.childrenPerRoom[room]=num;
		this.rooms_cnt(false);
	};

	this.suggester_loaded=false;
	this.show_suggest_wait=function(evt, fld){
		if(!this.suggester_loaded) return;

		var charCode=(evt.which)?evt.which:evt.keyCode;
		if(charCode!=38 && charCode!=40 && charCode!=37 && charCode!=39 && charCode!=13){
			this.show_suggest(0, fld);
			//var _self=this;
			//setTimeout(function(){_self.show_suggest(0, fld);}, 500);
		}

		document.getElementById('cur_hid').value='';
		document.getElementById('cur_sfb').value='';
	};

	this.show_suggest=function(IsSubmit, fld){
		suggest_val=document.getElementById(fld).value;

		if(suggest_val!='' && suggest_val.length>1){
			//this.hide_suggest();

			//document.getElementById('FrmSuggest').style.width=document.getElementById(fld).offsetWidth+'px';

			var left=GetElmAbsPos(document.getElementById(fld), 'offsetLeft');
			document.getElementById('FrmSuggest').style.top=(GetElmAbsPos(document.getElementById(fld), 'offsetTop')+document.getElementById(fld).offsetHeight)+'px';
			document.getElementById('FrmSuggest').style.left=left+'px';

			b=document.body.clientWidth;
			ch=parseInt(u('#FrmSuggest').offsetWidth);
			if((ch+left)>b) u('#FrmSuggest').style.width=u('#'+fld).offsetWidth+'px';

			document.getElementById('FrmSuggestFrame').contentWindow.SetSuggestFor(fld);
			document.getElementById('FrmSuggestFrame').contentWindow.SearchGeo('lst', suggest_val, IsSubmit);
		}else{
			this.hide_suggest();
		}
	};

	this.suggest_sel=function(evt, fld){
		if(document.getElementById('FrmFindCity').style.visibility=='hidden' && document.getElementById('FrmSuggest').style.visibility=='hidden') return true;

		var charCode=(evt.which)?evt.which:event.keyCode;

		document.getElementById('FrmSuggestFrame').contentWindow.SetSuggestFor(fld);
		document.getElementById('FrmSuggestFrame').contentWindow.SelOpt(charCode);
		if(charCode==13) return false;
		else return true;
	};

	this.show_city_sel=function(fld){
		u('#FrmFindCityFrame').contentWindow.SetCitySelFor(fld);

		u('#FrmFindCityFrame').contentWindow.SearchGeo('country', 'city', 'countries');

		var left=GetElmAbsPos(document.getElementById(fld), 'offsetLeft');

		//u('#FrmFindCity').style.width=(document.getElementById(fld).offsetWidth+28)+'px';
		u('#FrmFindCity').style.top=(GetElmAbsPos(document.getElementById(fld), 'offsetTop')+document.getElementById(fld).offsetHeight)+'px';
		u('#FrmFindCity').style.left=left+'px';

		b=document.body.clientWidth;
		ch=parseInt(u('#FrmFindCity').offsetWidth);
		if((ch+left)>b){
			left=b-ch-10;
			u('#FrmFindCity').style.left=left+'px';
		}

		u('#FrmFindCity').style.visibility='visible';
		u('#cur_hid').value='';
		u('#cur_sfb').value='';
	};

	this.hide_suggest=function(){
		this.hide_city_sel();
		document.getElementById('FrmSuggest').style.visibility='hidden';
	};

	this.hide_city_sel=function(){
		document.getElementById('FrmFindCity').style.visibility='hidden';
		if(this.init_dest==document.getElementById('dest').value) this.disp_advanced(true, false);
		else this.disp_advanced(false, false);
	};

	this.set_err=function(f, v){
		if(u('#'+f)===false) return false;
		if(v===false) return u('#'+f).hide();
		u('#'+f).show();
		u('#'+f).innerHTML=v;
	};

	this.clear_err=function(f){
		var okay='<!--'+f.replace('_',':')+'-->';
		if(elm=document.getElementById(f))
			if (elm.innerHTML!=okay) elm.innerHTML='<!-- -->';
	};

	this.check_elm=function(elm){
		meta=[];
		if(this.meta[this.mode]['all'][elm]==undefined) return false;
		meta[elm]=this.meta[this.mode]['all'][elm];
		this._setMode(meta);
	};

	this.owrt_mode=false;
	this.change_owrt=function() {
		if (this.mode=='T'){
			var sel_rt=document.getElementById('io_transfer_rt');
			var sel_ow1=document.getElementById('io_transfer_ow1');
			var sel_ow2=document.getElementById('io_transfer_ow2');
			var elm=document.getElementById('io_dest_block');
			var elm2=document.getElementById('io_orig_block');

			//if(this.owrt_mode=='') sel_rt.checked=true;

			if (sel_rt.checked){
				elm.style.display='';
				elm2.style.display='';
				this.owrt_mode='RT';
			}else if(sel_ow1.checked){
				elm.style.display='none';
				elm2.style.display='';
				this.owrt_mode='OW1';
			}else if(sel_ow2.checked){
				elm.style.display='';
				elm2.style.display='none';
				this.owrt_mode='OW2';
			}
			return;
		}else if(this.mode!='A' && this.mode!='AB' && this.mode!='HA') return;

		var sel=document.getElementById('io_ow');
		var elm=document.getElementById('io_dest_block');

		if(!u('#io_rt').checked && !u('#io_ow').checked) u('#io_rt').checked=true;

		this.owrt_mode='RT';
		if (sel.checked){ elm.style.display='none'; this.owrt_mode='OW'; }
		else elm.style.display='';
	};

	this.set_bold={
		init : function(){
			this.cabin();
			this.owrt();
			this.transfer_owrt();
		},
		set : function(a){
			var i;
			for(i in a){
				if(u('#'+a[i]+'_sel')===false) continue;
				if(u('#'+a[i]).checked) u('#'+a[i]+'_sel').addClass('bold');
				else u('#'+a[i]+'_sel').killClass('bold');
			}
		},
		cabin : function(){
			this.set(['master-cabin-Y','master-cabin-C','master-cabin-F']);
		},
		owrt : function(){
			this.set(['io_rt','io_ow']);
		},
		transfer_owrt : function(){
			this.set(['io_transfer_rt','io_transfer_ow1','io_transfer_ow2']);
		}
	};
}

function oSearchTransferCity(el_country, el_airport, el_city){
	this.countries=[];
	this.countries_show=[];
	this.airports=[];
	this.cities=[];
	this.bind=[];

	this.el_country=document.getElementById(el_country);
	this.el_airport=document.getElementById(el_airport);
	this.el_city=document.getElementById(el_city);

	this.add_country=function(id, name){
		this.countries[id]=name;
	};

	this.add_ap=function(id, name){
		this.airports[id]=name;
	};

	this.add_city=function(id, name){
		this.cities[id]=name;
	};

	this.add=function(country_id, ap_id, city_id){
		this.countries_show[country_id]=true;
		this.bind.push({
			'country':country_id,
			'ap':ap_id,
			'city':city_id
		});
	};

	this.init=function(sel_country, sel_ap, sel_city){
		for(i in this.countries_show) this.el_country.options.add(new Option(this.countries[i], i));
		this.el_country.value=sel_country;
		this.sel_country(sel_ap, sel_city);
	};

	this.sel_country=function(sel_ap, sel_city){
		ClearSelectElements(this.el_airport);
		ClearSelectElements(this.el_city);

		var country=this.el_country.value;
		var shown=[];
		for(i in this.bind){
			if(this.bind[i].country!=country || shown[ this.bind[i].ap ]!=undefined) continue;
			shown[ this.bind[i].ap ]=true;
			this.el_airport.options.add(new Option(this.airports[ this.bind[i].ap ], this.bind[i].ap));
		}
		sort_ListBox(this.el_airport);
		if(sel_ap!==false) this.el_airport.value=sel_ap;
		this.sel_airport(sel_city);
	};

	this.sel_airport=function(sel_city){
		ClearSelectElements(this.el_city);

		var country=this.el_country.value;
		var ap=this.el_airport.value;
		for(i in this.bind){
			if(this.bind[i].country!=country || this.bind[i].ap!=ap) continue;
			this.el_city.options.add(new Option(this.cities[ this.bind[i].city ], this.bind[i].city));
		}
		sort_ListBox(this.el_city);
		if(sel_city!==false) this.el_city.value=sel_city;
	};
}

var oTransferCity_init=false;
function oTransferCity(){
	this.elm={
		'group':document.getElementById('io_transfer_group'),
		'airport':document.getElementById('io_transfer_airport'),
		'city':document.getElementById('io_transfer_city')
	};
	this.cache=[];
	this.XMLer='transfer_city';
	this.items=null;
	if(!xLoad) xLoad=new oXMLload();
	this.added=0;
	this.sel={
		'group':false,
		'airport':false,
		'city':false
	};

	this.type=null;

	this.load=function(type){
		this.type=type;

		var add='';
		if(type=='airport'){
			ClearSelectElements(this.elm['airport']);
			this.elm['airport'].options.add(new Option(uGLOBALS.msg.loading, ''));
			ClearSelectElements(this.elm['city']);
			this.elm['city'].options.add(new Option(uGLOBALS.msg.loading, ''));
			add='&gr='+this.elm['group'].value;
		}else if(type=='city'){
			ClearSelectElements(this.elm['city']);
			this.elm['city'].options.add(new Option(uGLOBALS.msg.loading, ''));
			add='&gr='+this.elm['group'].value+'&ap='+this.elm['airport'].value;
		}

		xLoad.get('/xml/transfer_city?type='+type+add);
	};

	this.show=function(){
		ClearSelectElements(this.elm[this.type]);

		if(!oTransferCity_init)
			this.elm['group'].options.add(new Option(uGLOBALS.msg.search_wiz.T.select_gr, ''));

		for(i in this.items){
			var opt=new Option(this.items[i].name, this.items[i].id);
			if(!i) opt.selected=true;
			this.elm[this.items[i].type].options.add(opt);
		}

		var ok=true;
		if(!oTransferCity_init){
			if(this.sel[this.type]!==undefined && (this.sel[this.type]!='' || this.sel[this.type]>0)){
				this.elm[this.type].value=this.sel[this.type];
			}else{
				ok=false;
				this.elm['airport'].options.add(new Option(uGLOBALS.msg.search_wiz.T.select_ap, ''));
				this.elm['city'].options.add(new Option(uGLOBALS.msg.search_wiz.T.select_rs, ''));
				oTransferCity_init=true;
			}
		}
		if(ok){
			if(this.type=='group') oTC.load('airport');
			else if(this.type=='airport') oTC.load('city');
			else if(this.type=='city') oTransferCity_init=true;
		}
	};

	this.init=function(){
		oTC.load('group');
	};
}
