//alert( 'ddd' );
var WTB_countries_regions = new Array();
var WTB_countries_cities = new Array();
var WTB_cur_tab_block_name = "block1";

function WTB_tabs_blocks( block_name ) {
	if( block_name != WTB_cur_tab_block_name ) {
		getElement( WTB_cur_tab_block_name ).style.display = "none";
		getElement( block_name ).style.display = "block";
		WTB_cur_tab_block_name = block_name;
		}
	}


function WTB_changeCountryList( list ) {
	var tmp_el, tmp_obj;
	if( list && list.selectedIndex ) {
		var country_id = list[list.selectedIndex].value;
		if( country_id ) {
			var tmp_countries_regions_list = getElement( "countries_regions_list" );
			//alert( tmp_countries_regions_list );
			if( tmp_countries_regions_list ) {
				//alert( tmp_countries_regions_list.remove );
				for( var k = 0 ; k < tmp_countries_regions_list.childNodes.length ; k++ ) { //Удаляем предыдущие записи
					tmp_countries_regions_list.removeChild( tmp_countries_regions_list.childNodes[ k ] );
					k--;
					}
				
				if( list[list.selectedIndex].getAttribute( "_count_regions" ) > 0 ) {
					tmp_el = document.createElement( "OPTION" );
					tmp_el.value = "";
					tmp_el.text = "Загрузка...";
					tmp_countries_regions_list.appendChild( tmp_el );	
					
					tmp_countries_regions_list.disabled = false;
					
					if( !WTB_countries_regions[ "_" + country_id ] ) { //Данные по областям незагружены. Загружаем их.
						HR_runScript( "/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_countries_regions_list&countries_id=" + country_id , true , "GET" );
						}
					else {
						WTB_fillCountriesRegions( country_id );
						}
					}
				else {
					tmp_countries_regions_list.disabled = true;
					}
				}
			var tmp_countries_cities_list = getElement( "countries_cities_list" );
			if( tmp_countries_cities_list ) {
				for( var k = 0 ; k < tmp_countries_cities_list.childNodes.length ; k++ ) { //Удаляем предыдущие записи
					tmp_countries_cities_list.removeChild( tmp_countries_cities_list.childNodes[ k ] );
					k--;
					}
				
				tmp_el = document.createElement( "OPTION" );
				tmp_el.value = "";
				tmp_el.text = "Загрузка...";
				tmp_countries_cities_list.appendChild( tmp_el );	
				
				tmp_countries_cities_list.disabled = false;
				
				if( !WTB_countries_cities[ "_" + country_id ] ) { //Данные по городам незагружены. Загружаем их.
					HR_runScript( "/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_countries_cities_list&countries_id=" + country_id , true , "GET" );
					}
				else {
					WTB_fillCountriesCities( country_id , null );
					}
				}
			}
		}
	}
	
function WTB_fillCountriesRegions( country_id ) {
	//alert( 'dd' );
	
	var tmp_countries_regions_list = getElement( "countries_regions_list" );
	if( tmp_countries_regions_list ) {
		var tmp_el;
		tmp_countries_regions_list.setAttribute( "_cur_country_id" , country_id );
		for( var k = 0 ; k < tmp_countries_regions_list.childNodes.length ; k++ ) {
			tmp_countries_regions_list.removeChild( tmp_countries_regions_list.childNodes[ k ] );
			k--;
			}
						
		if( WTB_countries_regions[ "_" + country_id ] ) { //Список областей удачно загружен (или уже был загружен)
			for( var l = 0 ; l < WTB_countries_regions[ "_" + country_id ].length ; l++ ) {
				tmp_obj = WTB_countries_regions[ "_" + country_id ][ l ];
				tmp_el = document.createElement( "OPTION" );
				tmp_el.value = tmp_obj[ 0 ];
				tmp_el.text = tmp_obj[ 1 ];
				tmp_el.innerHTML = tmp_obj[ 1 ];
				tmp_countries_regions_list.appendChild( tmp_el );
				//tmp_countries_regions_list.options.remove( 0 );
				}
			}
		}
	/*	
	*/
	}
	
