var tipo = '';
var ajax = null;
function Ajaxizar(){
	if (window.XMLHttpRequest) {
		ajax = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e1) { 
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) { }
		 }
	}
}

function notificar(email,id){
	Ajaxizar();
		ajax.open('get', 'ajax_notificar.php?email=' + encodeURIComponent(email) + '&id=' + id, true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = retornar;
		ajax.send(null);	
}

function retornar(){
	if (ajax.readyState == 4){
		if(ajax.status == 200){
			document.getElementById('help').innerHTML = ajax.responseText;
			emProgresso = false;
		}
	}else{		
		if(ajax.readyState == 1){
			document.getElementById('help').innerHTML = "Carregando...";
		}
	}	
}

function retornar_dup(){
	if (ajax.readyState == 4){
		if(ajax.status == 200){
			if(tipo == 'email'){
				document.getElementById('email_help').innerHTML = ajax.responseText;
			}
			else{
				document.getElementById('cpf_help').innerHTML = ajax.responseText;
			}	
		}
	}else{		
		if(ajax.readyState == 1){
			if(tipo == 'email'){
				document.getElementById('email_help').innerHTML = 'Verificando email';
			}
			else{
				document.getElementById('cpf_help').innerHTML = 'Verificando CPF';
			}	
		}
	}	
}

function busca_cep(cep,cep2){
	Ajaxizar();
		ajax.open('get', 'ajax_busca_cep.php?cep=' + encodeURIComponent(cep) + '&cep2=' + encodeURIComponent(cep2), true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = retornar;
		ajax.send(null);	
}

function ajaxCep(cep){
		Ajaxizar();
			
				ajax.open('get', 'teste_cep_back.php?cep=' + encodeURIComponent(cep), true);
				ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				ajax.onreadystatechange = retornar_cep;
				ajax.send(null);	
}

function retornar_cep(){
	if (ajax.readyState == 4){
		if(ajax.status == 200){
			document.getElementById("endereco").value= ajax.responseXML.getElementsByTagName("endereco")[0].childNodes[0].nodeValue;
			document.getElementById("suburb").value= ajax.responseXML.getElementsByTagName("bairro")[0].childNodes[0].nodeValue;
			document.getElementById("city").value= ajax.responseXML.getElementsByTagName("cidade")[0].childNodes[0].nodeValue;
			document.getElementById("state").value= ajax.responseXML.getElementsByTagName("estado")[0].childNodes[0].nodeValue;			
			if( (document.getElementById("city").value == "CEP Inválido") || ((document.getElementById("city").value == "Erro"))){
				document.getElementById("city").readOnly = false;
				document.getElementById("city").style.color = "black";
				document.getElementById("state").readOnly = false;
				document.getElementById("state").style.color = "black";
				alert("CEP Inválido. Por favor, digite-o novamente");
				document.getElementById("postcode").focus();
				document.getElementById("postcode").select();
			}
			else{
				document.getElementById("numero").focus();
				document.getElementById("city").readOnly = true;
				document.getElementById("city").style.color = "gray";
			}
		}
	}else{		
		if(ajax.readyState == 1){
			document.getElementById("endereco").value= "Aguarde...";
			document.getElementById("suburb").value= "Aguarde...";
			document.getElementById("city").value= "Aguarde...";	
		}
	}	
}

function duplicado(campo,valor){
	tipo = campo;
	Ajaxizar();
	if(campo == 'email'){
		ajax.open('get', 'ajax_duplicado.php?email=' + encodeURIComponent(valor), true);
	}
	else if (campo == 'cpf'){
		ajax.open('get', 'ajax_duplicado.php?cpf=' + encodeURIComponent(valor), true);
	}		
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax.onreadystatechange = retornar_dup;
	ajax.send(null);	
}

function ajaxLicencas(lid){
		Ajaxizar();
			
				ajax.open('get', 'ajax_licencas.php?lid=' + encodeURIComponent(lid), true);
				ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				ajax.onreadystatechange = retornarLicencas;
				ajax.send(null);	
}

function retornarLicencas(){
		if(ajax.responseText.length > 0){
			document.getElementById('top_price').innerHTML = ajax.responseText; 
		}	
}

