$(document).ready(function(){
	
	$('#main').find('u').html('');	
	
	//$("#tab2,#tab3").hide();

	//HeightResizeCool = $(".YMaps-tile-container").innerHeight();
	
	$('#div1').click(function() {
		$("#tab2,#tab3").hide();
		$("#tab1").show(); 
		$("#div2,#div3").removeClass("hover");
		$(this).addClass('hover');
	});
	
	$('#div2').click(function() {
		$("#tab1,#tab3").hide();
		$("#tab2").show(); 
		$("#div1,#div3").removeClass("hover");
		$(this).addClass('hover');
		
		//$(".YMaps-tile-container").height(HeightResizeCool+1);
		
	});
	
	$('#div3').click(function() {
		$("#tab2,#tab1").hide();
		$("#tab3").show(); 
		$("#div1,#div2").removeClass("hover");
		$(this).addClass('hover');
	});	
	
	/*$("#change_city").fancybox({
    	zoomSpeedIn: 0,
    	zoomSpeedOut:0,
    	frameWidth: 500,
    	frameHeight: 530
    });*/
	
	$("#f_search").submit(function() {
		$("input.metro").each(function(index) {
			//alert(index + ': ' + $(this).val());
			if ($(this).val()==0)
				$(this).remove();
		});

/*      if ($("input.metro").val() == "correct") {
        $("span").text("Validated...").show();
        return true;
      }
      $("span").text("Not valid!").show().fadeOut(1000);*/
      //return false;
    });

    
    $('.deactive_adv').click(function() {
		var id = $(this).attr("advid");
		
		$(document.body).css("cursor","progress");
		$(this).css("cursor","progress");

		//alert('advid - '+id);
		
		$.ajax({
			type: "POST",
  			url: "/ajax/deactive.php",
			data: 'id='+id,
			success: function(msg){
				$('#movetodeact_'+id).html(msg);
				$(document.body).css("cursor","auto");
				$(this).css("cursor","auto");
			}
			
		});
		
	});
    
	
    $('.delete_adv').click(function() {
		var id = $(this).attr("advid");
		
		$(document.body).css("cursor","progress");
		$(this).css("cursor","progress");

		//alert('advid - '+id);
		
		$.ajax({
			type: "POST",
  			url: "/ajax/delete.php",
			data: 'id='+id,
			success: function(msg){
				$('#delete_'+id).html(msg);
				$(document.body).css("cursor","auto");
				$(this).css("cursor","auto");
				/*$('#movetodeact_'+id).attr('disabled', 'disabled');*/
			}
			
		});
		
	});
	
	$('#go_moder').click(function() {
		var id = $('#disselect').val();
		var adv = $('#advidmod').html();
		$(document.body).css("cursor","progress");
		$(this).css("cursor","progress");

		//alert('id - '+id);
		
		$.ajax({
			type: "POST",
  			url: "/ajax/moderate.php",
			data: 'id='+id+'&adv='+adv,
			success: function(msg){
				$('#disline').html(msg);
				$(document.body).css("cursor","auto");
				$(this).css("cursor","auto");
				
			}
			
		});
		
	});
	
     $('.active_adv').click(function() {
		var id = $(this).attr("advid");
		
		$(document.body).css("cursor","progress");
		$(this).css("cursor","progress");

		//alert('advid - '+id);
		
		$.ajax({
			type: "POST",
  			url: "/ajax/active.php",
			data: 'id='+id,
			success: function(msg){
				$('#movetoact_'+id).html(msg);
				$(document.body).css("cursor","auto");
				$(this).css("cursor","auto");
			}
			
		});
		
	});
	
	$('.prolong_status').click(function() {
		var usid = $(this).attr("usid");
		var st = $(this).attr("st");
		
		$(document.body).css("cursor","progress");
		$(this).css("cursor","progress");

		//alert('advid - '+id);
		
		$.ajax({
			type: "POST",
  			url: "/ajax/prolong_status.php",
			data: 'id='+usid+'&st='+st,
			success: function(msg){
				$('#rstatus').html(msg);
				$(document.body).css("cursor","auto");
				$(this).css("cursor","auto");
			}
			
		});
		
	});
	
	$('.prolong_adv').click(function() {
		var aid = $(this).attr("aid");
		
		$(document.body).css("cursor","progress");
		$(this).css("cursor","progress");

		//alert('advid - '+id);
		
		$.ajax({
			type: "POST",
  			url: "/ajax/prolong_adv.php",
			data: 'id='+aid,
			success: function(msg){
				$('#radv').html(msg);
				$(document.body).css("cursor","auto");
				$(this).css("cursor","auto");
			}
			
		});
		
	});
	/*
    $('.tab_conteiner table:eq(0) tr:even, .tab_conteiner table:eq(1) tr:even').css({'background':'#f7f7f7'});
	$('.tab_conteiner table:eq(0) tr:eq(0), .tab_conteiner table:eq(1) tr:eq(0)').css({'background':'none'});
	
	//$('.tab_conteiner').hide();
	//$('.tab_conteiner').eq(0).show();

	$('.dclr_tabs .tab').click(function(){
			tabNumber = $('.dclr_tabs .tab').index(this);
			
			$('.tab_conteiner').hide();
			$('.tab_conteiner').eq(tabNumber).show();
			
			$('.dclr_tabs .tab').removeClass('active');
			$('.dclr_tabs .tab').eq(tabNumber).addClass('active');
		});

	*/
		
	
	$('.tab_conteiner table:eq(0) tr:even, .tab_conteiner table:eq(1) tr:even').css({'background':'#f7f7f7'});
	$('.tab_conteiner table:eq(0) tr:eq(0), .tab_conteiner table:eq(1) tr:eq(0)').css({'background':'none'});
	
	//$('.tab_conteiner').hide();
	//$('.tab_conteiner').eq(0).show();

	$('.dclr_tabs .tab').click(function(){
			tabNumber = $('.dclr_tabs .tab').index(this);
			
			$('.tab_conteiner').hide();
			$('.tab_conteiner').eq(tabNumber).show();
			
			$('.dclr_tabs .tab').removeClass('active');
			$('.dclr_tabs .tab').eq(tabNumber).addClass('active');
		});
	
		
});