function WTB_fillCountriesCities( country_id , region_id ) {
	var tmp_countries_cities_list = getElement( "countries_cities_list" );
	if( tmp_countries_cities_list ) {
		var tmp_el;
		for( var k = 0 ; k < tmp_countries_cities_list.childNodes.length ; k++ ) {
			tmp_countries_cities_list.removeChild( tmp_countries_cities_list.childNodes[ k ] );
			k--;
			}
		if( region_id ) {
			tmp_el = document.createElement( "OPTION" );
			tmp_el.value = "";
			tmp_el.text = "Все города области";
			tmp_el.innerHTML = "Все города области";
			tmp_countries_cities_list.appendChild( tmp_el );
			}
		if( WTB_countries_cities[ "_" + country_id ] ) { //Список областей удачно загружен (или уже был загружен)
			for( var l = 0 ; l < WTB_countries_cities[ "_" + country_id ].length ; l++ ) {
				tmp_obj = WTB_countries_cities[ "_" + country_id ][ l ];
				if( region_id && tmp_obj[ 2 ] != region_id ) continue;
				tmp_el = document.createElement( "OPTION" );
				tmp_el.value = tmp_obj[ 0 ];
				tmp_el.text = tmp_obj[ 1 ];
				tmp_el.innerHTML = tmp_obj[ 1 ];
				tmp_countries_cities_list.appendChild( tmp_el );
				}
			}
		}
	}	


function WTB_getObjectsForFlash( country_id , region_id , city_id ) {
	//alert( country_id );
	//alert( region_id );
	//alert( city_id );
	//alert( '4344' );
	//return;
	//HR_runScript( "/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_objects&countries_id=" + country_id + "&countries_regions_id=" + region_id + "&countries_cities_id=" + city_id + "" , true );
	//if( !type ) type = "flash";
	//try{ WTB_tabs_blocks( "block2" ) } catch(exeption){};
	var loading_mesage = '<table class="cont_h"><tr><td>Загрузка...</td></tr></table>';
	//prompt("","/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_objects&countries_id=" + country_id + "&countries_regions_id=" + region_id + "&countries_cities_id=" + city_id + "&presentators_id=" + present_type + "&type_search=" + type);
	HR_getHTML( "/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_objects&countries_id=" + country_id + "&countries_regions_id=" + region_id + "&countries_cities_id=" + city_id + "&presentators_id=-1&type_search=flash" , true , getElement( "search_results_for_flash" ) , loading_mesage );
	WTB_flashTabChangeView( "objects" );
	//alert( objects_HTML );
	
	//return false;
	}	

function WTB_flashTabChangeView( mode ) {
	if( !mode ) mode = "flash";
	getElement( "search_results_for_flash_container" ).style.display = mode == "objects" ? "block" : "none";
	getElement( "flash_container" ).style.display = mode == "flash" ? "block" : "none";
	}
	
function WTB_getObjects( country_id , region_id , city_id , present_type , type ) {
	//alert( country_id );
	//alert( region_id );
	//alert( city_id );
	//HR_runScript( "/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_objects&countries_id=" + country_id + "&countries_regions_id=" + region_id + "&countries_cities_id=" + city_id + "" , true );
	if( !type ) type = "flash";
	try{ WTB_tabs_blocks( "block2" ) } catch(exeption){};
	var loading_mesage = '<table class="cont_h"><tr><td>Загрузка...</td></tr></table>';
	//prompt("","/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_objects&countries_id=" + country_id + "&countries_regions_id=" + region_id + "&countries_cities_id=" + city_id + "&presentators_id=" + present_type + "&type_search=" + type);
	HR_getHTML( "/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_objects&countries_id=" + country_id + "&countries_regions_id=" + region_id + "&countries_cities_id=" + city_id + "&presentators_id=" + present_type + "&type_search=" + type , true , getElement( "search_result_block" ) , loading_mesage );
	//alert( objects_HTML );
	
	//return false;
	}
	
