
var _max_sub_id = -1;

function _load_categs( id, tipo )
{
	var id = intval( id );
	var a = new Ajax();
	
	a.onreadystatechange = function()
	{
		var xmlHttp = a.xmlHttp;
		
		if ( xmlHttp.readyState != 4 )
		{
			return false;
		}
		
		var xml = xmlHttp.responseXML;
		var grupo = xml.childNodes.item( xml.childNodes.length - 1 );
		var lista = grupo.childNodes;
		
		// limpando
		if ( ! tipo )
		{
			_limpar_all();
		}
		else
		{
			_limpar( tipo );
		}
		
		// Preencha! já!
		if ( lista.length > 0 )
		{
			_pop_categs( lista, tipo );
		}
		
		if ( ! lista.length && ! tipo )
		{
			_limpar_sub();
		}
	};
	
	if ( id > 0 )
	{
		a.requisitar( 'ajax.php?v='+ id, 'GET' );
	}
	
	if ( ! id && ! tipo )
	{
		_limpar_sub();
		_limpar_all();
	}
}

function _pop_categs( lista, tipo )
{
	var it = '';
	var nTipo = 1;
	var created = 0;
	
	if ( ! tipo )
	{
		it = 'subs';
	}
	
	if ( tipo > 0 )
	{
		it = 'subs_'+ tipo;
		nTipo += tipo;
		_max_sub_id = tipo;
	}
	
	// Hi!
	var obj = document.getElementById( it );
	var div = document.getElementById( 'testes' );
	
	if ( ! obj )
	{
		obj = document.createElement( 'SELECT' );
		
		obj.name = it;
		obj.id = obj.name;
		
		obj.length = 1;
		obj.options[ 0 ].value = 0;
		obj.options[ 0 ].text = 'Escolha';
		obj.disabled = true;
		
		created++;
	}
	
	// Preenchendo...
	obj.length = lista.length + 1;
	obj.onchange = function(){ _load_categs( this.value, nTipo ); };
	obj.style.width = '125px';
	obj.options[ 0 ].selected = true;
	
	for ( var x = 0; x < lista.length; x++ )
	{
		y = x + 1;
		
		e = lista.item( x );
		id = e.attributes.item( 0 ).nodeValue;
		valor = e.firstChild.nodeValue;
		
		obj.options[ y ].value = id;
		obj.options[ y ].text = valor;
	}
	
	obj.disabled = false;
	
	if ( created )
	{
		div.appendChild( obj );
		
		// Quebra de linha
		var _my_br = document.createElement( 'BR' );
		
		_my_br.id = 'subs_br_'+ tipo;
		
		div.appendChild( _my_br );
		
		div.style.display = '';
	}
}

function chk_busca()
{
	var cat = document.getElementById( 'categorias' );
	var cat2 = document.getElementById( 'subs' );
	
	if ( cat.value <= 0 || cat2.value <= 0 )
	{
		alert( 'Selecione uma categoria.' );
		return false;
	}
	
	cat.form.submit();
	return true;
}

function _limpar( i )
{
	var div = document.getElementById( 'testes' );
	var a = div.childNodes;
	var i = intval( i );
	
	if ( _max_sub_id < 1 )
	{
		return;
	}
	
	for ( var x = 1; x <= _max_sub_id; x++ )
	{
		itm = document.getElementById( 'subs_'+ x );
		itm_br = document.getElementById( 'subs_br_'+ x );
		
		if ( ! itm )
		{
			return;
		}
		
		nome = new String( itm.name );
		val = intval( nome.substr( 5, 1 ) );
		
		if ( val > i )
		{
			div.removeChild( itm );
			div.removeChild( itm_br );
		}
	}
	
	_max_sub_id = i;
}

function _limpar_all()
{
	var div = document.getElementById( 'testes' );
	var a = div.childNodes;
	
	while ( a.length > 0 )
	{
		div.removeChild( a.item( 0 ) );
	}
}

function _limpar_sub()
{
	var itm = document.getElementById( 'subs' );
	
	itm.options.length = 1;
	itm.options[ 0 ].value = -1;
	itm.options[ 0 ].text = 'Escolha';
	itm.disabled = 'true';
}

function intval( i )
{
	i = parseInt( i );
	i = ! isNaN( i ) ? i : 0;
	
	return i;
}
