var xmlHttp
function cadastrarCEP(CEP, logradouro, bairro, cidade, estado, posicao) { 

		if (CEP.length==0)	{ 
		document.getElementById("enviaCad").innerHTML=""
		return	}
		
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null) {
		alert ("Seu navegador não dá suporte a HTTP Request")
		return	} 
		
		var dt = new Date();
		var url = "cep/mod_cadastracep.asp?cep="+CEP+"&logradouro="+logradouro+"&bairro="+bairro+"&cidade="+cidade+"&estado="+estado+"&posicao="+posicao+"&Data="+encodeURI("_" + dt.getTime())
		xmlHttp.onreadystatechange=stateChangedCEP
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
}

function BuscaEndereco(logradouro,cidade,posicao) { 
		
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null) {
		alert ("Seu navegador não dá suporte a HTTP Request")
		return	} 
		
		if (posicao=='limpar') { 
			document.getElementById('src_cep').value='(8 digitos)' 
			document.getElementById('src_logradouro').value=''
			document.getElementById('src_cidade').value=''
			}
		var dt = new Date();
		var url = "cep/mod_buscaend.asp?logradouro="+logradouro+"&cidade="+cidade+"&posicao="+posicao+"&Data="+encodeURI("_" + dt.getTime())
		xmlHttp.onreadystatechange=stateChangedCEP
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
}

function BuscaCEP(CEP,posicao) { 

		if (CEP.length==0 )	{
		posicao = 'limpar' 
		}
		if (CEP.length==5) { 
			CEP = CEP+"000"
		}
		
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null) {
		alert ("Seu navegador não dá suporte a HTTP Request")
		return	} 
		
		if (posicao=='limpar') { 
			document.getElementById('src_cep').value='(8 digitos)' 
			document.getElementById('src_logradouro').value=''
			//document.getElementById('src_cidade').value=document.getElementById('form_cidade').value
			//document.getElementById('src_cidade').value=document.getElementById('form_cidade').options[document.getElementById('form_cidade').selectedIndex].value
			document.getElementById('src_cidade').value=document.getElementById('form_sugcid').value;
			}
		if (posicao!='limpar')  { 
			document.getElementById('src_cep').value=CEP
			document.getElementById('src_logradouro').value=''
			//document.getElementById('src_cidade').value=document.getElementById('form_cidade').value
			//document.getElementById('src_cidade').value=document.getElementById('form_cidade').options[document.getElementById('form_cidade').selectedIndex].value
			document.getElementById('src_cidade').value=document.getElementById('form_sugcid').value;
		}
		var dt = new Date();
		var url = "cep/mod_buscacep.asp?cep="+CEP+"&posicao="+posicao+"&Data="+encodeURI("_" + dt.getTime())
		xmlHttp.onreadystatechange=stateChangedCEP
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		document.getElementById('BuscaCEP').className='mostra';
}

function stateChangedCEP() { 
	if (xmlHttp.readyState==1 || xmlHttp.readyState=="complete"){ 
	document.getElementById("ExibeLogradouro").innerHTML="<center style='color:#FF0000;font-weight:Bold;font-size:16px;'>Aguarde localizando... </center>" } 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	document.getElementById("ExibeLogradouro").innerHTML=xmlHttp.responseText
	document.getElementById('src_logradouro').focus()
		if (document.getElementById('CEP_encontrado').value=='sim') { 
		atribuiCEP()
		}
	 } 
} 	

function BuscaCidade(NomCid) { 
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) { 
		alert ("Seu navegador não dá suporte a HTTP Request")
		return	}
	
	var dt = new Date();
	var url = "cep/mod_buscacidade.asp?nomecid="+NomCid+"&Data="+encodeURI("_" + dt.getTime())
	xmlHttp.onreadystatechange=stateChangedCid
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	//document.getElementById('BuscaCEP').className='mostra';

}

function stateChangedCid() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		document.getElementById('ExibeLogradouro').innerHTML=xmlHttp.responseText
		document.getElementById('form_codcid').value=document.getElementById('Busca_CodigoCidade').value
		BuscaBairros(document.getElementById('Busca_CodigoCidade').value)
		//document.getElementById('BuscaCEP').className='some';
	} 
} 	

function BuscaBairros(Codcid) { 

		xmlHttp=GetXmlHttpObject()
	
		if (xmlHttp==null) {
		alert ("Seu navegador não dá suporte a HTTP Request")
		return	} 
		
		var dt = new Date();
		var url = "cep/mod_buscabairros.asp?codcid="+Codcid+"&Data="+encodeURI("_" + dt.getTime())
		xmlHttp.onreadystatechange=stateChangedBai
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
}

