// fokusuoja i auksciausia tuscia text lauka
function focus_field()
{
   var elements = document.getElementsByTagName("input");
   var num = elements.length;
   for(i=0;i<num;i++)
   {     
     if(elements[i].type == 'text'&&elements[i].value == '')
	 {	   
	   elements[i].focus();
	   break;
	 }
   }
}
// fokusuoja i auksciausia tuscia text lauka
function help()
{
  window.open( "../class/help.class.php?hid="+document.adminForm.opt.value, "Pagalba", "status = 1, height = 600, width = 600, resizable = 0, scrollbars=1" )
}
// tikrina ar ne tuscias text laukas
function validate(frm,validate_head)
{    
   if (!frm) 
   {		
	var frm = document.adminForm;
   }   
   var num = frm.elements.length;    
   var ch = 0;
   var err_str = '';
   for(i=0;i<num;i++)
   {       
     str = frm.elements[i].id.substr(0,1);	 
     if(str == '!' && frm.elements[i].value.length == 0)
     {	   
       err_str += frm.elements[i].id.substr(1)+'\n';
     }   
   }   
   if(err_str.length > 0)
   {
     err_str = validate_head+'\n\n'+err_str;	 
   }	    
return err_str;
}
// pagrindinio meniu IE pataisymas
/*
startList = function() 
{
 if (document.all&&document.getElementById) 
 {
  navRoot = document.getElementById("nav");
  for (i=0; i<navRoot.childNodes.length; i++) 
  {
    node = navRoot.childNodes[i];
     if (node.nodeName=="LI") 
	 {
      node.onmouseover=function() 
	  {
       this.className+=" over";
      }
      node.onmouseout=function() 
	  {
       this.className=this.className.replace(" over", "");
      }
     }
  }
 }
}
window.onload=startList;
*/

//  tikrina login laukus
function check_login()
{
str = '';
  if(document.login.user.value == ''||document.login.psw.value == ''||document.login.kodas.value == '')
  {
      if(document.login.user.value == '') str +='\n'+document.login.user.id;
	  if(document.login.psw.value == '') str +='\n'+document.login.psw.id;	
      alert('Prašome užildyti šuos laukus :'+str);
	  return false;
  } 
  else return true;
}

// zymi visus checkbox elementus, esancius 'adminForm' formoje, kuriu id yra 'cb'
function checkAll() 
{
var forma = document.adminForm;
var num = document.adminForm.elements.length;
var viso = 0;
  if(forma.toggle.checked)
  {    
    for(i=0;i<num;i++)
	{
	  if(forma.elements[i].id == 'cb')
	  {
        forma.elements[i].checked = true;
	  }
	}		
  }
  else
  {    
    for(i=0;i<num;i++)
	{
	  if(forma.elements[i].id == 'cb')
	  {
        forma.elements[i].checked = false;
	  }
	}		
  }  
}
// paspausto mygtuko valdymas
function submitbutton(pressbutton) 
{
	submitform(pressbutton, null, 'task');
}

function submitmenu(pressbutton) 
{    
	submitform(pressbutton, null, 'opt');	
}

// submitina forma
function submitform(pressbutton, frm, type)
{        	
	if (!frm) {
		frm = document.adminForm;
	}	
	if(type == 'task')
	{
	  frm.task.value=pressbutton;	
	}
    if(type == 'opt')
    {
	  frm.opt.value=pressbutton;
	}	
	try
	{
	  frm.onsubmit();
	}
	catch(e) {}
	frm.submit();

}

// tikrinam, ar yra pazymetas bent vienas checkbox
function is_checked(frm)
{
  if (!frm) 
  {
	 frm = document.adminForm;
  }	
var num = frm.elements.length;
var ch = false;

  for(i=0;i<num;i++)
  {
    if(frm.elements[i].id == 'cb'&&frm.elements[i].checked == true)
	{
	  ch = true;
	  return ch;
	  break;
	}
  }	   
}

