$(document).ready(function(){
	$('table.price tr, table.rubricator tr').hover(
		function(){$(this).css({'background-color':'#DFDFDF'});},
		function(){$(this).css({'background-color':'#FFFFFF'});}
	);
	$('#sel-price-city').change(function(){document.location.href = '?city='+$(this).val();});
	$('td.head-search form input[type=text]').focus(function(){
		if ($(this).attr('value') == 'поиск по порталу') $(this).attr('value', '');
	});
	$('td.head-search form input[type=text]').blur(function(){
		if ($(this).attr('value') == '') $(this).attr('value', 'поиск по порталу');
	});
});
function Prep_Req()
{
	if (window.XMLHttpRequest) { http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }}
	else if (window.ActiveXObject)
	{ 	try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}}
	}
	if (!http_request) { alert('Невозможно создать XMLHTTP объект'); return false; }
	return http_request;
}

function Message(element_id, text)
{
	if (document.getElementById(element_id) == null) return null;
	document.getElementById(element_id).style.textAlign = 'center';
	document.getElementById(element_id).style.fontSize = '20px';
	document.getElementById(element_id).style.color = '#990000';
	document.getElementById(element_id).innerHTML = '<b>'+text+'</b>';
	return 1;
}

function LoginEdit(id)
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/log_edit.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('id='+id+'&what=login');
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				document.getElementById('login_edit').innerHTML=http_request.responseText;
			}
			else alert('Сервер не отвечает.');
	}
}

function PassEdit()
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/log_edit.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('what=pass');
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				document.getElementById('pass_edit').innerHTML=http_request.responseText;
			}
			else alert('Сервер не отвечает.');
	}
}

function LoadFile(name, w, h, url)
{
	var image = document.getElementById('load_image');
	if (image != null && image.value != '')
	{
		var req_load = new JsHttpRequest();
		var args = '?name='+name;
		if (w != null) args += '&w='+w;
		if (h != null) args += '&h='+h;
		req_load.open(null, 'includes/fload.php'+args, true);
		req_load.send({q: image});
		req_load.onreadystatechange = function() {
			if (req_load.readyState == 4)
			{
				if (url != null) document.location.href = url;
				else location.reload(false);
			}
		}
	}
	else
		if (url != null) document.location.href = url;
		else location.reload(false);
	
}

function EditGet(id, what)
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/requery.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('id='+id+'&type=2&what='+what);
	http_request.onreadystatechange = function()
	{
//		alert(http_request.readyState+' - '+http_request.status);
		if (http_request.readyState == 4)
			if (http_request.status == 200)
			{
				if (document.getElementById('edit_form') != null) 
					document.getElementById('edit_form').innerHTML=http_request.responseText;
				else
					if (document.getElementById('edit_form_'+id) != null)
					{
						document.getElementById('edit_form_'+id).innerHTML=http_request.responseText;
					}
				widgInit();
			}
			else alert('Сервер не отвечает.');
	}
}

