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 buscaCidades(form)
{
	iniciaAjax();

	var estado = form.f_estado.value;
	
	if ( !Ajax )
	{
		alert("Não foi possivel iniciar o Ajax!");
		return;
	}
	else
	{
		Ajax.onreadystatechange = function(){mostraCidades(form)};
		
		if ( form.name.substring(4,7) != 'adm' )
			Ajax.open('GET','./xml/xml_cidades.php?estado='+estado,true);
		else
			Ajax.open('GET','../xml/xml_cidades.php?estado='+estado,true);
		
		Ajax.send(null);
	}
}

function mostraCidades(form)
{
	if ( Ajax.readyState == 4 )
	{
		if ( Ajax.status == 200 )
		{
			var xmlCidade = Ajax.responseXML;
			var sel 	= form.f_cidade;
			var sel2	= form.f_estado;
			var sel3	= form.f_cliente;
			var codigo	= xmlCidade.getElementsByTagName('codigo');
			var nome	= xmlCidade.getElementsByTagName('nome');
			
			if ( form.name.substring(4,7) == 'adm' )
				var vcidade	 = form.f_vcidade.value;
			else
				var vcidade = 'Selecione'; 
					
			limpa(sel);
			sel.disabled = false;
			sel.options[sel.options.length] = new Option("Selecione","Selecione",true,false);
			
			if ( xmlCidade.hasChildNodes() )
			{
				for ( i=0; i<nome.length; i++ )
				{
					var codigo2 = codigo[i].childNodes;
					var nome2 	= nome[i].childNodes;
					
					if ( codigo2[0].nodeValue == vcidade )
						sel.options[sel.options.length] = new Option(nome2[0].nodeValue,codigo2[0].nodeValue,false,true);
					else
			 			sel.options[sel.options.length] = new Option(nome2[0].nodeValue,codigo2[0].nodeValue);
						
				}
			}
			
			//if ( sel2.value == 'Selecione' )
//			{
//				sel.selectedIndex = 'Selecione';
//				sel.disabled = true;
//			}				
			
			if ( form.name.substring(4,7) != 'adm' ) 
				buscaGaragens(form);			
		}
		//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);
        }
    }
}