
// ----------------------------------------------------------------
// trademark class

function TradeMark( id, name, country_id, materials )
{

	this.id = id;
	this.name = name;
	this.country_id = country_id;
	this.materials = materials;

	this.div = document.getElementById('t_' + this.id );
	this.input = document.getElementById('t_i_' + this.id );
	this.show = false;
	
}

TradeMark.prototype.ContainsMaterials = function ( in_materials )
{
	for (var i = 0; i < this.materials.length; i++ )
		for (var j = 0; j < in_materials.length; j++ )
			if ( in_materials[j] == this.materials[i] )
				return true;

	return false;
}

TradeMark.prototype.ApplyMaterials = function ( in_materials )
{
	if ( this.ContainsMaterials( in_materials ) )
	{	
		this.show = true;
		this.div.style.display = 'block';
	}
	else
	{
		this.show = false;
		this.input.checked = false;
		this.div.style.display = 'none';
	}
}

TradeMark.prototype.Checked = function ()
{
	return this.show && this.input.checked;
}

TradeMark.prototype.Check = function( val )
{
	if ( this.show )
		this.input.checked = val;
	else
		this.input.checked = false;
}


// ----------------------------------------------------------------
// country class

function Country( id, name, trademarks )
{
	this.id = id;
	this.Expanded = false;
	
	this.div = document.getElementById('c_' + this.id );
	this.input = document.getElementById('c_i_' + this.id );
	this.a = document.getElementById('c_a_' + this.id );
	this.div_list = document.getElementById('c_l_' + this.id );
	this.div_names = document.getElementById('c_n_' + this.id );;

	this.name = name;
	this.trademarks = trademarks;

}

Country.prototype.GenerateNames = function()
{
	names = new Array();
	for (var i = 0; i < this.trademarks.length ; i++ )
		if ( this.trademarks[i].Checked() )
			names.push( this.trademarks[i].name );

	if ( names.length == 0)
	{
		this.div_names.innerText = '';		
		this.div_names.style.display = 'none';
	}
	else
	{
		this.div_names.innerText = names.join(', ');
	}
}

Country.prototype.Expand = function ( val )
{

	if ( val )
	{
		this.div_list.style.display = 'block';
		this.div_names.style.display = 'none';
		this.input.style.display = 'inline';
		this.Expanded = true;
		//document.getElementById( 'catalog_table_what').height = document.getElementById( 'catalog_div_T' ).height;
	}
	else
	{
		this.GenerateNames();
		this.div_names.style.display = 'block';
		this.div_list.style.display = 'none';
		this.input.style.display = 'none';
		this.Expanded = false;
	}
}

Country.prototype.CheckAll = function ( )
{
	for (var i = 0; i < this.trademarks.length ; i++ )
		this.trademarks[i].Check( this.input.checked );

	this.GenerateNames();

}

Country.prototype.ApplyMaterials = function ( materials )
{
	mstatus = false;
	for(var  i = 0; i < this.trademarks.length; i++ )
	{
		this.trademarks[i].ApplyMaterials( materials );
		mstatus |= this.trademarks[i].show;
	}
	
	if ( mstatus )
	{
		this.div.style.display = 'block';
		this.GenerateNames();
	}
	else
	{
		this.div.style.display = 'none';
	}		

}

// ----------------------------------------------------------------
// control class

function TrademarksControl( div, form, countries )
{	
	this.div = div;
	this.form = form;
	this.countries = countries;
}

TrademarksControl.prototype.Show = function ( val )
{
	if ( !val )
		this.div.style.display = 'none';
	else
		this.div.style.display = 'block';
}


TrademarksControl.prototype.Refresh = function ( )
{

	arr = new Array();
	for(var i = 0; i < 10; i++ )
		if (this.form['type_'+i])
			if ( this.form['type_'+i].checked )
				arr.push(i);

	for (var i = 0; i < this.countries.length; i++ )
		this.countries[i].ApplyMaterials( arr );
		
}

TrademarksControl.prototype.SwitchMode = function ( Mode )
{

	if ( Mode == 'T' )
	{
		document.getElementById( 'catalog_div_T' ).style.display = 'block';
		document.getElementById( 'catalog_div_F' ).style.display = 'none';
		document.getElementById( 'trademark_control' ).style.display = 'block';
		document.getElementById( 'target_control' ).style.display = 'none';
	}
	else
	{
		document.getElementById( 'catalog_div_T' ).style.display = 'none';
		document.getElementById( 'catalog_div_F' ).style.display = 'block';
		document.getElementById( 'trademark_control' ).style.display = 'none';
		document.getElementById( 'target_control' ).style.display = 'block';
	}

}

// -------------------------------------------
function CheckAllCountry( id )
{
	for ( var i = 0; i < control.countries.length; i++ )
		if ( control.countries[i].id == id )
			control.countries[i].CheckAll();
}

//
function ExpandCountry( id )
{
	for ( var i = 0; i < control.countries.length; i++ )
		if ( control.countries[i].id == id )
			control.countries[i].Expand( !control.countries[i].Expanded );
}

//
function HideCountry( id )
{
	for ( var i = 0; i < control.countries.length; i++ )
		if ( control.countries[i].id == id )
			control.countries[i].Expand( false );
}