// pazymi (action = 1) arba nuzymi (action=0) visus sheckbox'us
// kurie turi varda sb_name[] ir yra formoje kurios name = form  
function ch( form,sb_name,action )
{
   var sb = document.forms[ form ].elements[ sb_name + '[]' ];
	 if( !sb )
	     return;
	 if( sb.length ) { 
	 		var gl = sb.length;
	 		for( i = 0; i < gl; i++ )
	      	 sb[ i ].checked = action;
	 } sb.checked = action;
  return false;
}


function popup(obj,name,w,h)
{
  var href = obj;
	w += 10;
	h += 10;
	
	ww = window.open( href,name,"width=" + w + ",height=" + h + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,dependent=no' );
	ww.focus();
	
	return false;
}

// SVARBU: formoje turi buti kintamasis 'items' (dazniausiai hiden)
// visi select'ai turi tureti name='sel[]' grazina pazymetu selektu
// id lista
function cl( form )
{
   var sb = document.forms[ form ].elements[ 'sel[]' ];
	 var el = document.forms[ form ].elements[ 'items' ];
	 var rs = '';
	 var gl,gi;
	 if( !sb || !el )
	     return;
	 if( sb && sb.length ) {
	 		 gl = sb.length;
	 		 gi = 0;
	 		 for( i = 0; i < gl; i++ ) {
	      		if( sb[ i ].checked ) {
				    		if( gi )
						    		rs += ',';
				    		gi++;
				    		rs += sb[ i ].value;
						}
	 		 }
	 		 if( !gi ) 
			     rs = '';
	 } else rs = ( sb.checked ) ? sb.value : '';
	 el.value = rs;
} 

function js( form,value )
{
   var sb = document.forms[ form ];
	 show_wait();
	 sb.action += value;
	 sb.submit();
   return false;
}

function jg( form,value )
{
   var sb = document.forms[ form ];
	 
	 show_wait();
	 sb.action = value;
	 sb.submit();
   return false;
}

function jcf( name ) 
{
  return confirm( "Trinti \"" + name + "\" ?" );
}

function jgr()
{
  return confirm( "Ar tikrai norit ištrinti pasirinktą grupę ?" );
}

function jsicf( form,value )
{
  var r = confirm( "Ar tikrai norit ištrinti pažymėtus ?" );
  if( r ) 
      return jsi( form,value );
  return false;
}

function jsi( form,value )
{
   var sb = document.forms[ form ];
	 
	 show_wait();
	 cl( form );
	 sb.action += value;
	 sb.submit();
   return false;
}

function jsi_n( form,value,ch_box_name,result_name )
{
   var sb = document.forms[ form ];
	 
	 show_wait();
	 c2( form, ch_box_name, result_name );
	 sb.action += value;
	 sb.submit();
  return false;
}

function jsf( form )
{
  var sb = document.forms[ form ];
	
	if( sb ) {
	    sb.action += "&ac=filter";
			sb.submit();
	}
  return false;
}

function jwimg(obj,name,w,h)
{
  var href = obj.href;
	w += 10;
	h += 10;
	
	ww = window.open( href,name,"width=" + w + "height=" + h + "toolbar=no," +
	                + "location=no,directories=no,status=no,menubar=no,scrollbars=no," +
						      + "resizable=no,dependent=no" );
	ww.focus();
	
	return false;
}

function show_wait() 
{
  function tmp()
  {
   p = document.getElementById( "wait" );
	  if( p ) {
	      	  dw = document.body.clientWidth;
			  dh = document.body.clientHeight;
			  pw = p.clientWidth;
			  ph = p.clientHeight;
			  p.style.left = dw/2 - pw/2;
			  p.style.top  = dh/2 - ph/2;
			  p.style.visibility = "visible";
	  }
  }
  setTimeout( tmp,1000 ); 
}

function hide_wait()
{
  p = document.getElementById( "wait" );
	if( p ) 
	    p.style.visibility = "hidden";
}

function jhelp(obj)
{
  var href = obj.href;
	var x,y;
	
	dw = document.body.clientWidth;
	dh = document.body.clientHeight;
	x = dw/2 - 383/2;
	y = dh/2 - 300/2;
	
	ww = window.open( href,"Pagalba","width=383,height=300,toolbar=no,left=" + x + ",top=" + y +
	                + ",location=no,directories=no,status=no,menubar=no,scrollbars=no," +
						      + "resizable=no,dependent=yes,dialog=no,close=no" );
	ww.focus();
	
	return false;
}

function jbd(href,ow,oh)
{
	var x,y;
	
	dw = document.body.clientWidth;
	dh = document.body.clientHeight;
	x = (dw - ow)/2;
	y = (dh - oh)/2;
	
	ww = window.open( href,"","width=" + ow + ",height=" + oh + ",toolbar=no,left=" + x + ",top=" + y +
	                + ",location=no,directories=no,status=no,menubar=no,scrollbars=no," +
						      + "resizable=no,dependent=yes,dialog=no,close=no" );
	ww.focus();
	
	return false;
}

function jd(obj,ow,oh)
{
	return jbd(obj.href,ow,oh);
}

function jds(href,ow,oh)
{
	var x,y;
	
	dw = document.body.clientWidth;
	dh = document.body.clientHeight;
	x = (dw - ow)/2;
	y = (dh - oh)/2;
	
	ww = window.open( href,"","width=" + ow + ",height=" + oh + ",toolbar=no,left=" + x + ",top=" + y +
	                + ",location=no,directories=no,status=no,menubar=no,scrollbars=yes," +
						      + "resizable=yes,dependent=yes,dialog=no,close=no" );
	ww.focus();
	
	return false;
}

function gclose()
{
	window.opener.location.reload( false );
	self.close();
	
	return false;
}

function jss(id)
{
	if( typeof( document.getElementById ) != "undefined" ) { 
		var r = document.getElementById( "ch_" + id );
		if( r && typeof( r.checked ) != "undefined" ) {
			r.checked = r.checked ? false:true;
		}
	}
	return true;
}
		
function jcs(obj,id,col)
{
    var cells = null;

    // patikrinam ar browseris gerai grazina eilutes objekta 
    if ( typeof( obj.style ) == "undefined" ) {
        return false;
    }
	
	// patikrinam ar eilute nera pazymeta
	if( typeof( document.getElementById ) != "undefined" ) { 
		var r = document.getElementById( "ch_" + id );
		if( r && typeof( r.checked ) != "undefined" ) {
			if( r.checked )
				return false;
		}
	}

    // gauna esamos eilutes stulpelius 
    if ( typeof( document.getElementsByTagName ) != "undefined" ) {
        cells = obj.getElementsByTagName( "td" );
    } else if ( typeof( obj.cells ) != "undefined" ) {
        cells = obj.cells;
    } else return false;

    var l = cells.length;
    var DOM = false;
    
	// jei kartais nebutu ne vieno stulpelio
	if( !l )
	    return false;
	// tikrinam ar turim DOM 
    if ( typeof( window.opera ) == "undefined" && typeof( obj.setAttribute ) != "undefined" ) {
        DOM = true;
    } 
	
    // nustatom nauja spalva
    if ( col ) {
        var c = null;
        // jei DOM browseris iskyrus Opera
        if( DOM ) {
            for(c = 0; c < l; c++ ) {
                cells[ c ].setAttribute( "bgcolor",col,0 );
            } // for
        }
        // su visais kitais
        else {
            for (c = 0; c < l; c++) {
                cells[ c ].style.backgroundColor = col;
            }
        }
    } 
    // viskas OK todel grazinam true
	return true;
}

function list_move(fbox, tbox) 
{
	 // ar turim funkcija
     if( typeof( document.getElementById ) == "undefined" ) 
	     return false;
	 var fbox = document.getElementById( fbox );
	 var tbox = document.getElementById( tbox );
	 
	 if( typeof( fbox ) == "undefined" || typeof( tbox ) == "undefined" ) 
	     return false;
	 // elementu masyvai
	 var arrFbox = new Array();
     var arrTbox = new Array();
     var arrLookup = new Array();
     
	 var i;
     for( i = 0; i < tbox.options.length; i++) {
          arrLookup[ tbox.options[ i ].text ] = tbox.options[ i ].value;
          arrTbox[ i ] = tbox.options[ i ].text;
     }
     var fLength = 0;
     var tLength = arrTbox.length
     for( i = 0; i< fbox.options.length; i++) {
          arrLookup[ fbox.options[ i ].text ] = fbox.options[ i ].value;
          if( fbox.options[ i ].selected && fbox.options[ i ].value != "" ) {
               arrTbox[ tLength ] = fbox.options[ i ].text;
               tLength++;
          } else {
               arrFbox[ fLength ] = fbox.options[ i ].text;
               fLength++;
          }
     }
	 // surikiuojam elementus
     arrFbox.sort();
     arrTbox.sort();
     fbox.length = 0;
     tbox.length = 0;
     var c;
     for( c = 0; c < arrFbox.length; c++) {
          var no = new Option();
          no.value = arrLookup[ arrFbox[ c ] ];
          no.text = arrFbox[ c ];
          fbox[ c ] = no;
     }
     for( c = 0; c < arrTbox.length; c++) {
     	  var no = new Option();
     	  no.value = arrLookup[ arrTbox[ c ] ];
     	  no.text = arrTbox[ c ];
     	  tbox[ c ] = no;
     }
	 return true;
}

function list_select_all( box ) 
{
	 if( typeof( document.getElementById ) == "undefined" ) 
	     return false;
		 
	 var box = document.getElementById( box );
	 var i;
	 for( i = 0; i < box.length; i++ ) {
     	  box[ i ].selected = true;
     }
	 return true;
}

function jsli( form,action,list )
{
	if( typeof( document.getElementById ) == "undefined" ) 
	     return false;
		 
	 var box = document.getElementById( list );
	 var items = document.getElementById( "list_items" );
	 
	 if( typeof( box ) != "undefined" && typeof( items ) != "undefined" ) {
		 var i,l;
		 l = "";
		 for( i = 0; i < box.length; i++ ) {
			  if( l ) l += ",";
			  l += box.options[ i ].value;
		 }
		 items.value = l;
	 }
	 list_select_all( list );
	 
	 return js( form,action );
}

function jclose()
{
	if( typeof( opener ) != "undefined" )  {
		var w = opener.document.forms[ 0 ];
		w.method = "POST";
		w.action += "ac=popup_refresh";
		w.submit();
	}
	window.close();
	
	return false;
}


