function Completa_Endereco(cep, endereco, bairro, estado, cidade)
{
	var f_endereco = document.getElementById(endereco);
	var f_bairro = document.getElementById(bairro);
	var f_estado = document.getElementById(estado);
	var f_cidade = document.getElementById(cidade);

	if(f_endereco && f_bairro && f_estado && f_cidade &&cep.value!="")
	{
		var c = cep.value.replace("-", "");
		ajax = iniciaAjax();
	
		ajax.onreadystatechange = function() {Completa_Endereco_Ready(endereco, bairro, estado, cidade);};
		ajax.open("GET", "../Funcoes/PesquisaCepBD?cep="+c, true);
		
		ajax.send(null);
	}
}

function Completa_Endereco_Ready(v_endereco, v_bairro, v_estado, v_cidade)
{
	if (ajax.readyState == 4) 
	{
		//verifica o número do status, se for diferente de 200 tem algum erro 
		if (ajax.status == 200) 
		{
            var xml = ajax.responseXML;
			if(xml != null)
			{
			
				var endereco = xml.getElementsByTagName('endereco')[0];			
				if(endereco.hasChildNodes())
				{
					var logradouro = xml.getElementsByTagName('logradouro')[0];
					logradouro = logradouro.firstChild.nodeValue;
					var bairro = xml.getElementsByTagName('bairro')[0];
					bairro = bairro.firstChild.nodeValue;
					var cidade = xml.getElementsByTagName('cidade')[0];
					cidade = cidade.firstChild.nodeValue;
					var uf = xml.getElementsByTagName('uf')[0];
					uf = uf.firstChild.nodeValue;
					var f_endereco = document.getElementById(v_endereco);
					var f_bairro = document.getElementById(v_bairro);
					var f_estado = document.getElementById(v_estado);
					var f_cidade = document.getElementById(v_cidade);
					
					f_endereco.value = logradouro;
					f_bairro.value = bairro;
					f_estado.value = uf;
					Procura_Cidade(f_estado, v_cidade, cidade);
				}
			}
		}
	}
}

function CarregandoCidade(combo_cidade)
{
	combo_cidade.innerHTML='<option selected="selected>Carregando...</option>';	
}

function ObjFunc(fnc_array){
   this.funcs = fnc_array;
   this.Execute = function executar()
   {
	   	if(funcs.length>0)
		{
			var t = this.funcs.shift();
			t();
		}
   };
   this.setFunctions = function setar(fnc_array)
   {
	   this.funcs = fnc_array;
   };
}

function EstadoCidade(estado, cidade, cidade_valor)
{
	this.estado=estado;
	this.cidade=cidade;
	this.cidade_valor=cidade_valor;
}

function Procura_Cidade(estado, cidade, cidade_valor, objfunc)
{
	var cidade = document.getElementById(cidade);
	if(cidade && estado.value != -1)
	{
		//inicia o AJAX
		ajax = iniciaAjax();
		
		CarregandoCidade(cidade);

		ajax.onreadystatechange = function(){Procura_Cidade_Ready(estado, cidade, cidade_valor, objfunc);};
		
		ajax.open("get", "../Funcoes/ProcuraCidadesBD?uf="+estado.value, true);
		
		ajax.send(null);
	}
	else
	{
		if(objfunc)
		{
			objfunc.Execute();
		}
		cidade.innerHTML='<option selected value="-1">----</option>';
		cidade.disabled="disabled";
	}
}

function Procura_Cidade_Ready(estado, combo_cidade, cidade_valor, objfunc)
{
	if (ajax.readyState == 4) 
	{
		//verifica o número do status, se for diferente de 200 tem algum erro 
		if (ajax.status == 200) 
		{
            var xml = ajax.responseXML;
			if(xml != null)
			{
				if(xml.hasChildNodes())
				{
					var nos = xml.getElementsByTagName('cidade');
					combo_cidade.innerHTML="";
					if(nos[0] && nos[0].firstChild)
					{
						var opcao_inicio = document.createElement('option');
						opcao_inicio.setAttribute('value', '-1');
						opcao_inicio.appendChild(document.createTextNode('-----------------'));
						combo_cidade.appendChild(opcao_inicio);
						for(cont = 0; cont < nos.length; cont++)
						{
							//verifica se é o IE
							if(window.ActiveXObject)
							{						
								var id = nos[cont].childNodes[0].firstChild.nodeValue;
								var nome = nos[cont].childNodes[1].firstChild.nodeValue;
							}
							else
							{
								var id = nos[cont].childNodes[1].firstChild.nodeValue;
								var nome = nos[cont].childNodes[3].firstChild.nodeValue;
							}
							var opcao = document.createElement('option');
							opcao.setAttribute('value', nome);
							opcao.appendChild(document.createTextNode(id));
							combo_cidade.appendChild(opcao);
						}
						combo_cidade.value=cidade_valor;
						combo_cidade.disabled=estado.disabled;
						if(objfunc)
						{
							objfunc.Execute();
						}
					}
				}
			}
		}
	}
}
