
window.img_tree_array = new Array('tree_folder_closed', 'tree_folder_open', 'tree_leaf', 'tree_line_corner_end', 'tree_line_pick', 'tree_line_vertical', 'tree_minus', 'tree_plus');

window.picholder = {};

/*##################################*/

function tree_node_switch(n, prefix)
{
	var l_name = prefix + 'sublayer_' + n;

	if (byid(l_name) == null)
	{
		return;
	}

	var m = document.getElementById(l_name).style;
	var i = document.getElementById(prefix + 'image_icon_' + n);
	var s = document.getElementById(prefix + 'image_sign_' + n);

	if (m.display == 'block')
	{
		i.src = PATH_TPL_IMG + 'my_tree/tree_folder_closed.gif';
		s.src = PATH_TPL_IMG + 'my_tree/tree_plus.gif';
		m.display = 'none';
	}
	else
	{
		i.src = PATH_TPL_IMG + 'my_tree/tree_folder_open.gif';
		s.src = PATH_TPL_IMG + 'my_tree/tree_minus.gif';
		m.display = 'block';
	}
}

function tree_text_link_folder(node_id, prefix)
{
	tree_node_switch(prefix + node_id);
}


function tree_text_link_leaf(node_id, prefix)
{
	alert("[ " + prefix + node_id + " ]");
}

function structure_open_tree(arr)
{
	var last = 0;

	for (var i=0; i < arr.length; i++)
	{
		last = arr[i];
		tree_node_switch(arr[i], '_PRFX_');
	}

	if (last > 0)
	{
		tree_node_set_seleced(last);
	}
}

function tree_node_set_seleced(node_id)
{
	byid('node_d_' + node_id).style.color = '#FFFFFF';
	byid('node_d_' + node_id).style.backgroundColor = '#126BCD';
	byid('node_d_' + node_id).style.padding = '0px 3px 2px 3px';
}

/*##################################*/

window.tree_layer_id = '';
window.tree_handlers = {};
window.tree_images_loaded = false;
window.nyroModal_scroll_state = null;

// Собственно, инициализация отображения
function struct_layer_open(place_id)
{
	if ($.fn.nyroModal == undefined)
	{
		return;
	}

	window.nyroModal_scroll_state = $.fn.nyroModal.settings.css.content;

	if (jQuery.browser.opera && jQuery.browser.version < 9.50)
	{
		$.fn.nyroModal.settings.css.content = {'overflow': 'scroll'};
	}
	else
	{
		$.fn.nyroModal.settings.css.content = {'overflow': 'hidden', 'overflow-y': 'scroll'};
	}

	window.tree_layer_id = 'tree_layer';

	byid(tree_layer_id).innerHTML = byid('loader_code').innerHTML

	tree_handlers.func_leaf = tree_text_link_leaf;
	tree_handlers.func_folder = tree_text_link_folder;

	if (place_id == undefined)
	{
		tree_text_link_leaf = function (node_id)
		{
			if ( node_id == 36 || node_id == 37 || node_id == 38 )
			{
				//tree_node_switch(node_id, '_PRFX_'); 
			}
			else if (node_id == 0)
			{

			}
			else
			{
				structure_tree_click(node_id)
				byid(tree_layer_id).innerHTML = byid('loader_code').innerHTML;
			}
		}

		tree_text_link_folder = tree_text_link_leaf;
	}
	else if (place_id == 'search')
	{
		tree_text_link_leaf = function (node_id)
		{
			if (node_id == 0) {return;}

			search_set_category(node_id)
		}

		tree_text_link_folder = tree_text_link_leaf;
	}
	else if (place_id == 'move_advert')
	{
		tree_text_link_leaf = function (node_id)
		{
			if (node_id == 0) {return;}

			adv_move_to_dir(window.movedir_id, node_id);
		}

		tree_text_link_folder = tree_text_link_leaf;
	}
	else if (place_id == 'catfav')
	{
		tree_text_link_leaf = function (node_id)
		{
			if (node_id == 0) {return;}

			if ( node_id == 36 || node_id == 37 || node_id == 38 )
			{
				return;
			}

			office_catfav_add(node_id);
		}

		tree_text_link_folder = tree_text_link_leaf;
	}
	else if (place_id == 'imagelist')
	{
		tree_text_link_leaf = function (node_id)
		{
			if (node_id == 0) {return;}

			imagelist_set_cat(node_id);
		}

		tree_text_link_folder = tree_text_link_leaf;
	}
	
	
	//##############

	if (window.tree_images_loaded || (jQuery.browser.msie && jQuery.browser.version < 7) )
	{
		setTimeout("struct_load_and_place()", 1000);
	}
	else
	{
		setTimeout("tree_images_preload_init()", 1000);
	}

	//##############
}

