var gecko = navigator.userAgent.indexOf("Gecko")>-1;
var ie7 = navigator.userAgent.indexOf("MSIE 7.0")>-1;

var oo_window_onload = window.onload;
window.onload = function()
{
	get_form();
	if (oo_window_onload) oo_window_onload();
	window_onload();
	change_scrollbars();
	xbar_set_scroll();
	form_set_scroll();
	box_prepare_images();
}


var oo_window_onresize = window.onresize;
window.onresize = function()
{
	if (oo_window_onresize) oo_window_onresize();
	change_scrollbars_onresize();
}

function window_onload()
{
	var inputs = document.getElementsByTagName("input");
	var o;
	for (var i=0; i<inputs.length; i++)
	{
		if (inputs[i].type=="radio" && inputs[i].attributes["group"] && inputs[i].attributes["group"].value)
		{
			o = getElement(inputs[i].attributes["group"].value);
			if (o)
			{
				inputs[i].checked = (o.value==inputs[i].value);
				inputs[i].groupItem = o;
				inputs[i].onclick = RadioButtonOnClick;
			}
		}
	}
}

function urlencode(url)
{
	url = url.replace(/%/g,"%25").replace(/&/g,"%26").replace(/\?/g,"%3F").replace(/:/g,"%3A").replace(/[\/]/g,"%2F").replace(/#/g,"%23");
	return url;
}

function box_prepare_images()
{
	if (!ie7 && !gecko && document.body.style.filter!="undefined")
	{
		var imgs = document.getElementsByTagName("img");
		for (var i=0; i<imgs.length; i++)
		{
			if (imgs[i].tag=="pngfilter")
			{
				imgs[i].style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgs[i].src.replace("'","\'")+"');";
				imgs[i].src="/Frame/Images/Box/blank.gif";
			}
		}
		imgs = document.getElementsByTagName("input");
		for (var i=0; i<imgs.length; i++)
		{
			if (imgs[i].type=="image" && imgs[i].tag=="pngfilter")
			{
				imgs[i].style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgs[i].src.replace("'","\'")+"');";
				imgs[i].src="/Frame/Images/Box/blank.gif";
			}
		}
	}
}

var form = null;
function get_form()
{
	for (var i=0; i<document.forms.length; i++)
	{
		if (document.forms[i].className != "ignore")
		{
			form = document.forms[i];
			return;
		}
	}
}

function RadioButtonOnClick()
{
	this.groupItem.value = this.value;
}

function iif(expr, iftrue, iffalse)
{
	if (expr)
		return iftrue;
	else
		return iffalse;
}

var last_modeless = null;
function openmodeless(link, width, height, center)
{
	if (last_modeless)
	{
		last_modeless.close();
		last_modeless = null;
	}
	if (window.showModalDialog)
	{
		width+=10;
		height+=36;
		var params;
		params = "dialogHeight: " + height + "px; dialogWidth: " + width +"px; center:"+(center?"yes":"no")+"; help:no; resizable:yes; status:no;";
		if (!center)
			params += "dialogLeft: " + (screen.width - width - 16) + "px; dialogTop: 100px;";
		last_modeless = window.showModelessDialog(link, null, params);
	}
	else
	{
		var top;
		var left;
		if (center)
		{
			top = (screen.height-height)/2;
			left = (screen.width-width)/2;
		}
		else
		{
			top = 100;
			left = screen.width - width - 16;
		}
		var params;
		params = "directories=no, height=" + height + "px, location=no, menubar=no, resizable=yes, status=no, toolbar=no, width="+width+"px, left="+left + "px, top="+top+"px";
		last_modeless = window.open(link, "modal", params);
	}
}

function openwindow(link, width, height, center, name)
{
		if (!name) name = "_blank";
		var top;
		var left;
		if (center)
		{
			top = (screen.height-height)/2;
			left = (screen.width-width)/2;
		}
		else
		{
			top = 100;
			left = screen.width - width - 16;
		}
		var params;
		params = "directories=no, height=" + height + "px, location=no, menubar=no, resizable=yes, status=no, toolbar=no, width="+width+"px, left="+left + "px, top="+top+"px";
		window.open(link, name, params);
}

function openwindowsimple(link)
{
	window.open(link);
}

function openmodal(link, width, height)
{
	if (window.showModalDialog)
	{
		width+=10;
		height+=36;
		var params;
		params = "dialogHeight: " + height + "px; dialogWidth: " + width +"px; center:yes; help:no; resizable:no; status:no;";
		window.showModalDialog(link, null, params);
	}
	else
	{
		var top = (screen.height-height)/2;
		var left = (screen.width-width)/2;
		var params;
		params = "directories=no, height=" + height + "px, location=no, menubar=no, resizable=no, status=no, toolbar=no, width="+width+"px, left="+left + "px, top="+top+"px";
		window.open(link, "modal", params);
	}
}

function getElement(id)
{
	if (document.all)
		return document.all[id];
	else
		return document.getElementById(id);
}

function cancelBubble(e)
{
	if (!e) e=event;
	e.cancelBubble = true;
}

function getScrollX()
{
	a = parseInt(document.body.scrollLeft?document.body.scrollLeft:window.pageXOffset);
	if (!a) a=0;
	return a;
}
function getScrollY()
{
	a = parseInt(document.body.scrollTop?document.body.scrollTop:window.pageYOffset);
	if (!a) a=0;
	return a;
}

function getElementLeft(element)
{
	var left = 0;
	if (element.offsetLeft)
		left = parseInt(element.offsetLeft);
	if (element.scrollLeft)
		left -= parseInt(element.scrollLeft);
	while (element.offsetParent)
	{
		element = element.offsetParent;
		if (element.offsetLeft)
			left += parseInt(element.offsetLeft);
		if (element.scrollLeft)
			left -= parseInt(element.scrollLeft);
	}
	return left;
}
function getElementTop(element)
{
	var top = 0;
	if (element.offsetTop)
		top = parseInt(element.offsetTop);
	if (element.scrollTop)
		top -= parseInt(element.scrollTop);
	while (element.offsetParent)
	{
		element = element.offsetParent;
		if (element.offsetTop)
			top += parseInt(element.offsetTop);
		if (element.scrollTop)
			top -= parseInt(element.scrollTop);
	}
	return top;
}

function change_scrollbars()
{
	xbar_correct_height('xbar');
	if (!form) return;
	if (getElement('xbar'))
		form.style.width = 1;
	form.style.width = document.body.offsetWidth - form.offsetLeft - iif(gecko,4,0);
	form.style.height = "100%";
	form.style.height = form.offsetHeight - form.offsetTop - iif(gecko,10,0);
}

function change_scrollbars_onresize()
{
	xbar_correct_height('xbar');
	if (!form) return;
	form.style.width = 1;
	form.style.height = "100%";
	try
	{
		form.style.height = form.offsetHeight - form.offsetTop - iif(gecko,10,0);
		form.style.width = document.body.offsetWidth - form.offsetLeft - iif(gecko,4,0);
	}
	catch(e) {}	
}


function error_details(nr)
{
	var link = getElement("error_link_"+nr);
	var elem = getElement("error_"+nr);
	if (!elem.corrected)
	{
		elem.innerHTML = elem.innerHTML.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,"\"");
		elem.corrected=true;
	}
	if (elem.style.display=="block")
	{
		elem.style.display="none";
		link.innerHTML = "Apraksts";
	}
	else
	{
		elem.style.display="block";
		link.innerHTML = "Slēpt aprakstu";
	}
	error_viewer.style.height = "";
	error_viewer_check_size();
	change_scrollbars_onresize();
}

