/* 
SCRIPTS LIXAO 
ESTES SCRIPTS NAO DEVEM MAIS SER UTILIZADOS
O ARQUIVO FUNCOESCOMBO.JS SUBSTITUI ESTAS FUNCOES
*/
var objForm = document.frmFormulario;
var strEspaco = "---";

function preencheComboConsulta()
{
	if (typeof(arrPreencherComboValue) != 'undefined')
	{
		objForm = document.frmFormulario;		
		strEspaco = "---";	
		limparValoresCombo(objForm.cmbConteudo);	
		for (g=0; g<arrPreencherCombo.length; g++)
		{		
			strItemConteudo = arrPreencherCombo[g];
			strValorNivel = recuperarValorNivel(strItemConteudo);
			intEspacoNivel = retornarEspacoNivel(strValorNivel);
			strEspacoNivel = "";
			for (k=1; k<intEspacoNivel; k++)
			{
				strEspacoNivel = strEspaco + strEspacoNivel;			
			}
			objForm.cmbConteudo[objForm.cmbConteudo.length] = new Option (strEspacoNivel + strItemConteudo,arrPreencherComboValue[g]);	
		}
		assinalarNiveis();
		objForm.cmbConteudo.selectedIndex = 0;
	}
}

function preencheCombo()
{
	objForm = document.frmFormulario;		
	strEspaco = "---";	
	limparValoresCombo(objForm.cmbConteudo);	
	for (g=0; g<arrPreencherCombo.length; g++)
	{		
		strItemConteudo = arrPreencherCombo[g];
		strValorNivel = recuperarValorNivel(strItemConteudo);
		intEspacoNivel = retornarEspacoNivel(strValorNivel);
		strEspacoNivel = "";
		for (k=1; k<intEspacoNivel; k++)
		{
			strEspacoNivel = strEspaco + strEspacoNivel;			
		}
		objForm.cmbConteudo[objForm.cmbConteudo.length] = new Option (strEspacoNivel + strItemConteudo,"1");	
	}
	assinalarNiveis();
	objForm.cmbConteudo.selectedIndex = 0;
}


function retornarEspacoNivel(strValorDoItem)
{
	if (strValorDoItem.indexOf(".")<0)
	{
		return 0;
	}
	intCont = 1;	
	while(strValorDoItem.indexOf(".")>=0)
	{
		strValorDoItem = strValorDoItem.substring(strValorDoItem.indexOf(".")+1,strValorDoItem.length);
		intCont++;
	}
	return intCont;
}



function montarArray()
{
	objForm = document.frmFormulario;		
	strEspaco = "---";	
	if (objForm.cmbConteudo.length>0)
	{
		strItemCombo = objForm.cmbConteudo[0].text;
		
		arrNivel = new Array();
		arrInsere = new Array();	
		strRetorno = "";
		
		intNivelAnterior = 0;
		intOrdem = 0;
		
		arrNivel[0] = recuperarValorNivel(strItemCombo);
			
		strValor = trocarTodos(recuperarDescricao(strItemCombo),"'","%\"%");
		strRetorno += "$arrParametros[0] = array('CODCONTEUDO' => '0', 'CODCONTEUDOPAI' => 'null','DSCCONTEUDO' => '"+strValor+"', 'ORDCONTEUDO' => '"+intOrdem+"');";		
		
		for (z=1; z<objForm.cmbConteudo.length; z++)
		{
			strItemCombo = objForm.cmbConteudo[z].text;		
		
			intNivelAtual = retornarEspaco(strItemCombo)
			
			arrNivel[z] = recuperarValorNivel(strItemCombo);		
			intNivelAnterior = recuperarNivelAnteriorConteudo(arrNivel[z],arrNivel);			
			//alert("nivel anterior: "+intNivelAnterior);
			if (intNivelAnterior == -1)
			{
				intNivelAnterior = 'null';	
			}
			intOrdem++;
			strValor = trocarTodos(recuperarDescricao(strItemCombo),"'","%\"%");
			strRetorno += "$arrParametros["+z+"] = array('CODCONTEUDO' => '"+z+"', 'CODCONTEUDOPAI' => '"+intNivelAnterior+"','DSCCONTEUDO' => '"+strValor+"', 'ORDCONTEUDO' => '"+intOrdem+"');";									
			
		}		
		return strRetorno;
	}
	else
	{
		return "";	
	}
}

function recuperarValorNivel(strParametro)
{
	if (strParametro != undefined)
	{
		strParametro = ""+retirarEspaco(strParametro);
		if (strParametro.substring(0,1)==" "){
			strParametro = strParametro.substring(1,strParametro.length);	
		}
		return strParametro.substring(0,strParametro.indexOf(" "));
	}
	return "";
}