function getNearestObjects( city_on_klgeo_id , only_in_country ) {
	//alert( city_on_klgeo_id );
	//var zavIframe = getElement( "nearest_zav" );
	if( !city_on_klgeo_id ) {
		alert( 'Введите город' );
		document.forms.search_form.word.focus();
		return false;
		}
	//alert( only_in_country );	
	//var tmp_search_help_container = getElement( "search_help_container" );	
	//var tmp_search_results_container = getElement( "search_results_container" );	
	//if( tmp_search_help_container ) tmp_search_help_container.style.display = "none";
	//if( tmp_search_results_container ) tmp_search_results_container.style.display = "block";
		
	
	//alert( city.substr( 0 , 1 ) );
	//try{ city = city.substr( 0 , 1 ).toUpperCase() + city.substr( 1 ); }
	//catch(exeption){}

	
	var loading_mesage = '<table class="cont_h"><tr><td>Загрузка...</td></tr></table>';
	//prompt("","/klarnetCMSlocal/modules/where_to_buy/get_data.php?lang=" + lang + "&type_action=get_objects&countries_id=" + country_id + "&countries_regions_id=" + region_id + "&countries_cities_id=" + city_id + "&presentators_id=" + present_type + "&type_search=" + type);
	HR_getHTML( "/klarnetCMSlocal/modules/where_to_buy/get_data.php?test_encoding=тест&lang=" + lang + "&type_action=get_objects&city_on_klgeo_id=" + city_on_klgeo_id + "&only_in_country=" + only_in_country , true , getElement( "nearest_search_result_block" ) , loading_mesage );
	
	//alert( city );
	
	//var xmlhttp;
	//if (window.XMLHttpRequest) {
	//	xmlhttp = new XMLHttpRequest();
	//	}
	//else {
	//	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	//	}

	//if ( xmlhttp ) {
		//alert( "/klarnetCMSlocal/search_nearest_stat/stat.php?query=" + city );
	//	xmlhttp.open("GET", "/klarnetCMSlocal/modules/search_nearest_stat/stat.php?test_encode=тест&query=" + city + "&type=" + ( nbi ? "nbi" : "nearest" ) );
	//	xmlhttp.onreadystatechange = function()  { }
	//	xmlhttp.send(null);
	//	}

	/*
	
	var zavIframe = window.frames.nearest_zav;
	var zavContainer = getElement( "nearest_objects_zav" );
	var zavForm = getElement( "nearest_zav_form" );
	if( zavIframe && zavContainer && zavForm ) {
		zavContainer.innerHTML = unescape('%3C') + 'img src="/debug/img/indicator.gif" width="16" height="16" />';
		zavForm.city.value = city;
		zavForm.submit();
		}
		
	var tpIframe = window.frames.nearest_tp;
	var tpContainer = getElement( "nearest_objects_tp" );
	var tpForm = getElement( "nearest_tp_form" );
	if( tpIframe && tpContainer && tpForm ) {
		tpContainer.innerHTML = unescape('%3C') + 'img src="/debug/img/indicator.gif" width="16" height="16" />';
		tpForm.city.value = city;
		tpForm.submit();
		}
		
	var dealerIframe = window.frames.nearest_dealer;
	var dealerContainer = getElement( "nearest_objects_dealer" );
	var dealerForm = getElement( "nearest_dealer_form" );
	if( dealerIframe && dealerContainer && dealerForm ) {
		dealerContainer.innerHTML = unescape('%3C') + 'img src="/debug/img/indicator.gif" width="16" height="16" />';
		dealerForm.city.value = city;
		dealerForm.submit();
		}	
		
	var podIframe = window.frames.nearest_pod;
	var podContainer = getElement( "nearest_objects_pod" );
	var podForm = getElement( "nearest_pod_form" );
	if( podIframe && podContainer && podForm ) {
		podContainer.innerHTML = unescape('%3C') + 'img src="/debug/img/indicator.gif" width="16" height="16" />';
		podForm.city.value = city;
		podForm.submit();
		}	
	*/
	
	return false;
	
	/*
	*/
	}
