// Поддержка загрузки данных по тайм-ауту (1 секунда после
// последнего отпускания клавиши в текстовом поле).
var timeout = null;
var preloaders = new Array();
function doLoadUp() {
	if (timeout) clearTimeout(timeout);
	timeout = setTimeout(doLoadSPO, 1000);
}
function doLoadSPO() {
	showLoad('Идёт загрузка списка SPO');
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			document.getElementById('spos').innerHTML = req.responseText;
			hideLoad();
		}
	}
	req.caching = true;
	req.open('POST', '/tools/voyage/spo.php', true);
	req.send({ spo_id: document.getElementById('spo_id').value, quick_search: 1 });
//	doLoadDuration(true);
}

function doLoadCityFrom() {
	showLoad('Идёт загрузка списка городов вылета');
	var spo_list = getAllParam('spo');
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			document.getElementById('city_from').innerHTML = req.responseText;
			hideLoad();
		}
	}
	req.caching = true;
	req.open('POST', '/tools/voyage/city_from.php', true);
	req.send({ spo_id: spo_list });
	
	doLoadCountry();
}
function doLoadCountry() {
	showLoad('Идёт загрузка списка стран');
	var spo_list = getAllParam('spo');
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			document.getElementById('countries').innerHTML = req.responseText;
			hideLoad();
		}
	}
	req.caching = true;
	req.open('POST', '/tools/voyage/countries.php', true);
	req.send({ city_from_id: document.getElementById('city_from_id').value,spo_id: spo_list });
	
	doLoadResort(0);
	doLoadDuration(true);
}
function doLoadResort(id) {
	showLoad('Идёт загрузка списка курортов');
	var spo_list = getAllParam('spo');
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			document.getElementById('resorts').innerHTML = req.responseText;
			hideLoad();
		}
	}
	req.caching = true;
	req.open('POST', '/tools/voyage/resorts.php', true);
	req.send({ quick_search: 1, country_id: id, spo_id: spo_list,city_from_id: document.getElementById('city_from_id').value });
	
	doLoadHotel();
	doLoadDuration(true);
}
function doLoadHotel() {
	showLoad('Идёт загрузка списка отелей');
	var spo_list = getAllParam('spo');
	var resorts_list = getAllParam('resort');
	var category_list = getAllParam('category');
	
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			document.getElementById('hotels').innerHTML = req.responseText;
			hideLoad();
		}
	}
	req.caching = true;
	req.open('POST', '/tools/voyage/hotels.php', true);
	req.send({ quick_search: 1, resorts_id: resorts_list, categories_id: category_list, spo_id: spo_list,city_from_id: document.getElementById('city_from_id').value });
	
	doLoadDuration(true);
}
var loading_durations = false;
function doLoadDuration(update_price) {
	// Блокировка повторного запуска загрузки продолжительностей
	if (loading_durations == true) {
		return;
	}
	loading_durations = true;
	
//	showLoad('Идёт загрузка списка продолжительностей заезда');
	var spo_list = getAllParam('spo');
	var resorts_list = getAllParam('resort');
	var hotels_list = getAllParam('hotel');
	var category_list = getAllParam('category');
	
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			loading_durations = false;
			document.getElementById('durations').innerHTML = req.responseText;
			document.getElementById('date_min').innerHTML = req.responseJS.date_min;
			document.getElementById('date_max').innerHTML = req.responseJS.date_max;
			hideLoad();
//			if (update_price && req.responseJS.year_from != 1970) {
//				selectOption('day_from', req.responseJS.day_from);
//				selectOption('month_from', req.responseJS.month_from);
//				selectOption('year_from', req.responseJS.year_from);
//				getWeekDay('from');
//				
//				selectOption('day_to', req.responseJS.day_to);
//				selectOption('month_to', req.responseJS.month_to);
//				selectOption('year_to', req.responseJS.year_to);
//				getWeekDay('to');
//			}
		}
	}
	req.caching = true;
	req.open('POST', '/tools/voyage/durations.php', true);
	req.send({ 
		strict_date: update_price, 
		durations: getAllParam('duration'), 
		city_from_id: document.getElementById('city_from_id').value, 
		country_id: document.getElementById('country_id').value, 
		resorts_id: resorts_list,
		hotel_id: hotels_list,
		categories_id: category_list,
		spo_id: spo_list, 
		day_from: document.getElementById('from_day').value,
		month_from: document.getElementById('from_month').value,
		year_from: document.getElementById('from_year').value,
		day_to: document.getElementById('to_day').value,
		month_to: document.getElementById('to_month').value,
		year_to: document.getElementById('to_year').value
	});
}




