function BuscaCep(idCep){
	var oCdRua, oNmRua, oCdBairro, oNmBairro, oCdCidade, oNmCidade;
	var cepProccurado;
	var buscaCep = this;
	this.oCep = $(idCep);
	with(buscaCep.oCep){
		maxLength = 9;
		size = 8;
	}
	
	this.findCepCustom = new String();
	
	this.divWaitPos = new String();
	
	this.setRua = function(idCdRua, idNmRua){
		if(idCdRua!='') oCdRua = $(idCdRua);
		if(idNmRua!='') oNmRua = $(idNmRua);
	}
	this.setBairro = function(idCdBairro, idNmBairro){
		if(idCdBairro!='') oCdBairro = $(idCdBairro);
		if(idNmBairro!='') oNmBairro = $(idNmBairro);
	}
	this.setCidade = function(idCdCidade, idNmCidade){
		if(idCdCidade!='') oCdCidade = $(idCdCidade);
		if(idNmCidade!='') oNmCidade = $(idNmCidade);
	}

	buscaCep.oCep.onkeydown = function(){
		if(cepProccurado != buscaCep.oCep.value)
			limpaCampos();
	}
	buscaCep.oCep.onfocus = function(){
		if(buscaCep.oCep.value.length == 8) 
			cepProccurado = buscaCep.oCep.value;
		else
			cepProccurado = '';
	}
	buscaCep.oCep.onblur = function(){
		truncaCep();
		if(cepProccurado != buscaCep.oCep.value)
			buscaCep.oCep.onkeyup();
	};
	
	buscaCep.oCep.onkeyup = function(){
		if(buscaCep.oCep.value.match(/\D/g,''))
			buscaCep.oCep.value = buscaCep.oCep.value.replace(/\D/g,'');
		truncaCep();
		if((buscaCep.oCep.value.length == 8) &&
			(cepProccurado != buscaCep.oCep.value)){
			cepProccurado = buscaCep.oCep.value;
			limpaCampos();
			document.getElementById('divWait').style.top = buscaCep.divWaitPos;
			if (buscaCep.findCepCustom != ''){
				eval(buscaCep.findCepCustom +'(\'' + cepProccurado+'\')')
			} else {
				findCep(cepProccurado);
			}
		}
		else if(buscaCep.oCep.value.length < 8) cepProccurado = '';
	};
	function limpaCampos(){
		if(oCdRua) oCdRua.value='';
		if(oNmRua) oNmRua.value='';
		if(oCdBairro) oCdBairro.value='';
		if(oNmBairro) oNmBairro.value='';
		if(oCdCidade) oCdCidade.value='';
		if(oNmCidade) oNmCidade.value='';
	}
	function truncaCep(){
		if(buscaCep.oCep.value.length > 8) 
			buscaCep.oCep.value = buscaCep.oCep.value.substr(0, 8);
	}
}

function findCep(nome){
	if (nome.length == 8){
		showWait();
		cepManager.findCep(nome, findCepCallback);
	} else {
		alert("Para realizar a pesquisa de CEP, informe os 8 digitos do mesmo.");
		document.getElementById("cdCep").focus();
	}
}

function findCepCallback(lista){
	var div = document.getElementById('containerListaCep');
	var cep = document.getElementById('listaCep');

	if(lista.length == 1){
		selecionaEndereco( lista[0][2]
						 , lista[0][1]
						 , lista[0][4]
						 , lista[0][3]
						 , lista[0][6]
						 , lista[0][5] );
		cep.innerHTML = '';
		div.style.display = 'none';
	}else if(lista.length > 1){
		var x = '';
		
		x += "<table align='left' height='100%' width='320' border='1' cellspacing='0' bordercolor='#f0f0f0' style='margin-left: 1px'>";
		x += "<tbody>";
		for (var i = 0; i < lista.length; i++){
			x += "<tr><td width='80'><a href='#' onclick=\"selecionaEndereco('"+lista[i][2]+"','"+lista[i][1]+"','"+lista[i][4]+"','"+lista[i][3]+"','"+lista[i][6]+"','"+lista[i][5]+"')\">" + lista[i][0] 
					+ "</a></td><td width='80'>" + lista[i][2] 
					+ "</td><td width='80'>" + lista[i][4] 
					+ "</td><td width='80'>" + lista[i][6] 
					+ "</td></tr>";
		}
		x += "<tbody>";
		x += "</table>";
		cep.innerHTML = x;
		div.style.display = 'block';
	}
	hideWait();
}

function selecionaEndereco( rua
						  , cdrua
						  , bairro
						  , cdbairro
						  , cidade
						  , cdcidade ){
	document.getElementById("nmRua").value = rua;
	document.getElementById("cdRua").value = cdrua;
	document.getElementById("nmBairro").value = bairro;
	document.getElementById("cdBairro").value = cdbairro;
	document.getElementById("nmCidade").value = cidade;
	document.getElementById("cdCidade").value = cdcidade;

	document.getElementById('containerListaCep').style.display = 'none';
}