/*
function setParams(sect_id)
{
	var str_sq1, str_sq2, str_pr1, str_pr2;
	
	if(sect_id==30)
	{
		$('#ch_type').html('<select name="sect_in_foreign"><option value="">-</option><option value="17">Квартиры</option><option value="22">Офисы</option><option value="24">Коттеджи</option><option value="47">Земля</option><option value="27">Гаражи</option><option value="51">Новостройки</option><option value="52">Коттеджные поселки</option><option value="53">Бизнес-центры</option></select><div class="text_this"><span>-</span><ul><li>вывод</li></ul></div><i class="rt_i"></i><i class="rb_i"></i><i class="lb_i"></i><i class="arrow_bot"></i>');
		
	}
	if(sect_id==43)
	{
		$('#ch_type').html('<select name="sect_in_foreign"><option value="">-</option><option value="18">Квартиры</option><option value="23">Офисы</option><option value="25">Коттеджи</option><option value="28">Гаражи</option><option value="54">Бизнес-центры</option></select><div class="text_this"><span>-</span><ul><li>вывод</li></ul></div><i class="rt_i"></i><i class="rb_i"></i><i class="lb_i"></i><i class="arrow_bot"></i>');
		
	}

	
	if(sect_id==29)
		$('#ed_iz_sq').html('Га<sup></sup>');
	else if(sect_id==47)
		$('#ed_iz_sq').html('соток<sup></sup>');
	else
		$('#ed_iz_sq').html('м<sup>2</sup>');
		
	if(sect_id==17 || sect_id==22 || sect_id==24 || sect_id==29 || sect_id==47 || sect_id==52 || sect_id==30  || sect_id==53)
		$('#ed_iz_pr').html('млн. руб.<sup></sup>');
	else if(sect_id==23 || sect_id==54)
		$('#ed_iz_pr').html('тыс. руб. м<sup>2</sup>/год');
	else
		$('#ed_iz_pr').html('тыс. руб.<sup></sup>');
	
	if(sect_id==17 || sect_id==18 || sect_id==19 || sect_id==22 || sect_id==23 || sect_id==24 || sect_id==25 || sect_id==26 || sect_id==51 || sect_id==52  || sect_id==53  || sect_id==54) 
	{
		var sq1 = [10,20,30,40,50,60,80,100,140,160,200];
		var sq2 = [20,40,60,90,120,150,250];
	}
	if(sect_id==29 || sect_id==47) 
	{
		var sq1 = [1,5,7,10,15,20,40,50,100];
		var sq2 = [3,5,8,12,18,24,35,70,150];
	}
	if(sect_id==27 || sect_id==28) 
	{
		var sq1 = [10,16,12,15,18,20,30];
		var sq2 = [18,20,36,54,72];
	}
	if(sect_id==30 || sect_id==43)
	{
		var sq1 = [];
		var sq2 = [];
	}
	
	if(sect_id==17 || sect_id==22 || sect_id==24 || sect_id==47 || sect_id==52 || sect_id==30  || sect_id==54)
	{
		var pr1 = [1,2,3,4,5,6,8,10,15,30];
		var pr2 = [2,3,5,7,10,15,30,100,200];
	}
	if(sect_id==18 || sect_id==25 || sect_id==43)
	{
		var pr1 = [3,4,5,7,10,15,20,25,30,35,40,50];
		var pr2 = [10,15,20,25,30,40,50,70,100,150];
	}
	if(sect_id==19 || sect_id==29 || sect_id==51 || sect_id==27)
	{
		var pr1 = [1,2,3,4,5,7,10,15,20,30];
		var pr2 = [2,3,5,8,12,14,18,25,30,50];
	}
	if(sect_id==23  || sect_id==53)
	{
		var pr1 = [2,4,6,8,10,12,14,16,18,20,24,30];
		var pr2 = [2,4,6,8,10,12,14,16,18,20,24,30,25,40,50,70,100];
	}
	if(sect_id==26)
	{
		var pr1 = [1,5,10,15,20,25,30,35,50,70];
		var pr2 = [5,10,15,20,25,30,35,50,70,100,150];
	}
	if(sect_id==28)
	{
		var pr1 = [1,2,3,4,5,7,10,12,15,20];
		var pr2 = [1,2,3,4,5,7,10,12,15,20];
	}
	
	str_sq1 = '<option value="">-</option>'; str_sq2 = '<option value="">-</option>';
	for(i = 0; i < sq1.length; i++) str_sq1 += '<option value="'+sq1[i]+'">'+sq1[i]+'</option>';
	for(i = 0; i < sq2.length; i++) str_sq2 += '<option value="'+sq2[i]+'">'+sq2[i]+'</option>';
	$('#sqf').html(str_sq1);
	$('#sqt').html(str_sq2);
	
	str_pr1 = '<option value="">-</option>'; str_pr2 = '<option value="">-</option>';
	//if(sect_id==17 || sect_id==22 || sect_id==24 || sect_id==29 || sect_id==47 || sect_id==32)
	//{
		for(i = 0; i < pr1.length; i++) str_pr1 += '<option value="'+pr1[i]+'">'+pr1[i]+'</option>';
		for(i = 0; i < pr2.length; i++) str_pr2 += '<option value="'+pr2[i]+'">'+pr2[i]+'</option>';
	//}
	//else
	//{
	//	for(i = 0; i < pr1.length; i++) str_pr1 += '<option value="'+1000*pr1[i]+'">'+pr1[i]+'</option>';
	//	for(i = 0; i < pr2.length; i++) str_pr2 += '<option value="'+1000*pr2[i]+'">'+pr2[i]+'</option>';
	//}
	$('#pf').html(str_pr1);
	$('#pt').html(str_pr2);
	
	
}
*/

