function SetVisible( id, status )
{
	div = document.getElementById(id);
	if(status)
	{
		div.style.borderwidth = 1;
		div.style.visibility =  'visible' ;
	}
	else
	{
		div.style.borderwidth = 0;
		div.style.visibility =  'hidden' ;
	}
}

function ShowPleaseWait()
{
	for( i = 0; i < 10; i++)
	{
		if( document.getElementById( "BlockForHide_" + i ) != null )
			SetVisible( "BlockForHide_" + i, false );
		if( document.getElementById( "BlockForShow_" + i ) != null )
			SetVisible( "BlockForShow_" + i, true );
	}
	document.form.submit();
}





function OnSubmitClick(L, R, H)
{	
	var str = "";
	for( var i=0; i< L.options.length; i++ )
		L.options[i].selected = false;
	for( var i=0; i< R.options.length; i++ )
		str += R.options[i].value+" ";
	str = str.substring(0,str.length-1);
	H.value = str;
	return true;
}

function OnAddClick( left, right )
{
	for( var i=0; i< left.options.length; i++ )
	{
		if( left.options[ i ].selected )
		{
			var val = left.options[i].value;
			for( var j=0; j < right.options.length; j++ )
				if( right.options[j].value == val ) break;
			if( j == right.options.length )
			{
				var opt = document.createElement("OPTION");
				right.options.add( opt );
				opt.innerText = left.options[i].text;
				opt.value = val;
			}
		}
	}
}

function OnDelClick( left )
{
	for( var i=0; i< left.options.length;  )
	{
		if( left.options[i].selected )
			left.options.remove(i);
		else
			i++;
	}
}

function MoveItem(from,to)
{
  var fromList = document.getElementById(from);
  var toList = document.getElementById(to);	
  var lastSelectedIndex = -1;
 
  for (i=0;i<fromList.options.length;i++)
  {
    var current = fromList.options[i];
    if (current.selected)
    {
      lastSelectedIndex = i;
	  txt = current.text;
      val = current.value;
      toList.options[toList.length] = new Option(txt,val);
      fromList.options[i] = null;
      i--;
    }
  }
  if( lastSelectedIndex !=-1 )
  {
	  if(lastSelectedIndex < fromList.options.length )
 		 fromList.options[ lastSelectedIndex ].selected = true;
  	  else if( lastSelectedIndex - 1 < fromList.options.length )
	 	 fromList.options[ lastSelectedIndex - 1 ].selected = true;
	 }

}

function SetSort( sort, dir )
{
	document.form.sort.value = sort;
	document.form.dir.value = dir;
}

function SetReEntryFlag()
{
	document.form.reentry.value="1";
}


function MakeHref( Href, newfwsid )
{
	document.form.fwsid.value = newfwsid;
	SetReEntryFlag();
	document.form.submit();
}

function DisableSubmitIfCheckboxesReset( CheckBox1, CheckBox2, Button)
{
	var cbStatus1 = document.getElementById( CheckBox1 );
	var cbStatus2 = document.getElementById( CheckBox2 );
	
	control = document.getElementById( Button );
	control.disabled = !cbStatus1.checked && !cbStatus2.checked;
}

function SetControlStatus( CheckBox, c1, c2, c3 )
{
	var cbStatus = document.getElementById( CheckBox );
	var control = 0;
	
	if( c1 != null )
	{ 
		control = document.getElementById( c1 );
		control.disabled = !cbStatus.checked;
	}
	
	if( c2 != null )
	{ 
		control = document.getElementById( c2 );
		control.disabled = !cbStatus.checked;
	}
	
	if( c3 != null )
	{ 
		control = document.getElementById( c3 );
		control.disabled = !cbStatus.checked;
	}
}

// Создает новый элемент с тем же именем 
// и переименовывает старый. Сделано для того чтобы передать значения
// выключеных контролов. Если перед выходом с формы включать сыществующие
// выглядит некрасиво. Проверка выключеных контролов нужня для параметров моделей лицензирования
// недоступных для редактирования.

function EnableControl( elem_id )
{
	var control = document.getElementById( elem_id );
	var form= document.getElementById( "main_form");
	
	var node = document.createElement( "input" );
	node.setAttribute( "type" ,  "hidden" );
	
	node.setAttribute( "name" , control.name );
	
	
	
	if( control.type == "checkbox" )
	{
		node.setAttribute( "value" , control.checked ? "1": "0" );
	}
	else
	{
		if( control.type == "radio" && !control.checked )
			return;
		
		node.setAttribute( "value" , control.value );
	}
		
	form.appendChild( node ); 
	
	control.name = (control.name + "_old");
}

function DisableControl( elem_id )
{
	var control = document.getElementById( elem_id );
	
	control.disabled = true;
}

function OnlyEnableControl( elem_id )
{
	var control = document.getElementById( elem_id );
	
	control.disabled = false;
	
}

function SetRadioButton( elem_id, new_state)
{
	var control = document.getElementById( elem_id );
	control.checked = ( new_state == 1  ? true: false );
	
}


// Используется	для страницы генерации		
function PreSubmitTimeParameters()
{
	EnableControl( 'AKLTCB' );
	EnableControl( 'AKLTMes' );
	EnableControl( 'AKLTData' );
	
	EnableControl( 'SKLTCB' );
	EnableControl( 'SKLTMes' );
	EnableControl( 'SKLTData' );
	
	EnableControl( 'SKDLCB' );
	EnableControl( 'SKDLDay' );
	EnableControl( 'SKDLMonth' );
	EnableControl( 'SKDLYear' );
	
	EnableControl( 'LicenseBirthDateCB' );
	EnableControl( 'LicenseBirthDateDay' );
	EnableControl( 'LicenseBirthDateMonth' );
	EnableControl( 'LicenseBirthDateYear' );
	
	EnableControl( 'ExecutionTimeCB' );
	EnableControl( 'ExecutionTimeData' );
	EnableControl( 'ExecutionTimeMes' );
	
	EnableControl( 'TBACB' );
	EnableControl( 'TBAData' );
	EnableControl( 'TBAMes' );
}

function PreSubmitAdvParameters()
{
	EnableControl( 'HBL_a' );	// hardware binding level
	EnableControl( 'HBL_b' );	// hardware binding level
	EnableControl( 'HBL_c' );	// hardware binding level
	EnableControl( 'HBL_d' );	// hardware binding level
		
	EnableControl( 'MachineCount' );
	EnableControl( 'MachineCountCB' );
	
	EnableControl( 'UserDefinedValue' );
	EnableControl( 'UserDefinedValueCB' );
	
	EnableControl( 'RunCount' );
	EnableControl( 'RunCountCB' );
			
	EnableControl( 'FeatureMaskCB1' );
	EnableControl( 'FeatureMaskCB2' );
	EnableControl( 'FeatureMaskCB4' );
	EnableControl( 'FeatureMaskCB8' );
	EnableControl( 'FeatureMaskCB16' );
	
	if( document.getElementById( 'Left' ) != null )
		OnSubmitClick( this.form.Left, this.form.Right, this.form.Geo );
}
	
function PreSubmit()
{
	EnableControl( 'type' );	// key type - release or test
	//OnSubmitClick( this.form.Left, this.form.Right, this.form.Geo );
	//PreSubmitTimeParameters();
	//PreSubmitAdvParameters();
}

