function drawCalendar(dateField, from, to){
	window.dateField = dateField.nextSibling;
	var request_url = lng+'date.php?from='+from+'&to='+to+'&cur='+parseInt(window.dateField.value);
	var screen = window.screen;
	var left = screen.availWidth / 2 - 200 / 2;
	var top = screen.availHeight / 2 - 200 / 2;	
	window.open(request_url, "", "width=200, height=200, top="+top+", left="+left+", scrollbars=yes, status=yes, resizable=yes");
}
function elem(id){ return document.getElementById(id) }
function newElem(name){ return document.createElement(name); }
function elemPosition(el){
	var r = { x:el.offsetLeft, y:el.offsetTop };
	if(el.offsetParent){
		var tmp = elemPosition(el.offsetParent)
		r.x += tmp.x
		r.y += tmp.y
	}
	return r
}
function in_array(whot, where){
	for(var i in where)
		if(whot == where[i]) return true;
	return false;
}
///

var from = document.go.from;
var to = document.go.to;
var dateFrom = document.go.dateFrom;
var sheduleFrom = document.go.sheduleFrom;
var type = document.go.type;
var typeI = document.go.typeI;
var dateTo = document.go.dateTo;
var sheduleTo = document.go.sheduleTo;
var q,w,e;
from.citys = [];
for(q in city){
	for(w in shedule){
		e = shedule[w].from;
		if(in_array(e, from.citys)) continue;
		var option = newElem('option');
		from.appendChild(option);
		option.value = e;
		option.innerHTML = city[e].name;
		from.citys.push(e);
	}
}
from.onchange = function(){
	to.value = 0;
	dateFrom.previousSibling.disabled = true;
	dateFrom.previousSibling.value = select_;
	dateFrom.value = 0;
	sheduleFrom.disabled = true;
	while(sheduleFrom.childNodes.length>1)
		sheduleFrom.removeChild(sheduleFrom.lastChild);
	if(this.value == 0){
		to.disabled = true;
	}else{
		to.disabled = false;
		while(to.childNodes.length>1)
			to.removeChild(to.lastChild);
		to.citys = [];
		for(q in city){
			for(w in shedule){
				if(shedule[w].from != from.value) continue;
				e = shedule[w].to;
				if(in_array(e, to.citys)) continue;
				var option = newElem('option');
				to.appendChild(option);
				option.value = e;
				option.innerHTML = city[e].name;
				to.citys.push(e);
			}
		}
	}
	typeRef();
}
to.onchange = function(){
	sheduleFrom.value = 0;
	while(sheduleFrom.childNodes.length>1)
		sheduleFrom.removeChild(sheduleFrom.lastChild);

	dateFrom.previousSibling.value = select_;
	dateFrom.value = 0;
	sheduleFrom.disabled = true;
	
	if(this.value == 0){
		dateFrom.previousSibling.disabled = true;
	} else {
		dateFrom.previousSibling.disabled = false;
		//
		from.nbegin = 0;
		from.nend = 0;
		from.days = [];		
		for(w in shedule){
			if(!(shedule[w].from == from.value && shedule[w].to == this.value)) continue;
			if(shedule[w].nbegin < from.nbegin || from.nbegin == 0)
				from.nbegin = shedule[w].nbegin;
			if(shedule[w].nend > from.nend || from.nend == 0)
				from.nend = shedule[w].nend;
			var d = shedule[w].days.split(',');
			for(q in d)
				if(!in_array(d[q], from.days))
					from.days.push(d[q]);
			//
			if(shedule[w].type == 2){
				typeI.checked = true;
				typeI.disabled = true;
				type.value = 1;
			} else {
				typeI.disabled = false;
				type.value = 0;
			}
		}		
		var cur = new Date();
		cur = new Date(cur.getFullYear(), cur.getMonth(), cur.getDate(), 0, 0, 0); 
		cur = cur.getTime() + (86400*3*1000); // + 3 days
		//
		from.nbegin = new Date(cur);
		from.nend = new Date(from.nend*1000);
	}
	typeRef()
}
dateFrom.onchange = function(){
	sheduleFrom.value = 0;
	sheduleFrom.disabled = false;
	while(sheduleFrom.childNodes.length>1)
		sheduleFrom.removeChild(sheduleFrom.lastChild);
	
	var day = new Date(parseInt(this.value)*1000);
	day = day.getDay();
	if(day == 0) day = 7;
	for(w in shedule){
		if(!(shedule[w].from == from.value && shedule[w].to == to.value)) continue;
		//if(!(shedule[w].nbegin <= this.value && shedule[w].nend >= this.value)) continue;
		//alert(shedule[w].from)
		var d = shedule[w].days.split(',');
		if(!in_array(day, d)) continue;
		var option = newElem('option');
		sheduleFrom.appendChild(option);
		option.value = w;
		option.innerHTML = shedule[w].time;
	}
}
//
function typeRef(){
	sheduleTo.value = 0;
	while(sheduleTo.childNodes.length>1)
		sheduleTo.removeChild(sheduleTo.lastChild);
	
	sheduleTo.disabled = true;
	
	dateTo.previousSibling.value = select_;
	dateTo.value = 0;
	
	if(typeI.checked){
		if(from.value != 0 && to.value != 0){
			dateTo.previousSibling.disabled = false;
			//
			to.nbegin = 0;
			to.nend = 0;
			to.days = [];		
			for(w in shedule){
				if(!(shedule[w].from == to.value && shedule[w].to == from.value)) continue;
				if(shedule[w].nbegin < to.nbegin || to.nbegin == 0)
					to.nbegin = shedule[w].nbegin;
				if(shedule[w].nend > to.nend || to.nend == 0)
					to.nend = shedule[w].nend;
				var d = shedule[w].days.split(',');
				for(q in d)
					if(!in_array(d[q], to.days))
						to.days.push(d[q]);
			}		
			var cur = new Date();
			cur = new Date(cur.getFullYear(), cur.getMonth(), cur.getDate(), 0, 0, 0); 
			cur = cur.getTime() + (86400*3*1000); // + 3 days
			//
			to.nbegin = new Date(cur);
			to.nend = new Date(to.nend*1000);		
		} else {
			dateTo.previousSibling.disabled = true;
		}
		type.value = 1;
	} else {
		dateTo.previousSibling.disabled = true;
		type.value = 0;
	}
}
dateTo.onchange = function(){
	sheduleTo.value = 0;
	sheduleTo.disabled = false;
	while(sheduleTo.childNodes.length>1)
		sheduleTo.removeChild(sheduleTo.lastChild);
	var day = new Date(parseInt(this.value)*1000);
	day = day.getDay();
	if(day == 0) day = 7;
	for(w in shedule){
		if(!(shedule[w].from == to.value && shedule[w].to == from.value)) continue;
		//if(!(shedule[w].nbegin <= this.value && shedule[w].nend >= this.value)) continue;
		var d = shedule[w].days.split(',');
		if(!in_array(day, d)) continue;
		var option = newElem('option');
		sheduleTo.appendChild(option);
		option.value = w;
		option.innerHTML = shedule[w].time;
	}
}
//var request = ["{$form.from}","{$form.to}","{$form.dateFrom}","{$form.dateTo}","{$form.sheduleFrom}","{$form.sheduleTo}"];
from.onchange();
window.onload = function(){
	if(request[0]){
		from.value = request[0];
		from.onchange();
		request[0] = 0;
		window.setTimeout('window.onload()', 100);
		return;
	}
	if(request[1]){
		to.value = request[1];
		to.onchange();
		request[1] = 0;
	}
	if(request[2]!=0){
		dateFrom.value = parseInt(request[2]);
		var date = new Date(parseInt(request[2])*1000);
		dateFrom.previousSibling.value = date.getDate()+' '+months[date.getMonth()]+' '+date.getFullYear();
		dateFrom.onchange();
		request[2] = 0;
		window.setTimeout('window.onload()', 100);
		return;
	}
	if(request[6]!=0){
		typeI.checked = true;
		type.value = 1;
		typeRef()
		request[6] = 0;
	}
	if(request[3]!=0){
		dateTo.value = parseInt(request[3]);
		var date = new Date(parseInt(request[3])*1000);
		dateTo.previousSibling.value = date.getDate()+' '+months[date.getMonth()]+' '+date.getFullYear();
		dateTo.onchange();
		request[3] = 0;
		window.setTimeout('window.onload()', 100);
		return;		
	}
	if(request[4]){
		sheduleFrom.value = request[4];
		request[4] = 0;
	}
	if(request[5]){
		sheduleTo.value = request[5];
		request[5] = 0;
	}
	if(request[7]){
		document.go['class'].value = request[7];
		request[7] = 0;
	}
	if(request[8]){
		document.go.adult.value = request[8];
		request[8] = 0;
	}
	if(request[9]){
		document.go.child.value = request[9];
		request[9] = 0;
	}
	if(request[10]){
		document.go.baby.value = request[10];
		request[10] = 0;
	}
	if(request[11]){
		document.go.baby_plus.value = request[11];
		request[11] = 0;
	}
}
///