function isCep(campo) {
	var expr =  /^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/;

    if(campo.search(expr) == -1)
	{
		return false;
	}
	return true;
}



/*
 * Verifica se � um n�mero v�lido
 *
 */
function isNumber(pVal) {
	var reDigits = /^\d+$/;
	return reDigits.test(pVal);
}



/**
  * Para os campos cidades junto com cep, formata, caso os 2 primeiros caracteres sejam num�ricos, para cep
  *
  */
function formatarCidadeCep(campo) {
	if ( campo != undefined ) {
		if ( isNumber(campo.value.substr(0, 2)) ) {
			if ( campo.value.length == 5 ) {
				campo.value += "-";
			}
		}
	}
}

function pulaCampo(campoAtual, quantidadeCaracteres, campoSeguinte) {
    if (campoAtual.value.length == quantidadeCaracteres) {
        campo = document.getElementById(campoSeguinte);
        if ( campo != undefined ) {
        	campo.focus();
        }
    }
}

function formataHora(campo) {
	if ( campo.value.length == 2 ) {
		campo.value += ":";
	}
}


function contarCaracteres(textarea, span, quantidade) {
	var texto = "";
	
	if ( textarea.value.length <= quantidade ) {
		texto = (quantidade - textarea.value.length) + " caracteres restantes";
	} else {
		texto = "ultrapassou " + quantidade + " caracteres";
	}
	jQuery("#" + span).text( texto );
}

/* Forca links a abrirem em uma nova janela  */
function createExternalLinks() {
    if( document.getElementsByTagName ) {
        var anchors = document.getElementsByTagName('a');
        for(var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if ( anchor.getAttribute("href") && anchor.getAttribute('rel')=='externo' ) {
                anchor.target = '_blank';
            }
        }
    }
}

// Retira os caractes alfanumericos do CEP
function formataCEP( cep ) {
	cep = cep.replace(".","");
	return cep;
}


function sliderPatrocinadores() {
	var p = jQuery("#patrocinadores img");
	var size = p.size();
	var indice = 0;
	var anterior = size;

	var fnc_mostrar_patrocinadores = function(){
		p.css('display', 'none');
		var elem = jQuery('#imagem_parceiro_' + indice);
		jQuery('#imagem_parceiro_' + anterior).fadeOut(500);
		elem.fadeIn(1000);
		
		indice++;

		if ( indice >= size )indice = 0;
		if ( anterior < 0 ) anterior = size;
	};

	fnc_mostrar_patrocinadores();
	window.setInterval(fnc_mostrar_patrocinadores, 6000);
}

function getLatLng(idCampoCep, idCampolatitude, idCampolongitude) {
	var cep = jQuery("#" + idCampoCep).val(); 
	geocoder = new google.maps.ClientGeocoder();
	geocoder.getLocations(cep + ", Brasil",  function(point) {
   		if (point && point.Status.code == "200" && point.Placemark.length == 1 && 
    			point.Placemark[0].AddressDetails.Country.CountryNameCode == "BR") {
			jQuery("#" + idCampolatitude).val(point.Placemark[0].Point.coordinates[1]);
			jQuery("#" + idCampolongitude).val(point.Placemark[0].Point.coordinates[0]);
   		} else {
			alert("CEP: " + cep + " não encontrado!");
   		}
	} );
}

function getLatLngVoluntario(idCampoCep1, idCampoCep2, idCampolatitude, idCampolongitude) {
	var cep = jQuery("#" + idCampoCep1).val() +""+ jQuery("#" + idCampoCep2).val(); 
  	geocoder = new google.maps.ClientGeocoder();
    geocoder.getLocations(cep + ", Brasil", function(point) {
        if (point && point.Status.code == "200" && point.Placemark.length == 1 && 
                point.Placemark[0].AddressDetails.Country.CountryNameCode == "BR") {
        	jQuery("#" + idCampolatitude).val(point.Placemark[0].Point.coordinates[1]);
        	jQuery("#" + idCampolongitude).val(point.Placemark[0].Point.coordinates[0]);
        } else {
        	alert("CEP: " + cep + " não encontrado!");
        }
    } );
}

function addClass(id, className) {
	jQuery("#" + id).addClass(className);
}

function removeClass(id, className) {
	jQuery("#" + id).removeClass(className);
}

// Inicia vari&aacute;veis
function iniciaVariaveis( uf ) {
	// Inicia input de controle (cidade) com valor SC
	jQuery("#tEstado").val(uf);
	// Insere o valor default no select box de estados
	//document.getElementById("sEstado").options.selectedIndex = 0;
}

// Manipula a apresenta&ccedil;&atilde;o dos blocos
function manipulaBloco( open, close, focus, open2, open3, close2, close3  ) {
	jQuery("#" + open).show();
	jQuery("#" + open2).show();
	jQuery("#" + open3).show();
	
	jQuery("#" + close).hide();
	jQuery("#" + close2).hide();
	jQuery("#" + close3).hide();
	
	jQuery("#estado").hide();
	

	// Inicia o bloco com focus em um determinado campo
	if ( focus != "" ) {
		jQuery("#" + focus).focus();
	}

	// Limpa o campo CEP
	if ( open == "cep" ) {
		jQuery(".erro").hide();
		jQuery("#estado").hide();
		jQuery("#btnCidade").hide();		
		jQuery(".frmCidade").hide();
		
		jQuery("#tCEP").val("");
		jQuery("#btnCEP").removeClass("habilitado");
		jQuery("#btnCEP").addClass("inabilitado");
		
	} else if ( open == "cidade" ) {
		jQuery(".erro").hide();
		jQuery("#estado").show();
		jQuery("#btnCidade").show();		
		jQuery(".frmCidade").show();
	
	}
		

	return false;
}

// Manipula a escolhe de um estado
function manipulaEstado( estado ) {
	if ( estado.value == 0 ) {
		return false;
	}
	jQuery("#txtEstado").text(estado.innerHTML);
	jQuery("#tEstado").val(estado.value);
	jQuery("#tCidade").val("");
	
	//manipulaBloco('cidade','estado','tCidade');
}

// Verifica CEP e d&aacute; prosseguimento a a&ccedil;&atilde;o
function manipulaBotao( cep, botao ) {
	if ( jQuery("#" + cep).val().length == 9) {
		jQuery("#" + botao).removeClass("inabilitado");
		jQuery("#" + botao).addClass("habilitado");
	}
	else {
		jQuery("#" + botao).removeClass("habilitado");
		jQuery("#" + botao).addClass("inabilitado");
	}
}

// Verifica CEP e d&aacute; prosseguimento a a&ccedil;&atilde;o
function verificaCEP( id ) {
	var cep = jQuery("#" + id).val();
	if ( cep.length != 9 || cep.indexOf(".") > -1) {
		jQuery(".erro").text("CEP Incorreto, tente novamente.");
		jQuery(".erro").show();

		return false;
	}
	jQuery("#txtCEP").text(cep);
	jQuery("#txtCidade").text('');
	
	// esconde a distância e mostra após a execução da consulta da cidade pelo cep
	jQuery('#sDistancia').hide();
	
	manipulaBloco('distancia','cep');
}