// nustatom ar meniu slepti 
function hidemenu() 
{
	document.adminForm.hidemenu.value=1;
}
// ***************** kategoriju medis ***************************************
//      to turn off automatic conversion of UL's onLoad
// March 1, 2004: Changed it so if a <li> has a class already attached
//      to it, that class won't be erased when initialized. This allows
//      you to set the state of the tree when painting the page simply
//      by setting some <li>'s class name as being "liOpen" (see example)
/*
This code is inspired by and extended from Stuart Langridge's aqlist code:
		http://www.kryogenix.org/code/browser/aqlists/
		Stuart Langridge, November 2002
		sil@kryogenix.org
		Inspired by Aaron's labels.js (http://youngpup.net/demos/labels/) 
		and Dave Lindquist's menuDropDown.js (http://www.gazingus.org/dhtml/?id=109)
*/

// Automatically attach a listener to the window onload, to convert the trees
addEvent(window,"load",convertTrees);

// Utility function to add an event listener
function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

// utility function to set a global variable if it is not already set
function setDefault(name,val) {
	if (typeof(window[name])=="undefined" || window[name]==null) {
		window[name]=val;
	}
}

// Full expands a tree with a given ID
function expandTree(treeId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeOpenClass);
}

// Fully collapses a tree with a given ID
function collapseTree(treeId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	expandCollapseList(ul,nodeClosedClass);
}

// Expands enough nodes to expose an LI with a given ID
function expandToItem(treeId,itemId) {
	var ul = document.getElementById(treeId);
	if (ul == null) { return false; }
	var ret = expandCollapseList(ul,nodeOpenClass,itemId);
	if (ret) {
		var o = document.getElementById(itemId);
		if (o.scrollIntoView) {
			o.scrollIntoView(false);
		}
	}
}

// Performs 3 functions:
// a) Expand all nodes
// b) Collapse all nodes
// c) Expand all nodes to reach a certain ID
function expandCollapseList(ul,cName,itemId) {
	if (!ul.childNodes || ul.childNodes.length==0) { return false; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (itemId!=null && item.id==itemId) { return true; }
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					var ret = expandCollapseList(sitem,cName,itemId);
					if (itemId!=null && ret) {
						item.className=cName;
						return true;
					}
				}
			}
			if (subLists && itemId==null) {
				item.className = cName;
			}
		}
	}
}

// Search the document for UL elements with the correct CLASS name, then process them
function convertTrees() {
	setDefault("treeClass","mktree");
	setDefault("nodeClosedClass","liClosed");
	setDefault("nodeOpenClass","liOpen");
	setDefault("nodeBulletClass","liBullet");
	setDefault("nodeLinkClass","bullet");
	setDefault("preProcessTrees",true);
	if (preProcessTrees) {
		if (!document.createElement) { return; } // Without createElement, we can't do anything
		uls = document.getElementsByTagName("ul");
		for (var uli=0;uli<uls.length;uli++) {
			var ul=uls[uli];
			if (ul.nodeName=="UL" && ul.className==treeClass) {
				processList(ul);
			}
		}
	}
}

// Process a UL tag and all its children, to convert to a tree
function processList(ul) {
	if (!ul.childNodes || ul.childNodes.length==0) { return; }
	// Iterate LIs
	for (var itemi=0;itemi<ul.childNodes.length;itemi++) {
		var item = ul.childNodes[itemi];
		if (item.nodeName == "LI") {
			// Iterate things in this LI
			var subLists = false;
			for (var sitemi=0;sitemi<item.childNodes.length;sitemi++) {
				var sitem = item.childNodes[sitemi];
				if (sitem.nodeName=="UL") {
					subLists = true;
					processList(sitem);
				}
			}
			var s= document.createElement("SPAN");
			var t= '\u00A0'; // &nbsp;
			s.className = nodeLinkClass;
			if (subLists) {
				// This LI has UL's in it, so it's a +/- node
				if (item.className==null || item.className=="") {
					item.className = nodeClosedClass;
				}
				// If it's just text, make the text work as the link also
				if (item.firstChild.nodeName=="#text") {
					t = t+item.firstChild.nodeValue;
					item.removeChild(item.firstChild);
				}
				s.onclick = function () {
					this.parentNode.className = (this.parentNode.className==nodeOpenClass) ? nodeClosedClass : nodeOpenClass;
					return false;
				}
			}
			else {
				// No sublists, so it's just a bullet node
				item.className = nodeBulletClass;
				s.onclick = function () { return false; }
			}
			s.appendChild(document.createTextNode(t));
			item.insertBefore(s,item.firstChild);
		}
	}
}
//**********************************************************************