function xbar_correct_height(xbarid)
{
	var xbar = getElement(xbarid);
	if (!xbar) return;
	xbar.style.height = "100%";
	xbar.style.height = xbar.offsetHeight - xbar.offsetTop;
	
	if (gecko)
	{
		xbar.style.height = xbar.offsetHeight - 12;
	
		for (var i=0; i<xbar.childNodes.length; i++)
		{
			if (!xbar.childNodes[i].style) continue;
			if (xbar.scrollHeight>xbar.offsetHeight)
				xbar.childNodes[i].style.width=191;
			else
				xbar.childNodes[i].style.width=209;
		}
	}
}

function error_viewer_check_size()
{
	if (error_viewer.offsetHeight > document.body.offsetHeight / 3)
	{
		error_viewer.style.height = document.body.offsetHeight / 3;
	}
	else
	{
		error_viewer.style.height = "";
	}
}

function datagrid_row_onkeypress(ev, elem)
{
	if (!ev) ev = event;
	if (ev.keyCode == 13)
	{
		var btn_save = elem.cells[elem.cells.length-1].childNodes(0);
		if (btn_save && btn_save.id.indexOf("btn_save")!=-1)
			btn_save.click();
		ev.cancelBubble = true;
		return false;
	}
	if (ev.keyCode == 27)
	{
		var btn_cancel = elem.cells[elem.cells.length-1].childNodes(1);
		if (btn_cancel && btn_cancel.id.indexOf("btn_cancel")!=-1)
			btn_cancel.click();
		ev.cancelBubble = true;
		return false;
	}
}