function tree_images_preload_init()
{
	for (var i in window.img_tree_array)
	{
		var img = new Image();
		img.onload = tree_images_counter_inc;
		img.src = PATH_TPL_IMG + 'my_tree/' + window.img_tree_array[i] + '.gif';
	}
}

window.tree_images_loaded = 0;

function tree_images_counter_inc()
{
	window.tree_images_loaded++;

	if (window.tree_images_loaded == window.img_tree_array.length)
	{
		struct_load_and_place();
		window.tree_images_loaded = true;
	}
}


function struct_load_and_place()
{
	var send_data = {}

	send_data['open_id'] = 0;
	send_data['prefix_id'] = '_PRFX_';

	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/tree.php', send_data,
		function(result, errors)
		{
			byid(window.tree_layer_id).innerHTML = '<div style="margin-bottom: 5px; font: normal 11px tahoma; border-bottom: 1px solid #777777; padding-bottom: 5px; padding-left: 5px;">Кликайте на плюсик, чтобы раскрыть категорию,<br> и на названии категории, чтобы в неё перейти.</div>' + result["tree_html"];
			$.fn.nyroModal.settings.css.content = window.nyroModal_scroll_state;

			byid('debug').innerHTML = errors;
		},
		true
	);
}

// функция вызывается по клику на ноде дерева
// получает URL выбранной директории и отправляет по этому адресу пользователя
function structure_tree_click(a)
{
	var send_data = {}

	send_data['mode'] = 'path_by_id';
	send_data['id'] = a;

	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/common.php', send_data,
		function(result, errors)
		{
			byid('debug').innerHTML = errors;

			go_url(PATH_BASE + 'all/' + result["structure_path"]);
		},
		true
	);
}

//############################################################################################################

function auth_loader_display()
{
	LayerHide('loginform');
	LayerShow('authloader');
}

function auth_loader_hide()
{
	LayerShow('loginform');
	LayerHide('authloader');
}

function auth_do()
{
	var save_session = document.getElementsByName('ip');

	var auth_ip_lock_mode = 0;

	for (var i = 0; i < save_session.length ; i++)
	{
		if (save_session[i].checked)
		{
			auth_ip_lock_mode = save_session[i].value;
		}
	}

	//#######

	var send_data = {}

	send_data['auth_ip_lock_mode'] = auth_ip_lock_mode;
	send_data['auth_login'] = byid('auth_login').value;
	send_data['auth_password'] = byid('auth_password').value;
	send_data['auth_save'] = byid('auth_save').checked;

	send_data['POST_KEY'] = pk();
	
	auth_loader_display();

	JsHttpRequest.query
	(
		'POST /ajax_backend/login.php', send_data,
		function(result, errors)
		{
			if (result["error_message"] != 'ok')
			{
				auth_loader_hide();

				byid('auth_login').value = '';
				byid('auth_password').value = '';

				alert(result["error_message"]);

				byid('auth_login').focus();
			}
			else
			{
				page_reload();
			}

			byid('debug').innerHTML = errors;
		},
		true
	);
}


function auth_enter(e)
{
	var keynum;

	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if (e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}

	if (keynum == 13)
	{
		auth_do();
	}
}


function password_recovery_send()
{
	if (!byid('login_or_email').value.trim())
	{
		alert('Необходимо указать логин или e-mail');
		return;
	}


	var send_data = {}
	send_data['login_or_email'] = byid('login_or_email').value;
	send_data['POST_KEY'] = pk();

	auth_loader_display();

	JsHttpRequest.query
	(
		'POST /ajax_backend/password_recovery.php', send_data,
		function(result, errors)
		{
			auth_loader_hide();

			if (result["error_message"] != 'ok')
			{
				alert(result["error_message"]);
			}
			else
			{
				byid('login_or_email').value = '';
				alert('Вам было отправлено письмо, проверьте почту через несколько минут');
			}
		},
		true
	);
}


