// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see .
pscript['onload_Financial Statements'] = function() {
	
	// header and toolbar
	var h = new PageHeader('fs_header','Financial Statements','Profit & Loss and Balance Sheet Builder across multiple years');
	//$y(h.toolbar_area,{padding:'8px'});
	
	var dv = $a(h.toolbar_area,'div','',{margin:'4px 0px'});
	
	var t = make_table(dv,1,4,'640px', [], {padding:'4px', width:'160px'});
	
	var sel = $a($td(t,0,0),'select','',{width:'160px'});
	sel.id = 'stmt_type';
	
	var sel = $a($td(t,0,1),'select','',{width:'160px'});
	sel.id = 'stmt_company';
	
	var sel = $a($td(t,0,2),'select','',{width:'160px'});
	sel.id = 'stmt_period';
	var sel = $a($td(t,0,3),'select','',{width:'160px'});
	sel.id = 'stmt_fiscal_year';
	h.add_button('Create',function(){ pscript.stmt_new(); },0,'ui-icon-document');
	h.add_button('Print', function(){ _p.go($i('print_html').innerHTML); }, 0, 'ui-icon-print');
/*	
	var btn = $a($td(t,1,0),'button');
	btn.onclick = function(){ pscript.stmt_new(); }
	btn.innerHTML = 'Create';
	
	var btn = $a($td(t,1,1),'button');
	btn.onclick = function(){ alert('print'); }
	btn.innerHTML = 'Print';
  //Button to create new
  var btn = $a('stmt_new', 'button');
  btn.onclick = function() { pscript.stmt_new(); }
  btn.innerHTML = 'Create';*/
  // select for statement
  add_sel_options($i('stmt_type'), ['Select Statement...','Balance Sheet','Profit & Loss']);
  // select for companies
  add_sel_options($i('stmt_company'), ['Loading Companies...']);
  // load companies
  $c_obj('MIS Control','get_comp','', function(r,rt) {    
    // company
    empty_select($i('stmt_company'));
    add_sel_options($i('stmt_company'), add_lists(['Select Company...'], r.message.company), 'Select Company...');
    // period
    empty_select($i('stmt_period'));
    //add_sel_options($i('stmt_period'), add_lists(['Select Period...'], r.message.period), 'Select period...');
    add_sel_options($i('stmt_period'), add_lists(['Select Period...'], ['Annual', 'Quarterly', 'Monthly']), 'Select period...');
    
    // fiscal-year
    empty_select($i('stmt_fiscal_year'));
    add_sel_options($i('stmt_fiscal_year'), add_lists(['Select Year...'], r.message.fiscal_year), 'Select fiscal year...');
  });
}
pscript.stmt_new = function(stmt,company_name,level,period,year) {
    
  $i('stmt_tree').innerHTML = 'Refreshing....';
  $i('stmt_tree').style.display = 'block';
  
  var company = sel_val($i('stmt_company'))
  var arg = {
  	statement:sel_val($i('stmt_type')),
  	company:company,
  	period:sel_val($i('stmt_period')),
  	year:sel_val($i('stmt_fiscal_year'))
  }
  $c_obj('MIS Control', 'get_statement', docstring(arg), function(r,rt) {
      var nl = r.message;
      var t = $i('stmt_tree');
      var stmt_type = sel_val($i('stmt_type'));
      t.innerHTML = '';
      var tab = $a($a(t, 'div'),'table','stmt_table');
      tab.style.tableLayout = 'fixed';
      tab.style.width = '100%';
      
      $i('stmt_title1').innerHTML = sel_val($i('stmt_company'));
      $i('stmt_title2').innerHTML = sel_val($i('stmt_type')) + ' - ' +  sel_val($i('stmt_fiscal_year'));
      for(i=0;i