function addphotobrowse() {
	if (document.getElementById('photofields'))	{
		var obj=document.getElementById('photofields');
		kiekis=countChildElements('photofields', 'div');
		if (kiekis<5) {
			var divas=document.createElement('div');
			divas.innerHTML='<input type="file" class="photofield" name="photos'+kiekis+'"/>';
			obj.appendChild(divas);
		}
	}
}

function countChildElements(parent, child) {
	  var parent = document.getElementById(parent);
	  var childCount = parent.getElementsByTagName(child).length;
	  return childCount;
 }


function formuoti_pranesima(klaidos) {
    if (klaidos.length==1) 
        pranesimas = "Klaida!\n"+klaidos[0]+".";
    else {
        pranesimas = "Klaidos!"; 
        for (i=1; i<=klaidos.length; i++)
           pranesimas += "\n"+i+". "+klaidos[i-1]+".";
    }
    return pranesimas;
}

function findParentTag(obj, tag){
	obj=obj.parentNode;
	while(obj.tagName.toLowerCase() != tag.toLowerCase()){
		obj=obj.parentNode;
	}
	return obj;
}
function findChildTag(t, tag){
	for(var i=0; i<t.childNodes.length; i++){
		if(t.childNodes[i].tagName.toLowerCase() == tag.toLowerCase()){
			return t.childNodes[i];
			break;
		}
	}
	return false;
}

function top_meniu(t,v){
	
	var container=findParentTag(t, 'div');
	if(container){
		container.className=((v)?'on':'');
	}
	var div=findChildTag(container, 'div');
	if(div){
		div.onmouseout=function(){top_meniu(t,false);};
		div.onmouseover=function(){top_meniu(t,true);};
	}
}

//////////////////////////////////////////////////////////
function fixTopSub(){
	if(arguments.length > 0){
		var base_offset=0;
		var childs = document.getElementById(arguments[arguments.length-1]).childNodes;
		if(childs.length > 0){
			for(var k=0; k < childs.length; k++){
				if(childs[k].tagName.toLowerCase() == 'div'){
					var subdiv=findChildTag(childs[k], 'div');
					var link=findChildTag(childs[k], 'a');
					if(link){
						link.onmouseover=function(){ top_meniu(this,true);};
						link.onmouseout=function(){ top_meniu(this,false);};
					}
					if(subdiv && subdiv.className=='sub'){
						subdiv.style.left = (base_offset + childs[k].offsetLeft) + 'px';   
					}
				}
			}
		}
	}
}

//window.onload=set_pos_new;
//window.onresize=set_pos_new;

		function set_pos_new(){
			setTimeout("do_bott()",300);
			}
		function do_bott(){
			
			var minusas=163;//386;
			
			if(document.images && document.images.scr_h){
				document.images.scr_h.height=1;
				var agt = navigator.userAgent.toLowerCase();
				if(document.all){			
					if(typeof((document.body.offsetHeight)=='undefined' || (agt.indexOf("msie")==-1) && agt.indexOf("gecko")!=-1) || (agt.indexOf("msie 7.0")>0) || (agt.indexOf("opera")>0)){
						hh=document.body.scrollHeight;
					}
					else {
						hh=document.body.offsetHeight+((agt.indexOf("msie") != -1 && (agt.indexOf("opera") == -1))?(-4):0);	
					}
				}
				else {
					if (agt.indexOf("mozilla")>=0) {//mozilai
						hh = window.innerHeight + window.scrollMaxY;
						minusas=378;
					}
					else hh=document.body.scrollHeight;
				}
				document.images.scr_h.height=(((hh-minusas)>0) ? (hh-minusas):1);
			}

			if(document.images && document.images.scr_hx){
				document.images.scr_hx.height=1;
				document.images.scr_hx.height=(((hh-minusas)>0) ? (hh-minusas):1);
			}
		}
/*function set_pos_new(){
	setTimeout("do_bott()",300);
	}
function do_bott(){
	var sc_hh=0;
	var of_hh=0;
	if(document.body.scrollHeight) sc_hh=document.body.scrollHeight;
	if(document.body.offsetHeight) of_hh=document.body.offsetHeight;
	var hh=((sc_hh>of_hh)?sc_hh:of_hh);
	hh=hh-420;
	var h_obj=null;
	if(document.getElementById('content')){
		h_obj=document.getElementById('content');
	}
	else if(document.getElementById('content_index')){
		h_obj=document.getElementById('content_index');
	}
	if(h_obj){
	if(hh>h_obj.offsetHeight){
		var agt = navigator.userAgent.toLowerCase();
		if(agt.indexOf("msie") != -1 && agt.indexOf("opera") == -1){
			hh=hh-4;
		}
		h_obj.style.height=hh+'px';
	}
	}
}*/

/////////////////////////////////////////

