function max (a, b)
{
return (a>b)?a:b;
}

function min (a, b)
{
return (a>b)?b:a;
}

function getClientWidth()
{
return ((document.compatMode=='CSS1Compat') && (!window.opera))?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
return ((document.compatMode=='CSS1Compat') && (!window.opera))?document.documentElement.clientHeight:document.body.clientHeight;
}

function setInnerText(elem, text)
{
	elem.textContent=text;
	//elem.innerText=text;
	elem.innerHTML = text;
}

function setText(elem, text)
{
	elem.text = text;
}

function getInnerText(elem)
{
	if (elem.textContent) return elem.textContent;
	if (elem.innerText) return elem.innerText;
	return elem.nodeValue;
}

function enumerate_fields (me)
{
	var str='';
	for (a in me)
		str=str+a+"\n";
	alert(str);
}

function showtip1(current,event,text,align,size)
{
if (window.event) event=window.event;
tooltip=document.getElementById("Tooltip");
tooltip.style.width="auto";
tooltip.style.height="auto";
if (size >= 7)
	{
		text = '<FONT size='+size.toString()+'><BIG>'+text+'</BIG></FONT>'
	}
	else
		if (size) text = '<FONT size='+size.toString()+'>'+text+'</FONT>'; 

tooltip.innerHTML='<P align=\"'+align+'">'+text+'</P>';

max_width=(getClientWidth()>500)?getClientWidth()/2:250;
if(tooltip.clientWidth > max_width) tooltip.style.width = max_width;
width = tooltip.clientWidth;
height = tooltip.clientHeight;
eventX = event.clientX+document.body.scrollLeft;
eventY = event.clientY+document.body.scrollTop;

deltaX = 50;
pripuskX = 50;
a = getClientWidth()-pripuskX-width;
b = event.clientX+deltaX;
tleft=document.body.scrollLeft+min(a,b);

deltaY = 50;
pripuskY = 50;
a = getClientHeight()-pripuskY-height;
b = event.clientY+deltaY;
ttop=document.body.scrollTop+min(a,b);
if ((tleft<eventX+pripuskX)&&(ttop-pripuskY<eventY)) tleft=tleft-width-deltaX;
tooltip.style.top=ttop;
tooltip.style.left=tleft;
tooltip.style.visibility="visible";
}

function hidetip()
{
tooltip = document.getElementById("Tooltip");
tooltip.style.visibility="hidden";
tooltip.style.top=50;
tooltip.style.left=50;
}

function showtip (current,e,text)
{
showtip1 (current,e,text,"left",3);
}


function showsmarttip(current,event,align,size)
{
	showtip1(current,event,getInnerText(current)/*.textContent*/,align,size);
}

function toggle_visibility (current,hidetext,showtext,id)
{
	property="inline-block";
	elem = document.getElementById(id);
	visible = (elem.style.display==property);
	text=visible?showtext:hidetext;
	property = visible?"none":property;
	setInnerText(current, "Подождите");
	elem.style.display=property;
	setInnerText(current, text);
}

function toggle_visibility_by_class (current,hidetext,showtext,class_name,inline)
{
	var arr = document.getElementsByClassName(class_name);
	if(arr.length >0)
	{
		property=inline;
		visible = (arr[0].style.display==property);
		text=visible?showtext:hidetext;
		property = visible?"none":property;
		setInnerText(current, "Подождите");
		for(var i = 0; i < arr.length; i++)
		{
			elem = arr[i];
			elem.style.display=property;
		}
		setInnerText(current, text);
	}
}

function setList(select, list, position)
{
	while(select.options.length)
	{
		//select.options[0] = null;
		select.options.length = 0;
	}
	
	var i=1;
	for (str in list)
	{
		var opt = document.createElement("OPTION");//new Option(list[str], i);
		opt.text = list[str];
		opt.value = i;
		select.options.add(opt);
		i++;
	}
	select.options.selectedIndex = position;
	var event = document.createEvent("Event");
	event.initEvent("change", false, true);
	select.dispatchEvent(event);
}

function num_str(num, str_1, str_2, str_3)
{
  	if (((num%100)>10) && ((num%100) < 20))
	{
		return str_3;
	}
	else
	{
		switch (num%10)
			{
			case 1: return str_1;
			case 2: case 3: case 4: return str_2;
			default: return str_3;
			}
	}
}

function enumerate_fields (me)
{
	var str='';
	for (a in me)
		str=str+a+"\n";
	alert (str);
}


function enumerate_values (me)
{
	var str='';
	for (a in me)
		str=str+a+" = "+me.getAttribute(a)+"\n";
	alert (str);
}