function do_logout()
{
	if (confirm('Вы уверены, что хотите выйти?'))
	{
		byid('logout_form').submit();
	}
}

function video_get_nv(adv_id)
{
	var send_data = {}

	send_data['adv_id'] = adv_id;

	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/video_get_player.php', send_data,
		function(result, errors)
		{
			if (result.error_message != 'ok')
			{
				alert(result.error_message);
			}
			else
			{
				swfobject.embedSWF(result.player_html.link, "mvp", result.player_html.x, result.player_html.y, "9.0.0");

				$.nyroModalSettings({
					height: result.player_html.y
				});
			}
		},
		true
	);


}


function comment_load_captcha()
{
	LayerShow('captcha_layer');

	var now = new Date();
	var cachecode = now.getMinutes() + '-' + now.getSeconds();

	byid('captcha_cell').innerHTML = '<img src="/tools/captcha.php?form_code=' + byid('form_code').value + '&MODE=comment&cc=' + cachecode + '" id="captcha_img" width="160" height="30">';

	byid('comment_btn_next').style.display = 'none';
	byid('comment_btn_post').style.display = '';
}

function comment_post()
{
	var send_data = {}

	send_data['action'] = 'add';

	send_data['comment_message'] = byid('comment_message').value;
	send_data['comment_adv_id'] = byid('comment_adv_id').value;
	send_data['form_code'] = byid('form_code').value;



	if (byid('comment_name') !== null)
	{
		send_data['comment_name'] = byid('comment_name').value;
	}

	if (byid('comment_contacts') !== null)
	{
		send_data['comment_contacts'] = byid('comment_contacts').value;
	}

	if (byid('captcha') !== null)
	{
		send_data['captcha'] = byid('captcha').value;
	}

	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/comment.php', send_data,
		function(result, errors)
		{
			if (result.error_message != 'ok')
			{
				alert(result.error_message);
			}
			else
			{
				comment_get(send_data['comment_adv_id']);

				//#####

				byid('comment_message').value = '';

				if (byid('comment_name') !== null)
				{
					byid('comment_name').value = '';
				}

				if (byid('comment_contacts') !== null)
				{
					byid('comment_contacts').value = '';
				}

				if (byid('captcha') !== null)
				{
					byid('captcha').value = '';
				}

				//##############

				if (byid('captcha_layer') != undefined)
				{
					byid('comment_btn_next').style.display = '';
					byid('comment_btn_post').style.display = 'none';

					LayerHide('captcha_layer');
				}
			}
		},
		true
	);
}


function comment_get(adv_id)
{
	var send_data = {}

	send_data['action'] = 'list';
	send_data['adv_id'] = adv_id;

	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/comment.php', send_data,
		function(result, errors)
		{
			if (result.error_message != 'ok')
			{
				alert(result.error_message);
			}
			else
			{
				byid('comments_box').innerHTML = result.comments;

				if (byid('comments_box').innerHTML.length == 0)
				{
					LayerHide('comment_block');
					LayerShow('no_comments');
				}

				setTimeout("$('#comments_box a.nyroModal').nyroModal()", 100);
			}
		},
		true
	);
}

function advantage_load()
{
	var send_data = {}

	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/advantages.php', send_data,
		function(result, errors)
		{
			if (result.error_message != 'ok')
			{
				alert(result.error_message);
			}
			else
			{
				byid('advantage_text').innerHTML = result.text;
			}
		},
		true
	);
}

function pint(fid, val)
{
	var v = parseInt(val);

	if (isNaN(v))
	{
		v = '';
	}

	byid(fid).value = v;
}


window.report_id = 0;
window.report_section = '';

function report_set_params(id, section)
{
	window.report_id = id;
	window.report_section = section;

	LayerHide('report_ok_box');
	LayerShow('report_fld_box');
}

function report_comment_init(comment_id)
{
	report_set_params(comment_id, 'comment');
}

