var ___mtabDlg_arr = new Array();

function mtabDlgObj( id, name, cb_name )
  { this.id = id; this.name = name; this.cb_name = cb_name; }

function mtabDlg( form_name, onOk, use_basket, href, caption )
{
  this.choosed = new Array();
  this.fname = form_name;
  this.onOk = new Function(onOk);
  this.caption = caption;
  this.aggr = use_basket;
  this.idx = ___mtabDlg_arr.length;
  ___mtabDlg_arr[ this.idx ] = this;
  this.href = href + '?name=' + form_name + '&index=' + this.idx;
  this.savedChoice = new Array();
  this.sortArr = new Array();
    this.form = document.createElement( "form" );
    //form1.id=this.fname + '_refreshform';
    this.input0 = document.createElement( "input" );
    this.input0.type = 'hidden';
    this.input0.name = this.fname + '_count';
    this.form.appendChild( this.input0 );
    this.input1 = document.createElement( "input" );
    this.input1.type = 'hidden';
    this.input1.name = this.fname + '_step';
    this.form.appendChild( this.input1 );
    this.input2 = document.createElement( "input" );
    this.input2.type = 'hidden';
    this.input2.name = this.fname + '_sortline';
    this.form.appendChild( this.input2 );
}

function ___mtabDlg_StartDlg( ids2hide )
{
  if( !this.box )
  {
    this.box = face.newBox( "action", this.href, this.caption, '___mtabDlg_arr[' + this.idx + '].onOk();'
	         , '___mtabDlg_arr[' + this.idx + '].copyChoosed( 0 );');
    //this.box = face.newBox( "action", this.href, this.caption, 'alert("ok")' , '');
    this.box.user_func = '___mtabDlg_arr[' + this.idx + '].onLoad();';
  }
  this.copyChoosed( 1 );
  this.ids2hide = ids2hide;
//  var add_get = '';
//  if( this.ids2hide ) add_get += '&ids2hide=' + this.ids2hide;
  var str = this.getGet();
  this.box.showWindow( str );
//  this.box.showWindow( add_get );
}
mtabDlg.prototype.startDlg = ___mtabDlg_StartDlg;

function ___mtabDlg_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 ];
}
mtabDlg.prototype.copyChoosed = ___mtabDlg_CopyChoosed;

function ___mtabDlg_Click( cb_name, id, name )
{
  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 );
}
mtabDlg.prototype.click = ___mtabDlg_Click;

function ___mtabDlg_Choose( id, name, cb_name )
{
  if( !this.aggr ) this.choosed.length = 0;
  this.choosed.push( new mtabDlgObj( id, name, cb_name ) );
  if( this.aggr ) { document.getElementById( cb_name ).checked = true; this.refresh(); }
  else { this.box.hideWindow(); this.onOk( this.choosed ); }
}
mtabDlg.prototype.choose = ___mtabDlg_Choose;

function ___mtabDlg_Refresh()
{
  var s, i;
  for( s = '', i = 0; i < this.choosed.length; i++ )
  {
    if( i != 0 ) s += "<br>";
    s+= '<img src="/common_img/del.gif" onclick="___mtabDlg_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>';
}
mtabDlg.prototype.refresh = ___mtabDlg_Refresh;

function ___mtabDlg_UnChoose( choice )
{
  var k;
  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();
}
mtabDlg.prototype.unChoose = ___mtabDlg_UnChoose;
  
function ___mtabDlg_OnLoad()
{
  var el;
  this.countsel = document.getElementById( this.fname + "_countsel" );
  if( this.countsel ) this.input0.value = this.countsel.value;
  for( k = 0; k < this.choosed.length; k++ )
    if( ( el = document.getElementById( this.choosed[ k ].cb_name ) ) )
      el.checked = true;
  this.refresh();
  el = document.getElementById( 'text1' );
  if( el ) el.focused = true;
}
mtabDlg.prototype.onLoad = ___mtabDlg_OnLoad;

function ___mtabDlg_SelResort( el ){
  if( el.value.search( "```" ) >= 0 || el.value.search( "@@@" ) >= 0 )
    { el.value = ''; return; }
//  this.form.elements[0].value = this.countsel.value;
  this.input1.value = 0;
  this.setSortLine( el );
  this.submit();
}
mtabDlg.prototype.selResort = ___mtabDlg_SelResort;

function ___mtabDlg_SetSortLine( el ){
  var str, k;
  if( el )
  {
    for( k = 0; k < this.sortArr.length; k++ )
      if( this.sortArr[k][0] == el.name )
        { this.sortArr[k][1] = el.value; break; }
    if( k == this.sortArr.length )
      this.sortArr.push( [ el.name, el.value ] );
  }
  for( str = '', k = 0; k < this.sortArr.length; k++ )
    if( this.sortArr[k][1] )
    {
      if( str ) str += '@@@';
      str += this.sortArr[k][0] + '```' + this.sortArr[k][1];
    }
  this.input2.value = str;
//  alert(this.input2.value);
}
mtabDlg.prototype.setSortLine = ___mtabDlg_SetSortLine;

function ___mtabDlg_SwitchPageCount()
{
  this.input0.value = this.countsel.value;
  this.input1.value = 0;
  this.submit();
}
mtabDlg.prototype.switchPageCount = ___mtabDlg_SwitchPageCount;

function ___mtabDlg_SwitchPage( num )
{
  this.input1.value = num;
  this.submit();
}
mtabDlg.prototype.switchPage = ___mtabDlg_SwitchPage;

function ___mtabDlg_GetGet()
{
  var str = '&'+this.input0.name+'='+this.input0.value+
            '&'+this.input1.name+'='+this.input1.value+
            '&'+this.input2.name+'='+this.input2.value;
  if( this.ids2hide ) str += '&ids2hide=' + this.ids2hide;
  return str;
}
mtabDlg.prototype.getGet = ___mtabDlg_GetGet;

function ___mtabDlg_Submit()
{
//  var str = '&'+this.input0.name+'='+this.input0.value+
//            '&'+this.input1.name+'='+this.input1.value+
//            '&'+this.input2.name+'='+this.input2.value;
//  if( this.ids2hide ) str += '&ids2hide=' + this.ids2hide;
  var str = this.getGet();
  this.box.refreshWindow( str );
}
mtabDlg.prototype.submit = ___mtabDlg_Submit;

