var buscando=0, idselectedcity = 0;
function buscarSelectCidade(idCidadeSelected,ide,idc)
{
	if(buscando == 0)
	{
		buscarSelectCidadeAjax(idCidadeSelected,ide,idc);
	}
	else
	{
		setTimeout("buscarSelectCidade('"+idCidadeSelected+"','"+ide+"','"+idc+"')","100");
	}
}

function buscarSelectCidadeAjax(idCidadeSelected,ide,idc)
{
	var idestado='';
	if(!ide) ide = 'estado';
	if(!idc) idc = 'cidade';
	if(document.getElementById(ide).value)
	{
	
	if(buscando == 0)
	{
		selectreturn = idc;
		buscando = 1;
		createObjectAjax();
		dateobject = new Date(); //parâmetro passado para que não haja cache no ie
		
		if(idCidadeSelected)
		{
			idselectedcity = idCidadeSelected;
		}
		
		document.getElementById(selectreturn).innerHTML = '';
		option = document.createElement("option");
		option.setAttribute('value','');
		option.innerHTML = 'carregando...';
		document.getElementById(selectreturn).appendChild(option);
			
		idestado = document.getElementById(ide).value;
		if(idestado.length)
		{
			var urlchamada = url+'cidade.php?hour='+dateobject.getTime()+'&idEstado='+escape(idestado)+'&xjp=AjAx';
			xmlHttp.open('GET', urlchamada, true);
			xmlHttp.onreadystatechange = atualizar;
			xmlHttp.send(null);
		}
		else
		{
			document.getElementById(selectreturn).innerHTML = '';
			option = document.createElement("option");
			option.setAttribute('value','');
			option.innerHTML = ':: Selecione ::';
			document.getElementById(selectreturn).appendChild(option);
			buscando = 0;
		}
	}
	
	}
}

function atualizar()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			var resposta = xmlHttp.responseText;
			var id;
			document.getElementById(selectreturn).innerHTML = '';
			option = document.createElement("option");
			option.setAttribute('value','');
			option.innerHTML = ':: Selecione ::';
			document.getElementById(selectreturn).appendChild(option);
			
			if(resposta)
			{
				array = eval(resposta);
				arraynames = array[0];
				arrayvalue = array[1];
				if(arrayvalue.length == 1)
					document.getElementById(selectreturn).innerHTML = '';
				for(x=0;x<arrayvalue.length;x++)
				{
					option = document.createElement("option");
					option.setAttribute('value',arrayvalue[x]);
					if(idselectedcity == arrayvalue[x])
					option.setAttribute('selected',true);
					option.setAttribute('id',arrayvalue[x]);
					option.innerHTML = rawurldecode(arraynames[x]);
					document.getElementById(selectreturn).appendChild(option);
					idselectedcity = 0;
				}
			}
		}
		buscando = 0;
	}
}

function buscarSelectCidadeEstatico(id_estado, select_return, id_selected_city)
{
	document.getElementById(select_return).innerHTML = '';
	if(id_estado)
	{
		var arraycity = array_cidade_estatico[id_estado];
		if(arraycity.length == 1) document.getElementById(select_return).innerHTML = '';
		for(var key in arraycity)
		{
			option = document.createElement("option");
			option.setAttribute('value',key);
			if(id_selected_city == key)
			option.setAttribute('selected',true);
			option.setAttribute('id',key);
			option.innerHTML = rawurldecode(arraycity[key]);
			document.getElementById(select_return).appendChild(option);
		}
	}
}