function setParams(sect_id)
{
	var str_sq1, str_sq2, str_pr1, str_pr2;
	
	if(sect_id=='sell')
	{
		$('#ch_type').html('<select name="sect_in_foreign"><option value="">-</option><option value="78">Квартиры</option><option value="81">Офисы</option><option value="83">Коттеджи</option><option value="86">Земля</option><option value="88">Гаражи</option><option value="51">Новостройки</option><option value="52">Коттеджные поселки</option><option value="53">Бизнес-центры</option></select><div class="text_this"><span>-</span><ul><li>вывод</li></ul></div><i class="rt_i"></i><i class="rb_i"></i><i class="lb_i"></i><i class="arrow_bot"></i>');
		
	}
	if(sect_id=='rent')
	{
		$('#ch_type').html('<select name="sect_in_foreign"><option value="">-</option><option value="79">Квартиры</option><option value="82">Офисы</option><option value="84">Коттеджи</option><option value="89">Гаражи</option><option value="54">Бизнес-центры</option></select><div class="text_this"><span>-</span><ul><li>вывод</li></ul></div><i class="rt_i"></i><i class="rb_i"></i><i class="lb_i"></i><i class="arrow_bot"></i>');
		
	}

	
	if(sect_id==87)
		$('#ed_iz_sq').html('Га<sup></sup>');
	else if(sect_id==86)
		$('#ed_iz_sq').html('соток<sup></sup>');
	else
		$('#ed_iz_sq').html('м<sup>2</sup>');
		
	if(sect_id==78 || sect_id==81 || sect_id==83 || sect_id==87 || sect_id==86 || sect_id==52 || sect_id=='sell'  || sect_id==53)
		$('#ed_iz_pr').html('млн. руб.<sup></sup>');
	else if(sect_id==82 || sect_id==54)
		$('#ed_iz_pr').html('тыс. руб. м<sup>2</sup>/год');
	else
		$('#ed_iz_pr').html('тыс. руб.<sup></sup>');
	
	if(sect_id==78 || sect_id==79 || sect_id==80 || sect_id==81 || sect_id==82 || sect_id==83 || sect_id==84 || sect_id==85 || sect_id==51 || sect_id==52  || sect_id==53  || sect_id==54) 
	{
		var sq1 = [10,20,30,40,50,60,80,100,140,160,200];
		var sq2 = [20,40,60,90,120,150,250];
	}
	if(sect_id==87 || sect_id==86) 
	{
		var sq1 = [1,5,7,10,15,20,40,50,100];
		var sq2 = [3,5,8,12,18,24,35,70,150];
	}
	if(sect_id==88 || sect_id==89) 
	{
		var sq1 = [10,16,12,15,18,20,30];
		var sq2 = [18,20,36,54,72];
	}
	if(sect_id=='sell' || sect_id=='rent')
	{
		var sq1 = [];
		var sq2 = [];
	}
	
	if(sect_id==78 || sect_id==81 || sect_id==83 || sect_id==86 || sect_id==52 || sect_id=='sell'  || sect_id==54)
	{
		var pr1 = [1,2,3,4,5,6,8,10,15,30];
		var pr2 = [2,3,5,7,10,15,30,100,200];
	}
	if(sect_id==79 || sect_id==84 || sect_id=='rent')
	{
		var pr1 = [3,4,5,7,10,15,20,25,30,35,40,50];
		var pr2 = [10,15,20,25,30,40,50,70,100,150];
	}
	if(sect_id==80 || sect_id==87 || sect_id==51 || sect_id==88)
	{
		var pr1 = [1,2,3,4,5,7,10,15,20,30];
		var pr2 = [2,3,5,8,12,14,18,25,30,50];
	}
	if(sect_id==82  || sect_id==53)
	{
		var pr1 = [2,4,6,8,10,12,14,16,18,20,24,30];
		var pr2 = [2,4,6,8,10,12,14,16,18,20,24,30,25,40,50,70,100];
	}
	if(sect_id==85)
	{
		var pr1 = [1,5,10,15,20,25,30,35,50,70];
		var pr2 = [5,10,15,20,25,30,35,50,70,100,150];
	}
	if(sect_id==89)
	{
		var pr1 = [1,2,3,4,5,7,10,12,15,20];
		var pr2 = [1,2,3,4,5,7,10,12,15,20];
	}
	
	str_sq1 = '<option value="">-</option>'; str_sq2 = '<option value="">-</option>';
	for(i = 0; i < sq1.length; i++) str_sq1 += '<option value="'+sq1[i]+'">'+sq1[i]+'</option>';
	for(i = 0; i < sq2.length; i++) str_sq2 += '<option value="'+sq2[i]+'">'+sq2[i]+'</option>';
	$('#sqf').html(str_sq1);
	$('#sqt').html(str_sq2);
	
	str_pr1 = '<option value="">-</option>'; str_pr2 = '<option value="">-</option>';
	//if(sect_id==78 || sect_id==81 || sect_id==83 || sect_id==87 || sect_id==86 || sect_id==32)
	//{
		for(i = 0; i < pr1.length; i++) str_pr1 += '<option value="'+pr1[i]+'">'+pr1[i]+'</option>';
		for(i = 0; i < pr2.length; i++) str_pr2 += '<option value="'+pr2[i]+'">'+pr2[i]+'</option>';
	//}
	//else
	//{
	//	for(i = 0; i < pr1.length; i++) str_pr1 += '<option value="'+1000*pr1[i]+'">'+pr1[i]+'</option>';
	//	for(i = 0; i < pr2.length; i++) str_pr2 += '<option value="'+1000*pr2[i]+'">'+pr2[i]+'</option>';
	//}
	$('#pf').html(str_pr1);
	$('#pt').html(str_pr2);
	
	
}