function recuperarDescricao(strItemDesc)
{
	strItemDesc = retirarEspaco(strItemDesc);
	return strItemDesc;	
}


function recuperarDescricaoSemIndice(strItemDesc)
{
	strItemDesc = retirarEspaco(strItemDesc);
	while(strItemDesc.indexOf(" ")==0)
	{
		strItemDesc = strItemDesc.substring(strItemDesc.indexOf(" ")+1,strItemDesc.length);
	}
	strItemDesc = strItemDesc.substring(strItemDesc.indexOf(" ")+1,strItemDesc.length);
	return strItemDesc;
}

function recuperarNivelAnteriorConteudo(strItemValor,arrNiveisCombo) {
	strItemValor = strItemValor.substring(0,strItemValor.length-2);
	for (j=0; j<arrNiveisCombo.length; j++)
	{		
		if (arrNiveisCombo[j]==strItemValor)
		{
			return j;	
		}
	}
	return -1;
}


/*
* Adiciona um novo item
*/
function adicionarValor() 
{
	objForm = document.frmFormulario;		
	strEspacoPadrao = "---";	

	strValorNovoItem = prompt("Entre com o valor:","");
	if (strValorNovoItem!=''&strValorNovoItem!=null)
	{
		if (objForm.cmbConteudo.selectedIndex>=0)
		{		
			if (objForm.cmbConteudo.selectedIndex+1 != objForm.cmbConteudo.length)
			{
				objForm.cmbConteudo[objForm.cmbConteudo.length] = new Option("","");	
				for (x=objForm.cmbConteudo.length-2; x>objForm.cmbConteudo.selectedIndex; x--)
				{
					objForm.cmbConteudo[x+1] = new Option(objForm.cmbConteudo[x].text,objForm.cmbConteudo[x].value);				
				}		
			}
			objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex+1] = new Option(" 1 "+strValorNovoItem,"1");
			strItemPai = objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text
			objForm.cmbConteudo.selectedIndex = objForm.cmbConteudo.selectedIndex+1;
			
			if (objForm.cmbConteudo.length>0&objForm.cmbConteudo.selectedIndex!=0)
			{
				intNivelAtual = retornarEspaco(strItemPai)
				identarNivel(intNivelAtual);
			}

				assinalarNiveis();							

		}
		else if (objForm.cmbConteudo.length==0) {
			objForm.cmbConteudo[0] = new Option(" 1 "+strValorNovoItem,"1");											
			objForm.cmbConteudo.selectedIndex = objForm.cmbConteudo.length-1;			
		}
		
	}	
}

/*
* Remove o indice selecionado
*/
function removerValor() 
{	
	if (objForm.cmbConteudo.selectedIndex>=0)
	{
		intIndice = objForm.cmbConteudo.selectedIndex;
		objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex] = null;
		if (intIndice==objForm.cmbConteudo.length) {
			intIndice--;
		}
		objForm.cmbConteudo.selectedIndex = intIndice;				
		assinalarNiveis();		
	}
}


function modificarValor()
{
	objForm = document.frmFormulario;	
	strEspaco = "---";	

	if (objForm.cmbConteudo.selectedIndex>=0)
	{
		
			strItem = objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text
			//Retorna nivel
			strEspacoPadrao = "---";
			var strValorEspaco = "";
			intNivelAtual = retornarEspaco(strItem)
			for (y=0; y<intNivelAtual; y++)
			{
				strValorEspaco = strValorEspaco + strEspacoPadrao;
			}		
			
			if (intNivelAtual==0)
			{
				strResto = strItem.substring(0,strItem.indexOf(" "));
				strItem = strItem.substring(strItem.indexOf(" "),strItem.length);		
			}
			else
			{												
				strResto = strItem.substring(0,strItem.indexOf(" ",strValorEspaco.length+1));				
				strItem = strItem.substring(strValorEspaco.length+1,strItem.length);								
				strItem = strItem.substring(strItem.indexOf(" ")+1,strItem.length);																
			}		

		strTeste = prompt("Entre com o valor:",strItem);
		if (strTeste!=''&strTeste!=null)
		{						
			objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text = strResto+" "+strTeste;
		}
		assinalarNiveis();
	}					
}



