_HT_DICT_INDEX = 0;

_S_DICT_TBL = '';

function HTDict(_p,_l,_t,_w,_h,_nm,_visible) {
 
  if (_w < 410) _w=410;

  this.prototype = new WGT_Window(_p,_l,_t,_w,_h,_nm,_visible);

  //запрос
  var tables = '';
  var fields = new Array();
  var fnames = new Array();
  var where  = '';
  var order  = '';
  //результат
  var max_cnt = 20;
  var max_cnt_s = max_cnt;
  var offs    = 0;
  var fld_cnt = 0;

  var reccount = 0;
  var frec = 0;
  var erec = 0; 

  var filters = new Array();

  var wnd = this.prototype;
  var _this = this;

  var top_panel = new WGT_Panel(wnd,'30px');
  var btnRefresh = new WGT_Control(top_panel.obj,'button','Обновить','','auto','left');
                                                
  var body =  new WGT_Container(wnd);

  var bot_panel = new WGT_Panel(wnd,'30px');
  var btnOk = new WGT_Control(bot_panel.obj,'button','Выбрать','','auto','right');
  var btnCancel = new WGT_Control(bot_panel.obj,'button','Закрыть','','auto','right');
  var sep1 = new WGT_Control(bot_panel.obj,'separator','','','50px','right');
  var btnNext = new WGT_Control(bot_panel.obj,'button','Следующие >','','auto','right');
  var btnAll = new WGT_Control(bot_panel.obj,'button','Все','','auto','right');
  var btnPrev = new WGT_Control(bot_panel.obj,'button','< Предыдущие','','auto','right');

  var active_rec = null;
  var active_rec_color = _CL_WINDOW;

  var ext_ctrls = new Array();
  var ext_ctrls_cnt = 0;

  var wstatus = new WGT_Status(wnd,'25px');

  
  
  ////////////////////////////////////////////////////////////////////////////////

  function MakeSql() {
    var i;
    var s = 'fix all;select ';
    for (i=0;i<fld_cnt;i++){
      s+= (i == 0) ? '' : ',';
      s+=fields[i];
    }
    s+=' from '+tables;
    s+= (where != '') ? ' where '+where : '';
    s+= (order != '') ? ' order by '+order : '';
    s+=';';
    return s;
  }

  


  ////////////////////////////////////////////////////////////////////////////////



  this.GetData = function (_cnt,_offs) {

     var i,_fields='';
     for (i=0;i<fld_cnt;i++){
       _fields+= (i == 0) ? '' : ',';
       _fields+=fields[i];
     }
    
     var j,_filter='',s='',_where='';
     for (j in filters) {
       if (filters[j] != null) {
         s=filters[j].input.GetValue();

         if(s != '') {
             if (_filter != '') _filter+=' and ';
             _filter+=j+' '+filters[j].oper+' ';
             if (filters[j].type == 'string') _filter+='"';
             _filter+=s;
             if (filters[j].oper == 'likecase' || 
                 filters[j].oper == 'like') _filter+='*';
             if (filters[j].type == 'string') _filter+='"';
         }
       }
     }

     //alert(_filter);

     _where=where;
     if (_filter != '')
       if (where != '')
         _where+=' and '+_filter;
       else
         _where+=_filter;
    
     run('dict','window_callback',wnd.id,tables,_fields,_where,order,_cnt,_offs);
  }


  this.prototype.OnDynData = function (_data,_reccount,_f,_e){
    
    reccount=_reccount;
    frec=_f;
    erec=_e;

    if (erec+1 == reccount) btnNext.SetVisible(false); 
    if (frec == 0) btnPrev.SetVisible(false); 

    //for(i in navigator) alert(i+' = '+navigator[i]);

    var i,s='<center><table id="'+wnd.id+'_res_table" width="100%" style="'+_S_DICT_TBL+'" border="1">';
    s+='<thead><tr style="position: relative; background-color:'+_CL_BUTTON+'">';
    for(i=0;i<fld_cnt;i++){
      s+='<td>'+fnames[i]+'</td>';
    }
    s+='</tr></thead><tbody>'+_data+'</tbody></table></center>';

    wnd.SetBody(s);
    
    wnd.SetStatus('Записи с '+(_f+1)+' по '+(_e+1)+' ( всего : '+_reccount+' )');

    _this.tr_set_onmouseup(wnd.id+'_res_table');
  }

  this.Refresh = function (){
    _this.GetData(max_cnt,offs);
  }
  btnRefresh.Set_onmouseup(this.Refresh);

  this.First = function (){
    btnNext.SetVisible(true); 
    offs=0;
    _this.GetData(max_cnt,offs);
  }

  this.Next = function (){

    var _cnt=max_cnt;
    if (reccount > 0) {
      if (erec+1 < reccount) {
        if (offs+max_cnt > reccount) _cnt=reccount-offs;
      }
      else return 0;
    }

    btnPrev.SetVisible(true); 
    offs+=_cnt;
    _this.GetData(_cnt,offs);
  }
  btnNext.Set_onmouseup(this.Next);


  this.Prev = function (){
    btnNext.SetVisible(true); 
    offs-=max_cnt;
    if (offs >= 0)
      _this.GetData(max_cnt,offs);
    else
      offs=0;
  }
  btnPrev.Set_onmouseup(this.Prev);

  this.All = function (){
    if (reccount == 0) return 0;
    if (reccount >= 1000) {
      max_cnt=1000;
      _this.GetData(max_cnt,0);
      return 0;
    };
    btnNext.SetVisible(false);
    btnPrev.SetVisible(false);
    btnAll.SetVisible(false);
    offs=0;
    max_cnt=reccount;
    _this.GetData(reccount,0);
  }
  btnAll.Set_onmouseup(this.All);


  this.tr_set_onmouseup = function(id) {

    var t = document.getElementById(id);
    t.sel_row = null;
    for(i=0;i<t.rows.length;i++){
      t.rows[i].onmouseup = function () {_this.select_row(t,this);};
    }
  }

  this.select_row = function(tab,row) {
    if (row.rowIndex == 0) return 0;
    if (active_rec != row) {
      if (active_rec) active_rec.style.backgroundColor=save_rec_color;
      save_rec_color=row.style.backgroundColor;
      row.style.backgroundColor=_CL_SELECTED;
      active_rec=row;

    }
  }

  this.GetRecCnt = function () { 
    return reccount;
  }

  ////////////////////////////////////////////////////////////////////////////////


  this.SetTable = function (tnm){
    tables=tnm;
  }

  this.SetFields = function (){
    var s='',i;
    fld_cnt = 0;
    
    for (i=0;i<arguments.length;i++) {
      s=arguments[i].split(':');
      if (s[0] != '') {
        fields[fld_cnt] = s[0];
        fnames[fld_cnt] = (s[1] != '') ? s[1] : s[0];
        fld_cnt++;
      } 
    }
  }

  this.SetWhere = function (wh){
    where = wh;
  };

  this.SetOrder = function (or){
    order = or;
  };

  this.SetRecCount = function (_rc){
    max_cnt=_rc;
    max_cnt_s=max_cnt;
  }
    

  this.AddFind = function (_fnm,_tp,_oper,_len){
    var i,ind=-1;
    for(i=0;i<fld_cnt;i++)
      if (fields[i] == _fnm) {
        ind=i;
        break;
      }

    filters[_fnm]=new Object();
    filters[_fnm].type = _tp;
    filters[_fnm].oper = _oper;
    filters[_fnm].sep = new WGT_Control(top_panel.obj,'separator','','','20px','left');
    filters[_fnm].label = new WGT_Control(top_panel.obj,'separator',fnames[ind],'','auto','left');
    filters[_fnm].input = new WGT_Control(top_panel.obj,'input','','',_len+'px','left');
  }

  this.Show = function (){
    max_cnt=max_cnt_s;
    if (active_rec) active_rec.style.backgroundColor=save_rec_color;
    active_rec = null;
    wnd.SetVisible(true);
  }

  this.Hide = function (){
    wnd.SetVisible(false);
  }
  btnCancel.Set_onmouseup(this.Hide);


  //запоминаем поля, в которые надо писать
  this.SetValue = function (){
    var i,j,ind=-1,o=null;
    ext_ctrls_cnt = 0;
    for (i=0;i<arguments.length;i++) {
      s=arguments[i].split(':');
      if (s[0] != '' && s[1] != '') {
        o=document.getElementById(s[0]);
        if (o != null) {
          ind=-1;
          for(j=0;j<fld_cnt;j++)
            if (fnames[j] == s[1]) {
              ind=j;
              break;
            }
          if (ind >= 0) {
            ext_ctrls[ext_ctrls_cnt] = new Object();
            ext_ctrls[ext_ctrls_cnt].o = o;
            ext_ctrls[ext_ctrls_cnt].i = ind;
            ext_ctrls_cnt++;
          }
        }
      } 
    }
    _this.First();
    _this.Show();
    get_sku_cnt(_this.GetRecCnt());
    if(_this.GetRecCnt() == 0)
      btnOk.SetVisible(false);
    else
      btnOk.SetVisible(true);
  }

  //записываем значения в соотв. поля
  this._SetValue = function (_fnm){
    var i;
    if (active_rec != null) {
      for(i=0;i<ext_ctrls_cnt;i++){
        if (ext_ctrls[i] != null &&
            ext_ctrls[i].o != null)
          
          ext_ctrls[i].o.value=active_rec.cells[ext_ctrls[i].i].innerHTML;
      }
    }
    _this.Hide();
    check_inn();
    if(tables == 'SKU')
      GetSKUData();
    else
      GetJJPData();
  }
  btnOk.Set_onmouseup(this._SetValue);

  wnd.SetBtnCloseOnMouseUp(this.Hide);


  
}