var BrowserDetect = {
        init: function () {
                this.id = '';
		this.browser = this.searchString(this.dataBrowser, true) || "An unknown browser";
                this.version = this.searchVersion(navigator.userAgent)
                        || this.searchVersion(navigator.appVersion)
                        || "an unknown version";
                this.OS = this.searchString(this.dataOS) || "an unknown OS";
		this.DHTML = this.testActions();
        },
        searchString: function (data) {
                for (var i=0;i<data.length;i++) {
                        var dataString = data[i].string;
                        var dataProp = data[i].prop;
                        this.versionSearchString = data[i].versionSearch || data[i].identity;
                        if (dataString) {
                                if (dataString.indexOf(data[i].subString) != -1){
					if(arguments[1]){
						this.id = data[i].id || '';
					}
                                        return data[i].identity;
				}
                        }
                        else if (dataProp){
				if(arguments[1]){
					this.id = data[i].id || '';
				}
                                return data[i].identity;
			}
                }
        },
        searchVersion: function (dataString) {
                var index = dataString.indexOf(this.versionSearchString);
                if (index == -1) return;
                return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
        },
	testActions: function(){
		var actions=new Array('document.getElementById', 'document.getElementsByTagName');
		for(var i=0; i<actions.length; i++){
			if(!eval(actions[i])){
				return false;
			}
		}
		return true;
	},
        dataBrowser: [
                {       string: navigator.userAgent,
                        subString: "OmniWeb",
                        versionSearch: "OmniWeb/",
                        identity: "OmniWeb",
			id: 'ow'
                },
                {
                        string: navigator.vendor,
                        subString: "Apple",
                        identity: "Safari",
			id: 'sf'
                },
                {
                        prop: window.opera,
                        identity: "Opera",
			id: 'op'
                },
                {
                        string: navigator.vendor,
                        subString: "iCab",
                        identity: "iCab",
			id: 'ic'
                },
                {
                        string: navigator.vendor,
                        subString: "KDE",
                        identity: "Konqueror",
			id: 'kq'
                },
                {
                        string: navigator.userAgent,
                        subString: "Firefox",
                        identity: "Firefox",
			id: 'fx'
                },
                {
                        string: navigator.vendor,
                        subString: "Camino",
                        identity: "Camino",
			id: 'ca'
                },
                {               // for newer Netscapes (6+)
                        string: navigator.userAgent,
                        subString: "Netscape",
                        identity: "Netscape",
			id: 'ns'
                },
                {
                        string: navigator.userAgent,
                        subString: "MSIE",
                        identity: "Explorer",
                        versionSearch: "MSIE",
			id: 'ie'
                },
                {
                        string: navigator.userAgent,
                        subString: "Gecko",
                        identity: "Mozilla",
                        versionSearch: "rv",
			id: 'gc'
                },
                {               // for older Netscapes (4-)
                        string: navigator.userAgent,
                        subString: "Mozilla",
                        identity: "Netscape",
                        versionSearch: "Mozilla",
			id: 'mz'
                }
        ],
        dataOS : [
                {
                        string: navigator.platform,
                        subString: "Win",
                        identity: "Windows"
                },
                {
                        string: navigator.platform,
                        subString: "Mac",
                        identity: "Mac"
                },
                {
                        string: navigator.platform,
                        subString: "Linux",
                        identity: "Linux"
                }
        ]

};
BrowserDetect.init();

/////////////////////////////////////////
var use_frame=false;
var bgframe=new Object();
bgframe.isset=false;
var prevDiv=new Object();
var b_tb;

function hideThis(t){
	prevDiv.style.visibility='hidden';
	unSetFrame();
	t.onmouseout=null;
	t.title=br2nl(prevDiv.innerHTML);
}
function show_hint(e, t){
	if(prevDiv){
		var ww=200;
		prevDiv.innerHTML=nl2br(t.title);
		if(t.title.length<35){
			ww=Math.ceil(Math.abs(t.title.length*7));
			prevDiv.align='center';
		}
		else{
			prevDiv.align='left';
		}
		if(t.title.length>260){
			ww=300;
		}
		t.title='';
		if(ww<40){
			ww=40;
		}
		prevDiv.style.width=ww+'px';
		if(!e) var e=window.event;
		
		var eX=e.clientX+document.body.scrollLeft+10;
		var eY=e.clientY+document.body.scrollTop+10;
		//if((eX+ww+10)>(b_tb.offsetLeft+b_tb.offsetWidth)){
			//eX=((b_tb.offsetLeft+b_tb.offsetWidth)-(ww+10));
		//}
		prevDiv.style.left=eX+'px';
		prevDiv.style.top=eY+'px';
		setFrame(eX, eY, prevDiv.offsetWidth, prevDiv.offsetHeight);
		t.onmouseout=function(){hideThis(this);};
		prevDiv.style.visibility='visible';
	}
}

