var DEBUG = false;
var load_html_count = 0;
var load_html_overall_time = 0;
var suggest_street_url = "/scripts/suggest_realty_street.php";
var realty_search_do_not_submit_mf = false;

var RealtyAdd = {


	init: function(realty_type){
		RealtyAdd.mode = 'edit';
		RealtyAdd.realty_type = realty_type;
		RealtyAdd.selectRealtyForm(realty_type);
		//geoSetPrefix(realty_type + "_");
		RealtyAdd.action = $(RealtyAdd.realty_type+'_action1') ? $(RealtyAdd.realty_type+'_action1') : ($(RealtyAdd.realty_type+'_action2') ? $(RealtyAdd.realty_type+'_action2') : 'buy');
		RealtyAdd.selectRealtyForm(realty_type);
	},
	
	checkTransport: function(){
		var el = $(RealtyAdd.realty_type +"_"+'transport');
		if(el){
			if(el.value == 'all_one') {
				$(RealtyAdd.realty_type +"_"+'time_min').disabled = $(RealtyAdd.realty_type +"_"+'time_max').disabled = true;
			}else{
				$(RealtyAdd.realty_type +"_"+'time_min').disabled = $(RealtyAdd.realty_type +"_"+'time_max').disabled = false;
			}
		}
		return;
	},
	
	selectRealtyForm: function(new_realty_type){
		if (RealtyAdd.realty_type != '' && $(RealtyAdd.realty_type + '_form')) $(RealtyAdd.realty_type + '_form').style.display = 'none';
		if (new_realty_type != '' && $(new_realty_type + '_form')) $(new_realty_type + '_form').style.display = 'block';
		RealtyAdd.realty_type = new_realty_type;
		RealtyAdd.getAjaxLocation(RealtyAdd.action, 'country', 'Россия', '1', 'Россия');	
		RealtyAdd.getAjaxLocation(RealtyAdd.action, 'region', 'Россия', '1', 'Москва');
		RealtyAdd.getAjaxLocation(RealtyAdd.action, 'city_name', 'Россия', '1', 'Москва');
	},

	on_change_country : function (element) {
		RealtyAdd.getAjaxLocation(RealtyAdd.action, 'region', $('sel_country_'+RealtyAdd.realty_type).getValue(), '1');
		RealtyAdd.getAjaxLocation(RealtyAdd.action, 'city_name', $('sel_country_'+RealtyAdd.realty_type).getValue(), '1','');
	},	

	on_change_region : function () {
		RealtyAdd.getAjaxLocation(RealtyAdd.action, 'city_name', $('sel_country_'+RealtyAdd.realty_type).getValue(), '1', '', $('sel_region_'+RealtyAdd.realty_type).getValue());
	},		

	on_change_city : function () {

	},		
	
	loadHTML: function(container_obj, url, data, extra_data){
		var s = $H(data).toQueryString() + '&' + extra_data;
		new Ajax.Request(url + '?' + s, // !!! no 'parameters'
		  {
		    method: 'post',
		    onSuccess: function(transport){

				s = transport.responseText;
				s = s.replace(/size="7"/i,'size="1"');
				s = s.replace(/multiple/i,'');
				s = s.replace(/width:130px/i,'width:215px;');
				s = s.replace(/disabled/i,'');
				container_obj.innerHTML = s;
		    }
		  });
	},
	
	getAjaxLocation: function(realty_action, realty_place_name, country, get_all, selected_value, region){
		var selected_data = '';
		if (typeof window[realty_place_name + '_selected_data'] != 'undefined') selected_data = '&' + window[realty_place_name + '_selected_data'];;
		var data = {'location': selected_value, 'mode': RealtyAdd.mode, 'realty_type': RealtyAdd.realty_type, 'realty_action': realty_action, 'realty_place_name': realty_place_name, 'type': RealtyAdd.prefix, 'realty_country' : country ? country : RealtyAdd.country , 'get_all' : get_all, 'realty_region' : region};
		RealtyAdd.loadHTML($('ajax-' + realty_place_name + '_' + RealtyAdd.realty_type), '/scripts/get_realty_place.php', data, selected_data);
	},

	setSelectDisabled: function(container, disabled){
		for (i = 0; select_obj = container.getElementsByTagName('select').item(i); i++)
			select_obj.disabled = disabled;
	}
		
};
