try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

function selecionarCidades(selecionado) {
	var c=document.getElementById('city');
	var e=document.getElementById('hometown').value;
	
	while(c.options.length>0)c.options[0]=null
	c.options[c.options.length] = new Option('Gerando lista, aguarde...','---');
	
	xmlhttp.open("GET", baseurl +'/selecionar_cidades.php?uf='+ e, true);
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			var resultado = unescape(xmlhttp.responseText)
			
			// Atualiza a lista de subgrupos
			var c=document.getElementById('city');
			while(c.options.length>0)c.options[0]=null
			
			
			var linhas = resultado.split("|");
			
			if (linhas.length == 0) {
				c.options[c.options.length] = new Option('Não foi encontrado cidades para o Estado: '+ e, 'N');
			} else {
				//popula o select com a lista de subgrupos obtida
				c.options[c.options.length] = new Option('-- Selecione uma cidade --','---');
				
				for(var i=0;i<(linhas.length-1);i++){
					
					c.options[c.options.length] = new Option(linhas[i],linhas[i]);

					/*
					var valor_linha = linhas[i];
			
					var opcoes = valor_linha.split("@");
					
					alert('Opcao 1: '+ opcoes[0] +'\nOpcao 2: '+ opcoes[1]);
			
					c.options[c.options.length] = new Option(opcoes[1],opcoes[0]);
					*/

					if ((selecionado != 0) && (linhas[0] == selecionado)) {
						var index_selecionado = i;
					}
				}
				
				c.selectedIndex = index_selecionado;
			}
		}
	}
	xmlhttp.send(null)
}
