#include <prefix.h>
#include <utility.h>

#include "sc2101.h"                    
#include "sc2100a.h"                    

#include <comuni.h>

bool TEC_mask::codprof_handler(TMask_field& f, KEY k)
{                                                             
  if (f.to_check(k))
  {
    TEdit_field & e = (TEdit_field &) f;
    
    e.check_type(CHECK_REQUIRED);
    
    const bool ok = e.check();
    
    e.check_type(CHECK_NONE);
    
    if (k == K_ENTER && !ok)
    {
      if (f.is_editable())
      {
        TEditable_field& ef = (TEditable_field&) f;
        return f.error_box(ef.get_warning());
      }
    }
  }
  return TRUE;
}                   


bool TEC_mask::lingua_handler(TMask_field& f, KEY k)
{             
  if (k == K_TAB && f.focusdirty())
  {
    TCursor& cur = *(((TEdit_field&)(f.mask().field(F_CODPROF))).browse()->cursor());
    // whew
    TString16 lin = f.get();      
    if (lin != cur.curr().get("CODPROF").mid(4))
      f.mask().field(F_CODPROF).check(RUNNING_CHECK);
  }                           
  return TRUE;
} 



TEC_mask::TEC_mask(const char* name) : TSelection_mask(name), _ditta(LF_NDITTE)
{
  _ditta.add(LF_ANAG,   "TIPOA==TIPOA|CODANAGR==CODANAGR");
  _ditta.add(LF_COMUNI, "COM==COMRF(COMRES)", 1, LF_ANAG, 101);
  _ditta.add(LF_COMUNI, "COM==COMRES", 1, LF_ANAG, 102);

   set_handler(F_CODPROF, codprof_handler);
   set_handler(F_LINPROF, lingua_handler);
}

TEC_mask::~TEC_mask()
{
}             

void TEC_mask::on_firm_change()
{    
  TMask::on_firm_change();

  _ditta[0].put("CODDITTA", prefix().get_codditta());
  if (_ditta.read() == NOERR)
  {                        
    const int alias = _ditta[LF_ANAG].get_char("TIPOA") == 'F' ? -101 : -102;
    set(F_LUOGOSEND, _ditta.lfile(alias).get("DENCOM"));  
  }    
}

void TEC_mask::start_run()
{
  on_firm_change();
}

const char* TEC_mask::get_prof_base() const
{
  return BASE_EC_PROFILE;
} 

const TString& TEC_mask::get_prof_code() const
{
  return get(F_CODPROF);
} 

const TString& TEC_mask::get_prof_lang() const
{
  return get(F_LINPROF);
}

const char* TEC_mask::get_prof_name() const
{                
  TString& tmp=(TString&)_tmp;
  tmp = get(F_CODPROF);
  tmp << get(F_LINPROF);
  return _tmp;
}