function EditOk(id, type, what)
{
	http_request = Prep_Req();
	if (type == 1)
	{
		http_request.open('POST', 'includes/parser.php', true);
		http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
		var struct = 'structure=';
		if (document.getElementById('edit_form') != null) 
			struct += encodeURI(document.getElementById('edit_form').innerHTML);
		else
			if (document.getElementById('edit_form_'+id) != null)
				struct += encodeURI(document.getElementById('edit_form_'+id).innerHTML);

		http_request.send(struct);
		http_request.onreadystatechange = function()
		{
			if (http_request.readyState == 4)
				if (http_request.status == 200)
				{
					//document.getElementById('edit_form').innerHTML = http_request.responseText;
					var arr = http_request.responseText.split(' ');
					//document.getElementById('edit_form').innerHTML = arr;
					var args;
					args = 'id='+id;
					args += '&type=1&what='+what;
					for(i=0; i<arr.length; i++) args += '&'+arr[i]+'='+encodeURI(document.getElementById(arr[i]).value);
					//document.getElementById('edit_form').innerHTML = args;
					http_request = Prep_Req();
					http_request.open('POST', 'includes/requery.php', true);
					http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
					http_request.send(args);
					http_request.onreadystatechange = function()
					{
						if (http_request.readyState == 4)
							if (http_request.status == 200)
							{
								var txt = http_request.responseText.split('&');
								if (what == 'ann_estate' || what == 'ready_bus' || what == 'ann_build')
								{
									if (what == 'ann_build' && txt[1] != null)
									{
										document.getElementById('error').innerHTML = txt[1];
										return;
									}
									else LoadFile('../images/'+txt[0], 500);
								}
								else
								{
									var url;
									if (what == 'vizit')
									{
										if (txt[2] != null)
										{
											document.getElementById('error').innerHTML = txt[2];
											return;
										}
										if (txt[1] != null) url = 'http://'+txt[1]+'.promobud.ua/';
										LoadFile('../images/'+txt[0], 250, null, url);
									}
									else LoadFile('../images/'+txt[0], 250);
								}
							}
							else alert('Сервер не отвечает.');
					}
				}
				else alert('Сервер не отвечает.');
		}
	}
	else
	{
		if (confirm('Пдтверждаете удаление?') == true)
		{
			http_request.open('POST', 'includes/requery.php', true);
			http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
			http_request.send('id='+id+'&type=0&what='+what);
			http_request.onreadystatechange = function()
			{
				if (http_request.readyState == 4)
					if (http_request.status == 200)
					{
						if(Message('edit_form', 'Объявление удалено') == null) location.reload(false);
					}
					else alert('Сервер не отвечает.');
			}
		}
	}
}

function PriceSelSet()
{
	var point = document.getElementById('price_rub_new_all').selectedIndex;
	var i=0;
	while(document.getElementById('price_rub_new_'+i) != null)
	{
		document.getElementById('price_rub_new_'+i).selectedIndex = point;
		i++;
	}
}

function PriceEditGet(id)
{
	var val;
	val = document.getElementById('p_name_'+id).innerHTML;
	document.getElementById('p_name_'+id).innerHTML = '<input id="p_name_new_'+id+'" class="text" value="'+val+'" style="width:100%" maxlength="120" type="text">';
	val = document.getElementById('p_price_'+id).innerHTML;
	document.getElementById('p_price_'+id).innerHTML = '<input id="p_price_new_'+id+'" class="text" value="'+val+'" style="width:50px" maxlength="9" type="text">';
	val = document.getElementById('p_city_'+id).innerHTML;
	document.getElementById('p_city_'+id).innerHTML = '<input id="p_city_new_'+id+'" class="text" value="'+val+'" style="width:50px" maxlength="6" type="text">';
	val = document.getElementById('p_phone_'+id).innerHTML;
	document.getElementById('p_phone_'+id).innerHTML = '<input id="p_phone_new_'+id+'" class="text" value="'+val+'" style="width:65px" maxlength="9" type="text">';
	document.getElementById('adm_'+id).innerHTML = '<a href="javascript:PriceEditOk('+id+', 1)"><img src="img/b_apply32.png" /></a>';
}

function PriceEditOk(id, type)
{
	http_request = Prep_Req();
	if (type == 1)
	{
		var args;
		args = 'id='+id;
		args += '&type='+type;
		args += '&p_name_new_'+id+'='+encodeURI(document.getElementById('p_name_new_'+id).value);
		args += '&p_price_new_'+id+'='+encodeURI(document.getElementById('p_price_new_'+id).value);
		args += '&p_city_new_'+id+'='+encodeURI(document.getElementById('p_city_new_'+id).value);
		args += '&p_phone_new_'+id+'='+encodeURI(document.getElementById('p_phone_new_'+id).value);
		http_request.open('POST', 'includes/req_price.php', true);
		http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
		http_request.send(args);
		http_request.onreadystatechange = function()
		{
			if (http_request.readyState == 4)
				if (http_request.status == 200) location.reload(false);
				else alert('Сервер не отвечает.');
		}
	}
	else
	{
		if (confirm('Пдтверждаете удаление: '+document.getElementById('p_name_'+id).innerHTML+'?') == true)
		{
			http_request.open('POST', 'includes/req_price.php', true);
			http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
			http_request.send('id='+id+'&type=0');
			http_request.onreadystatechange = function()
			{
				if (http_request.readyState == 4)
					if (http_request.status == 200) location.reload(false);
					else alert('Сервер не отвечает.');
			}

		}
	}
}