function subirNivel(intNivelMove)
{
	objForm = document.frmFormulario;	
	strEspaco = "---";

	if (objForm.cmbConteudo.selectedIndex+intNivelMove>=0 && objForm.cmbConteudo.selectedIndex+intNivelMove<objForm.cmbConteudo.length)
	{
	
		if (objForm.cmbConteudo.selectedIndex>=0)
		{	
			strItem1 = objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex+intNivelMove].text;
			
			
			//Retorna nivel
			strEspacoPadrao = "---";
			var strValorEspaco = "";
			intNivelAtual = retornarEspaco(strItem1)
			for (y=0; y<intNivelAtual; y++)
			{
				strValorEspaco = strValorEspaco + strEspacoPadrao;
			}		
			
			if (intNivelAtual==0)
			{
				strResto1 = strItem1.substring(0,strItem1.indexOf(" "));
				strItem1 = strItem1.substring(strItem1.indexOf(" "),strItem1.length);		
			}
			else
			{
				strResto1 = strItem1.substring(0,strValorEspaco.length+1 + strItem1.indexOf(" "));				
				strItem1 = strItem1.substring(strValorEspaco.length+1,strItem1.length);		
				strItem1 = strItem1.substring(strItem1.indexOf(" "),strItem1.length);			
			}
			
			strItem = objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text
			//Retorna nivel
			strEspacoPadrao = "---";
			var strValorEspaco = "";
			intNivelAtual = retornarEspaco(strItem)
			for (y=0; y<intNivelAtual; y++)
			{
				strValorEspaco = strValorEspaco + strEspacoPadrao;
			}		
			
			if (intNivelAtual==0)
			{
				strResto = strItem.substring(0,strItem.indexOf(" "));
				strItem = strItem.substring(strItem.indexOf(" "),strItem.length);		
			}
			else
			{
				strResto = strItem.substring(0,strValorEspaco.length+1 + strItem.indexOf(" "));				
				strItem = strItem.substring(strValorEspaco.length+1,strItem.length);		
				strItem = strItem.substring(strItem.indexOf(" "),strItem.length);			
			}
				
			objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text = strResto + strItem1;
			objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex+intNivelMove].text = strResto1 + strItem;		
			objForm.cmbConteudo.selectedIndex = objForm.cmbConteudo.selectedIndex+intNivelMove;
		}	
	}
	assinalarNiveis();
}


//Reconstroi o combo atribuindo os niveis
function assinalarNiveis()
{
	objForm = document.frmFormulario;		
	strEspaco = "---";	
	nivel = 0;
	subNivel = 0;

	
	for (x=0; x<objForm.cmbConteudo.length; x++)
	{	
		strItem = objForm.cmbConteudo[x].text;
		if (x==0)
		{
			strItem = retirarEspaco(strItem);
		}
		
		//Retorna nivel
		strEspacoPadrao = "---";
		var strValorEspaco = "";
		intNivelAtual = retornarEspaco(strItem)
		for (y=0; y<intNivelAtual; y++)
		{
			strValorEspaco = strValorEspaco + strEspacoPadrao;
		}		
		
		if (intNivelAtual == 0)
		{
			if (strItem.substring(0,1)==" "){
				strItem = strItem.substring(1,strItem.length);	
			}
			strItem = strItem.substring(strItem.indexOf(" "),strItem.length);		
		}
		else
		{
			strItem = strItem.substring(strValorEspaco.length+1,strItem.length);		
			strItem = strItem.substring(strItem.indexOf(" "),strItem.length);			
		}		
				
		if (intNivelAtual == 0)
		{
			nivel++;
			nivelItem = nivel;
			objForm.cmbConteudo[x].text = nivelItem + strItem;
			nivelAnterior = intNivelAtual;
		}
		else
		{
			if (intNivelAtual == nivelAnterior)
			{
				intUltimoNivel = nivelItem.substring(nivelItem.length-1,nivelItem.length);
				intValor = parseInt(intUltimoNivel)+1;
				nivelItem = nivelItem.substring(0,nivelItem.length-1) +""+ intValor;
			}
			else if(intNivelAtual > nivelAnterior)			
			{
				nivelItem = nivelItem +".1";
			}
			else {			
				nivelItem = retornarValorNivel(nivelItem,intNivelAtual);
			}
			nivelAnterior = intNivelAtual;
			objForm.cmbConteudo[x].text = strValorEspaco +" "+ nivelItem + strItem;											
		}
		
	}
}


//Retorna o valor do nivel selecionado + 1;
function retornarValorNivel(strNivelItem,strNivel)
{
	//strNivelItem = strNivelItem.substring(0,strNivelItem.length-2);
	intContNivel = 0;
	strTemp = strNivelItem;
	intIndice=0;
	while(strNivel>=intContNivel)
	{
		intContNivel++;
		intIndice = strTemp.indexOf(".");
		strTemp = strTemp.replace(".","$");
//		alert(strTemp);
	}
	strNivelItem = strNivelItem.substring(0,intIndice);
//	alert(strNivelItem);

	intUltimoNivel = strNivelItem.substring(strNivelItem.lastIndexOf(".")+1,strNivelItem.length);
	intValor = parseInt(intUltimoNivel)+1;
	strNivelItem = strNivelItem.substring(0,strNivelItem.lastIndexOf(".")+1) +""+ intValor;				
	return strNivelItem;
	//Implementacao antiga	
}

