var node_tree= new Array();
var done_node_tree=0;
var total_cr=0;
var total_dr=0;

function tree_submit( container_post, id,  inst,  menu )
{
  var input_name=container_post+"_"+id;
  var value=eval( "document.menuform."+input_name+".value" );
  var inp=eval( "document.menuform."+input_name );
  var newvalue=++value%2;
  inp.value=newvalue;
  var has_cp=document.getElementById( "_tree_"+container_post+"_node_end" );
  var pushcount=0;
  if ( has_cp != null && done_node_tree == 0 ){
    var node_end=has_cp.value;
    for( i=1;i<=node_end;i++ ){
      obj=document.getElementById( container_post+"_"+i+"_parent" );
      par=obj.value
      if ( obj != null ){
        if ( ! node_tree.hasOwnProperty(par) ){
          node_tree[par]=new Array();
        } 
        node_tree[par].push(i);
      }
    }
    done_node_tree=1;
  }
  for( var node in node_tree ){
    var outstring="";
    for( var node1 in node_tree[node] ){
      outstring=outstring+","+node_tree[node][node1];
    }
  }
  var accounts= document.getElementById( "_TAB__div_"+inst+"_"+input_name+"_page_1" );
  if ( newvalue == 0 ){
    var el=document.getElementById( "_div_"+inst+"_"+input_name );
    el.style.display='none';
    if ( accounts != null ){
      accounts.style.display='';
    }
    graphic_close =document.getElementById( "_graphic_"+inst+"_"+input_name+"_close" );
    graphic_open = document.getElementById( "_graphic_"+inst+"_"+input_name+"_open" );
    graphic_close.style.display='none';
    graphic_open.style.display='';

  }else{
    var el=document.getElementById( "_div_"+inst+"_"+input_name );
    var loaded=eval( "document.menuform."+input_name+'_loaded' );
    if ( loaded.value == 1 ){
      if ( accounts != null ){
        accounts.style.display='none';
      }
      el.style.display='';
      graphic_close =document.getElementById( "_graphic_"+inst+"_"+input_name+"_close" );
      graphic_open = document.getElementById( "_graphic_"+inst+"_"+input_name+"_open" );
      graphic_open.style.display='none';
      graphic_close.style.display='';
    }else{
       do_submit( menu, "" );
       return;
    }
  }
  total_dr=0;
  total_cr=0;
  add_up( container_post, inst, 0 );
  tot_disp_dr=tree_to_money( total_dr );
  tot_disp_cr=tree_to_money( total_cr );
  var render_dr=document.getElementById( container_post+"_total_dr" );
  var render_cr=document.getElementById( container_post+"_total_cr" );
  /* was a total line rendered */
  if ( render_dr == null && render_cr == null ){
    return;
  }
  render_dr.innerHTML=tot_disp_dr;
  render_cr.innerHTML=tot_disp_cr;
  var bal_total_dr;
  var bal_total_cr;
  var bal_render_dr=document.getElementById( container_post+"_bal_dr" );
  var bal_render_cr=document.getElementById( container_post+"_bal_cr" );
  /* was a balance line rendered */
  if ( bal_render_dr == null && bal_render_cr == null ){
    return;
  }
  if ( total_dr > total_cr ){
    var res=total_dr - total_cr;
    var res_string=tree_to_money( res );
    bal_render_dr.innerHTML=res_string;
    bal_render_cr.innerHTML='';
  }else{
    var res=total_cr - total_dr;
    var res_string=tree_to_money( res );
    bal_render_dr.innerHTML='';
    bal_render_cr.innerHTML=res_string;
  }
  return;
}

function add_up( container_post, inst, start_node  )
{
  var input_name;
  var el;
  for( var node in node_tree[start_node] ){
    input_name=container_post+"_"+node_tree[start_node][node];
    el=document.getElementById( "_div_"+inst+"_"+input_name );
    if ( el == null || el.style.display == 'none' ){
      dr=document.getElementById( container_post+"_"+node_tree[start_node][node]+"_dr" );
      /*alert( "found dr("+dr.value+") at node "+node_tree[start_node][node] );*/
      if ( dr == null ){
        alert( "ERROR(add_up): Couldn't find ("+container_post+"_"+node_tree[start_node][node]+"_dr)" );
      }else{
        totaldr=parseInt(dr.value);
        if ( ! isNaN(totaldr) ){
          total_dr+=totaldr;
        }
      }
      cr=document.getElementById( container_post+"_"+node_tree[start_node][node]+"_cr" );
      if ( cr == null ){
        alert( "ERROR(add_up): Couldn't find ("+container_post+"_"+node_tree[start_node][node]+"_cr)" );
      }else{
        totalcr=parseInt(cr.value);
        if ( ! isNaN( totalcr ) ){
          total_cr+=totalcr;
        }
      }
    }else{
      /*alert( "decending into "+node_tree[start_node][node] );*/
      add_up( container_post, inst, node_tree[start_node][node] );
    }
  }
}

function tree_to_money( num )
{
  var currency_comma=',';
  var currency_period='.';
  if ( isNaN( num ) ){
    var ns='000';
  }else{
    var ns=num.toString();
  }
  var lhsa=new Array();
  var comma;
  while( ns.length < 3 ){
    ns='0'+ns;
  }
  var lhs=ns.replace(/^(.*)(..)$/, '$1' );
  var rhs=ns.replace(/^(.*)(..)$/, '$2' );
  while( lhs.match( /...$/ ) ){
    pushvalue=lhs.replace( /^.*(...)$/, '$1' );
    lhsa.unshift(pushvalue);
    lhs=lhs.replace( /^(.*)(...)$/, '$1' );
  }
  var new_lhs=lhs;
  if ( lhs.length > 0 ){
    comma=currency_comma;
  }else{
    comma='';
  }
  for( var lhs_i in lhsa ){
    new_lhs=new_lhs+comma+lhsa[lhs_i];
    comma=currency_comma;
  }
  return( new_lhs+currency_period+rhs );
}

