var HtlMaster_setCHDCnt={0:0, 1:0};

function htl_book_master(id){
	HtlMaster_setCHDCnt={0:0, 1:0};
	document.getElementById('HtlMaster_hid').value=id;

	PMASTER=new oValid();
	PMASTER.frm_id='pmaster_frm';

	if(u('#pmaster_frm_havedates').value==1){
		PMASTER.parse_frm();
		if(PMASTER.validate()){
			htl_book_submit();
			u('#pmaster_frm').submit();
			return;
		}
	}

	popup_msg.show({
		width : 460,
		title : 'Укажите даты',
		elm : 'HtlMaster_frm'
	});

	PMASTER.parse_frm();
}

function htl_book_submit(){
	if(popup_msg.visible) popup_msg.hide();
	var el;
	if(el=document.getElementById('in_date')) el.value=document.getElementById('HtlMaster_in_date').value;
	if(el=document.getElementById('out_date')) el.value=document.getElementById('HtlMaster_out_date').value;
	show_waitme();
	return true;
}

function HtlMaster_show_cld(owner,ofs,sh) {
	var since=document.getElementById('HtlMaster_in_date'), till=document.getElementById('HtlMaster_out_date');
	var cld=document.getElementById('HtlMaster_cldr'); if (!cld) return;
	var frm=document.getElementById('HtlMaster_cldr_frm'); if (!frm) return;
	if (!sh && !is_cld) { cld.style.display='none'; return; }
	var allowed='';
	wkd=false;

	frm.contentWindow.cldr.allowed=allowed;
	frm.contentWindow.cldr.show(owner,since,till,wkd,allowed);

	cld.style.left=GetElmAbsPos(document.getElementById(owner), 'offsetLeft')+'px';

	cld.style.display='';

	var top=GetElmAbsPos(document.getElementById(owner), 'offsetTop')+(document.getElementById(owner).offsetHeight+5),
	a=getScrollXY(), b=document.body.clientHeight, ch=parseInt(u('#cldr').offsetHeight);
	if((a[1]+b)<=(GetElmAbsPos(since, 'offsetTop')+ch)) top=a[1]+b-ch;

	cld.style.top=top+'px';
	return false;
}

function HtlMaster_rooms(i){
	if(i==1) document.getElementById('HtlMaster_room_CHD2').style.display='none';
	else document.getElementById('HtlMaster_room_CHD2').style.display='';
	document.getElementById('HtlMaster_room_ADT2').style.display=document.getElementById('HtlMaster_room_CHD2').style.display;

	//document.getElementById('rooms_cnt').value=i;
	//SW.rooms_cnt();
}

function HtlMaster_setCHDNum(rm, cnt){
	HtlMaster_setCHDCnt[rm]=cnt;

	//SW.setCHDNum(rm, cnt);

	var ok=false;
	for(i in HtlMaster_setCHDCnt) if(HtlMaster_setCHDCnt[i]>0) ok=true;
	if(!ok){
		document.getElementById('HtlMaster_io_childs_list').style.display='none';
		return;
	}

	document.getElementById('HtlMaster_io_childs_list').style.display='';

	var childs_tpl={
		'h_begin':'<table><tr><td>&nbsp;</td>{body}</tr>', 'h_body':'<td>Реб. {x}</td>',
		'b_begin':'<tr><td width=75>Комната {x}:</td>{body}</tr>', 'b_body':'<td>{sel}</td>',
		'f_begin':'</table>'
	};

	var rooms_num=2;

	max_chd=0;
	for(i=0; i<rooms_num; i++) if(this.HtlMaster_setCHDCnt[i]>max_chd) max_chd=this.HtlMaster_setCHDCnt[i];

	var html=''; var a='';
	for(x=0; x<max_chd; x++) a+=childs_tpl['h_body'].replace('{x}', (x+1));
	html+=childs_tpl['h_begin'].replace('{body}', a);

	for(i=0; i<rooms_num; i++){
		if(this.HtlMaster_setCHDCnt[i]>0){
			var a='';
			for(x=0; x<this.HtlMaster_setCHDCnt[i]; x++){
				a+=childs_tpl['b_body'].replace('{sel}', buildSelect('room_CHD_ages['+i+']['+x+']', 'SW.setCHDAge('+i+', '+x+', this.value);', 1, 17, 0, 1));
			}
			var b=childs_tpl['b_begin'].replace('{x}', parseInt(i)+1);
			b=b.replace('{body}', a);
			html+=b;
		}
	}
	if(html!='') document.getElementById('HtlMaster_io_childs_sel').innerHTML=html+childs_tpl['f_begin'];
}