function report_send()
{
	if (!byid('report_reason').value)
	{
		alert("Пожалуйста, выберите из списка причину Вашего сообщения");
		return;
	}

	//##################

	var send_data = {}

	send_data['action'] = 'send';
	send_data['rec_id'] = window.report_id;
	send_data['rec_section'] = window.report_section;

	send_data['reason'] = byid('report_reason').value;
	send_data['comment'] = byid('report_comment').value;

	send_data['POST_KEY'] = pk();

	LayerHide('report_fld_box');

	JsHttpRequest.query
	(
		'POST /ajax_backend/report.php', 
		send_data,
		function(result, errors)
		{
			if (result.error_message == 'ok')
			{
				LayerShow('report_ok_box');

				byid('report_reason').value = '';
				byid('report_comment').value = '';

				setTimeout("$.nyroModalRemove()", 1000);
			}
			else
			{
				LayerShow('report_fld_box');
				alert('Ошибка: ' + result.error_message);
			}

			byid('debug').innerHTML = errors;

		},
		true
	);	

}


function bookmark_it(adv_id)
{
	byid('fav_star' + adv_id).src = window.PATH_TPL_IMG + 'loading_small1.gif';

	var send_data = {}

	send_data['adv_id'] = adv_id;
	send_data['MODE'] = 'bookmark_add';
	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/tools.php', send_data,
		function(result, errors)
		{
			if (result.error_message == 'ok')
			{
				//
			}
			else
			{
				alert('\n\nОШИБКА: ' + result.error_message + '\n\n');
			}

			byid('debug').innerHTML = errors;

			byid('fav_star' + adv_id).src = window.PATH_TPL_IMG + 'i_c_fav.png';
		},
		true
	);
}


window.search_default_value = 'быстрый поиск предложений';

function search_default_text_focus()
{
	if (byid('search_header').value == window.search_default_value)
	{
		byid('search_header').value = '';
		byid('search_header').style.color = '#705650';
	}
}

function search_default_text_blur()
{
	if (!byid('search_header').value)
	{
		byid('search_header').value = window.search_default_value;
		byid('search_header').style.color = '#A9C2BE';
	}
}


function search_fav_do(q)
{
	byid('search_header').value = q;
	search_header_do();
	byid('search_header').value = window.search_default_value;
}


function search_header_do()
{
	//#####

	var sstring = byid('search_header').value.trim();

	if (sstring.length < 3 || sstring == window.search_default_value)
	{
		mxgbox_show('Необходимо ввести строку поиска, не короче трёх символов');
		return;
	}

	//#####

	var send_data = {}

	send_data['search_string'] = sstring;
	send_data['search_type'] = 'o';
	send_data['search_age'] = 7;
	send_data['search_in_dir'] = '';
	send_data['search_only_titles'] = ''; // Y
	send_data['search_only_with_photo'] = '';
	send_data['captcha'] = '';
	send_data['form_code'] = '';
	send_data['search_in_extra_fields'] = false;

	//#####

	byid('search_header').style.backgroundImage = "url('" + window.PATH_TPL_IMG + "roller.gif')";

	//#####

	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/search.php', send_data,
		function(result, errors)
		{
			byid('search_header').style.backgroundImage = "none";

			if (result["error_message"] != 'ok')
			{
				//alert(result["error_message"]);
				mxgbox_show(result["error_message"]);
			}
			else
			{
				go_url('/search/?q=' + result["search_code"]);
			}

			byid('debug').innerHTML = errors;
		},
		true
	);
}

function search_enter(e)
{
	var keynum;

	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if (e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}

	if (keynum == 13)
	{
		search_header_do();
	}
}


window.adv_photo_more = {};

function adv_photo_more_show(advert_id, pics)
{
	var mk = byid('phi_' + advert_id);
	var ml = byid('phlink_' + advert_id);
	
	if (mk.style.display != 'block')
	{
		if (pics.length > 0)
		{
			for (i in pics)
			{
				byid('pic' + pics[i]).src = window.picholder[pics[i]];
//				byid('pic' + pics[i]).style.visibility = 'hidden';
//				byid('pic' + pics[i]).usemap = pics[i];
//				byid('pic' + pics[i]).onload = function ()
//				{
//					var ic = this.usemap;
//					this.usemap = null;
//
//					this.style.visibility = 'visible';
//
//					LayerHide('loader' + ic);
//				}
			}
		}

		window.adv_photo_more[advert_id] = ml.innerHTML;
		ml.innerHTML = 'скрыть фото';
	}
	else
	{
		ml.innerHTML = window.adv_photo_more[advert_id];
	}

	$('#phi_' + advert_id).slideToggle(200);
}


