
var http_root = "";
var posX = 0;
var posY = 0;

function setFocus(elName)
{
	var el = document.getElementById(elName);
	if(el)
	{
		el.focus();
	}
} // setFocus

function submitForm(form)
{
	el = document.getElementById(form);
	if(el.submit)
	{
		el.submit();
	}
} // submitForm

function changeStyle(el, className)
{
	el.className = className;
} // changeStyle

function pop(theURL,w,h,name,feat)
{
	var l = (screen.width - w) / 2;
	var t = (screen.height - h) / 2;

	if(!feat)
	{
		feat = 'fullscreen=0,toolbar=0,status=0,scrollbars=1,menubar=0,location=0,resizable=0,channelmode=0,directories=0';
	}

	feat += ',width=' + w + ',height=' + h + ',top=' + t +',left=' + l;

	window.open(theURL, name, feat);
} // pop

function checkAll(form, what, prefix)
{
	if(!prefix)
	{
		prefix = '';
	}

	for (var i = 0; i < form.elements.length; i++)
	{
		var e = form.elements[i];
		if ( (e.type == 'checkbox') && (e.name.substr(0, prefix.length) == prefix))
		{
			if(form[what.name].checked != e.checked)
			{
				e.click();
			}
		}
	}
}

function resizeWindowFrom(referer)
{
	var el = document.getElementById(referer);

	if(el && el.clientHeight && el.clientWidth)
	{
		var nw = el.clientWidth + 24;
		var nh = el.clientHeight + 80;

		if(nh > screen.availHeight * 0.9)
		{
			nh = screen.availHeight * 0.9;
		}

		if(nw > screen.availWidth * 0.9)
		{
			nw = screen.availWidth * 0.9;
		}

		window.resizeTo(nw, nh);
		window.moveTo((screen.width - nw) / 2, (screen.height - nh) / 2);
	}
} // resizeWindowFrom

function div_show(id)
{
	var div = document.getElementById(id);
	if(div && div.style)
	{
		div.style.display = 'block';
	}
} // div_show

function div_hide(id)
{	var div = document.getElementById(id);
	if(div && div.style)
	{
		div.style.display = 'none';
	}
} // div_hide

function div_show_hide(id)
{
	var div = document.getElementById(id);
	if(div && div.style)
	{
		if(div.style.display == 'none')
		{
			div_show(id);
		} else {
			div_hide(id);
		}
	}
} // div_show_hide

function toFloat(val)
{
	return isNaN(val) ? 0 : parseFloat(val);
} // toFloat

function toInt(val)
{
	return isNaN(val) ? 0 : parseInt(val);
} // toInt

function confirmAction(msg, href)
{
	if(confirm(msg))
	{
		location.href = href;
	}
} // confirmAction

function getElement(elName)
{
	var el = document.getElementById(elName);

	return el;
} // getElement

function isChecked(id)
{
	var id = getElement(id);

	return id && id.checked;
} // isChecked

function check_tps_mitnes(el, id)
{
	if(el.checked)
	{
		div_show(id);
	} else {
		div_hide(id);
	}
} // check_tps_mitnes

function getCalendar(fieldName)
{
	window.open(http_root + '/calendar/calendar.php?field_name=' + fieldName, 'cal', 'width=180,height=180,resizable=no,status=no,scrollbars=no,titlebar=no');
} // getCalendar

function expand(img, div_id)
{
	div_show(div_id);
	img.src = http_root + '/img/expanded.gif';
	img.title = 'Sakļaut';
	img.alt = 'Sakļaut';
} // expand

function collapse(img, div_id)
{
	div_hide(div_id);
	img.src = http_root + '/img/expand.gif';
	img.title = 'Izvērst';
	img.alt = 'Izvērst';
} // expand

function expand_collapse(img, div_id)
{
	var div = document.getElementById(div_id);
	if(div && div.style)
	{
		if(div.style.display == 'none')
		{
			expand(img, div_id);
		} else {
			collapse(img, div_id);
		}
	}
} // expand_collapse

function setFocusFirst()
{
	var forms = document.forms;
	for(var i = 0; i < forms.length; i++)	{
		if(forms[i]) {
			var el = forms[i].elements;
			for(var j = 0; j < el.length; j++)	{
				if(el[j] && (el[j].type == 'text')) {
					el[j].focus();
					break;
				} // el
			} // loop el
		} // forms
	} // loop forms
} // setFocusFirst

function getScrollerWidth()
{
	var scr = null;
	var inn = null;
	var wNoScroll = 0;
	var wScroll = 0;
	
	// Outer scrolling div
	scr = document.createElement('div');
	scr.style.position = 'absolute';
	scr.style.top = '-1000px';
	scr.style.left = '-1000px';
	scr.style.width = '100px';
	scr.style.height = '50px';
	// Start with no scrollbar
	scr.style.overflow = 'hidden';
	
	// Inner content div
	inn = document.createElement('div');
	inn.style.width = '100%';
	inn.style.height = '200px';
	
	// Put the inner div in the scrolling div
	scr.appendChild(inn);
	// Append the scrolling div to the doc
	document.body.appendChild(scr);
	
	// Width of the inner div sans scrollbar
	wNoScroll = inn.offsetWidth;
	// Add the scrollbar
	scr.style.overflow = 'auto';
	// Width of the inner div width scrollbar
	wScroll = inn.offsetWidth;
	
	// Remove the scrolling div from the doc
	document.body.removeChild(document.body.lastChild);
	
	// Pixel width of the scroller
	return (wNoScroll - wScroll);
}

function getMouseXY(e)
{
	if(navigator.appName == "Netscape")
	{
		posX = e.pageX;
		posY = e.pageY;
	} else {
		posX = event.clientX;
		posY = event.clientY;
		if(document.body)
		{
			posX += document.body.scrollLeft;
			posY += document.body.scrollTop;
		}
	}

	if (posX <= 0) {posX = 0}
	if (posY <= 0) {posY = 0}

	return true;
}