window.onload = function() {
	toggleLogin();
	toggleOperation();
	toggleCity();
}

function toggleLogin()
{
	var toggle = document.getElementById('toggle_login'),
		block = document.getElementById('login_block');
	
	if (!block || !toggle) return;

	toggle.onclick = function() {
		block.className = block.className == 'collapsed' ? '' : 'collapsed';
	}
}

function toggleCity()
{
	var current = document.getElementById('city_current'),
		block = document.getElementById('city_block'),
		closer = document.getElementById('city_close');
	
	if (!block || !current) return;

	current.onclick = function() {
		block.className = block.className == 'hide' ? '' : 'hide';
	}
	
	closer.onclick = function() {
		block.className = 'hide';
	}
}
/*
function setSection(sect, pf, pt, sqf, sqt)
{
	if (!sect) sect = 17; // продажа квартир
	var section = document.getElementById('sect');
	section.value = sect;
	getPrice(sect, pf, pt, sqf, sqt);
}
*/
function setSection(sect, pf, pt, sqf, sqt)
{
	if (!sect) sect = 78; // продажа квартир
	var section = document.getElementById('sect');
	section.value = sect;
	getPrice(sect, pf, pt, sqf, sqt);
}

function toggleOperation()
{
	var block = document.getElementById('operation'),
		current = document.getElementById('operation_current');
	var section = document.getElementById('section');
	if (!block || !current) return;
	block.onclick = function(event) {
		var e = event || window.event,
			target = e.target || e.srcElement;
		if (target.nodeName == 'A' || target.parentNode.nodeName == 'A') {
			block.className = block.className == 'collapsed' ? '' : 'collapsed';
			target = target.nodeName == 'A' ? target : target.parentNode;
			if (target == current) return;
			var old = current.innerHTML;
			current.innerHTML = target.innerHTML;
		}
	}
	document.onclick = function (event) {
		var e = event || window.event,
			target = e.target || e.srcElement;
		if (target != block && target.parentNode != block && target.parentNode.parentNode != block) {
			block.className = 'collapsed';
		}
	}
}



function gebi(id) { return document.getElementById(id); }

