 var active_obj=false;function my_round(v){if( v==0 )return parseFloat(String(v));if( !v || v=="")return "";obj=document.getElementById("dokla"); 	
    if( obj )x=obj.selectedIndex+2;else x=2;x_max=Math.pow(10,x); 	
    if( Math.abs(parseFloat(v)) < parseFloat(1/x_max) ){ 	
      var v=new Number(v);
      return v.toExponential(x);
      } 
    if( v > 100000 ){ 
      var v=new Number(v); 
      return v.toExponential(x); 
      } 
    num=Math.round(v*x_max); 
    return parseFloat(num/x_max); 
    } 
    
  function cpos(obj) {
	var cp = 0;	// IE Support
	if (document.selection) {
	obj.focus();
		var sel = document.selection.createRange ();
		sel.moveStart ('character', -obj.value.length);
		cp = sel.text.length;
	}
	// Firefox support
	else if (obj.selectionStart || obj.selectionStart == '0')
		cp = obj.selectionStart;
	return (cp);
}
    
  function licz(wg){
  if( !wg ){ sum_all();return; }
  rid=wg.parentNode.parentNode.id;
  r=rid.substr(1);
  //alert(r);
  c="c3_"+r;
  p=wg.value.split(":");
  text="";
  h=m=s=0;
  for(i=0;i<3;i++){
    if( typeof( p[i] )=="undefined" || !p[i] || p[i]=="." )
      p[i]=0;
    }
    p0=parseFloat(p[0]);
    p1=parseFloat(p[1]);
    p2=parseFloat(p[2]);
    //alert(p0);
    if( isNaN(p0) || isNaN(p1) || isNaN(p2) ){
      text="Error";
      }
    else {
      t=p0*3600+p1*60+p2;
      wg.parentNode.parentNode.seconds=t;
      tab=sec2tab(t);
      text="&nbsp;"+tab[0]+" h "+tab[1]+" min "+tab[2]+" sec ";
      }
  document.getElementById(c).innerHTML=text;
  sum_all();
  }
  
function sum_all(){
  //licz wszystkie
  tc=document.getElementById('tcalc');
  rows=tc.getElementsByTagName("TR");
  sum=0;
  for( i=0; i<rows.length; i++ ){
    x=rows[i].seconds;
    s=rows[i].sign;
   // alert(x);
    if( !x )x=0;
    if( s )
      sum-=x;
    else
      sum+=x;
    }
  tab2=sec2tab(sum);
  text=ftim(tab2[0])+":"+ftim(tab2[1])+":"+ftim(tab2[2]);
  document.getElementById('tco').innerText=text;
  document.getElementById('tco').textContent=text;
  ss=my_round(sum);
  mm=my_round(sum/60);
  hh=my_round(sum/3600);
  text=hh+"<br>"+mm+"<br>"+ss;
  document.getElementById('atco').innerHTML=text;
  }
  
function ftim(d){
  x=new String(d);
  if( x.length<2 )
    return "0"+x;
  return x;
  }
  
function sec2tab(t){
  ans= new Array();
  z="";
  if( t<0 )z="-";
    t=Math.abs(t);
  ans[0]=Math.floor(t/3600);
  ans[1]=Math.floor((t-ans[0]*3600)/60);
  s=Math.floor(t-ans[0]*3600-ans[1]*60);
  ans[0]=z+ans[0];
  ms=new String(t);
  ix=ms.indexOf(".");
  if( ix>-1 )
      s+=ms.substr(ix);
  ans[2]=s;
  return ans;
  }
 
  function ckey(e){ 
    
    if( typeof(act_opis)!="undefined" ){
      act_opis=0;
      opis(0);
      }
    if( typeof(dtc_clear)!="undefined" ){
      dtc_clear();
      }
    fld=(!e.target ? window.event.srcElement : e.target );
    if( !e )e=window.event; 
      k=e.keyCode; 
    if( !k )k=e.which;
    a=String.fromCharCode( k ); 
    if( a=="+" || k==13 ){
      add_row(0);
      return false;
      }
    if( a=="-" ){
      add_row(1);
      return false;
      }
    v=new String(fld.value); 
    
    if( a=="." ){ 
      cp=cpos(fld);
      nv=v.substr(0,cp)+"."+v.substr(cp);
      p=nv.split(":");
      for(i=0;i<p.length;i++){
        ix=p[i].indexOf(".");
        if( ix>-1 )
          if( p[i].indexOf(".",ix+1)>-1 )
            return false;
         } 
        return true; 
      } 
    if( a==":" ){ 
      p=v.split(":");
      if( p.length > 2 )
        return false;
      return true;
      }
    
    if( ((a<'0' || a>'9') && k >=32)){ 
        return false; 
        }   
    return true; 
    } 
    
var nodes=1;
  function add_row(d,val){
    if( nodes>=50 ){
      alert( "Too many input fields");
      return;
      }
    t=document.getElementById('tcalc');
    r=document.getElementById('r0');
    rn=r.cloneNode(true);
    c=rn.getElementsByTagName("TD");
    if(d){
    c[0].innerText="-";
    c[0].textContent="-";
    }
    else{
    c[0].innerText="+";
    c[0].textContent="+";
    }
    rn.sign=d;
    rn.seconds=0;  
    c[0].id="c0_"+nodes;
    c[1].id="c1_"+nodes;
    c[2].id="c2_"+nodes;
    c[3].id="c3_"+nodes;
    rn.id="r"+nodes;
    nodes++;
    inp=c[1].firstChild;
    if( typeof(val)=="undefined" )
    inp.value="00:00:00";
    else
    inp.value=val;
    t.appendChild(rn);
    inp.select();
    inp.focus();
    }
  function del(o){
    if( o.id=="c2_0" )
      return;
    t=document.getElementById('tcalc');
    t.removeChild(o.parentNode);
    sum_all();
    }
  function clear_tab(){
    t=document.getElementById('tcalc');
    r=t.getElementsByTagName("TR");
    for(i=r.length-1;i>0;i--){
      t.removeChild(r[i]);
      }
    r[0].seconds=0;
    r[0].sign=0;
    c=r[0].getElementsByTagName("TD");
    c[0].innerText="+";
    c[0].textContent="+";
    c[1].firstChild.value="00:00:00";
    licz(c[1].firstChild);
    nodes=1;
    sum_all();
    }
  function schg(o){
    t=document.getElementById('tcalc');
    p=o.parentNode;
    if( p.sign ){
      p.sign=0;
      o.innerText="+";
      o.textContent="+";
      }
    else { 
      p.sign=1;
      o.innerText="-";
      o.textContent="-";
      }
    sum_all();
    }
    
 

