function redirect(url) 
{
    location.href = url;
}

function filterOfficeCity() 
{
    out=document.getElementById('town');
    location.href = '/office.php?office=all&town='+out.options[out.selectedIndex].value;
}

function filterCountry()
{
    out=document.getElementById('country');
    if(out.options[out.selectedIndex].value=='другая')
    {	
	var req = new JsHttpRequest();
	var value = 'country';
    	req.onreadystatechange = function() {
        	if (req.readyState == 4) { 	
			document.getElementById('country').innerHTML = req.responseText;
        	}
	}
    	req.open('POST', '/ajax/ajax_country.php', true);
    	req.send({value: value});
    }   
    else getList('city','country','city'); 
}

function uploadFile(id,type,obj)
{
    var value = document.getElementById(obj);
    var req = new JsHttpRequest(); 
    req.onreadystatechange = function() {
        if (req.readyState == 4) { 	
			document.getElementById(type).innerHTML = req.responseText + document.getElementById(type).innerHTML;
        }
    }
    req.open('POST','/ajax/ajax_upload_file.php', true);
    req.send({q: value, id: id, type:type});
}

function deleteFile(id,path) {
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
            if (req.readyState == 4) document.getElementById(id).style.display = 'none';
    }
    req.open('POST', '/ajax/ajax_delete_file.php', true);
    req.send({path: path});
    return false;
}

function filterEstateByType()
{
    out=document.getElementById('estate_type');
    location.href = '/estate.php?estate=all&edit=on&type='+out.options[out.selectedIndex].value;
}

function filterEstateForm()
{
    out=document.getElementById('estate_type');
    el = document.getElementById('estate_'+out.options[out.selectedIndex].value);
	alert('estate_'+out.options[out.selectedIndex].value);
    el.style.display='';
	for(i=0;i<6;i++)
	{
		elem=document.getElementById('estate_'+i);
		if(out.options[out.selectedIndex].value != i)
		{
			elem.style.display='none';
		}
	}
}

function filterCity(obj)
{
	el = document.getElementById(obj);
	el.style.display='';
}

function addStatus(obj)
{
	status=document.getElementById('object[Status]');
	el = document.getElementById(obj);
	sum=status.value+el.options[el.selectedIndex].value;
	if(sum>=0){status.value=sum;}
}

function getList(type, obj, outid) {
	var objid = document.getElementById(obj).value;
	var req = new JsHttpRequest();
	
    	req.onreadystatechange = function() {
        	if (req.readyState == 4) {
        		out = document.getElementById(outid); out.innerHTML="";
        		eval(req.responseText);
        	}
    	}
    	req.open('POST', '/ajax/ajax_getlist.php', true);
    	req.send({type: type, objid: objid});
}

function selectItem(li) {
	if (li.extra) {
		alert("That's '" + li.extra[0] + "' you picked.")
	}
}

// скрывает (отображает) элемент
function toggle(obj) {
    var el = document.getElementById(obj);
    if (el.style.display != 'none') {
        el.style.display = 'none';
    }
    else {
        el.style.display = '';
    }
}

function descCheck(obj)
{
   sl=document.getElementById(obj);
   if(sl.selectedIndex !="0")
   {
    desc = document.getElementById('Description');
    shortdesc = document.getElementById('ShortDesc');
    if(desc.value.length > 400){
	alert('ОБЯЗАТЕЛЬНО заполните краткое описание для объекта! ');
    }
    else
    {
	shortdesc.value=desc.value;
    }
   }
}

function formatItem(row) {
	return row[0] + "<br><i>" + row[1] + "</i>";
}

//$(document).ready(function() {
	//$("#cityedit").autocomplete("/ajax/ajax_search.php", { minChars:1, cacheLength:10, onItemSelect:selectItem, selectOnly:1 });
	//$("#ac_me2").autocomplete("search.php", { minChars:3, matchSubset:false, matchContains:false, cacheLength:10, onItemSelect:selectItem, formatItem:formatItem, selectOnly:1 });
//});

function addInput(obj)
{
	var base = document.getElementById(obj);
	var t = document.createElement('input');
		t.setAttribute('type', 'file');
		t.setAttribute('value', '');
		t.setAttribute('name', obj);
	var br = document.createElement('br');
		base.appendChild(t);
		base.appendChild(br);
}

// автоматически увеличивает размер textarea при ее заполнении
function enlargeTextarea(obj,heightDefault,heightLimit) {
    obj.style.height = (a=(e=14*(obj.value.split('\n').length+2)) > (e2=heightLimit)?e2:e) < heightDefault ? heightDefault : a;
}


