function postFamilia(myUrl,myParam,idFam)
{
	var xmlFam = objRequest();
	xmlFam.open("POST", myUrl, true);
	//	xmlFam.open("GET", myUrl, true);
	xmlFam.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
	xmlFam.setRequestHeader("Content-length",myParam.length); // we need to specify the length of the contents
	
	xmlFam.onreadystatechange=function() {
		if (xmlFam.readyState == 1) {
			Aguarde("familia"+idFam);
		}
		if (xmlFam.readyState == 4) { // Wait until everything is fetched!
			var xmlDocFam = xmlFam.responseXML;
			try{
				var indice = buscaFamilia(idFam);
				if(indice == -1) {
					indice = addFamilia(xmlDocFam.getElementsByTagName("familia"),"familia"+idFam);
	
				}
				MontarTabela(indice,idFam);
			} catch(erro){
				
				document.getElementById("familia"+idFam).innerHTML = erro;
			}
	
		}
	}
	xmlFam.send(myParam); // This time, we need to send the text.
}
function postTipo(myUrl,myParam,idFam,idTipo)
{

	var xmlTipo = objRequest();

	xmlTipo.open("POST", myUrl, true);
	//	xmlFam.open("GET", myUrl, true);
	xmlTipo.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
	xmlTipo.setRequestHeader("Content-length",myParam.length); // we need to specify the length of the contents
	
	xmlTipo.onreadystatechange=function() {
		if (xmlTipo.readyState == 1) {
			Aguarde("tipo"+idTipo);
		}
		if (xmlTipo.readyState == 4) { // Wait until everything is fetched!
			var xmlDocFam = xmlTipo.responseXML;
			try{
				var indice = buscaFamilia(idFam);
				if(indice >=0) {
//					indiceTipo = buscaTipo(indice,idTipo);
					
					var indiceTipo = buscaTipo(indice,idTipo);

					var retornoGrp = Lergrupos(indice,indiceTipo,xmlDocFam.getElementsByTagName("grupo"));
					
					MontarTabelaGrupo(indice,idFam,indiceTipo,idTipo);
				} else {
					document.getElementById.innerHTML ="";
					document.getElementById.appendChild(document.createTextNode("Família não foi encontrada."));
				}
			} catch(erro){
				
				document.getElementById("tipo"+idTipo).appendChild(document.createTextNode("Grupo de produtos não encontrado."));
			}
	
		}
	}
	xmlTipo.send(myParam); // This time, we need to send the text.
}

function EnviaDados(myUrl,myParam,idFam,idTipo) 
{
	var indice = buscaFamilia(idFam);

	if(indice <0) {
		postFamilia(myUrl,myParam,idFam);
	} else {
		if(idTipo !=0)
		{
			var indiceTipo = buscaTipo(indice,idTipo);

			if(arrFamilia[indice].tipos[indiceTipo].grupos.length == 0)
			{
				postTipo(myUrl,myParam,idFam,idTipo);
			} else {
				MontarTabelaGrupo(indice,idFam,indiceTipo,idTipo);
			}
		} else {
			MontarTabela(indice,idFam);
		}
		
	}
	

//	xmlFam.send(null);
}
var arrFamilia = new Array();

function addFamilia(fam,nomeDiv)
{
	var elemDiv = document.getElementById(nomeDiv);
	var mensagem = false;
	try{
		arrFamilia.push(new familia(fam[0].getElementsByTagName("codFam")[0].firstChild.nodeValue,"N"));
		LerTipo((arrFamilia.length-1),fam[0].getElementsByTagName("tipo"));

		return (arrFamilia.length-1);
	} catch(e) {
		 return -1;
	}
}
function LerTipo(indice,tp)
{

	for(n=0;n<tp.length;n++)
	{

			arrFamilia[indice].tipos.push(new objTipo(tp[n].getElementsByTagName("codTipo")[0].firstChild.nodeValue,tp[n].getElementsByTagName("nomeTipo")[0].firstChild.nodeValue,"N"));
//			Lergrupos(indice,tp[n].getElementsByTagName("grupo"));
	}

}
function Lergrupos(indiceFam,indiceTipo,grp)
{
	for(z=0;z<grp.length;z++)
	{
		try{
			arrFamilia[indiceFam].tipos[indiceTipo].grupos.push(new objGrupo(grp[z].getElementsByTagName("codGrupo")[0].firstChild.nodeValue,grp[z].getElementsByTagName("nomeGrupo")[0].firstChild.nodeValue));
		} catch(e) {
			return "No momento não há produtos.";
		}
	}
}
function openfamilia(id)
{
	if(id == "")
		return;
	
	var param = "idfam="+id;
	EnviaDados("xml/xml_tipo.asp",param,id,0);
	
}
function opentipo(fam,idtipo)
{
	if(fam == "" || idtipo == "")
		return;
	
	var param = "idfam="+fam;
	param+= "&idtipo="+idtipo;
	EnviaDados("xml/xml_grupo.asp",param,fam,idtipo);
}
function buscaFamilia(id)
{
	
	for(i=0;i<arrFamilia.length;i++)
	{
		if(arrFamilia[i].getCod() == id){
			break;
		}
	}
	retorno =  (i<arrFamilia.length) ? i:-1;
	return retorno;
}
function buscaTipo(indiceFam,id)
{
	
	for(i=0;i<arrFamilia[indiceFam].tipos.length;i++)
	{
		if(arrFamilia[indiceFam].tipos[i].getCod() == id){
			break;
		}
	}
	retorno =  (i<arrFamilia[indiceFam].tipos.length) ? i:-1;
	return retorno;
}
var imgSeta = new Image();
imgSeta.src = "/images/setaBaixoMenor.gif";