function sq_switch_hide()
{
	byid('sqmsg').innerHTML = '';
	LayerHide('indicator');
	LayerHide('form_body');
	LayerHide('qsearch_query_table');
}

function sq_switch()
{
	if (byid('qsblock').style.display == 'block')
	{
		// скрываем форму
		$('#qsblock').slideUp(200, sq_switch_hide);	

		if (byid('sq_res_info') != null)
		{
			LayerShow('sq_res_info');
		}

	}
	else
	{
		// отображаем форму

		if (byid('sq_res_info') != null)
		{
			LayerHide('sq_res_info');
		}

		byid('sqmsg').innerHTML = 'скрыть ';

		if (byid('qsearch_option_enabled').value == 'Y')
		{
			LayerShow('indicator');
			qsearch_get_form();
		}
		else
		{
			LayerShow('qsearch_query_table');
			LayerShow('form_body');
			$('#qsblock').slideDown(200);
		}
	}
}

function qsearch_get_form()
{
	var send_data = {}

	send_data['node_id'] = byid('curr_dir_id').value;
	send_data['direction'] = 'o';
	send_data['place'] = 'search';
	send_data['type'] = 'quick';
	send_data['POST_KEY'] = pk();

	window.icosearch_src = byid('icosearch').src;

	byid('icosearch').src = window.PATH_TPL_IMG + 'loading_small2.gif';	

	JsHttpRequest.query
	(
		'POST /ajax_backend/extra_fields.php', send_data,
		function(result, errors)
		{
			byid('icosearch').src = window.icosearch_src;

			LayerHide('indicator');
			LayerShow('form_body');

			byid('search_extra').innerHTML = result.extra_fields_html;

			byid('debug').innerHTML = errors;

			$('#qsblock').slideDown(200);
		},
		true
	);

	//########
}

function qsearch_do()
{
	byid('error_msg').innerHTML = '';

	var send_data = {}

	//############

	send_data['search_string'] = byid('qsearch_query').value;
	send_data['search_type'] = byid('qsearch_mode').value;
	send_data['search_age'] = 7;
	send_data['search_in_dir'] = byid('curr_dir_id').value;
	send_data['search_only_titles'] = false;
	send_data['search_only_with_photo'] = byid('search_only_with_photo').checked;
	send_data['search_in_extra_fields'] = true;

	send_data['search_format'] = 'simple';

	
	//############

	if (byid('user_auth').value != '1' && byid('captcha_enabled').value == '1')
	{
		send_data['captcha'] = byid('captcha').value;
	}
	else
	{
		send_data['captcha'] = '';
	}

	
	send_data['form_code'] = byid('form_code').value;

	//############

	var telements = {};

	var types = ['input', 'textarea', 'select'];

	if (byid('qsearch_option_enabled').value == 'Y')
	{
		for (var e = 0; e < types.length; e++)
		{
			var inputs = byid('fields_list_table').getElementsByTagName(types[e]);

			for (var i = 0; i < inputs.length; i++)
			{
				if (types[e] == 'input' && inputs[i].type == 'button')
				{
					continue;
				}

				var nkey = inputs[i].id; 

				telements[nkey] = inputs[i].value;
			}
		}
	}


	send_data['search_extra'] = telements;

	//############

	send_data['POST_KEY'] = pk();

	byid('search_qi').style.visibility = 'visible';

	JsHttpRequest.query
	(
		'POST /ajax_backend/search.php', send_data,
		function(result, errors)
		{
			byid('search_qi').style.visibility = 'hidden';

			if (result["error_message"] != 'ok')
			{
				byid('error_msg').innerHTML = '<div style="color: red; font: bold 11px verdana;">' + result["error_message"] + '</div> <div class="ruler_dot">-</div>';

				if (byid('user_auth').value != '1' && byid('captcha_enabled').value == '1')
				{
					captcha_change('search');
				}
			}
			else
			{
				byid('error_msg').innerHTML = '';

				go_url(byid('curr_dir_url').value + '/?q=' + result["search_code"]);
			}

			byid('debug').innerHTML = errors;

		},
		true
	);

	//############
}