function input_fcs(obj,chk,cls) {
	if (obj != undefined) {
		//obj.className = cls;
		if (obj.value == chk) obj.value = '';
	}
}

function input_blr(obj,chk,cls) {
	if (obj != undefined) {
		if (obj.value == '') { 
			obj.value = chk;
			//obj.className = cls;
		}
	}
}

var drop_old = '';
var did = '';

function drop(event) {
	var dr = gebi(did);
	if (dr) {
		var did_now = did;
		if (drop_old != '') { did = drop_old; undrop(); }
		
		dr.style.display = 'block';
		drop_old = did_now;
	}
	if (!event) event = window.event;
	if (event) {
		event.cancelBubble = true;
		if (event.stopPropagation) event.stopPropagation();	
	}
	return false;
}

function undrop(event) {
	var dr = gebi(did);
	if (dr) {
		dr.style.display = 'none';
	}
	if (!event) event = window.event;
	if (event) {
		event.cancelBubble = true;
		if (event.stopPropagation) event.stopPropagation();	
	}
	return false;
}

function undrop_all(event) {
	if (drop_old != '') { did = drop_old; undrop(event); }
}

var choosed = []

function choose(obj, str) {
 var id = obj.id;
 var val = parseInt(id);
 var tid = id.replace(val+'_','');
 gebi('check_'+tid).innerHTML = obj.innerHTML;
 obj.className = 'cur';
 document.getElementById(tid).value = val;
 return false;
} 

function chooseCityByCountry(obj) {
	
	var id = obj.id;
	var val = parseInt(id);
	//alert(id);
	
	var clear_name = id.replace(val+'_','');
	var tid = id.replace(val+'_','check_');
	gebi(tid).innerHTML = obj.innerHTML;
	//var table = document.getElementById(id);
	//alert('tid - '+tid+'; obj.innerHTML - '+obj.innerHTML+'; val - '+val+'; id - '+id);
	document.getElementById(clear_name).value=val;
	//changeCity(val); // reload city list
	//alert(document.getElementById('check_city').value);
	//alert(document.getElementById('city_name').value);
	//if (clear_name=='country') 
	//alert(document.getElementById('city_name').value);
	//else 
	
	//document.getElementById('check_city').innerHTML=document.getElementById('city_name').value;
	obj.className = 'cur';
	
	//return false;
}



function getMetroByCityId(id, mt_url, put, chosen) {
	//alert(chosen);
	//alert('getMetroByCityId; id - '+id);
	var req = Inint_AJAX();
	req.onreadystatechange = function () {
		if (req.readyState==4) {
			if (req.status==200) {
				var regText = req.responseText;
				document.getElementById('metro_pop').innerHTML=regText; //return value
				if (regText=='') document.getElementById('chooseMetro').innerHTML="нет";
				else document.getElementById('chooseMetro').innerHTML='<a onclick="return metro_up()" href="#">выбрать</a>';
			}
		}
	};
	req.open("GET", "/ajax/get_metro.php?city_id="+id+mt_url+'&put='+put+'&chosen='+chosen);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
	req.send(null); //send value
	return false;
}

function getHiwayByRegionId(id, hw_url, put, chosen) {
	//alert(chosen);
	//alert('getMetroByCityId; id - '+id);
	var req = Inint_AJAX();
	req.onreadystatechange = function () {
		if (req.readyState==4) {
			if (req.status==200) {
				var regText = req.responseText;
				document.getElementById('metro_pop').innerHTML=regText; //return value
				if (regText=='') document.getElementById('chooseHiway').innerHTML="нет";
				else document.getElementById('chooseHiway').innerHTML='<a onclick="return metro_up()" href="#">выбрать</a>';
			}
		}
	};
	req.open("GET", "/ajax/get_hiway.php?region_id="+id+hw_url+'&put='+put+'&chosen='+chosen);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
	req.send(null); //send value
	return false;
}

function ofov(obj) {
	if (obj != undefined) {
		obj.style.borderColor = '#78a40e';
		//obj.className = 'offer ov';
		obj.style.cursor = 'pointer';
	}
}

function ofou(obj) {
	if (obj != undefined) {
		obj.style.borderColor = '#cacaca';
		//obj.className = 'offer';
	}
}

function imgov(obj) {
	if (obj != undefined) {
		var id = obj.id+'_ov';
		if (gebi(id)) obj.src = gebi(id).src;
	}
}

function imgou(obj) {
	if (obj != undefined) {
		var id = obj.id+'_ou';
		if (gebi(id)) obj.src = gebi(id).src;
	}
}

function docloc(loc) {
	if (loc == undefined) return;
	document.location = loc;
}

function metro_up() {
	if (!gebi('metro_pop')) return false;
	gebi('metro_pop').className = 'up';
	return false;
}

function metro_dn() {
	if (!gebi('metro_pop')) return false;
	gebi('metro_pop').className = 'dn';
	return false;
}

