var arrCatTech = new Array();

function openCatTech(id,tahlogado)
{
	if(id == "")
		return;
	
	var param = "idcattech="+id;
	EnviaDados("/v20/paginas/xml/xml_tech_artigos.asp",param,id,tahlogado);
	
}

function EnviaDados(myUrl,myParam,id,tahlogado) 
{
	var indice = buscaCatTech(id);

	if(indice <0) {
		postCatTech(myUrl,myParam,id,tahlogado);
	} else {
			MontarTabela(indice,id,tahlogado);
		
	}
	

//	xmlFam.send(null);
}

function buscaCatTech(id)
{
	
	for(i=0;i<arrCatTech.length;i++)
	{
		if(arrCatTech[i].getCod() == id){
			break;
		}
	}
	retorno =  (i<arrCatTech.length) ? i:-1;
	return retorno;
}



function postCatTech(myUrl,myParam,id,tahlogado)
{
	var xmlCatTech = objRequest();
	xmlCatTech.open("POST", myUrl, true);
	//	xmlFam.open("GET", myUrl, true);
	xmlCatTech.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // sending it as encoded formdata
	xmlCatTech.setRequestHeader("Content-length",myParam.length); // we need to specify the length of the contents
	
	xmlCatTech.onreadystatechange=function() {
		if (xmlCatTech.readyState == 1) {
			Aguarde("CatTech"+id);
		}
		if (xmlCatTech.readyState == 4) { // Wait until everything is fetched!
			var xmlDocCatTech = xmlCatTech.responseXML;
			try{
				var indice = buscaCatTech(id);
				if(indice == -1) {
					indice = addCatTech(xmlDocCatTech.getElementsByTagName("cat_tech"),"CatTech"+id);
	
				}
				MontarTabela(indice,id,tahlogado);
			} catch(erro){
				
				document.getElementById("CatTech"+id).innerHTML = erro;
			}
	
		}
	}
	xmlCatTech.send(myParam); // This time, we need to send the text.
}


function addCatTech(ctech,nomeDiv)
{
	var elemDiv = document.getElementById(nomeDiv);
	var mensagem = false;
	try{
		arrCatTech.push(new CatTech(ctech[0].getElementsByTagName("codcat_tech")[0].firstChild.nodeValue,"N"));
		LerTech((arrCatTech.length-1),ctech[0].getElementsByTagName("tech"));

		return (arrCatTech.length-1);
	} catch(e) {
		 return -1;
	}
}
function LerTech(indice,tch)
{

	for(n=0;n<tch.length;n++)
	{

			arrCatTech[indice].tech.push(new objTech(tch[n].getElementsByTagName("codTech")[0].firstChild.nodeValue,tch[n].getElementsByTagName("nomeTech")[0].firstChild.nodeValue,"N"));
//			Lergrupos(indice,tp[n].getElementsByTagName("grupo"));
	}

}

function MontarTabela(indice,id,tahlogado)
{

	if(indice <0)
		indice = buscaCatTech(id);
	if(arrCatTech[indice].aberto == "N")
	{
		document.getElementById("CatTech"+id).innerHTML = "";
	
		for(i=0;i<arrCatTech[indice].tech.length;i++)
		{
			var newEl = document.createElement('TABLE');
			newEl.setAttribute('cellPadding',1);
			newEl.setAttribute('cellSpacing',1);
			newEl.setAttribute('border',1);
			newEl.setAttribute('width',"98%");
			newEl.setAttribute("borderColor","#FFFFFF");
			var tmp = document.createElement('TBODY');
			
			newEl.appendChild(tmp);

			var row = document.createElement('TR');
			var col1 = document.createElement('TD');
			var col2 = document.createElement('TD');
			var col = document.createElement('TD');
			var lk  = document.createElement('a');
			
			
			row.setAttribute("borderColor","#FFFFFF");
			col1.setAttribute("borderColor","#FFFFFF");
			col1.setAttribute("width","2%");
			col2.setAttribute("width","1%");
			col2.setAttribute("bgColor","#0411FF");
			col2.setAttribute("className","style31");
			col2.setAttribute("class","style31");
			col2.appendChild(document.createTextNode("."));
			col.setAttribute("colSpan","2");
			col.setAttribute("width","97%");
			col.setAttribute("borderColor","#0411FF");
			var img = new Image();
			img.border = 0;
			img.width = 0;
			img.height = 0;
			img.name = "CatTechImg"+id;
			img.id = "CatTechImg"+id;
			
			lk.appendChild(img);
			lk.appendChild(document.createTextNode(arrCatTech[indice].tech[i].getNome()));
			if (tahlogado != '' ) {
				lk.href="javascript:viewTech('"+arrCatTech[indice].tech[i].getCod()+"')";
			} else {
				lk.href="javascript:SwapLoginBox('/v20/default_login.asp?tipologin=2','"+arrCatTech[indice].tech[i].getCod()+"','CatTechImg"+id+"',2)";
			}
			lk.setAttribute("className","style12");
			lk.setAttribute("class","style12");			

			col.appendChild(lk);
			row.appendChild(col1);
			row.appendChild(col2);
			row.appendChild(col);
			tmp.appendChild(row);
			document.getElementById("CatTech"+id).appendChild(newEl);
			
		}
		document.getElementById("CatTech"+id).style.display="";	
		//document.getElementById("familia"+idFam).appendChild(newEl);
		arrCatTech[indice].aberto = "S";
	}else{
		document.getElementById("CatTech"+id).innerHTML = "";
		document.getElementById("CatTech"+id).style.display="none";
		arrCatTech[indice].aberto = "N";
		
		for(i=0;i<arrCatTech[indice].tech.length;i++)
			arrCatTech[indice].tech[i].aberto = "N";

	}
}
var imgQua = new Image();
imgQua.src = "/v20/images/quadrado.gif";


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,tahlogado);
				if(idTipo!= "0")
					postTipoReabrir("/v20/paginas/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.
}

function viewTech(codtech) {
	
	window.open ('/v20/paginas/main_techsupport_view.asp?codtech='+codtech,'_blank');

}