function search_quick_enter(e)
{
	var keynum;

	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if (e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}

	if (keynum == 13)
	{
		qsearch_do();
	}
}


function captcha_change(place)
{
	if (place == null || !place)
	{
		alert('Place not set');
	}

	var now = new Date();
	var cachecode = now.getMinutes() + '-' + now.getSeconds();

	byid('captcha_img').src = window.PATH_BASE + 'tools/captcha.php?form_code=' + byid('form_code').value + '&MODE=' + place + '&nocache=' + cachecode;

	byid('captcha').value = '';
}

/*##################################*/

function advert_deleting_by_owner(advert_id)
{
	//#####

	var del_code = prompt('Введите код для удаления записи,\nкоторый был указан при добавлении этого объявления', '');

	if (del_code == null || del_code == '')
	{
		return;
	}

	//#####

	var send_data = {}

	send_data['adv_id'] = advert_id;
	send_data['del_code'] = del_code;
	send_data['MODE'] = 'advert_del';
	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/tools.php', send_data,
		function(result, errors)
		{
			if (result.error_message == 'ok')
			{
				alert('Объявление удалено');
				go_url('/');
			}
			else
			{
				alert(result.error_message);
			}

			byid('debug').innerHTML = errors;
		},
		true
	);

	//#####
}

function advert_deleting_by_registred_owner(adv_id, redirect)
{
	if (!confirm('Удалить объявление?'))
	{
		return;
	}

	//#####

	var send_data = {}

	send_data['mode'] = 'office_adv_del';
	send_data['adv_id'] = adv_id;
	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/office.php', 
		send_data,
		function(result, errors)
		{
			if (result.error_message == '')
			{
				if (redirect == undefined)
				{
					page_reload();
				}
				else if (redirect == 'index')
				{
					go_url('/');
				}
			}
			else
			{
				alert(result.error_message);
			}

			byid('debug').innerHTML = errors;
		},
		true
	);
}


function video_stop()
{
	window.video_player_code = byid('video_wrapper').innerHTML;
	byid('video_wrapper').innerHTML = '';
	setTimeout("byid('video_wrapper').innerHTML = window.video_player_code", 100);
}


function setflash(id, flash_html_code)
{
	byid(id).innerHTML = flash_html_code;
}


function nyro_focus_to(focus_field)
{
	window.My_nyroModal_ESC = $.fn.nyroModal.settings.endShowContent;
	window.My_nyroModal_FocusField = focus_field;

	$.fn.nyroModal.settings.endShowContent = function()
	{
		byid(window.My_nyroModal_FocusField).focus();

		$.fn.nyroModal.settings.endShowContent = window.My_nyroModal_ESC;
		window.My_nyroModal_FocusField = '';
	};
}


function login_form_settings()
{
	if (byid('ipconfig').style.display == 'block')
	{
		byid('ipconfig').style.display = 'none';
		$.nyroModalSettings({height: 160, width: 530});
	}
	else
	{
		byid('ipconfig').style.display = 'block'
		$.nyroModalSettings({height: 290, width: 530});
	}
}


function feedback_send()
{
	if (!byid('fb_text').value.trim())
	{
		alert('Чтобы что-то отправить - нужно что-то написать.');
		return;
	}

	var send_data = {}

	send_data['MODE'] = 'feedback_send';
	send_data['POST_KEY'] = pk();
	send_data['fb_text'] = byid('fb_text').value;
	send_data['fb_contacts'] = byid('fb_contacts').value;

	JsHttpRequest.query
	(
		'POST /ajax_backend/tools.php', send_data,
		function(result, errors)
		{
			if (result.error_message == 'ok')
			{
				byid('fb_text').value = '';
				alert('Спасибо. Ваше сообщение отправлено');
				$.nyroModalRemove();
			}
			else
			{
				alert('Ошибка: ' + result.error_message);
			}

			byid('debug').innerHTML = errors;
		},
		true
	);
}


function feedback_close()
{
	if (byid('fb_text').value.trim() && !confirm('Вы уверены, что хотите закрыть окно?'))
	{
		return false;
	}

	$.nyroModalRemove();
}