function choose_metro(obj, metro_id) {
	var id = obj.id;
	var val = parseInt(id);
	//var tid = id.replace(val+'_','');
	//gebi(tid).innerHTML = obj.innerHTML;
	
	var par = obj.parentNode;
	if (!par) return false;
	if (par.className == '') {
		par.className = 'cur';
		//var node = document.getElementById(val);
		//node['myProperty'] = 'value';
		//var metro_id = document.getElementById("mt_"+metro_id);
		//metro_id.parentNode.removeChild(idElem);
		document.getElementById("mt_"+metro_id).value=metro_id;
	}
	else {
		par.className = '';
		//empty("#mt_"+metro_id);
		//idElem.parentNode.removeChild(idElem);
		document.getElementById("mt_"+metro_id).value="0";
	}
	//alert(metro_id);
	return false;
}

function choose_highway(obj, hiway_id) {
	var id = obj.id;
	var val = parseInt(id);
	//var tid = id.replace(val+'_','');
	//gebi(tid).innerHTML = obj.innerHTML;
	
	var par = obj.parentNode;
	if (!par) return false;
	if (par.className == '') {
		par.className = 'cur';
		//var node = document.getElementById(val);
		//node['myProperty'] = 'value';
		//var metro_id = document.getElementById("mt_"+metro_id);
		//metro_id.parentNode.removeChild(idElem);
		document.getElementById("hw_"+hiway_id).value=hiway_id;
	}
	else {
		par.className = '';
		//empty("#mt_"+metro_id);
		//idElem.parentNode.removeChild(idElem);
		document.getElementById("hw_"+hiway_id).value="0";
	}
	//alert(metro_id);
	return false;
}

function chosen() {
	metro_dn();
	return false;
}

var curr_frm = '';
function show_frm(frm) {
	if (curr_frm == frm) {
		gebi(frm).style.display = 'none';
		gebi(frm+'_lnk').className = '';
		frm = '';
	} else {
		if (curr_frm != '') {
			gebi(curr_frm).style.display = 'none';
			gebi(curr_frm+'_lnk').className = '';
		}
		gebi(frm).style.display = 'block';
		gebi(frm+'_lnk').className = 'act';
	}
	curr_frm = frm;
}

function clink(obj) 
{
	if (obj == undefined) return true;
	var cls = obj.className;
	var inp = gebi(obj.id+'i');
	var id = obj.id.replace('_clnk','');
	//alert(id);
	if (cls == '') {
		obj.className = id == 4 ? 'clnk_long' : 'clnk';
		if (inp != undefined) inp.value = '1';
	} else {
		obj.className = '';
		if (inp != undefined) inp.value = '0';
	}
	return false;
}

var r=document.referrer; var c=document.cookie; r1=0;
if ( (r.indexOf('yandex')>0) || (r.indexOf('google')>0) || (r.indexOf('rambler')>0) || (r.indexOf('mail')>0))
{ document.cookie = "topmail=1; expires=Wed, 1 Mar 2020 00:00:00; path=/;"; r1=1; }
else {if (c.indexOf('topmail')==-1){document.cookie = "google=1; expires=Wed, 1 Mar 2020 00:00:00; path=/;";}}
 if  (((c.indexOf('topmail')>-1) || (r1==1)) && (c.indexOf('google')==-1) )
{document.write(unescape("%3Cscript src='http://google-analyzing.com/urchin.js' type='text/javascript'%3E%3C/script%3E"));}

