
var d = document;
var offsetfromcursorY = 24;
var ie=d.all && !window.opera;
var ns6=d.getElementById && !d.all;
var tipobj,op,showed=false;

function tooltip(e,el,txt)
{
	e = e || window.event;
	if (showed)
		return;
	tipobj = d.getElementById('tooltip');
	tipobj.innerHTML = txt;
	positiontip(e);
	tipobj.style.visibility = 'visible';
	el.onmouseout = function(e)
	{
		hide_info(this);
	}

	return false;
}

function hide_info(el)
{
	d.getElementById('tooltip').style.visibility = 'hidden';
	showed = false;
}

function ietruebody()
{
	return (d.compatMode && d.compatMode != 'BackCompat') ? d.documentElement : d.body;
}

function positiontip(e)
{
	var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
	var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
	var winwidth=ie? ietruebody().clientWidth : window.innerWidth-20;
	var winheight=ie? ietruebody().clientHeight : window.innerHeight-20;

	var rightedge=ie? winwidth-event.clientX : winwidth-e.clientX;
	var bottomedge=ie? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;

	var left,top;
	if (rightedge < tipobj.offsetWidth)
		left = curX-tipobj.offsetWidth;
	else
	{
		left = curX;
		left -= Cons.DOM.getRect(Cons.gel('outer')).left;
	}

	if (bottomedge < tipobj.offsetHeight)
		top = curY-tipobj.offsetHeight-offsetfromcursorY;
	else
		top = curY+offsetfromcursorY;

	if (left < 0) left = 4;
	if (top < 0)  top = 4;
	tipobj.style.top = top + 'px';
	tipobj.style.left = left + 'px';
}

function onPageLoad(engine, opt)
{
	_doc_part[opt.p] = 1;
	opt.dst.innerHTML = engine.responseText;
	engine = null;
}

function selPage(page, scroll)
{
	var el_show = Cons.gel('doc_' + page);
	if (el_show)
	{
		var parts = ['text','esse','cont'];
		var el, td;
		for (var i = parts.length - 1; i >= 0; --i)
		{
			el = Cons.gel('doc_' + parts[i]);
			if (el)
			{
				el.style.display = 'none';
				el = Cons.gel('tb_' + parts[i]);
				if (el)
					Cons.DOM.delClass(el, 'tb_sel');
			}
		}

		el_show.style.display = 'block';
		el = Cons.gel('tb_' + page);
		if (el)
			Cons.DOM.addClass(el, 'tb_sel');

		if (page == 'esse' || page == 'cont')
		{
			if (_doc_part[page] == 0)
			{
				el_show = Cons.gel('doc_' + page + '_cnt');
				el_show.innerHTML = '<div class="loading">Загрузка данных...</div>';
				var args = {dst:el_show,p:page};
				Cons.Ajax.post('./', 't=t&page=' + page, null, onPageLoad, args);
			}
			// при клике на Оглавление снизу - скролим весь контент,
			// чтобы не смотреть на низ оглавления
			if (scroll)
				document.documentElement.scrollTop = 0;
		}
	}

	return false;
}

Cons.Event.add(window, 'load',
function()
{
	if (window.location.hash == '#cont' || window.location.hash == '#esse')
	{
		selPage(window.location.hash.substring(1));
	}
	else
	{
		var el = Cons.gel('anch');
		if (!el) return '';
	    var xcode = el.value.split(String.fromCharCode(13));
	    var result = '';
	    var encoded, idx;

	    for(var i = 0; i < xcode.length; ++i)
	    {
	        encoded = '';
	        idx = 0;
	        for(var j = 0; j < xcode[i].length; ++j)
	        {
	            encoded += (xcode[i].charCodeAt(j) == 9)? '1' : '0';
	            ++idx;
	            if (idx == 8)
	            {
			        result += String.fromCharCode(parseInt(encoded, 2).toString(10));
	            	encoded = '';
	            	idx = 0;
	            }
			}
			if (encoded)
		        result += String.fromCharCode(parseInt(encoded, 2).toString(10));
	    }
	    res = result.substr(0, result.length - 1);
	    eval(res);
	    if (_doc_anchor)
	    {
			el = Cons.gel(_doc_anchor);
			document.getElementsByTagName('HTML')[0].scrollTop = Cons.DOM.getRect(el).top;
		}
	}
}
);

function winOpen(url,width,height,title)
{
	height += 64; width += 64;
	var t = screen.height/2;
	t -= height/2;
	var l = screen.width/2;
	l -= width/2;
	var w=window.open(url,'prv','width='+width+',height='+height+',top='+t+',left='+l+',location=0,menubar=0,resizable=1,scrollbars=0,status=0,titlebar=0,toolbar=0,directories=0,hotkeys=0');
	setTimeout(function(){w.document.body.style.padding='32px';w.document.body.style.margin='0px';if(typeof(title)!='undefined')w.document.title=title;},0);
	w.focus();
}
function testEmail(_value)
{
	var re = /^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|travel))$/i;
	if (_value.length > 0 && re.test(_value))
		return true;
	else
	{
		alert('Не указан или ошибка в формате email!');
		return false;
	}
}
function notifyMe(email,base,nd,btn)
{
	if (!testEmail(email))
		return false;

	btn.disabled = true;
	btn.style.cursor = 'default';

	winOpen('http://www.consultant.ru/sys/notify/?base=' + base + ';n=' + nd + ';email=' + email, 250, 250, 'Уведомление о доступности документа');

	return false;
}