function setBodyFocus()
{
	document.getElementsByTagName('BODY')[0].focus();
}

//##################

window.close_options_menu = null;

function options_menu_close_init()
{
	window.close_options_menu = setTimeout("options_menu_close()", 500);
}

function options_menu_close_cancel()
{
	clearTimeout(window.close_options_menu);
}

function options_menu_close()
{
	$('DIV#options_block').slideUp(200);
}

//##################

window.addingmenu_menu = null;

function addingmenu_close_init()
{
	window.addingmenu_menu = setTimeout("addingmenu_close()", 500);
}

function addingmenu_close_cancel()
{
	clearTimeout(window.addingmenu_menu);
}

function addingmenu_showsearch()
{
	if (byid('umlinkz') != null && jQuery.browser.msie)
	{
		byid('umlinkz').style.visibility = 'visible';
	}
}

function addingmenu_close()
{
	$('DIV#addmenu').slideUp(200, addingmenu_showsearch);
}

function addingmenu_open()
{
	$('DIV#addmenu').slideDown(200);

	if (byid('umlinkz') != null && jQuery.browser.msie)
	{
		byid('umlinkz').style.visibility = 'hidden';
	}
}


//##################

window.ad_cats_submenu = {};

function ad_cats_close_init(blockid)
{
	window.ad_cats_submenu[blockid] = setTimeout("ad_cats_close(" + blockid + ")", 500);
}

function ad_cats_close_cancel(blockid)
{
	clearTimeout(window.ad_cats_submenu[blockid]);
}

function ad_cats_open(blockid)
{
	$('DIV#obl' + blockid).slideDown(200);
}

function ad_cats_close(blockid)
{
	$('DIV#obl' + blockid).slideUp(200);
}

//##################

function sendmail_send()
{
	var send_data = {}


	if (!byid('sendmail_text').value.trim())
	{
		alert('Необходимо ввести текст письма');
		return false;
	}

	if (!byid('user_auth').value.trim())
	{
		if (!byid('sendmail_from').value.trim())
		{
			alert('Введите Ваше имя или ник');
			return false;
		}

		if (!byid('sendmail_contacts').value.trim())
		{
			alert('Укажите свою контактную информацию');
			return false;
		}

		send_data['sendmail_from'] = byid('sendmail_from').value;
		send_data['sendmail_contacts'] = byid('sendmail_contacts').value;

	}


	if (byid('captcha_required').value == 'Y' && byid('captcha').value.length != 5)
	{
		alert('Необходимо ввести код с картинки');
		return false;
	}
	
	//#######

	send_data['MODE'] = 'sendmail';
	send_data['POST_KEY'] = pk();

	send_data['sendmail_text'] = byid('sendmail_text').value;
	send_data['recipient_id'] = byid('recipient_id').value;
	send_data['form_code'] = byid('form_code').value;

	if (byid('captcha_required').value == 'Y')
	{
		send_data['captcha'] = byid('captcha').value;
	}
	else
	{
		send_data['captcha'] = '';
	}

	JsHttpRequest.query
	(
		'POST /ajax_backend/tools.php', send_data,
		function(result, errors)
		{
			if (result.error_message == 'ok')
			{
				alert('Ваше письмо отправлено');
				page_reload();
			}
			else
			{
				if (byid('captcha_required').value == 'Y')
				{
					captcha_change('sendmail');
				}

				
				alert('Ошибка: ' + result.error_message);
			}

			byid('debug').innerHTML = errors;
		},
		true
	);

	//#######
}

//##################

function swo_wrapper(id, link, x, y)
{
	var params = {};
	var flashvars = {};
	var attributes = {wmode: "transparent"};

	swfobject.embedSWF(link, id, x, y, "9.0.0", flashvars, params, attributes);
}