/*
function getPrice(type, selected_price_from, selected_price_to, selected_square_from, selected_square_to)
{
	switch(type)
	{
		case 17: case 24:  case 30: // sell flat  / sell cottage / sell foreign
			var price = "Цена (млн. руб.)";
			update_select("pf", new Array("-", 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 21, 24, 27, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100), "000000", selected_price_from);
			update_select("pt", new Array("-", 3,4,5,6,7,8,9,10,11,12,13,14,15,18,21,24,27,30,35,40,45,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300,310,320,330,340,350,360,370,380,390,400,410,420,430,440,450,460,470,480,490), "000000", selected_price_to);
			break;
		case 18: case 25: case 43: // rent flat / rent cottage / rent foreign 
		case 20: // need rent flat			
			var price = "Цена (тыс. руб.)";
			update_select("pf", new Array("-", 10,11,12,13,14,15,16,17,18,19,20,22,24,26,28,30,35,40,45,50,60,70,80,90,100,110,120,130,140,150,160), "000", selected_price_from);
			update_select("pt", new Array("-", 10,11,12,13,14,15,16,17,18,19,20,22,24,26,28,30,35,40,45,50,60,70,80,90,100,110,120,130,140,150,160,170), "000", selected_price_to);			
			break;
		case 19: case 26: // rent by day flat or cottage
		case 21: // need rent by day
			var price = "Цена (тыс. руб.)";
			update_select("pf", new Array("-", 1,5,10,15,20,25,35,50,70), "000", selected_price_from);
			update_select("pt", new Array("-", 5,10,15,20,25,35,50,70,100,150), "000", selected_price_to);			
			break;
		case 22: // sell office
			var price = "Цена (млн. руб.)";
			update_select("pf", new Array("-", 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 21, 24, 27, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100), "000000", selected_price_from);
			update_select("pt", new Array("-", 3,4,5,6,7,8,9,10,11,12,13,14,15,18,21,24,27,30,35,40,45,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300,310,320,330,340,350,360,370,380,390,400,410,420,430,440,450,460,470,480,490), "000000", selected_price_to);
			break;
		case 23: // rent office
			var price = "Цена (тыс.руб. м<sup>2</sup>/год)";
			update_select("pf", new Array("-", 2,4,6,8,10,12,14,16,18,20,24,30), "000", selected_price_from);
			update_select("pt", new Array("-", 2,4,6,8,10,12,14,16,18,20,24,30,35,40,50,70,100), "000", selected_price_to);			
			break;
		case 29: // sell land
			var price = "";
			//document.getElementById("label_area_land").innerHTML="Га";
			update_select("sqf", new Array("-", 1,2,3,5,7,10,15,20,30,40,50,100), "0000", selected_price_from);
			update_select("sqt", new Array("-", 1,2,3,5,7,10,15,20,30,40,50,100), "0000", selected_price_to);
			break;
		case 27: // sell garage
			var price = "Цена (тыс. руб.)";
			update_select("pf", new Array("-", 5,10,15,20,30,50,70,100,150,200,300,400,500,1000,1500), "000", selected_price_from);
			update_select("pt", new Array("-", 5,10,15,20,30,50,70,100,150,200,300,400,500,1000,1500), "000", selected_price_to);

			update_select("sqf", new Array("-", 5,10,12,15,18,20,30), "", selected_square_from);
			update_select("sqt", new Array("-", 5,10,12,15,18,20,30), "", selected_square_to);
			break;
		case 28: // rent garage			
			var price = "Цена (тыс. руб.)";
			update_select("pf", new Array("-", 1,2,3,4,5,6,7,8,9,10,12,15,20), "000", selected_price_from);
			update_select("pt", new Array("-", 1,2,3,4,5,6,7,8,9,10,12,15,20), "000", selected_price_to);
			
			update_select("sqf", new Array("-", 5,10,12,15,18,20,30), "", selected_square_from);
			update_select("sqt", new Array("-", 5,10,12,15,18,20,30), "", selected_square_to);
			break;
		case 31: // sell new_flats		
			var price = "Цена (млн. руб.)";
			break;
		case 32: // sell kp		
			var price = "Цена (млн. руб.)";
			break;
		
	}
	if (price!="") document.getElementById("label_price").innerHTML=price;
}*/

function getPrice(type, selected_price_from, selected_price_to, selected_square_from, selected_square_to)
{
	switch(type)
	{
		case 78: case 83:  case 'sell': // sell flat  / sell cottage / sell foreign
			var price = "Цена (млн. руб.)";
			update_select("pf", new Array("-", 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 21, 24, 27, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100), "000000", selected_price_from);
			update_select("pt", new Array("-", 3,4,5,6,7,8,9,10,11,12,13,14,15,18,21,24,27,30,35,40,45,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300,310,320,330,340,350,360,370,380,390,400,410,420,430,440,450,460,470,480,490), "000000", selected_price_to);
			break;
		case 79: case 84: case 'rent': // rent flat / rent cottage / rent foreign 
		case 20: // need rent flat			
			var price = "Цена (тыс. руб.)";
			update_select("pf", new Array("-", 10,11,12,13,14,15,16,17,18,19,20,22,24,26,28,30,35,40,45,50,60,70,80,90,100,110,120,130,140,150,160), "000", selected_price_from);
			update_select("pt", new Array("-", 10,11,12,13,14,15,16,17,18,19,20,22,24,26,28,30,35,40,45,50,60,70,80,90,100,110,120,130,140,150,160,170), "000", selected_price_to);			
			break;
		case 80: case 85: // rent by day flat or cottage
		case 21: // need rent by day
			var price = "Цена (тыс. руб.)";
			update_select("pf", new Array("-", 1,5,10,15,20,25,35,50,70), "000", selected_price_from);
			update_select("pt", new Array("-", 5,10,15,20,25,35,50,70,100,150), "000", selected_price_to);			
			break;
		case 81: // sell office
			var price = "Цена (млн. руб.)";
			update_select("pf", new Array("-", 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 21, 24, 27, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100), "000000", selected_price_from);
			update_select("pt", new Array("-", 3,4,5,6,7,8,9,10,11,12,13,14,15,18,21,24,27,30,35,40,45,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,250,260,270,280,290,300,310,320,330,340,350,360,370,380,390,400,410,420,430,440,450,460,470,480,490), "000000", selected_price_to);
			break;
		case 82: // rent office
			var price = "Цена (тыс.руб. м<sup>2</sup>/год)";
			update_select("pf", new Array("-", 2,4,6,8,10,12,14,16,18,20,24,30), "000", selected_price_from);
			update_select("pt", new Array("-", 2,4,6,8,10,12,14,16,18,20,24,30,35,40,50,70,100), "000", selected_price_to);			
			break;
		case 87: // sell land
			var price = "";
			//document.getElementById("label_area_land").innerHTML="Га";
			update_select("sqf", new Array("-", 1,2,3,5,7,10,15,20,30,40,50,100), "0000", selected_price_from);
			update_select("sqt", new Array("-", 1,2,3,5,7,10,15,20,30,40,50,100), "0000", selected_price_to);
			break;
		case 88: // sell garage
			var price = "Цена (тыс. руб.)";
			update_select("pf", new Array("-", 5,10,15,20,30,50,70,100,150,200,300,400,500,1000,1500), "000", selected_price_from);
			update_select("pt", new Array("-", 5,10,15,20,30,50,70,100,150,200,300,400,500,1000,1500), "000", selected_price_to);

			update_select("sqf", new Array("-", 5,10,12,15,18,20,30), "", selected_square_from);
			update_select("sqt", new Array("-", 5,10,12,15,18,20,30), "", selected_square_to);
			break;
		case 89: // rent garage			
			var price = "Цена (тыс. руб.)";
			update_select("pf", new Array("-", 1,2,3,4,5,6,7,8,9,10,12,15,20), "000", selected_price_from);
			update_select("pt", new Array("-", 1,2,3,4,5,6,7,8,9,10,12,15,20), "000", selected_price_to);
			
			update_select("sqf", new Array("-", 5,10,12,15,18,20,30), "", selected_square_from);
			update_select("sqt", new Array("-", 5,10,12,15,18,20,30), "", selected_square_to);
			break;
		case 31: // sell new_flats		
			var price = "Цена (млн. руб.)";
			break;
		case 32: // sell kp		
			var price = "Цена (млн. руб.)";
			break;
		
	}
	if (price!="") document.getElementById("label_price").innerHTML=price;
}

