/*****************************************
Programmer : Sidnei S Alves
Date       : 19/10/2008
E-Mail     : sidnei@gerempre.com
Site       : http://www.gerempre.com
Version    : 1.0
*****************************************/
var Ajax;
var endereco = new Array();
var campos;
function AjaxRequest(){
	Ajax = false;
	if (window.XMLHttpRequest){
		Ajax = new XMLHttpRequest();
	} else if (window.ActiveXObject){
		try{	
			Ajax = new ActiveXObject("Msxm12.XMLHTTP");
		} catch(e) {
				try{
					Ajax = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {}
			}
		}
}

function processaAjax(metodo, url, parametros, fctresposta){
	AjaxRequest();
	if (metodo == 'GET') {Ajax.open(metodo,url+'?'+parametros,true);}
	if (metodo == 'POST') {Ajax.open(metodo,url,true);}	
	
	Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
	Ajax.setRequestHeader("Encoding","ISO-8859-1"); 	
	Ajax.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");			
	Ajax.setRequestHeader("Cache-Control","post-'=0, pre-check=0");
	Ajax.setRequestHeader("Pragma", "no-cache");
	Ajax.onreadystatechange = fctresposta;	
	if (metodo == 'GET') {Ajax.send(null);}
	if (metodo == 'POST') {Ajax.send(parametros);}
}

function processandoAjax(){
	if (Ajax.readyState == 1){
		document.write('Carregando Informações...');
	} else if (Ajax.readyState == 2){
		document.write('Carga Completa');
	} else if (Ajax.readyState ==3){
		document.write('Processando...');
	} else if (Ajax.readyState == 4) {		
		document.write('Processado Com Sucesso');
	}
}

function formatarMascara(src, mask){
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida){
		src.value += texto.substring(0,1);
	}
}

function alterarClasse(id,classe){	
	var e = document.getElementById(id);
	if (e) {
		e.setAttribute('class',classe);
		e.setAttribute('className',classe);
	}
}

function CEP(){
	this.params = '';	
	this.urlProcess = 'js/cep/client.php';
	this.method = 'GET';
	this.validar = function (){
		var c = document.getElementById(campos[2]);
		if (c.value == ''){
			alert('Falta Informar CEP');
			c.focus();
			return false;
		} else {
			return true
		}
	}
}

function consultarCEP(valor){
	getCampos(valor);
	var cep = new CEP();
	if (cep.validar()){
		alterarClasse('progress','#');
		cep.params = "acao=consultar&cep="+document.getElementById(campos[2]).value;
		processaAjax(cep.method, cep.urlProcess, cep.params, requestCEP);
	}
}

function requestCEP(){
	//processandoAjax();
	if (Ajax.readyState == 4) {
		if (Ajax.status == 200) {
			//var xmldoc = Ajax.responseXML;
			var xmldoc = getXML(Ajax);
			var resultado = xmldoc.getElementsByTagName('resultado');

			endereco['erro'] = xmldoc.getElementsByTagName('erro')[0].firstChild.nodeValue;
			if (endereco['erro'] == 0){
				endereco['logradouro'] = (xmldoc.getElementsByTagName('logradouro')[0].firstChild ? xmldoc.getElementsByTagName('logradouro')[0].firstChild.nodeValue : '&nbsp;');
				endereco['bairro'] = (xmldoc.getElementsByTagName('bairro')[0].firstChild ? xmldoc.getElementsByTagName('bairro')[0].firstChild.nodeValue : '&nbsp;');
				endereco['cidade'] = (xmldoc.getElementsByTagName('cidade')[0].firstChild ? xmldoc.getElementsByTagName('cidade')[0].firstChild.nodeValue : '&nbsp;');
				endereco['uf'] = (xmldoc.getElementsByTagName('uf')[0].firstChild ? xmldoc.getElementsByTagName('uf')[0].firstChild.nodeValue : '&nbsp;');
			} else {
				endereco['logradouro'] = '';
				endereco['bairro'] = '';
				endereco['cidade'] = '';
				endereco['uf'] = '';
				alert('CEP Não Localizado');
			}
			setCampos();
			alterarClasse('progress','esc');
		} else {
			alert('Erro status = '+Ajax.status);
		}
	}
}

function setCampos(){
	document.getElementById(campos[0]).value = endereco['logradouro'];
	document.getElementById(campos[1]).value = endereco['bairro'];
	//document.getElementById(campos[2]).value = endereco['cep'];
	document.getElementById(campos[3]).value = endereco['cidade'];
	document.getElementById(campos[4]).value = endereco['uf'];
}

function getCampos(valor){
	campos = valor.split('|');	
}
