// JavaScript Document
//var modulos/busca/interna.php = "modulos/busca/interna.phps/busca/interna.php";
function mostrarPreco() {
  var apt = document.getElementById('aptidao').value	
  //executar('modulos/busca/interna.php','comando=mostrarPreco&aptidao='+apt,mostrarPrecoRe)
  include('modulos/busca/show_preco.php','aptidao='+apt,'valor');
}

function mostrarPrecoRe()
{
	if (xhReq.readyState == 4) {
		limpaCombo(document.getElementById('preco'),0);
		addCombo('0','Selecione...',document.getElementById('preco'));
		if (xhReq.responseXML) {
			obj = xhReq.responseXML.getElementsByTagName('dados');
			alert(obj.length);
			for (i=0; i < obj.length; i++) {
				id 		= obj[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				nome 	= unescape(obj[i].getElementsByTagName('nome')[0].firstChild.nodeValue);
				addCombo(id,nome,document.getElementById('preco'));
			}
		}
	}
}
function buscarBai() {
  var cid = document.getElementById('cid').value;	
  var apt = document.getElementById('aptidao').value;	
  executar('modulos/busca/interna.php','comando=buscarBai&cidade='+cid+'&aptidao='+apt,buscarBaiRe)
}

function buscarBaiRe()
{
	if (xhReq.readyState == 4) {
		limpaCombo(document.getElementById('bai'),0);
		addCombo('0','Selecione...',document.getElementById('bai'));
		if (xhReq.responseXML) {
			obj = xhReq.responseXML.getElementsByTagName('dados');
			for (i =0; i < obj.length; i++) {
				id 		= obj[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				nome 	= unescape(obj[i].getElementsByTagName('nome')[0].firstChild.nodeValue);
				addCombo(id,nome,document.getElementById('bai'));
			}
		}
	}
}

function buscarCid() {
  var uf  = document.getElementById('uf').value;
  var apt = document.getElementById('aptidao').value;
  executar('modulos/busca/interna.php','comando=buscarCid&estado='+uf+'&aptidao='+apt,buscarCidRe);
}

function buscarCidRe()
{
	if (xhReq.readyState == 4) {
		limpaCombo(document.getElementById('cid'),0);
		addCombo('0','Selecione...',document.getElementById('cid'));
		if (xhReq.responseXML) {
			obj = xhReq.responseXML.getElementsByTagName('dados');
			for (i =0; i < obj.length; i++) {
				id 		= obj[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				nome 	= unescape(obj[i].getElementsByTagName('nome')[0].firstChild.nodeValue);
				addCombo(id,nome,document.getElementById('cid'));
			}
		}
	}
}

function buscarUf() {
  var apt = document.getElementById('aptidao').value;
  executar('modulos/busca/interna.php','comando=buscarUf&aptidao='+apt,buscarUfRe);
}

function buscarUfRe()
{
	if (xhReq.readyState == 4) {
		limpaCombo(document.getElementById('uf'),0);
		addCombo('0','Selecione...',document.getElementById('uf'));
		if (xhReq.responseXML) {
			obj = xhReq.responseXML.getElementsByTagName('dados');
			for (i =0; i < obj.length; i++) {
				id 		= obj[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				nome 	= unescape(obj[i].getElementsByTagName('nome')[0].firstChild.nodeValue);
				addCombo(id,nome,document.getElementById('uf'));
			}
		}
	}
}

function buscarTipo(apt) {
  executar('modulos/busca/interna.php','comando=buscarTipo&aptidao='+apt,buscarTipoRe)
}

function buscarTipoRe()
{
	if (xhReq.readyState == 4) {
		limpaCombo(document.getElementById('tipo'),0);
		addCombo('0','Selecione...',document.getElementById('tipo'));
		if (xhReq.responseXML) {
			obj = xhReq.responseXML.getElementsByTagName('dados');
			for (i =0; i < obj.length; i++) {
				id 		= obj[i].getElementsByTagName('id')[0].firstChild.nodeValue;
				nome 	= unescape(obj[i].getElementsByTagName('nome')[0].firstChild.nodeValue);
				addCombo(id,nome,document.getElementById('tipo'));
			}
		}
	}
}

function limpaCombo(combo,inicio)
{
var tamanho=combo.options.length;
for (i=tamanho-1;i>=inicio;i--)
	combo.remove(i);
}

function addCombo(valor,texto,combo) {
	var opcao = new Option(texto, valor);
    combo.options[combo.length] = opcao;
}
/*
function buscarCarac(cod) {
  //limparBuffer();
  http.open("GET", "interna.php?comando=buscarCaracteristicas&tipo="+cod, true);
  http.onreadystatechange = handleHttpResponseCar;
  http.send(null);
}
*/

function atender() {
   url = "modulos/atendimento/chat.php";
   window.open(url,"atendimento","width=495, height=428, scrollbars=no");
}

function verificarUsu() {
   executar('modulos/atendimento/verifica_usu.php','',verificarUsuRe);
}

function verificarUsuRe()
{
	if (xhReq.readyState == 4)
	{
		resposta = xhReq.responseXML;
		if (resposta)
		{
			obj = resposta.getElementsByTagName('dados');
			// variaveis do XML
			Xid		= obj[0].getElementsByTagName('id')[0].firstChild;
			Xmsg	= obj[0].getElementsByTagName('msg')[0].firstChild;
			if (Xmsg.nodeValue == '1')
			{
				atender();
			}
			else {
				 var url = "modulos/contatos/contato.form.php";
			     window.open(url,"frame_principal","");
			}
		}
		else { 
		   alert("Falha de xml"); 
		}
	}
}