function setFrame(left, top, width, height){
	if(use_frame && bgframe.isset){
		bgframe.style.left=left;
		bgframe.style.top=top;
		bgframe.style.width=width;
		bgframe.style.height=height;
		bgframe.style.visibility='visible';
	}
}

function unSetFrame(){
	if(use_frame && bgframe.isset){
		bgframe.style.left='0';
		bgframe.style.top='0';
		bgframe.style.visibility='hidden';
	}
}



function init_hints(){
	if(BrowserDetect.DHTML){
		var cname=((arguments[0])?arguments[0]:'content');
		prevDiv=document.createElement('div');
		//prevDiv.align="left";
		prevDiv.id='prevDIV';
		document.body.appendChild(prevDiv);
		b_tb=document.getElementById(cname);
		if(BrowserDetect.id == 'ie' && BrowserDetect.version < 7){
			use_frame=true;
			bgframe=document.createElement('iframe');
			bgframe.style.position='absolute';
			bgframe.style.visibility='hidden';
			bgframe.style.left='0';
			bgframe.style.top='0';
			bgframe.style.border='none';
			bgframe.scrolling="no";
			bgframe.frameBorder="0px";
			bgframe.style.width='10';
			bgframe.style.height='10';
			bgframe.style.zIndex='0';
			bgframe.isset=true;
			document.body.appendChild(bgframe);
		}
			
		var spans = b_tb.getElementsByTagName('span');
		for(var i=0; i<spans.length; i++){
			//if(spans[i].className=='expl'){
			if(spans[i].title!=''){
				spans[i].onmouseover=function(e){show_hint(e, this);};
				//spans[i].onmouseover=function(){hideThis(this);};
			}
		}
		var divs = b_tb.getElementsByTagName('div');
		for(var i=0; i<divs.length; i++){
			//if(spans[i].className=='expl'){
			if(divs[i].title!=''){
				divs[i].onmouseover=function(e){show_hint(e, this);};
				//spans[i].onmouseover=function(){hideThis(this);};
			}
		}
		
		
		
	}
}

function nl2br(val){
	val=val.replace(/\[B\]/, '<strong>');
	val=val.replace(/\[\/B\]/, '</strong>');
	return val.replace(/\\n/g,'<br>');
}
function br2nl(val){
	val=val.replace(/<strong>/, '[B]');
	val=val.replace(/<\/strong>/, '[/B]');
	return val.replace(/<br>/g,'\\n');
}

function show_bigimage(img, title, w, h){
	window.open(root_url+'show_photo.php?img='+escape(img)+'&title='+title+'&w='+w+'&h='+h, '_image', 'width='+w+', height='+h+', top=10, left=10');
}


function check_Regform (forma) {
	klaidos = new Array();
	if (forma.reg_mail.value=="El. paðto adresas" || !forma.reg_mail.value || forma.reg_mail.value.match(/^\s+$/)) {
		klaidos.push("Neivestas_el_pasto_adresas");
	}
	if (klaidos.length > 0) {
		alert (formuoti_pranesima(klaidos));
	return false;
	}
 
	else{
		forma.submit();
	}
}
 
 
function doRegOut(forma) {
	forma.act.value='regout';
	check_Regform(forma);
}
 
 
function doRegIn(forma) {
	forma.act.value='reg';
	check_Regform(forma);
}

function changeTitImage(obj, photo, isurl, isblank, url) {
	if (document.getElementById('titgallerynumberact'))	{
		var selected=document.getElementById('titgallerynumberact');
		selected.removeAttribute('id');
	}
	
	obj.id='titgallerynumberact'; 
	var elem=document.getElementById("sliderContent");
	var source='';
	if (isurl){
		source+='<a href="'+url+'"';
		if (isblank)
			source+=' target="blank"';
		source+='>';
	}
	source+='<img src="'+photo+'" alt="" border="0"/>';
	if (isurl){
		source+='</a>';
	}
	elem.innerHTML=source;
}

function addRelatedCountry() {
	var obj=document.getElementById('relatedsalis');
	var selected=obj.options[obj.selectedIndex];
	if (selected.value!=0) {
		var list=document.getElementById('relatedlist');
		elem=document.createElement('div');
		elem.innerHTML=selected.text+'<input type="hidden" name="rellist[]" value="'+selected.value+'"/>';
		list.appendChild(elem);
	}
}

function submitenter(myfield,e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13) {
	   myfield.form.submit();
	   return false;
	}
	else
	   return true;
}


function submitsearch(myfield) {
	   myfield.form.submit();
	   return false;
}