function blogcomment_send()
{
	if (byid('blogcomment_auth').value == 'N' && !byid('blogcomment_name').value.trim())
	{
		alert('Пожалуйста, укажите Ваше имя или ник');
		return false;
	}

	if (!byid('blogcomment_text').value.trim())
	{
		alert('Пожалуйста, введите текст комментария');
		return false;
	}
	
	//#######

	var send_data = {}

	send_data['MODE'] = 'blogcomment';
	send_data['POST_KEY'] = pk();

	send_data['text'] = byid('blogcomment_text').value;
	send_data['blog_id'] = byid('blog_id').value;

	if (byid('blogcomment_auth').value == 'N')
	{
		send_data['name'] = byid('blogcomment_name').value;
		send_data['email'] = byid('blogcomment_email').value;
		send_data['site'] = byid('blogcomment_site').value;
	}

	JsHttpRequest.query
	(
		'POST /ajax_backend/tools.php', send_data,
		function(result, errors)
		{
			if (result.error_message == 'ok')
			{
				page_reload();
			}
			else
			{
				alert('Ошибка: ' + result.error_message);
			}

			byid('debug').innerHTML = errors;
		},
		true
	);

	//#######
}

function mxgbox_show(msg)
{
	byid('mcont').innerHTML = msg;
	byid('messagebox').style.display = 'block';
//	$('#messagebox').fadeIn("slow", mxgbox_hide_init);

	setTimeout("mxgbox_hide()", 2000);
}

//function mxgbox_hide_init()
//{
//	setTimeout("mxgbox_hide()", 2000);
//}

function mxgbox_hide()
{
	byid('messagebox').style.display = 'none';
//	$('#messagebox').fadeOut("slow");
}

//alert(Dump(document.cookie));

function openinfoblock(id)
{
	$('#infoblock_' + id).slideToggle(200);

//	alert(byid('infoblock_' + id).style.display);
//
//	if (byid('infoblock_' + id).style.display != 'block')
//	{
//		byid('infolink_' + id).innerHTML = 'подробней';
//	}
//	else
//	{
//		byid('infolink_' + id).innerHTML = 'скрыть подробности';
//	}
}

function user_av_list(id)
{
	var send_data = {}

	send_data['MODE'] = 'user_av_list';
	send_data['user_id'] = id;
	send_data['POST_KEY'] = pk();

	JsHttpRequest.query
	(
		'POST /ajax_backend/tools.php', send_data,
		function(result, errors)
		{
			if (result.error_message == 'ok')
			{
				var html = '';


				if (result.list == 'blocked')
				{
					html = '<b>Пользователь запретил просмотр списка своих объявлений</b>';
				}
				else if (result.list.length)
				{
					html += '<table class="data_table" id="useravlist">';
					html += '<tr>';
					html += '<th>дата</th>';
					html += '<th>заголовок</th>';
					html += '</tr>';


					for (i=0; i < result.list.length; i++)
					{
						var cls = (i % 2) ? 'color_2' : '';
						var ds = (result.list[i]['ds']) ? 'cds' : '';

						html += '<tr class=" ' + cls + ' ' + ds + ' " >';

						html += '<td class="date">' + result.list[i]['time_added'] + '</td>';
						html += '<td><a href="/' + result.list[i]['advert_id'] + '/' + result.list[i]['advert_url'] + '">' + result.list[i]['advert_title'] + '</a></td>';

						html += '</tr>';
					}

					html += '</table>';
				}
				else
				{
					html = '<b>Объявлений у этого пользователя пока нет.</b>';
				}

				byid('avlist').innerHTML = html;
			}

			byid('debug').innerHTML = errors;
		},
		true
	);
}


function currency_show()
{
	byid('currency_cut').style.display = 'block';

	$('DIV.currency_data').css("height", parseInt(byid('curr_quantity').value) * 48 + 2 + "px");
	
	LayerHide('curr_show_link');	
}



function set_curr_time()
{
	var timediff = Date.parse(new Date()) - window.my_starttime;

	var l_month = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'];

	var currentTime = new Date(window.my_basetime + timediff)

	////

	var mins = '' + currentTime.getMinutes() + '';

	if (mins.length == 1)
	{
		mins = '0' + mins;
	}

	////

	var hours = '' + currentTime.getHours() + '';

	if (hours.length == 1)
	{
		hours = '0' + hours;
	}

	////

	var fulldate = currentTime.getDate() + ' ' + l_month[currentTime.getMonth()] + ' ' + currentTime.getFullYear() + ' ' + hours + ':' + mins;

	document.getElementById('currtime').innerHTML = fulldate;
}

function set_curr_time_init()
{
	set_curr_time();
	setInterval ( "set_curr_time()", 3000);
}