function getAllParam(name) {
	var elem = document.getElementsByTagName('INPUT');
	var sel = new Array();
	for(var i=0;i<elem.length;i++) {
		if (elem[i].id.substr(0, name.length) == name && elem[i].checked == true && elem[i].value!='') {
			sel[i] = elem[i].value;
		}
	}
	return sel;
}
	
function getWeekDay(type) {
	year = document.forms['searchform'].elements['year_'+type].value;
	month = document.forms['searchform'].elements['month_'+type].value;
	day = document.forms['searchform'].elements['day_'+type].value;
	
	mydate = new Date(year, month-1, day);
	
	div = document.getElementById('weekday_'+type+'');
	
	switch (mydate.getDay()) {
		case 0:
			div.innerHTML = '<font color="red">Воскресенье</font>';
		break;
		case 1:
			div.innerHTML = 'Понедельник';
		break;
		case 2:
			div.innerHTML = 'Вторник';
		break;
		case 3:
			div.innerHTML = 'Среда';
		break;
		case 4:
			div.innerHTML = 'Четверг';
		break;
		case 5:
			div.innerHTML = 'Пятница';
		break;
		case 6:
			div.innerHTML = '<font color="red">Суббота</font>';
		break;
	}
}
function cleanList(name, status) {
	var elem = document.getElementsByTagName('INPUT');
	var sel = new Array();
	for(var i=0;i<elem.length;i++) {
		if (elem[i].id.substr(0, name.length) == name) {
			elem[i].checked = status;
		}
	}
}
function formOverlap(hide) {
	var e = document.getElementsByTagName('select');
	var act = 'hidden';
	if (hide != true) {
		act = 'visible';
	}
	for(i=0;i<e.length;i++) {
		e[i].style.visibility=act;
	}
	
}
function showLoad(message) {
	formOverlap(true);
	preloaders[preloaders.length] = message;
	document.getElementById('loading').style.display='block';
	document.getElementById('loading').style.left=document.body.clientWidth / 2 - 125;
	document.getElementById('load_message').innerText=message;
}
function hideLoad() {
	preloaders.pop();
	if (preloaders.length == 0) {
		formOverlap(false);
		document.getElementById('loading').style.display='none';
	} else {
		document.getElementById('load_message').innerText=preloaders[preloaders.length - 1];
	}
}
function show_age(child) {
	document.getElementById("show_age_0").style.display = "none";
	document.getElementById("show_age_1").style.display = "none";
	document.getElementById("show_age_2").style.display = "none";
	document.getElementById("show_age_3").style.display = "none";
	document.getElementById("show_age_4").style.display = "none";
	if (child == 0) document.getElementById("show_age_0").style.display = "inline";
	if (child >= 1) document.getElementById("show_age_1").style.display = "inline";
	if (child >= 2) document.getElementById("show_age_2").style.display = "inline";
	if (child >= 3) document.getElementById("show_age_3").style.display = "inline";
	if (child >= 4) document.getElementById("show_age_4").style.display = "inline";
}
function showHideForm() {
	if(document.getElementById('search_form').style.display=='block') {
		document.getElementById('search_form').style.display='none';
	} else {
		document.getElementById('search_form').style.display='block';
	}
}







function setCommission(id, price_id) {
	showLoad('Устанавливается комиссия');
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.responseText != '') {
				alert(req.responseText);
				document.getElementById('commission_'+id).checked = false;
			}
			hideLoad();
		}
	}
	req.caching = false;
	req.open('POST', '/tools/voyage/set_commission.php', true);
	var early_booking = false;
	if (document.getElementById('commission_programm_'+id)) {
		var early_booking = document.getElementById('commission_programm_'+id).checked;
	}
	if (document.getElementById('commission_programm_'+id)) {
		var checked = document.getElementById('commission_programm_'+id).checked;
	} else {
		var checked = 0;
	}
	req.send({ 'id': id, 'early_booking':early_booking, 'price_id' : price_id, 'commission_programm' : checked });
}