function MontarTabela(indice,idFam)
{

	if(indice <0)
		indice = buscaFamilia(idFam);
	if(arrFamilia[indice].aberto == "N")
	{
		document.getElementById("familia"+idFam).innerHTML = "";
		var newEl = document.createElement('TABLE');
		newEl.setAttribute('cellPadding',0);
		newEl.setAttribute('width',"100%");
		var tmp = document.createElement('TBODY');
		
		newEl.appendChild(tmp);
	
		for(i=0;i<arrFamilia[indice].tipos.length;i++)
		{
	
			var row = document.createElement('TR');
			var col = document.createElement('TD');
			var lk  = document.createElement('a');
			var camada = document.createElement("DIV");
			var img = new Image();
			img.src = imgSeta.src;
			img.border=  0;
			camada.id = "tipo"+arrFamilia[indice].tipos[i].getCod();
			
			row.setAttribute("className","TextoTipo");
			row.setAttribute("class","TextoTipo");
			
			col.appendChild(img);
			col.appendChild(document.createTextNode(" "));
			lk.appendChild(document.createTextNode(arrFamilia[indice].tipos[i].getNome()));
			lk.href="javascript:opentipo('"+arrFamilia[indice].getCod()+"','"+arrFamilia[indice].tipos[i].getCod()+"')";
			

			col.appendChild(lk);
			col.appendChild(camada);
			row.appendChild(col);
			
			tmp.appendChild(row);
		}
		document.getElementById("familia"+idFam).style.display="";	
		document.getElementById("familia"+idFam).appendChild(newEl);
		arrFamilia[indice].aberto = "S";
	}else{
		document.getElementById("familia"+idFam).innerHTML = "";
		document.getElementById("familia"+idFam).style.display="none";
		arrFamilia[indice].aberto = "N";
		
		for(i=0;i<arrFamilia[indice].tipos.length;i++)
			arrFamilia[indice].tipos[i].aberto = "N";

	}
}
var imgQua = new Image();
imgQua.src = "/images/quadrado.gif";

