function iniciaAjax()
{
    Ajax = false;
    if ( window.XMLHttpRequest )
        Ajax = new XMLHttpRequest();
    else if ( window.ActiveXObject )
    {
        try {
            Ajax = new ActiveXObject("Msxm12.XMLHTTP");
        } catch (e) {
            try {
            Ajax = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    else
        alert("Não foi possivel iniciar o Ajax!")
}

function buscaClientes(form)
{
	var categoria = form.f_categoria_guia.value;
	iniciaAjax();
	
	if ( !Ajax )
	{
		alert("Não foi possivel iniciar o Ajax!");
		return;
	}
	
	Ajax.onreadystatechange = function(){mostraClientes(form)};
	
	if ( form.f_server.value == '/guia/index.php' )
		Ajax.open('GET','./xml/xml_clientes.php?categoria='+categoria,true);
	else
		Ajax.open('GET','./guia/xml/xml_clientes.php?categoria='+categoria,true);
	
	Ajax.send(null);
}

function mostraClientes(form)
{
	if ( Ajax.readyState == 4 )
	{
		if ( Ajax.status == 200 )
		{
			var xmlClientes = Ajax.responseXML;
			var sel 		= form.f_cliente;
			var sel2		= form.f_categoria_guia;
			
			if ( xmlClientes.hasChildNodes() )
			{
				var codigo		= xmlClientes.getElementsByTagName('codigo');
				var nome		= xmlClientes.getElementsByTagName('nome');

				limpa(sel);
				sel.disabled = false;
				sel.options[sel.options.length] = new Option("Selecione","null",true,false);

				for ( i=0; i<nome.length; i++ )
				{
					var codigo2 = codigo[i].childNodes;
					var nome2 	= nome[i].childNodes;
					
		 			sel.options[sel.options.length] = new Option(nome2[0].nodeValue,codigo2[0].nodeValue);
				}
			}
		}
		else
			alert("Erro: "+Ajax.statusText)
	}
}

function limpa(ob)
{
    if ( ob.hasChildNodes())
    {
        var nos = ob.childNodes;

        while (nos.length > 0)
        {
            var no = nos[nos.length-1];
            ob.removeChild(no);
        }
    }
}