var m_bValidatedBody = false;
var m_nDocumentHeight = 0;
var Config = {};

function ValidateBody()
{
	// Изменение уже производилось, выходим из функции
	if (m_bValidatedBody)
		return;
	// Производим изменение размера тела документа
	try
	{
		var Node = document.getElementById("BodyPlaceHolder");
		var nDocumentHeight = 500;
		var nTdHeight = 0;
		nDocumentHeight = GetDocumentHeight();
		var th = document.body.getElementsByTagName("table")[0].offsetHeight;
		var txtcontainer = document.getElementById("BodyTextPlaceHolder");
		if (th < nDocumentHeight && txtcontainer)
		{
			m_nDocumentHeight = txtcontainer.offsetHeight + nDocumentHeight - th;
			txtcontainer.style.height = m_nDocumentHeight + "px";
		}
		m_bValidatedBody = true;
	}
	catch (e)
	{
		// Ошибка
	}
}

function GenerateEmailHref(nStart, nEnd)
{
	var strHref = "";
	try
	{
		var emailParts = new Array("mailto:", "info", "@", m_strHostName, ".ru", "?subject=запрос с сайта");
		if (!nStart || nStart == null)
			nStart = 0;
		if (!nEnd || nEnd == null)
			nEnd = 0;
		for (var i = nStart; i < emailParts.length - nEnd; i++)
		{
			strHref += emailParts[i];
		}
	}
	catch (e)
	{
		// Ошибка	
	}
	return strHref;
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Функция производит изменение размеров окна iframe новостей и вызывает открытие страницы	//
// для вывода новостей																		//
//////////////////////////////////////////////////////////////////////////////////////////////
function ValidateNews()
{
	var w = 200;
	var Div = document.getElementById("ContentPlaceHolderLeft");
	var Frame = document.getElementById("NewsPlaceHolder");
	if (m_nDocumentHeight > 0 && Div)
	{
		w = Div.clientWidth;
		nLeftSideHeight = 0;
		for (var i = 0; i < Div.childNodes.length; i++)
		{
			try
			{
				if (Div.childNodes[i].clientHeight)
					nLeftSideHeight += Div.childNodes[i].clientHeight;
				else if (Div.childNodes[i].height)
					nLeftSideHeight += Div.childNodes[i].height;
				else if (Div.childNodes[i].style.height)
					nLeftSideHeight += parseInt(Div.childNodes[i].style.height);
			}
			catch (e) { }
		}
		// 100 пкс - дополнительная высота исходя из практических наблюдений //
		if (nLeftSideHeight)
			Frame.height = m_nDocumentHeight - nLeftSideHeight + ((Frame.clientHeight) ? Frame.clientHeight : 150);
		// Окно новостей не может быть меньше 120 пкс //
		if (Frame.height <= 150)
			Frame.height = 150;
	}
	else if (!Div)
	{
		Frame.height = 160;
		w = Frame.width;
	}
	Frame.src = "news.php?h=" + Frame.height + "&w=" + w;
}

// Функция ищет родительский элемнт по id или class
function FindParent(sender, attributes/* Object {id, class, tagname} */, allowsender/* Boolean */)
{
	if (!sender)
		return;
	if (allowsender == null || allowsender == undefined)
		allowsender = false;
	var target = null;
	var parent = sender;
	while (parent.parentNode)
	{
		if (!allowsender)
			parent = parent.parentNode;
		allowsender = false;
		if (attributes["id"] && parent.id == attributes["id"])
		{
			target = parent;
			break;
		}
		if (attributes["class"] && parent.className)
		{
			var classes = parent.className.split(/[ ]{1,}/g);
			if (classes)
			{
				for (var i = 0; i < classes.length; i++)
					if (classes[i] == attributes["class"])
					{
						target = parent;
						break;
					}
			}
		}
		if (attributes["tagname"] && parent.tagName.toLowerCase() == attributes["tagname"].toLowerCase())
		{
			target = parent;
			break;
		}
		if (attributes["attr"] && parent.getAttribute(attributes["attr"]))
		{
			target = parent;
			break;
		}
	}
	return target;
}

Config.orderwidth = 380;
Config.orderheight = 420;
function OpenOrderForm()
{
	var w = (screen.availWidth) ? screen.availWidth : screen.width;
	var h = (screen.availHeight) ? screen.availHeight : screen.height;
	var left = Math.floor((w - Config.orderwidth - 30) / 2);
	var top = Math.floor((h - Config.orderheight - 50) / 2);
	var wnd = window.open("order_form.php", "", "width=" + Config.orderwidth + ",height=" + Config.orderheight + ",location=0,resizable=yes,scrollbars=yes,menubar=no,statusbar=no,left=" + left + ",top=" + top);
	return false;
}
