function montaCombo(lista, objCombo, vlrPadrao) {
	itens =	lista.split(":");
	if(itens.length < 1) return false;
	
	for(i=0;i<itens.length-1;i++) {
		sttItem = itens[i].toString().split(";");
		itemSelected= false;	
		if(sttItem[0] == vlrPadrao || sttItem[1] == vlrPadrao)
			itemSelected = true;
		
		adicionaItemCombo(objCombo, sttItem[0], sttItem[1],itemSelected);
	}
	
	if(vlrPadrao != "" && vlrPadrao != undefined && document.getElementById("onchange_"+objCombo.id)) {
		eval(document.getElementById("onchange_"+objCombo.id).value);
	}
}

function pesquisa(objCombo, cSql,dftValor, frases, diretorio) {
	removeItemCombo(objCombo);
	adicionaItemCombo(objCombo, " ", "Aguarde..");
	if(diretorio==undefined) diretorio = "";

	$.ajax({
		type: "post",
		dataType: "html",
		data: "sql="+cSql,
		url:  diretorio + "system/combo/pesquisa.php",
		success: function(lista) {
			removeItemCombo(objCombo);
		 	if(frases.indexOf(";;") >=0) {
		 		arrFrases = frases.split(";;");
		 		for(i=0;i<=frases.length-1;i++) {
		 			adicionaItemCombo(objCombo, " " ,arrFrases[i]);
		 		}
		 	} else 			 	
			 	adicionaItemCombo(objCombo, " ",frases);

		 	montaCombo(lista, objCombo, dftValor);

		 	if(document.getElementById('onload_'+objCombo.id)) {
		 		eval(document.getElementById('onload_'+objCombo.id).value);
		 	}
		}
	});

}
function pegaValorSelecionadoCombo(objCombo) {
	if(objCombo.selectedIndex)
		if(objCombo.options) 
			return objCombo.options[objCombo.selectedIndex].value;
		else return false;
	else return objCombo.value;
}

function pegaTextoSelecionadoCombo(objCombo) {
	if(objCombo.selectedIndex)
		return objCombo.options[objCombo.selectedIndex].text;
	else return " ";
}

function adicionaItemCombo(objCombo, valor, descricao,itemSelected) {

	if(descricao==undefined || descricao == "") 
		descricao = " ";
	objOption = document.createElement('option');
		objOption.value = valor;
		objOption.text = descricao;
		
		if(itemSelected)
			objOption.selected="selected";
		
		try {
			objCombo.add(objOption, null);
		} catch(err) {
			objCombo.add(objOption);
		}

}

function verificaSeTemPai(idPP) {
	if(document.getElementById("pai_"+idPP)) {
		if(document.getElementById("pai_"+idPP).value) {
			
			nomePP = document.getElementById("pai_"+idPP).value;
			valorPP = pegaValorSelecionadoCombo(document.getElementById(nomePP));
			if(!valorPP) return "";

			return "&nome_pai[]="+nomePP+"&valor_pai[]="+valorPP+verificaSeTemPai(nomePP);
		}
	}
	return "";
}

function carregaFilhos(idPai, comecarInicio) {
	if(comecarInicio !== true) comecarInicio = false;
	

	if(!document.getElementById("filhos_"+idPai)) {
		objInput = document.createElement('input');
			objInput.value = idPai;
		objHidden = objInput;
	} else {
		objHidden = document.getElementById("filhos_"+idPai);
	}
	objPai = document.getElementById(idPai);
	var autoNumerarHierarquia = "";
	if(document.getElementById('autoNumerrar_'+idPai) && !comecarInicio) {
		autoNumerarHierarquia = "&autoNumerarHierarquia=true";
	}
	
	if(document.getElementById("vlrCombo_"+objHidden.value)) {
		objFilho = document.getElementById("vlrCombo_"+objHidden.value);
	}
	objFilho = document.getElementById("vlrCombo_"+objHidden.value);
	vlrObjPai = pegaValorSelecionadoCombo(objPai);
	vlrObjFilho = objFilho.value;
	
	var strConsulta = "";

	if(objHidden.value != objPai.name) 
		strConsulta = verificaSeTemPai(idPai);
	else if(vlrObjPai.replace(" ", "") != "") {
//		alert("entrou");
//		idPai = idPai+"_pai";
	}
 
	if(vlrObjPai.replace(" ", "") == "" && objHidden.value != objPai.name) {
		removeItemCombo(document.getElementById(objHidden.value));
		adicionaItemCombo(document.getElementById(objHidden.value), " ", "Nenhum item foi encontrado");
		return false;
	}
	
	if(document.getElementById("SqlObj_"+objHidden.value)) {
		objPesquisaSrlzd = document.getElementById("SqlObj_"+objHidden.value).value;


//        alert(document.getElementById("SqlObj_"+objHidden.value).value);
		
		pesquisa(
				document.getElementById(objHidden.value), 
				document.getElementById("SqlObj_"+objHidden.value).value+"&nome_pai[]="+idPai+"&valor_pai[]="+vlrObjPai+strConsulta+autoNumerarHierarquia, 
				vlrObjFilho, "Selecione algum item","../");
				
	}
	
}