function stateChangedBai() { 
	if (xmlHttp.readyState==1 || xmlHttp.readyState=="complete"){ 
	document.getElementById("ListaBairros").innerHTML="<center style='color:#FF0000;font-weight:Bold;font-size:16px;'>Aguarde localizando... </center>" } 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	document.getElementById("ListaBairros").innerHTML=xmlHttp.responseText
		var Lista = document.getElementById('form_lstbairros')
		var CepBairro = document.getElementById('form_codbairro').value
		var possuiRef = ''
		if (CepBairro!='') { 
			for (i=0;i<Lista.length;i++)
				{ 
					if (Lista.options[i].value==CepBairro) { 
						Lista.selectedIndex = i
						possuiRef='s'
					}
				}
		}
		if (document.getElementById('form_codbairro').value=='' || possuiRef!='s') { 
			document.getElementById('form_codbairro').value = Lista.options[0].value
		}
    } 
} 	


function GetXmlHttpObject()	{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()	}
	else if (window.ActiveXObject)	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")	}
	return objXMLHttp
} 		

function atribuiCEP() { 
	document.getElementById('form_end').value=document.getElementById('CEP_logradouro').value
	document.getElementById('form_bairro').value=document.getElementById('CEP_bairro').value
	document.getElementById('form_cidade').value=document.getElementById('CEP_cidade').value
	//alert(document.getElementById('CEP_cidade').value)
	document.getElementById('form_estado').value=document.getElementById('CEP_estado').value
	document.getElementById('form_bairroref').value=document.getElementById('CEP_bairroref').value
	document.getElementById('form_codbairro').value=document.getElementById('CEP_codbairro').value
	document.getElementById('form_codbaisft').value=document.getElementById('CEP_codbaisft').value
	
	if (document.getElementById('form_codcid').value!=document.getElementById('CEP_codcid').value) { 
		document.getElementById('form_codcid').value=document.getElementById('CEP_codcid').value
	}	
	
	var CEP = document.getElementById('src_cep').value
	
	var CEP1 = CEP.substring(0,5);
	var CEP2 = CEP.substring(5,8);

	document.getElementById('form_cep1').value=CEP1
	document.getElementById('form_cep2').value=CEP2
	
	document.getElementById('BuscaCEP').className='some'
	document.getElementById('form_end').focus();
	BuscaBairros(document.getElementById('CEP_codcid').value)
}

function CritBuscaEnd(logradouro,cidade) { 
	if (cidade=='' || logradouro=='' ) { 
		alert('Digite o endereço e a cidade')
	} else { 
		BuscaEndereco(logradouro,cidade,'')
	}

}

function atribuiEnd(logradouro, bairro, cidade, estado, CEP, Codcid) { 

	if (CEP.length==0)	{
	posicao = 'limpar' 
	}
	
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) {
	alert ("Seu navegador não dá suporte a HTTP Request")
	return	} 

	document.getElementById('src_cep').value=CEP
	
	var dt = new Date();
	var url = "cep/mod_buscacepend.asp?cep="+CEP+"&logradouro="+logradouro+"&bairro="+bairro+"&cidade="+cidade+"&estado="+estado+"&codcid="+Codcid+"&Data="+encodeURI("_" + dt.getTime())
	xmlHttp.onreadystatechange=stateChangedEnd
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
		
}
function stateChangedEnd() { 
	if (xmlHttp.readyState==1 || xmlHttp.readyState=="complete"){ 
	document.getElementById("ExibeLogradouro").innerHTML="<center style='color:#FF0000;font-weight:Bold;font-size:11px;'>Aguarde localizando... </center>" } 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	document.getElementById("ExibeLogradouro").innerHTML=xmlHttp.responseText
		if (document.getElementById('CEP_encontrado').value=='sim') { 
		atribuiCEP()
		}
	 } 
} 	
function criticaCEP(valor) {
	if (valor.length<8){
		alert("O CEP informado deve conter 8 digitos")
	} else { 
		if (isNaN(valor)==true) { 
			alert("Apenas digite números")
		} else { 
			BuscaCEP(valor)
		}
	}

}
function preencheCEP() { 
	if (document.getElementById('form_cep1').value.length==5) { 
		document.getElementById('form_cep2').focus()
	}
}