var cacher;
function xbar_load_cache()
{
	var xbar = getElement("xbar");
	if (!xbar || !xbar.attributes || !xbar.attributes["tag"]) return;
	var xbarid = xbar.attributes["tag"].value;
	var s, ypos;
	if (!gecko)
	{
		cacher = getElement("cacher");
		cacher.load(xbarid);
		s = cacher.getAttribute("cachedData");
		
		ypos = cacher.getAttribute("cachedYPos");
	}
	if (ypos)
	{
		xbar.scrollTop = ypos;
	}
	if (s)
	{
		var c = s.split(";");
		for (var i=0; i<c.length; i++)
		{
			if (c[i]!="")
				xbar_toggle(c[i],getElement("xic"+c[i]));
		}
	}
}

function xbar_set_scroll()
{
	if (gecko) return;
	var xbar = getElement("xbar");
	if (!xbar || !xbar.attributes || !xbar.attributes["tag"]) return;
	var xbarid = xbar.attributes["tag"].value;
	var  ypos;
	if (!cacher)
		cacher = getElement("cacher");
	cacher.load(xbarid);
	ypos = cacher.getAttribute("cachedYPos");
	if (ypos)
		xbar.scrollTop = ypos;

	xbar.onscroll = function()
	{
		if (gecko) return;
		cacher.setAttribute("cachedData",xbar_cached_data);
		cacher.setAttribute("cachedYPos",this.scrollTop);
		cacher.save(xbarid);
	}
	
}

var xbar_cached_data="";

function xbar_toggle(id)
{
	var header = getElement("xh"+id);
	var items = getElement("xbar_"+id);
	var xbar = getElement("xbar");
	var xbarid = xbar.attributes["tag"].value;
	var btnexp = getElement("xie" + id);
	var btncol = getElement("xic" + id);
	if (!header || !items || !xbar || !items.style) return;
	if (items.style.display.toLowerCase()=="none")
	{ //expandojam
		btnexp.style.display="none";
		btncol.style.display="inline";
		items.style.display="block";
		xbar_cached_data=xbar_cached_data.replace(id+";","");
	}
	else
	{ //collapsojam
		btnexp.style.display="inline";
		btncol.style.display="none";
		items.style.display="none";
		xbar_cached_data += id + ";";
	}
	if (gecko)
	{
		xbar_correct_height('xbar');
	}
	else
	{
		cacher.setAttribute("cachedData",xbar_cached_data);
		cacher.save(xbarid);
	}
}

var ignore_form_set_scroll = false;
function form_set_scroll()
{
	if (gecko) return;
	if (!form) return;
	var ypos;
	if (!cacher)
		cacher = getElement("cacher");
	if (!cacher) return;
	cacher.load(document.location.pathname.replace(/(\/)/g,"_"));
	ypos = cacher.getAttribute("cachedFormYPos");
	if (ypos && !ignore_form_set_scroll)
		form.scrollTop = ypos;

	form.onscroll = function()
	{
		if (gecko) return;
		cacher.setAttribute("cachedData", "");
		cacher.setAttribute("cachedFormYPos",this.scrollTop);
		cacher.save(document.location.pathname.replace(/(\/)/g,"_"));
	}
}

function scroll_into_view(eln)
{
	var top = getElementTop(eln);
	if (top < form.scrollTop)
		form.scrollTop =  top;
	else if (top + eln.offsetHeight > form.scrollTop + form.offsetHeight)
		form.scrollTop = top + eln.offsetHeight - form.offsetHeight;
		
	ignore_form_set_scroll = true;
}

function focus_first_input(eln)
{
	if (!eln.firstChild) return false;
	var c;
	for (c = eln.firstChild; ; c=c.nextSibling)
	{
		if (!c) break;
		if (c.tagName)
		{
			if ((c.tagName.toLowerCase()=="input" && c.type.toLowerCase()!="hidden") || c.tagName.toLowerCase()=="select")
			{
				try
				{
					c.setActive();
					c.focus();
				} catch(ex){};
				return true;
			}
		}
		if (focus_first_input(c)) return true;
	}
	return false;
}
