pscript['onload_Accounts Browser'] = function(){
  // if the user directly loads the page, ask to select the chart
  var parent = $i('ab_body');
  parent.innerHTML = 'Please select your chart: '
  var sel = $a(parent,'select');
  add_sel_options(sel, ['Account', 'Cost Center'], 'Account');
  var btn = $btn(parent, 'Go', function() { pscript.make_chart(sel_val(sel)); }, {marginTop:'8px'});
}
pscript.make_chart = function(b) {
  pscript.chart_type = b;
  $i('ab_header').innerHTML ='';
  $i('ab_body').innerHTML ='';
  //===============comment area========================================
  var comment = $a($i('ab_body'),'div','comment',{marginBottom:"8px"});
  comment.innerHTML = "Note: Explore and click on the tree node to add a new child";
  
  var select_area = $a('ab_body', 'div', '', {margin:'8px 0px'});
  
  //================== table body======================================  
  var ac_main_grid = make_table($i('ab_body'),1,2,'100%',['60%','40%'],{border:"0px", padding:"4px",tableLayout: "fixed", borderCollapse: "collapse"});
  $y($td(ac_main_grid,0,0),{border: "1px solid #dddddd", padding: "8px"});
  pscript.account_tree = $a($td(ac_main_grid,0,0),'div');
  $y($td(ac_main_grid,0,1),{border: "1px solid #DDD"});
  pscript.la = $a($td(ac_main_grid,0,1),'div');
  pscript.acc_period_bal = $a($td(ac_main_grid,0,1),'div');
  
  //=====================footer area ==================================
  if (pscript.chart_type == 'Account') {
    var footer = $a($i('ab_body'),'div','',{backgroundColor: "#FFD", padding: "8px", color: "#444", fontSize: "12px", marginTop: "14px"});
    
    var help1 = $a(footer,'span');
    help1.innerHTML = "Note: To create accounts for Customers and Suppliers, please create Customer and Supplier"
      + " Masters. This will ensure that the accounts are linked to your Selling and Buying Processes. The Account Heads for Customer and Supplier will automatically be created."
  }
  // header and toolbar
  // ------------------
  
  var h1 = 'Chart of '+pscript.chart_type+'s';
  if(pscript.chart_type == 'Account') var d = 'accounting';
  else var d = 'cost center';
  var desc = 'Manage multiple companies and the '+d+' structures of each company.';
  $i('ab_body').page_head = new PageHeader('ab_header',h1,desc);
  // select company
  // --------------
  var tab = make_table(select_area, 1, 2, null, [], {verticalAlign:'middle', padding: '2px'});
  $td(tab,0,0).innerHTML = 'Select Company'.bold();
  var sel = $a($td(tab,0,1),'select','',{width:'160px'});
  // set tree
  var set_tree = function() {
    if(pscript.ac_tree) {
      pscript.ac_tree.body.innerHTML = '';
    }
    pscript.make_ac_tree();
    var cn = sel_val(sel);
    var n = pscript.ac_tree.addNode(null, cn, null,pscript.ac_tree.std_onclick, pscript.ac_tree.std_onexp);
    n.rec = {}; 
    n.rec.name = 'Root Node'; 
    n.rec.account_name = cn;
    n.rec.cost_center_name = cn;
    pscript.set_ac_head('',n.rec);
    $ds(pscript.ac_head_area);
  }
  // select company
  add_sel_options(sel, ['Loading...']);
  var callback = function(r,rt) {    
    empty_select(sel); 
    add_sel_options(sel,r.message.cl,sys_defaults.company);    
    set_tree();
    sel.onchange = function() { set_tree(); }
  }
  $c_obj('GL Control', 'get_companies', '', callback);
  
  pscript.ab_company_sel = sel; 
  pscript.make_ac_head();
  pscript.make_group_area();
  pscript.make_ledger_area();
  pscript.make_new_acc_dialog();
  pscript.make_new_comp();
  pscript.make_new_cost_center_dialog();
}
//New company link
pscript.make_new_comp = function(){
  $i('ab_body').page_head.add_button('New Company', function() { new_doc('Company'); }, 0, 'ui-icon-plus');
}
pscript.make_ac_tree = function() {
  //var type= sel_val($i('chart_type'))
  var type= pscript.chart_type;
  var tree = new Tree(pscript.account_tree, '90%');
  pscript.ac_tree = tree;
  // on click
  tree.std_onclick = function(node) {
    
    pscript.cur_node = node;
    // show ledger
    pscript.set_ac_head(node.parent_account, node.rec,type);
  }
  // on expand
  tree.std_onexp = function(node) {
    if(node.expanded_once)return;
    $ds(node.loading_div);
    //set_ac_head
    var callback = function(r,rt) {
      $dh(node.loading_div);
      var n = tree.allnodes[r.message.parent_acc_name];
      var cl = r.message.cl;
      if(type=='Account'){
        for(var i=0;i