function setSizesNoticeForm()
{
	if ('none' == $('#noticeBack').css('display'))
		return;

	$('#noticeBack').css('width', $('#docTable').width() + 'px');
	$('#noticeBack').css('height', $('#docTable').height() + 'px');

	panelWidth = $('#noticePanel table').width();
	panelHeight = $('#noticePanel table').height();

	if ($.browser.msie)
	{
		windowHeight = document.documentElement.clientHeight;
		windowWidth = document.documentElement.clientWidth;
	}
	else
	{
		windowHeight = window.innerHeight;
		windowWidth = window.innerWidth;
	}

	$('#noticePanel').css('top', parseInt((windowHeight - panelHeight) / 2 + document.documentElement.scrollTop) + 'px');
	$('#noticePanel').css('left', parseInt((windowWidth - panelWidth) / 2 + document.documentElement.scrollLeft) + 'px');
}

function setVisibilityNoticeForm()
{
	var display = 'block' == $('#noticeBack').css('display');

	$('#noticeBack').toggle();
	$('#noticePanel').toggle();

	if (display)
	{
		$('#docTD').removeClass('dDoc2');
		$('#docTD').addClass('dDoc1');

		$('#widther').css('width', '0');
		$('#widther').css('height', '0');
		
		if (!$.browser.mozilla)
			$('select').show();
	}
	else
	{
		if (!$.browser.mozilla)
		{
			$('select').hide();
			$('#noticePanel select').show();
		}

		var width = $('#noticePanel').width() + 100;
		var height = $('#noticePanel').height() + 100;

		if ($('#docTable').width() < width)
			$('#widther').css('width', width + 'px');
		if ($('#docTable').height() < height)
			$('#widther').css('height', (height - $('#docTable').height()) + 'px');

		setSizesNoticeForm();
		$('#noticeCatID').focus();
	}

	return false;
}

function checkNoticeForm(path)
{
	var error = false;

	var fields = ['Category', 'Data', 'Email', 'Name', 'Phone', 'Type'];

	for (var i = 0; i < fields.length; i++)
		$('#notice' + fields[i] + 'Error').hide();

	fields = ['Data', 'Email', 'Name', 'Phone'];

	for (var i = 0; i < fields.length; i++)
		$('#notice' + fields[i]).val($.trim($('#notice' + fields[i]).val()));

	var re = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;

	if ('' == $('#noticeData').val())
	{
		$('#noticeDataError').text('Необходимо заполнить текст объявления');
		$('#noticeDataError').show();

		error || $('#noticeData').focus();
		error = true;
	}

	if ('' == $('#noticeEmail').val() && '' == $('#noticePhone').val())
	{
		$('#noticeEmailError').text('Необходимо заполнить e-mail или телефон');
		$('#noticeEmailError').show();

		error || $('#noticeEmail').focus();
		error = true;
	}
	else if ('' != $('#noticeEmail').val() && !re.test($('#noticeEmail').val()))
	{
		$('#noticeEmailError').text('Неправильный формат e-mail');
		$('#noticeEmailError').show();

		error || $('#noticeEmail').focus();
		error = true;
	}

	if (!error)
	{
		$('.bNoticeForm').hide();
		$('.bNoticeSuccess').show();
		var type = 0;

		if ($('#noticeType1').attr('checked'))
			type = 1;

		$.post(path, { cat_id: $('#noticeCatID').val(), data: $('#noticeData').val(), email: $('#noticeEmail').val(), name: $('#noticeName').val(), phone: $('#noticePhone').val(), type: type }, function(data)
		{
			$('#noticeContent').html(data);
		});
	}

	return false;
}

function setNoticeWarn(id)
{
	if ('noticeEmail' == id || 'noticePhone' == id)
	{
		if ($('#noticeEmail').val() || $('#noticePhone').val())
		{
			$('#noticeEmail').css('border-color', '#7f9db9');
			$('#noticePhone').css('border-color', '#7f9db9');
		}
		else
		{
			$('#noticeEmail').css('border-color', '#f00');
			$('#noticePhone').css('border-color', '#f00');
		}

		var re = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;

		if ($('#noticeEmail').val() && !re.test($('#noticeEmail').val()))
			$('#noticeEmail').css('border-color', '#f00');

		return;
	}

	if ($('#' + id).val())
		$('#' + id).css('border-color', '#7f9db9');
	else
		$('#' + id).css('border-color', '#f00');
}

$(document).ready(function()
{
	$(window).resize(setSizesNoticeForm);
	$(window).scroll(setSizesNoticeForm);
});
