/*
 * ikalm 2010
 * Скрипт для формы "быстрого" поиска
 * Кодировка файла: utf-8
 * 
 */

$(document).ready(function(){
 	// Обработчик на изменение типа недвижимости
	$("#selRealtyTypes").change(function(e){ 
		// See http://www.javascriptkit.com/jsref/event.shtml 
		// to understand why all this shit is needed here...
		// (FF uses srcElement, IE uses target, etc., the same - concerns the other Event props, alas...)
		var evt=window.event || e;
		if (!evt.target) //if event obj doesn't support e.target, presume it does e.srcElement
		evt.target=evt.srcElement; //extend obj with custom e.target prop
		//alert(evt.target.selectedIndex);
		//alert(evt.target.value);
		
		// Обновляем список опций для выбранного типа недвижимости
		$.ajax({
			url: "/find-realty/_ajaxHandler.php?info=ot-by-rt&id=" + evt.target.value,
			cache: false,
			timeout: 3000,
			dataType: 'script',
			success: function(dataIn){
				data = $.json.decode(dataIn);
				$("#realtyType").empty();
				var options = '';
				var options = '<option value="0">- Не выбрано -</option>';
				$.each(data, function(i,item) {
					if(typeof(item) != "undefined") {
						options += '<option value="' + item.ot_id + '">' + item.ot + '</option>';
					};
				});
				//alert(options);
			    $("#realtyType").append(options);
				//$("#realtyType").attr("selectedIndex", 1);
				$("#object_subtype_id").empty();
			},
			error: function(xhr) {
				alert('Error: ' + xhr.status + ' ' + xhr.statusText);
			}		
		});
	});
	
});

