function testAjax()
{
	try {
	 return new ActiveXObject("Microsoft.XMLHTTP");
	} 
	catch(e) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(ex) {
			try {
				return new XMLHttpRequest();
			}
			catch(exc) {
			   alert("Esse browser não tem recursos para uso do Ajax");
			   return null;
			}
		}
	}
}

function procura(valor,evt,tipo) {
	div = document.getElementById(tipo);
	if(div.className == "display-desativado")
		div.className = "display-ativado";
		
	var charCode = (evt.which) ? evt.which : event.keyCode
	
	if (charCode == 40){
	
		document.getElementById(tipo).focus();
	
	}else{
		
		if(ajax = testAjax()) {
			   
			ajax.open("POST", "../../modulos/produtos/categoria.php", true);
			 
			listaProdutos 	= document.getElementById(tipo);
			 
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			 
			ajax.onreadystatechange = function() {
				 
				if(ajax.readyState == 1) {
					listaProdutos.innerHTML = "Carregando...";
				}
				 
				if(ajax.readyState == 4 ) {
					if(ajax.responseText != "nada") {
						montaSelect(ajax.responseXML,tipo);
					}else
						listaProdutos.innerHTML = "Sem Categorias!";
				}
				
			}
			var params = "valor="+valor+"&procurar"+tipo+"=1";
			ajax.send(params);	 
		}
	}
}

function lista(tipo) {
		div = document.getElementById(tipo);
		if(div.className == "display-ativado"){
			div.className = "display-desativado";			
		}else{
				div.className = "display-ativado";
		if(ajax = testAjax()) {
			   
			ajax.open("POST", "../../modulos/produtos/categoria.php", true);
			 
			listaProdutos 	= document.getElementById(tipo);
			 
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			 
			ajax.onreadystatechange = function() {
				 
				if(ajax.readyState == 1) {
					//listaProdutos.innerHTML = "Carregando...";
				}
				 
				if(ajax.readyState == 4 ) {
					if(ajax.responseText != "nada") {
						montaSelect(ajax.responseXML,tipo);
					}else
						listaProdutos.innerHTML = "Sem Categorias!";
				}
				
			}
			var params = "listar"+tipo+"=1";
			ajax.send(params);
				 
		}
	}
}

//monta o select

function montaSelect(obj,tipo){
	
	var dataArray   = obj.getElementsByTagName("opcao");
	document.forms["produtoProcura"].subgrupos.options.length = 0;
	document.forms["produtoProcura"].grupos.options.length = 0;
	
	if(dataArray.length > 0) {
	 	for(var i = 0; i < (dataArray.length); i++) {
			var item = dataArray[i];
			var valor  		=  item.getElementsByTagName("valor")[0].firstChild.nodeValue;
			var texto 		=  item.getElementsByTagName("texto")[0].firstChild.nodeValue;
			
			if(tipo == "subgrupos"){
				with (document.forms["produtoProcura"].subgrupos) {
				  options[i] = new Option (texto, valor);
				}
			}
			if(tipo == "grupos"){
				with (document.forms["produtoProcura"].grupos) {
				  options[i] = new Option (texto, valor);
				}
			}
		}
	
	}

}

// Muda o Grupo e faz a lista de subgrupos

function muda(input,subselect,valor){
	elemento = document.getElementById(input);
	valor = unescape(valor.replace(/\+/g," "));
	elemento.value = valor;
	
	
	elementoZerado = document.getElementById('subgrupo');
	elementoZerado.value = 'Escolha um sub-grupo';
	
	if(ajax = testAjax()) {
			   
		ajax.open("POST", "../../modulos/produtos/categoria.php", true);
		 
		listaProdutos 	= document.getElementById(subselect);
		 
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		ajax.onreadystatechange = function() {
			 
			if(ajax.readyState == 1) {
				//listaProdutos.innerHTML = "Carregando...";
			}
			 
			if(ajax.readyState == 4 ) {
				if(ajax.responseText != "nada") {
					montaSelect(ajax.responseXML,subselect);
					
				}else
					listaProdutos.innerHTML = "Sem Categorias!";
			}
			
		}
		var params = "listar"+subselect+"=1&codGrupo="+valor;
		ajax.send(params);
	}
}
// só coloca o valor do select no input

function mudaElemento(input,valor){
	elemento = document.getElementById(input);
	valor = unescape(valor.replace(/\+/g," "));
	elemento.value = valor;
}

//mostrar qlq div

function mostraDiv(div){
	
	div = document.getElementById(div);
	if(div.className == "display-ativado")
		div.className = "display-desativado";
			
	else
		div.className = "display-ativado";
	
}

//ao apertar enter (13) ou tab(9)

function onenter(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode == 13 || charCode == 9){
		div = document.getElementById("categorias");
	 	div.className = "display-desativado";
	}
}