function EstateEditGet(id)
{
	http_request = Prep_Req();
	http_request.open('POST', 'includes/req_estate.php', true);
	http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
	http_request.send('id='+id+'&type=2');
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
			if (http_request.status == 200) document.getElementById('ann_estate_form').innerHTML=http_request.responseText;
			else alert('Сервер не отвечает.');
	}
}

function EstateEditOk(id, type)
{
	http_request = Prep_Req();
	var image = document.getElementById('ann_estate_image');
	if (type == 1)
	{
		var args;
		args = 'id='+id;
		args += '&type='+type;
		if (document.getElementById('sell').checked == true) args += '&sell_buy='+encodeURI('sell');
		else args += '&sell_buy='+encodeURI('buy');
		args += '&es_type='+encodeURI(document.getElementById('es_type').value-0);
		args += '&region='+encodeURI(document.getElementById('region').value);
		args += '&city='+encodeURI(document.getElementById('city').value);
		args += '&part='+encodeURI(document.getElementById('part').checked-0);
		args += '&floors='+encodeURI(document.getElementById('floors').value-0);
		args += '&rooms='+encodeURI(document.getElementById('rooms').value-0);
		args += '&ready='+encodeURI(document.getElementById('ready').value-0);
		args += '&state='+encodeURI(document.getElementById('state').value-0);
		args += '&year='+encodeURI(document.getElementById('year').value-0);
		args += '&sq_all='+encodeURI(document.getElementById('sq_all').value);
		args += '&sq_resid='+encodeURI(document.getElementById('sq_resid').value);
		args += '&sq_cuis='+encodeURI(document.getElementById('sq_cuis').value);
		args += '&sq_land='+encodeURI(document.getElementById('sq_land').value);
		args += '&es_gas='+encodeURI(document.getElementById('es_gas').checked-0);
		args += '&es_water='+encodeURI(document.getElementById('es_water').checked-0);
		args += '&es_sew='+encodeURI(document.getElementById('es_sew').checked-0);
		args += '&es_heat='+encodeURI(document.getElementById('es_heat').checked-0);
		args += '&info='+encodeURI(document.getElementById('info').value);
		http_request.open('POST', 'includes/req_estate.php', true);
		http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
		http_request.send(args);
		http_request.onreadystatechange = function()
		{
			if (http_request.readyState == 4)
				if (http_request.status == 200) 
				{
					LoadFile('../images/'+http_request.responseText);
				}
				else alert('Сервер не отвечает.');
		}
	}
	else
	{
		if (confirm('Пдтверждаете удаление вашего объявления?') == true)
		{
			http_request.open('POST', 'includes/req_estate.php', true);
			http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
			http_request.send('id='+id+'&type=0');
			http_request.onreadystatechange = function()
			{
				if (http_request.readyState == 4)
					if (http_request.status == 200)
					{
						Message('ann_estate_form', 'Объявление удалено');
					}
					else alert('Сервер не отвечает.');
			}
		}
	}
}

function w(obj)
{
	var value = obj.value;
	if (value.match(/[^а-яА-Я-їЇєЄ]/))
	{
		value = value.replace(/[^а-яА-Я-їЇєЄ]/, '');
		value = value.substr(0, 50);
		obj.value = value;
	}
}

function show_clock()
{
	var Digital = new Date();
	var hours = Digital.getHours();
	var minutes = Digital.getMinutes();
	var seconds = Digital.getSeconds();

	if (minutes <= 9) { minutes = "0"+minutes; }
	if (seconds <= 9) { seconds = "0"+seconds; }
	myclock = '';
	myclock += hours+':'+minutes+':'+seconds;
	document.getElementById("clock").innerHTML = myclock;
	setTimeout("show_clock()", 1000);
}

function bookmark()
{
        var title = "Строительный портал promobud.ua";
        var url = "http://www.promobud.ua/";

        if (window.sidebar) {
                window.sidebar.addPanel(title, url, "");
        } else if(window.opera && window.print) {
                var elem = document.createElement('a');
                elem.setAttribute('href',url);
                elem.setAttribute('title',title);
                elem.setAttribute('rel','sidebar');
                elem.click();
        }else if(document.all) {
                window.external.AddFavorite(url, title);
        }else {
                alert("Попробуйте добавить в избранное вручную ");
        }
        return false;
}