if( !___listDlg_arr )
  var ___listDlg_arr = new Array();

function listDlgObj( id, name, cb_name )
  { this.id = id; this.name = name; this.cb_name = cb_name; }

function listDlg( form_name, onOk, use_basket, href, caption )
{
  this.choosed = new Array();
  this.fname = form_name;
  this.onOk = onOk ? new Function( onOk ) : this.nothing;
  this.caption = caption;
  this.aggr = use_basket;
  this.idx = ___listDlg_arr.length;
  ___listDlg_arr[ this.idx ] = this;
//  this.href = href + '?name=' + form_name + '&index=' + this.idx;
  this.href = href;
  this.savedChoice = new Array();
  this.sortArr = new Array();
  this.sel_all = false;
  this.sticky_ids = new Array();
}

function ___listDlg_StartDlg( addsearch )
{
  if( !this.box )
  {
    this.box = face.newBox( "action", this.href, this.caption, '___listDlg_arr[' + this.idx + '].onOk();'
	         , '___listDlg_arr[' + this.idx + '].copyChoosed( 0 );');
    this.box.user_func = '___listDlg_arr[' + this.idx + '].onLoad();';
  }
  this.copyChoosed( 1 );
  this.addsearch = addsearch;
//  var add_get = '';
//  if( this.ids2hide ) add_get += '&ids2hide=' + this.ids2hide;
  var str = this.getGet();
  this.box.showWindow( str );
//  this.box.showWindow( add_get );
}
listDlg.prototype.startDlg = ___listDlg_StartDlg;

function ___listDlg_CopyChoosed( dir )
{
  var from = dir ? this.choosed : this.savedChoice;
  var to = !dir ? this.choosed : this.savedChoice;
  var i;
  for( to.length = 0, i = 0; i < from.length; i++ ) to[ i ] = from[ i ];
}
listDlg.prototype.copyChoosed = ___listDlg_CopyChoosed;

function ___listDlg_Click( cb_name, id, name )
{
  if( this.onOk == this.nothing ) return;
  var i;
  for( i = 0; i < this.choosed.length && this.choosed[ i ].id != id; i++ )
    ;
  if( i == this.choosed.length ) this.choose( id, name, cb_name );
  else this.unChoose( i );
}
listDlg.prototype.click = ___listDlg_Click;

function ___listDlg_Choose( id, name, cb_name )
{
  if( !this.aggr ) this.choosed.length = 0;
  this.choosed.push( new listDlgObj( id, name, cb_name ) );
  if( this.aggr ) { document.getElementById( cb_name ).checked = true; this.refresh(); }
  else { this.box.hideWindow(); this.onOk( this.choosed ); }
}
listDlg.prototype.choose = ___listDlg_Choose;

function ___listDlg_Refresh()
{
  var s, i;
  for( s = '', i = 0; i < this.choosed.length; i++ )
  {
    if( i != 0 ) s += "<br>";
    s+= '<img src="/common_img/ico_del.gif" onclick="___listDlg_arr['
      + this.idx + '].unChoose(' + i + ');" style="cursor:pointer" alt = "Удалить из выбранного" title="Удалить из выбранного" > ' 
	  + this.choosed[ i ].name;
  }
  if( !s ) s = 'ничего не выбрано';
  if(document.getElementById( this.fname + '_basket' )!=null)
  	document.getElementById( this.fname + '_basket' ).innerHTML = '<p>' + s + '</p>';
}
listDlg.prototype.refresh = ___listDlg_Refresh;

function ___listDlg_UnChoose( choice )
{
  var k;
  for( k = 0; k < this.sticky_ids.length; k++ )
    if( this.sticky_ids[ k ] == this.choosed[ choice ].id )
	  return;
  var el = document.getElementById( this.choosed[ choice ].cb_name );
  if( el ) el.checked = false;
  for( k = choice; k < this.choosed.length; k++ )
    this.choosed[ k ] = this.choosed[ k + 1 ];
  this.choosed.pop();
  this.refresh();
}
listDlg.prototype.unChoose = ___listDlg_UnChoose;
  
function ___listDlg_OnLoad()
{
  var h_el = document.getElementById( '___href_select_all' );
  if( h_el && this.choosed.length ) h_el.innerHTML = ' ';
  if( this.sel_all )
  {
//    alert('Yeahh!!!');
    var ids_el = document.getElementById( '___sel_all' );
    var arr = ids_el.innerHTML.split( ', ' );
    ids_el.innerHTML = '';
        var ns_el = document.getElementById( '___sel_all_names' );
        if( ns_el ) var a_names = ns_el.innerHTML.split( ', ' );
    this.choosed.length = 0;
    for( k = 0; k < arr.length; k++ )
        {
      var nam = a_names ? a_names[ k ] : 'el' + arr[ k ];
      var cb = a_names ? this.fname + arr[ k ] : 'no_such_cb';
      this.choosed.push( new listDlgObj( arr[ k ], nam, this.fname + arr[ k ] ) );
    }
//alert(1);
//    this.box.box.style.display="none";
//    alert( this.box.box.innerHTML );
//    var fn = new Function( this.box.btn_ok.click );
//        fn();
//    this.box.btn_ok.button.click();
        this.qs = document.getElementById( '___sel_all_qs' ).innerHTML;
        this.onOk();
        if( !this.noclean ) this.choosed.length = 0;
        this.sel_all = false;
        setTimeout( '___listDlg_arr[' + this.idx + '].box.hideWindow();', 0 );
  }
  else
  {
        this.qs = false;
    ___listView_OnLoad();
    var el;
    for( k = 0; k < this.choosed.length; k++ )
      if( ( el = document.getElementById( this.choosed[ k ].cb_name ) ) )
        el.checked = true;
    this.refresh();
  }
//  this.form = document.getElementById( this.fname + '_form' );
}
listDlg.prototype.onLoad = ___listDlg_OnLoad;

listDlg.prototype._addInput = ___listView_AddInput;

listDlg.prototype.selResort = ___listView_SelResort;

listDlg.prototype.setSortArr = ___listView_SetSortArr;

listDlg.prototype.setSortLine = ___listView_SetSortLine;

listDlg.prototype.switchPageCount = ___listView_SwitchPageCount;

listDlg.prototype.switchPage = ___listView_SwitchPage;

listDlg.prototype.setOrder = ___listView_SetOrder;

listDlg.prototype.submitSearch = ___listView_SubmitSearch;

function ___listDlg_SelectAll( noclean )
{
  this.sel_all = !this.choosed.length;
  this.choosed.length = 0;
  this.noclean = !!noclean;
  this.submit();
}
listDlg.prototype.selectAll = ___listDlg_SelectAll;

function ___listDlg_GetGet()
{
  var glue = this.href.match(/\?/) ? '&' : '?'
  var str = glue + 'name=' + this.fname + '&index=' + this.idx;
  if( this.addsearch ) str += '&' + this.addsearch;
  if( this.sel_all ) str += '&sel_all=y';
  if( this.noclean ) str += '&noclean=y';
  if( !this.form ) return str;
  for( i = 0; i < this.form.elements.length; i++ )
    str += '&'+this.form.elements[ i ].name+'='+this.form.elements[ i ].value;
  return str;
}
listDlg.prototype.getGet = ___listDlg_GetGet;

function ___listDlg_Submit()
{
  var str = this.getGet();
//alert(str);
  this.box.refreshWindow( str );
}
listDlg.prototype.submit = ___listDlg_Submit;

function ___listDlg_Nothing() {}
listDlg.prototype.nothing = ___listDlg_Nothing;