//Retira a sequencia padrao de identacao
function retirarEspaco(strValorRetirarEspaco)
{
	if (strValorRetirarEspaco != undefined)
	{
		while(strValorRetirarEspaco.indexOf(strEspaco)>=0)
		{
			strValorRetirarEspaco = strValorRetirarEspaco.replace(strEspaco,"");
		}				
	}
	return strValorRetirarEspaco;
}


/*
* Retorna a quantidade de espacos usada na identacao
*/
function retornarEspaco(strValorItem)
{	
	intTrocas = 0;
	while(strValorItem.indexOf(strEspaco)>=0)
	{
		strValorItem = strValorItem.replace(strEspaco,"");
		intTrocas++;
	}		
	return intTrocas;
}


function identarNivel(intVezes)
{	
	for(t=0; t<intVezes; t++)
	{
		objForm = document.frmFormulario;	
		strEspaco = "---";	
		strItem = objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text;				
		strItem = strEspaco+strItem;			
		objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text = strItem;		
		assinalarNiveis();
	}
}

 
function identarNivelDireita()
{	
	objForm = document.frmFormulario;	
	strEspaco = "---";
	
	if (objForm.cmbConteudo.selectedIndex-1>=0)
	{
		strItemAnterior = objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex-1].text;		
		strItem = objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text;		
		if (retornarEspaco(strItemAnterior)<retornarEspaco(strItem))
		{
			alert("Não é possivel mover nivel.");
		}
		else
		{	
			strItem = strEspaco+strItem;			
			objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text = strItem;
			assinalarNiveis();
		}
	}
}


function identarNivelEsquerda()
{
	objForm = document.frmFormulario;		
	strEspaco = "---";	
	strValor = objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text;
	if (strValor.indexOf(strEspaco)>=0)
	{		
		objForm.cmbConteudo[objForm.cmbConteudo.selectedIndex].text = strValor.substring(strEspaco.length,strValor.length+1);	
	}
	assinalarNiveis();	
}
 
//Insere um item em uma determinada posicao de um combo
function inserirItemCombo(strTextoAdiciona,strValorAdiciona,intPosicaoAdiciona,objCampoAdiciona)
{				
		objCampoAdiciona[objCampoAdiciona.length] = new Option('','');	
		for (x=(objCampoAdiciona.length-1); x>intPosicaoAdiciona; x--)
		{
			objCampoAdiciona[x] = new Option(objCampoAdiciona[x-1].text,objCampoAdiciona[x-1].value);
		}
		objCampoAdiciona[intPosicaoAdiciona] = new Option(strTextoAdiciona,strValorAdiciona);
}

//Insere um item em uma determinada posicao de um combo
function inserirItemComboNoFinal(strTextoAdiciona,strValorAdiciona,objCampoAdiciona)
{				
		inserirItemCombo(strTextoAdiciona,strValorAdiciona,objCampoAdiciona.length,objCampoAdiciona);
}

function inserirItemComboOrdenado(strTextoAdiciona,strValorAdiciona,objCampoAdiciona,intInicio)
{
	intIndice=objCampoAdiciona.length;
	for (i=intInicio; i < objCampoAdiciona.length; i++)			
	{
		if (strTextoAdiciona.toString() < objCampoAdiciona.options[i].text.toString())
		{
			intIndice = i;
			break;
		}
	}
	inserirItemCombo(strTextoAdiciona,strValorAdiciona,intIndice,objCampoAdiciona);
}


function selecionaValorComboAssociado(objCombo,objComboAssociado)
{
	arrValoresSelecionados = new Array;
	intValoresSelecionados = 0;
	for (x=0; x<objCombo.length; x++)
	{
		if (objCombo[x].selected)
		{
			arrValoresSelecionados[intValoresSelecionados] = objCombo[x].text;
			intValoresSelecionados++;
		}
	}
	
	objComboAssociado.selectedIndex = -1;
	
	for (x=0; x<objComboAssociado.length; x++)
	{
		for (y=0; y<arrValoresSelecionados.length; y++)
		{
			if (objComboAssociado[x].text == arrValoresSelecionados[y])
			{
				objComboAssociado[x].selected = true;
			}
		}
	}
}

