var preenchimento = 'fill';

function createXmlHttpRequestObject(){  
	var xmlHttp;
	if(window.ActiveXObject){
		try{
			xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			nav = 'ie';
		}catch(e){
			xmlHttp = false;
		}
	}else{
		try {
			xmlHttp = new XMLHttpRequest();
			nav = 'ff';
		}catch(e){
		xmlHttp = false;
		}
	}	
	if(!xmlHttp){
		alert('Erro na criação do XMLHTTP.');
	}else{
		return xmlHttp;
	}
}

function dar_get(x, xmlHttp){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", x, true);  
		xmlHttp.send(null);
	}else{
		alert('Tente novamente.');
	}
}

function res_html(xmlHttp){
	var cont = '';
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			if(nav=='ie'){
				res_xml = xmlHttp.responseXML;
				dados = res_xml.documentElement;
				cont = dados.childNodes(0).childNodes(0).data;
			}else{
				res_xml= xmlHttp.responseXML;
				dados = res_xml.getElementsByTagName('retorno').item(0);
				cont =  dados.firstChild.data;
			}
			resposta = cont.replace(/\+/g," ");
			d(preenchimento).innerHTML = unescape(resposta);
			cont = '';
			refreshMaker();
		}else{
			erro_ajax();
		}
	}
}

function erro_ajax(){
	alert('Ooops!\nNão foi possível completar sua requisição. Por favor tente novamente.');	
}

function d(id){
	return document.getElementById(id);
}


function k_enter(campo, e, funcao){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13){
		eval(funcao);
		return false;
	}else{
		return true;
	}
}

function cep(cep){
	var xmlHttp = createXmlHttpRequestObject(); 
	xmlHttp.onreadystatechange = function(){ resp(xmlHttp) };
	k = Math.random();
	dar_get('_retornacep.php?act=true&v_CEP='+cep+'&k='+k, xmlHttp);
}

function resp(xmlHttp){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
		if (xmlHttp.status == 200){
			if(nav=='ie'){
				res_xml = xmlHttp.responseXML;
				dados = res_xml.documentElement;
				cont = dados.childNodes(0).childNodes(0).data;
			}else{
				res_xml= xmlHttp.responseXML;
				dados = res_xml.getElementsByTagName('dados').item(0);
			}
			if(dados.childNodes(0).childNodes(0).data=='err'){
				d('lb_status').innerHTML = 'CEP NÃO ENCONTRADO';
				d('endereco').focus();
				setTimeout("document.getElementById('lb_status').innerHTML = '';",2000);
			}else{
				d('rua').value = dados.childNodes(0).childNodes(0).data;
				d('bairro').value = dados.childNodes(1).childNodes(0).data
				d('cidade').value = dados.childNodes(2).childNodes(0).data
				d('uf').value = dados.childNodes(3).childNodes(0).data
				d('lb_status').innerHTML = '';
				d('numero').focus();
			}
		}else{
			alert("Problemas durante a conexão com o servidor: " + xmlHttp.statusText);
		}
		}
	}
}