function removeItemCombo(objCombo) {
	while(objCombo.length!=0) {
		objCombo.remove(objCombo.selectedIndex);
		for(i=0;i<=objCombo.length-1;i++) {
			objCombo.remove(i);
		}
	}
}

/** Funções do programa de manutenção **/

function processaManutencao(idManutencao) {

	nomeTabela = document.getElementById('tblManutencao_'+idManutencao).value;
	txtListaCampos = document.getElementById('listaCampos_'+idManutencao);
	
	arrCampos = txtListaCampos.value.split(",");
	
	strCadastro = "?idcampo="+idManutencao+"&nometabela="+nomeTabela+"&listaCampos="+txtListaCampos.value;
	
	for(i=0;i<arrCampos.length;i++) {
		vlrCampo = document.getElementById(idManutencao+"_"+arrCampos[i]).value;
		strCadastro = strCadastro + "&"+arrCampos[i]+"="+vlrCampo;
	}
	
	cadastroSimples(idManutencao, strCadastro, document.getElementById('scriptCadastro').value);
}

function cadastroSimples(idManutencao, strParametros, diretorio) {
	document.getElementById("divFormManutencao_"+idManutencao).style.display = "";
	document.getElementById("divAviso_"+idManutencao).style.display = "none";
//	alert('teste');

	if(diretorio==undefined) diretorio = "";
	
	http = getHTTPObject();
		if(http == null) alert("Erro ao carregar propriedade XHTTPRequest de seu browser, favor habilitar esta função");
			url = diretorio + "/system/combo/manutencao_cadastro.php"+strParametros;
			
			http.open('POST', url, true); 
			http.onreadystatechange = retornaLista;
			
			
			function retornaLista() {
				 if(http.readyState == 4) { 
				 	lista = http.responseText; 
				 	objAviso = document.getElementById('divAviso_'+idManutencao);
				 	objAviso.style.display = "";
				 	objAviso.innerHTML = "<span>"+lista+"</span><br /><a href='#'"+'onclick="this.parentNode.style.display='+"'none'; document.getElementById('divFormManutencao_"+idManutencao+"').style.display=''"+'">Adicionar mais um</a>';
			 		document.getElementById("divFormManutencao_"+idManutencao).style.display = "none";
			 		pesquisa(document.getElementById(idManutencao), document.getElementById('SqlObj_'+idManutencao).value, "", "Selecione algum item",  '../');
				 }
			}
	http.send(null);
	
}

function setaValorComboAux(objCombo) {
	vlrSelecionado = pegaValorSelecionadoCombo(objCombo); 
	
	if(document.getElementById('vlrSelecionado_'+objCombo.id).innerHTML.toString().replace(" ", "") != "") {
		document.getElementById('vlrSelecionado_'+objCombo.id).innerHTML+= " -&gt;  " ;	
	}
	
	document.getElementById('vlrSelecionado_'+objCombo.id).innerHTML+= "<a href='#' onclick="+'"'+"limpaValoresAnteriores(this, '"+objCombo.id+"'); adicionaItemCombo(document.getElementById('"+objCombo.id+"'), "+vlrSelecionado+", '&nbsp;', true); carregaFilhos('"+objCombo.id+"');"+'"'+">"+pegaTextoSelecionadoCombo(objCombo)+"</a>";
	document.getElementById('aux_'+objCombo.id).value = vlrSelecionado;

}

function limpaValoresAnteriores(objA, idCombo) {


	arrObjA = document.getElementById('vlrSelecionado_'+idCombo).getElementsByTagName('a');
	var arrRemove = new Array();
	var boolRemove = false;
	for(x=0;x<=arrObjA.length;x++) {
		if(arrObjA[x]) {
			if(boolRemove) {
				arrRemove[arrRemove.length] = arrObjA[x];
			} else if (arrObjA[x].innerHTML == objA.innerHTML) {
				boolRemove = true;
			}
		}
	}
	
	for(i=0;i<arrRemove.length;i++) {
		document.getElementById('vlrSelecionado_'+idCombo).removeChild(arrRemove[i]);
	}
	x = document.getElementById('vlrSelecionado_'+idCombo).innerHTML.toString().indexOf(objA.innerHTML.toString());
	len = document.getElementById('vlrSelecionado_'+idCombo).innerHTML.toString().length;
	objSel = document.getElementById('vlrSelecionado_'+idCombo);
	vlrFinal = objSel.innerHTML.toString().substring(x, len).toString().replace("-&gt;", " ");
	while(vlrFinal.indexOf("-&gt;")>0) {
		vlrFinal = vlrFinal.replace("-&gt;", " ");
	}
	objSel.innerHTML = objSel.innerHTML.toString().substring(0, x) + vlrFinal
}