function adduser() {
	var users=document.getElementById('usersfrom');
	var s_arr=new Array();
	for(var i=0; i<users.options.length; i++){
		if(users.options[i].selected){
			users.options[i].selected=false;
			var one_sel=document.createElement("option");
			document.getElementById('usersto').appendChild(one_sel);
			one_sel.value=users.options[i].value;
			one_sel.text=users.options[i].text;
			s_arr[s_arr.length]=users.options[i];
		}
	}
	for(var j=0; j<s_arr.length; j++){
		users.removeChild(s_arr[j]);
	}
}


function removeuser() {
	var users=document.getElementById('usersto');
	var s_arr=new Array();
	for(var i=0; i<users.options.length; i++){
		if(users.options[i].selected){
			users.options[i].selected=false;
			var one_sel=document.createElement("option");
			document.getElementById('usersfrom').appendChild(one_sel);
			one_sel.value=users.options[i].value;
			one_sel.text=users.options[i].text;
			s_arr[s_arr.length]=users.options[i];
		}
	}
	for(var j=0; j<s_arr.length; j++){
		users.removeChild(s_arr[j]);
	}
}


function select_all(){
	if(document.getElementById('usersto')){
	var allow=document.getElementById('usersto');
		for(var i=0; i<allow.options.length; i++){
			allow.options[i].selected=true;
		}
	}
}


function addcontent() {
	var users=document.getElementById('contentfrom');
	var s_arr=new Array();
	for(var i=0; i<users.options.length; i++){
		if(users.options[i].selected){
			users.options[i].selected=false;
			var one_sel=document.createElement("option");
			document.getElementById('contentto').appendChild(one_sel);
			one_sel.value=users.options[i].value;
			one_sel.text=users.options[i].text;
			s_arr[s_arr.length]=users.options[i];
		}
	}
	for(var j=0; j<s_arr.length; j++){
		users.removeChild(s_arr[j]);
	}
}


function removecontent() {
	var users=document.getElementById('contentto');
	var s_arr=new Array();
	for(var i=0; i<users.options.length; i++){
		if(users.options[i].selected){
			users.options[i].selected=false;
			var one_sel=document.createElement("option");
			document.getElementById('contentfrom').appendChild(one_sel);
			one_sel.value=users.options[i].value;
			one_sel.text=users.options[i].text;
			s_arr[s_arr.length]=users.options[i];
		}
	}
	for(var j=0; j<s_arr.length; j++){
		users.removeChild(s_arr[j]);
	}
}


function select_allcontent(){
	if(document.getElementById('contentto')){
	var allow=document.getElementById('contentto');
		for(var i=0; i<allow.options.length; i++){
			allow.options[i].selected=true;
		}
	}
}



function getLeidinysRubrikos(t){
	var selected=t.options[t.options.selectedIndex].value;
	if(selected>0 && getrubrikos_url){
		//t.className=t.className.replace(/ ajax/gi, ' ajaxloading');
		var post=new Array();
		post[0]='q='+selected;
		execPost(getrubrikos_url, post, 'showRubrikos', t);
	}
	else{
		document.getElementById('leidinio_rubrikos').innerHTML='Nėra rubrikų';
	}
}

function showRubrikos(content, obj){
	//obj.className=obj.className.replace(/ ajaxloading/gi, ' ajax');
	if(content.responseText != ''){
		var prev=document.getElementById('leidinio_rubrikos');
		prev.innerHTML=content.responseText;
	}
	else{
		document.getElementById('leidinio_rubrikos').innerHTML='Nėra rubrikų';
	}
}


function addLeidinioRubrika() {
	if (document.getElementById('leidiniorubrikos') && document.getElementById('leidiniai')) {
		var rubrika=document.getElementById('leidiniorubrikos');
		var leidinys=document.getElementById('leidiniai');
		var selectedrub=rubrika.options[rubrika.options.selectedIndex].value;
		var selectedleid=leidinys.options[leidinys.options.selectedIndex].value;
		if (selectedrub>0 && selectedleid>0) {
			var selrubrikos=document.getElementById('sel_leid_rubr');
			var item=document.createElement('div');
			item.innerHTML='<div style="width:150px; height:20px; float:left; padding-bottom:3px">'+leidinys.options[leidinys.options.selectedIndex].text+'<input type="hidden" name="selleidiniai[]" value="'+selectedleid+'"/></div><div style="width:150px; float:left">'+rubrika.options[rubrika.options.selectedIndex].text+'<input type="hidden" name="selrubrikos[]" value="'+selectedrub+'"/></div><div style="float:left; width:100px"><a href="javascript:void(0);" class="dellink" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);">Šalinti</a></div><div style="clear:both"></div>';
			selrubrikos.appendChild(item);
		}
		else {
			alert('Pasirinkite leidinį ir rubriką');
		}
	}
	else {
		alert('Pasirinkite leidinį ir rubriką');
	}
}