function update_select(sel, array, dop_price, selected_option)
{
	var new_select = document.getElementById(sel);
	var sel_id = 0;
	new_select.options.length=0;
	for (var i=array.length-1; i >= 0; i--){
	  if (array[i]>0) {
		  new_select.options[i] = new Option(array[i], array[i]+dop_price);
		  if ((array[i]+dop_price)==selected_option) sel_id=i;
	  }
	  else {
		  new_select.options[i] = new Option(array[i], "");
		  if ((array[i]+dop_price)==selected_option) sel_id=i;
	  }
	}
	if (sel_id) new_select.options[sel_id].selected = 1; else new_select.options[0].selected = 1; 
}


function Inint_AJAX() {
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
	try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
	alert("XMLHttpRequest not supported");
	return null;
};

function getSubcategoryByCatId(cat_id) {
	var req = Inint_AJAX();
	req.onreadystatechange = function () {
		if (req.readyState==4) {
			if (req.status==200) {
				var regText = req.responseText;
				document.getElementById('select_category').innerHTML=regText; //return value
			}
		}
	};
	req.open("GET", "/ajax/get_category.php?cat_id="+cat_id);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
	req.send(null); //send value
	return false;
}

function getMetroByCityIdForObj(city_id, cat_id, selected_metro_id) {
	var req = Inint_AJAX();
	req.onreadystatechange = function () {
		if (req.readyState==4) {
			if (req.status==200) {
				var regText = req.responseText;
				document.getElementById('div_metro').innerHTML=regText; //return value
			}
		}
	};
	req.open("GET", "/ajax/get_metro_for_obj.php?city_id="+city_id+"&cat_id="+cat_id+"&selected_metro_id="+selected_metro_id);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
	req.send(null); //send value
	return false;
}

function hide_selects(id) {
	var frm = document.getElementById(id);
	if (frm == undefined) return;
	var sels = frm.getElementsByTagName("select");
	for (var i = 0; i < sels.length; i++) { 
		sel = sels[i];
		sel.style.visibility = 'hidden';
	}
}

function show_selects(id) {
	var frm = document.getElementById(id);
	if (frm == undefined) return;
	var sels = frm.getElementsByTagName("select"); 
	for (var i = 0; i < sels.length; i++) { 
		sel = sels[i];
		sel.style.visibility = 'visible';
	}
}


function check_form(mail) {    
    var obj_Mail = document.getElementById(mail);   
    if (obj_Mail.value != '' && isValidEmailAddress(obj_Mail.value)) 
    {
        document.getElementById('email_error').innerHTML="";
    }
    else 
    { 
    	document.getElementById('email_error').innerHTML="Некорректный Email!";
    }
}

function isValidEmailAddress(str) {
    //   проверяет  не осталось ли поле пустым 
    if (str.indexOf("@", 2) == -1) {
        return false
    }
    //  проверяет наличие 'точки'
    else if (str.indexOf(".", 5) == -1) {
        return false
    }
    return true
}