function MontarTabelaGrupo(indice,idFam,indiceTipo,idTipo)
{

	if(indice <0)
		indice = buscaFamilia(idFam);

	if(indiceTipo <0)
		indiceTipo = buscaTipo(indice,idTipo);
		
	if(arrFamilia[indice].tipos[indiceTipo].aberto == "N")
	{
		document.getElementById("tipo"+idTipo).innerHTML = "";
		var newEl = document.createElement('TABLE');
		newEl.setAttribute('cellPadding',0);
		newEl.setAttribute('width',"100%");
		var tmp = document.createElement('TBODY');
		
		newEl.appendChild(tmp);
		
		if(arrFamilia[indice].tipos[indiceTipo].grupos.length ==0)
		{
			var row = document.createElement('TR');
			var col = document.createElement('TD');
			var img = new Image();
			img.src = imgQua.src;
			img.border=  0;
			img.align="absmiddle";
			
			col.appendChild(img);
			col.appendChild(document.createTextNode(" "));
			
			row.setAttribute("className","TextoGrupo");
			row.setAttribute("class","TextoGrupo");
			
			col.appendChild(document.createTextNode("No momento não há produtos neste item."));
			row.appendChild(col);
			
			tmp.appendChild(row);
		
		} else {
			for(i=0;i<arrFamilia[indice].tipos[indiceTipo].grupos.length;i++)
			{
		
				var row = document.createElement('TR');
				var col = document.createElement('TD');
				var lk  = document.createElement('a');
				var img = new Image();
				img.src = imgQua.src;
				img.border=  0;
				img.align="absmiddle";
				
				col.appendChild(img);
				col.appendChild(document.createTextNode(" "));
				
				lk.setAttribute("className","TextoGrupo");
				lk.setAttribute("class","TextoGrupo");
	
				row.setAttribute("className","TextoGrupo");
				row.setAttribute("class","TextoGrupo");
				
				lk.appendChild(document.createTextNode(arrFamilia[indice].tipos[indiceTipo].grupos[i].getNome()));
				lk.href="escolheproduto_cci.asp?Familia="+idFam+"&Tipo="+idTipo+"&Grupo="+arrFamilia[indice].tipos[indiceTipo].grupos[i].getCod();
				col.appendChild(lk);
				row.appendChild(col);
				
				tmp.appendChild(row);
			}
		
		}
		
		document.getElementById("tipo"+idTipo).style.display="";	
		document.getElementById("tipo"+idTipo).appendChild(newEl);
		arrFamilia[indice].tipos[indiceTipo].aberto = "S";
	}else{
		document.getElementById("tipo"+idTipo).innerHTML = "";
		document.getElementById("tipo"+idTipo).style.display="none";	
		arrFamilia[indice].tipos[indiceTipo].aberto = "N";
	}
}

// funcao para abertura dos itens quando já escolhido
function reabrir(idFam,idtipo)
{
	postFamiliaReabrir("xml/xml_tipo.asp","idfam="+idFam,idFam,idtipo);
}

function postFamiliaReabrir(myUrl,myParam,idFam,idTipo)
{
	var xmlFam = objRequest();
	xmlFam.open("POST", myUrl, true);
	//	xmlFam.open("GET", myUrl, true);
	xmlFam.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
	xmlFam.setRequestHeader("Content-length",myParam.length); // we need to specify the length of the contents
	
	xmlFam.onreadystatechange=function() {
		if (xmlFam.readyState == 1) {
			Aguarde("familia"+idFam);
		}
		if (xmlFam.readyState == 4) { // Wait until everything is fetched!
			var xmlDocFam = xmlFam.responseXML;
			try{
				var indice = buscaFamilia(idFam);
				if(indice == -1) {
					indice = addFamilia(xmlDocFam.getElementsByTagName("familia"),"familia"+idFam);
				}
				MontarTabela(indice,idFam);
				if(idTipo!= "0")
					postTipoReabrir("xml/xml_grupo.asp","idfam="+idFam+"&idtipo="+idTipo,idFam,idTipo);
			} catch(erro){
				document.getElementById("familia"+idFam).innerHTML = erro;
			}
		}
	}
	xmlFam.send(myParam); // This time, we need to send the text.
}
function postTipoReabrir(myUrl,myParam,idFam,idTipo)
{

	var xmlTipo = objRequest();
	xmlTipo.open("POST", myUrl, true);
	//	xmlFam.open("GET", myUrl, true);
	xmlTipo.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
	xmlTipo.setRequestHeader("Content-length",myParam.length); // we need to specify the length of the contents
	
	xmlTipo.onreadystatechange=function() {
		if (xmlTipo.readyState == 1) {
			Aguarde("tipo"+idTipo);
		}
		if (xmlTipo.readyState == 4) { // Wait until everything is fetched!
			var xmlDocFam = xmlTipo.responseXML;
			try{
				var indice = buscaFamilia(idFam);
				
				if(indice >=0) {
					indiceTipo = buscaTipo(indice,idTipo);
					var indiceTipo = buscaTipo(indice,idTipo);
					var retornoGrp = Lergrupos(indice,indiceTipo,xmlDocFam.getElementsByTagName("grupo"));
					
					MontarTabelaGrupo(indice,idFam,indiceTipo,idTipo);
				} else {
					document.getElementById.innerHTML ="";
					document.getElementById.appendChild(document.createTextNode("Família não foi encontrada."));
				}
			} catch(erro){
				
				document.getElementById("tipo"+idTipo).appendChild(document.createTextNode("Grupo de produtos não encontrado."));
			}
	
		}
	}
	xmlTipo.send(myParam); // This time, we need to send the text.
}