var allow_search_update = false;

//////////////////////////////////////////////////////////////////////////
// Апдейт                                                               //
//////////////////////////////////////////////////////////////////////////

function update_search()
{
	if ( !allow_search_update ) return;
	
	var body_size = get_body_size();
	var scroll_position = get_scroll_position();
	
	var background = document.getElementById("search_background");
	var container = document.getElementById("search_container");
	
	background.style.left = scroll_position["left"] + "px";
	background.style.top = scroll_position["top"] + "px";
	background.style.width = body_size["width"] + "px";
	background.style.height = body_size["height"] + "px";
	
	container.style.left = scroll_position["left"] + "px";
	container.style.top = scroll_position["top"] + "px";
	container.style.width = body_size["width"] + "px";
	container.style.height = body_size["height"] + "px";	
	
}

setInterval("update_search()", 1);

//////////////////////////////////////////////////////////////////////////
// Показываем                                                           //
//////////////////////////////////////////////////////////////////////////

function show_search()
{
	allow_search_update = true;
	
	document.getElementById("search_background").style.display = "block";
	document.getElementById("search_container").style.display = "block";
}

//////////////////////////////////////////////////////////////////////////
// Скрываем                                                             //
//////////////////////////////////////////////////////////////////////////

function hide_search()
{
	allow_search_update = false;

	document.getElementById("search_background").style.display = "none";
	document.getElementById("search_container").style.display = "none";
}

function update_collections(factory_id)
{
	var collections = document.getElementById('collection_id');
	collections.options.length = 0;
	
	var ajax = create_ajax();

	if ( !ajax )
	{
		alert("Не удалось создать объект XMLHTTP.");
		return;
	}

	// Выполняем запрос

	ajax.open("GET", "/blocks/public/common/search/ajax.php?ajax=list_factory_collections&factory_id="+factory_id, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	ajax.onreadystatechange =
	function ()
	{
		if ( ajax.readyState != 4 ) return;

		if ( ajax.status != 200 )
		{
			alert("Сервер вернул HTTP код отличный от 200." + ajax.responseText);
			return;
		}
		
		// Парсим ответ сервера
		
		var expression = /(.*?)\|(.*?)\#/g;
		
		var result;
		while ( result = expression.exec(ajax.responseText) )
		{
			collections.options[collections.options.length] = new Option(result[2], result[1]);
		}

	}		

	ajax.send(null);
}

function update_destinations(collection_id)
{
	var destinations = document.getElementById('destination');
	destinations.options.length = 0;
	
	var ajax = create_ajax();

	if ( !ajax )
	{
		alert("Не удалось создать объект XMLHTTP.");
		return;
	}

	// Выполняем запрос

	ajax.open("GET", "/blocks/public/common/search/ajax.php?ajax=list_collection_destinations&collection_id="+collection_id, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	ajax.onreadystatechange =
	function ()
	{
		if ( ajax.readyState != 4 ) return;

		if ( ajax.status != 200 )
		{
			alert("Сервер вернул HTTP код отличный от 200." + ajax.responseText);
			return;
		}
		
		// Парсим ответ сервера
		
		var expression = /(.*?)\|(.*?)\#/g;
		
		var result;
		while ( result = expression.exec(ajax.responseText) )
		{
			destinations.options[destinations.options.length] = new Option(result[2], result[1]);
		}

	}		

	ajax.send(null);
}

function update_sizes(collection_id)
{
	var sizes = document.getElementById('size');
	sizes.options.length = 0;
	
	var ajax = create_ajax();

	if ( !ajax )
	{
		alert("Не удалось создать объект XMLHTTP.");
		return;
	}

	// Выполняем запрос

	ajax.open("GET", "/blocks/public/common/search/ajax.php?ajax=list_collection_sizes&collection_id="+collection_id, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	ajax.onreadystatechange =
	function ()
	{
		if ( ajax.readyState != 4 ) return;

		if ( ajax.status != 200 )
		{
			alert("Сервер вернул HTTP код отличный от 200." + ajax.responseText);
			return;
		}
		
		// Парсим ответ сервера
		
		var expression = /(.*?)\|(.*?)\#/g;
		
		var result;
		while ( result = expression.exec(ajax.responseText) )
		{
			sizes.options[sizes.options.length] = new Option(result[2], result[1]);
		}

	}		

	ajax.send(null);
}

function update_search_content(selector_id)
{
	var factories = document.getElementById('factory_id');
	var collections = document.getElementById('collection_id');
	var sizes = document.getElementById('size');
	var destinations = document.getElementById('destination');
	
	switch (selector_id)
	{
		case 'factory_id':
			update_collections(factories.value); 
			update_destinations(collections.value); 
			update_sizes(collections.value);
		break;
		
		case 'collection_id':
			update_destinations(collections.value);
			update_sizes(collections.value);
		break;
		
		case 'size':
		
		break;
		
		case 'destination':
		
		break;	
	
	}

}

function submit_search_form()
{
	var form  = document.getElementById("search_form");
	
	var factories = document.getElementById('factory_id');
	var collections = document.getElementById('collection_id');
	var sizes = document.getElementById('size');
	var destinations = document.getElementById('destination');
	
	if(factories.value == -1 && collections.value == -1 && sizes.value == -1 && destinations.value == -1)
		alert("Укажите хотя бы один критерий поиска.");
	else
		form.submit();
}


