//oTC// geo processing
var is_cld=false;
var lastWkdSity=''; var lastWkdCache={};

var wt_pos=0, wt_dir=1; wmax=40;

function oGeo(eid) {
	this.clist=null;
	if (eid) this.eid=eid; else this.eid='dest';

	this.show = function() {
		var elm=document.getElementById(this.eid); if (!elm) return;
		// fit select
		for (i in this.clist) elm.options.add( new Option(this.clist[i].name,this.clist[i].gid));
	}
}

function set_cities(prm){
	var elm=document.getElementById('dest'); if (!elm) return;
		// clear select
	var cnt = elm.options.length;
	for (var i=cnt; i--; i>0) elm.remove(i);
	if (prm.value=='') return;
	geo.cntr=prm.value;
	xLoad.get('/xml/geo/'+prm.value);
}

function show_cld(owner,ofs,sh,since_fld,till_fld,not_win,date_limit) {
	if(not_win==undefined) not_win=false;
	if(date_limit==undefined) date_limit=false;
	if(window.HtlMaster_show_cld) HtlMaster_show_cld();
	if(since_fld==undefined) since_fld='in_date';
	if(till_fld==undefined) till_fld='out_date';

	var since=document.getElementById(since_fld), till=document.getElementById(till_fld), wkd=document.getElementById('wkd_date');
	var cld=document.getElementById('cldr'); if (!cld) return;
	var frm=document.getElementById('cldr_frm'); if (!frm) return;
	if (!sh && !is_cld) { cld.style.display='none'; return; }
	var allowed='';

	frm.contentWindow.cldr.opts_reset();

	// get allowed
	if (owner=='wkd_date' ||  HPW!=null) {
		if(HPW!=null) HPW.update();
		var wkdCity=u('#dest').value;
		if (wkdCity=='' && HPW==null) {
			SW.show_city_sel('io_destination'); return;
		}
		if (lastWkdSity!=wkdCity) {
			if (!xLoad) xLoad=new oXMLload();
			var url='/xml/wkd/'+owner+'/'+wkdCity;
			if(lastWkdCache[owner+wkdCity]==undefined) xLoad.get(url,false,true);
			else xLoad['wkd_allow']=lastWkdCache[owner+wkdCity];
			lastWkdSity=wkdCity;
		}
		allowed='|';
		if(xLoad['wkd_allow']!=undefined && xLoad['wkd_allow'].length!=0) {
			lastWkdCache[url]=xLoad['wkd_allow'];
			allowed=''; var tmp,wrk,i,j;
			for (i in xLoad['wkd_allow']) {
				var itm=xLoad['wkd_allow'][i];
				if(itm.t!='date') continue;
				wrk=itm.dt.split('-');
				tmp=new Date(wrk[0],wrk[1]-1,wrk[2],2,0,0);
				if(HPW==null)
					for (j=0;j<3;j++) {allowed+='|'+tmp.getFullYear()+'-'+tmp.getMonth()+'-'+tmp.getDate(); tmp.setDate(tmp.getDate()+1);}
				else
					allowed+='|'+tmp.getFullYear()+'-'+tmp.getMonth()+'-'+tmp.getDate();
			}

			// list of nights
			HPW.cache_date2nights={};
			for(i in xLoad['wkd_allow']){
				var itm=xLoad['wkd_allow'][i];
				if(itm.t!='nights') continue;
				if(HPW.cache_date2nights[itm.dt]==undefined) HPW.cache_date2nights[itm.dt]={};
				HPW.cache_date2nights[itm.dt][itm.k]=itm.v;
			}

			//print_r(xLoad['wkd_allow']);
			//alert(allowed);// return;
			allowed+='|';
		}

		u('#cldr').addClass('frmSuggesterCld-wkd');
	}else
		u('#cldr').killClass('frmSuggesterCld-wkd');

	if(date_limit!==false) frm.contentWindow.cldr.set_month_limit(date_limit[0], date_limit[1]);

	frm.contentWindow.cldr.allowed=allowed;
	frm.contentWindow.cldr.show(owner,since,till,wkd,allowed);

	if(not_win || document.getElementById('wizard_frm')==undefined)
		cld.style.left=GetElmAbsPos(document.getElementById(owner), 'offsetLeft')+'px';
	else
		cld.style.left=GetElmAbsPos(document.getElementById('wizard_frm'), 'offsetLeft')+4+'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(document.getElementById(owner), 'offsetTop')+ch)) top=a[1]+b-ch;
	cld.style.top=top+'px';

	left=GetElmAbsPos(document.getElementById(owner), 'offsetLeft');
	b=document.body.clientWidth;
	ch=parseInt(u('#cldr').offsetWidth);
	if((ch+left)>b){
		left=b-ch-10;
		cld.style.left=left+'px';
	}

	return false;
}


function show_cld2(owner,ofs,sh) {
	var since=document.getElementById(owner);
	var cld=document.getElementById('cldr'); if (!cld) return;
	var frm=document.getElementById('cldr_frm'); if (!frm) return;
	if (!sh && !is_cld) { cld.style.display='none'; return; }
	var allowed='';

	frm.contentWindow.cldr.allowed=allowed;
	frm.contentWindow.cldr.show(owner,since,null,null,allowed);

	cld.style.left=GetElmAbsPos(document.getElementById(owner), 'offsetLeft')+4+'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;
}

var tmp_val='', tmp_mode;
function wzSubmit(val, mode, nowait) {
	if(!uGLOBALS['noreg'] && !is_logged && mode!='' && mode!='htl'){
		tmp_val=val; tmp_mode=mode;
		popups.auth('l');
		return false;
	}

	if(mode=='avia') return true;
	setTimeout('show_wait(1)', 200);
	var elm=document.getElementById('cid'); if(elm) elm.value=val;
	var elm=document.getElementById('book_mode'); if(elm) elm.value=mode;
	if(mode=='htl') document.getElementById('book').submit();
	if(mode!='rid' && !nowait) show_waitme();

	return true;
}

img_pay_ok=uGLOBALS.msg.book_btn_pay;
img_pay_ina=uGLOBALS.msg.book_btn_pay_ina;

preload_img([img_pay_ok, img_pay_ina]);
function enabler(elm) {
	var form=elm.form;
	elm=document.getElementById('uButtons'); if (!elm) return;

	var ok=confirmer(form, '');

	var img=img_pay_ok;
	if(!ok) img=img_pay_ina;
	elm.innerHTML='<input type="image" id="uNext" name="uNext" src="'+img+'">';
}

function confirmer(form, msg){
	var ok=true;
	for(var i=0; i<form.elements.length; i++) if(form.elements[i].type=='checkbox') ok=ok && form.elements[i].checked;
	if(!ok && msg!=undefined && msg!='') alert(msg);
	return ok;
}

function selusr() {
	var adts=document.getElementById('adt').value; adts=parseInt(adts,10);
	if (adts==0) adts=1;
	for (var i=1; i<5; i++) {
		var elm1=document.getElementById('usr'+i); var elm2=document.getElementById('usrs'+i);
		var elm1v=document.getElementById('fname'+i); var elm2v=document.getElementById('lname'+i);
		if (i<=adts) {
			elm1.style.display=elm2.style.display='';
			if (elm1v.value=='none') elm1v.value='';
			if (elm2v.value=='none') elm2v.value='';
		} else {
			elm1.style.display=elm2.style.display='none';
			elm1v.value=elm2v.value='none';
		}
	}
}
