//Função que verifica campos =======================================
function verifica_campo(id,valor,valor_comparado){
	if(valor == valor_comparado){
		document.getElementById(id).value = '';
	} else if(valor == ''){
		document.getElementById(id).value = valor_comparado;
	}

}

//Slide Conteudos ==================================================
function slide_conteudos(id,px,direcao,tempo){
	if(direcao == 'left'){
		$(id).animate({'left' : '-='+px}, tempo);
	} else if(direcao == 'right'){
		$(id).animate({'left' : '+='+px}, tempo);
	} else if(direcao == 'bottom'){
		$(id).animate({'bottom' : '+='+px}, tempo);
	} else if(direcao == 'top'){
		$(id).animate({'bottom' : '-='+px}, tempo);
	}
}

//Pular Campo ======================================================
function pular_campo(valor,campo,proximo_campo,limite){
	if(isNaN(valor)){	
	document.getElementById(campo).value = '';
		return false;
	}
	if(valor.length == limite){
		document.getElementById(proximo_campo).focus();	
	}

}

//Formata telefone =================================================================================
function formata_telefone(campo , teclapres){
	var tecla = teclapres.keyCode;
	var vr = new String(campo.value);
	tam = vr.length + 1;	
	if(tam > 4 && tam < 6){
		campo.value = vr.substr(0, 4) + '-';
	}
}

//Formatar CPF =====================================================================================
function formata_cpf(campo , teclapres){
	var tecla = teclapres.keyCode;
	var vr = new String(campo.value);
	tam = vr.length + 1;
	if (tecla != 8 && tecla != 8){
		if(tam > 3 && tam < 5){
			campo.value = vr.substr(0, 4) + '.';
		}
		if(tam > 7 && tam < 9){
			campo.value = vr.substr(0, 8) + '.';
		}
		if(tam > 11 && tam < 13){
			campo.value = vr.substr(0, 11) + '-';
		}
	}	
}

/*Função que formata campos =======================================================================*/
function formata_campos(elemento,tipo){

	if(tipo == 'cnpj'){
		
		$(elemento).attr('maxlength' , '18');
		var valor_elemento = elemento.value;
		var valor = valor_elemento.replace(/\D/g,'');
		valor = valor.replace(/\D/g,"");                   
		valor = valor.replace(/^(\d{2})(\d)/,"$1.$2");     
		valor = valor.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3"); 
		valor = valor.replace(/\.(\d{3})(\d)/,".$1/$2");
		valor = valor.replace(/(\d{4})(\d)/,"$1-$2");
		elemento.value = valor;
		
	} else if(tipo == 'cpf'){
		
		$(elemento).attr('maxlength' , '14');
		var valor_elemento = elemento.value;
		var valor = valor_elemento.replace(/\D/g,'');
		valor = valor.replace(/\D/g,"");
		valor = valor.replace(/(\d{3})(\d)/,"$1.$2");
		valor = valor.replace(/(\d{3})(\d)/,"$1.$2");
		valor = valor.replace(/(\d{3})(\d{1,2})$/,"$1-$2");
		elemento.value = valor;
		
	} else if(tipo == 'telefone'){
		
		$(elemento).attr('maxlength' , '14');
		var valor_elemento = elemento.value;
		var valor = valor_elemento.replace(/\D/g,'');
		valor = valor.replace(/^(\d\d)(\d)/g,"($1) $2");
		valor = valor.replace(/(\d{4})(\d)/,"$1-$2");    
		elemento.value = valor;
		
	} else if(tipo == 'cep'){
		
		$(elemento).attr('maxlength' , '9');
		var valor_elemento = elemento.value;
		var valor = valor_elemento.replace(/\D/g,'');
		valor = valor.replace(/D/g,"");
		valor = valor.replace(/^(\d{5})(\d)/,"$1-$2");
		elemento.value = valor;
		
	} else if(tipo == 'data'){
		
		$(elemento).attr('maxlength' , '10');
		var id_elemento = elemento.id;
		var valor_elemento = elemento.value;
		var valor = valor_elemento.replace(/\D/g,'');
		valor = valor.replace(/(\d{2})(\d)/,"$1/$2");
		valor = valor.replace(/(\d{2})(\d)/,"$1/$2");
		
		elemento.value = valor;
		
	}
	
}

//Função que verifica campos vazios ===============================
function campo_vazio(elemento,valor_comparado){
	
	var id_elemento = elemento.id;
	var valor_elemento = elemento.value;
	
	if(valor_elemento == valor_comparado){
		elemento.value = '';
	} else if(valor_elemento == ''){
		elemento.value = valor_comparado;
	}
	
}

/*Formatar CNPJ ===================================================================================*/
function formata_cnpj(elemento){
	
	$(elemento).attr('maxlength' , '18');
	var valor_elemento = elemento.value;
	var valor = valor_elemento.replace(/\D/g,'');
	valor = valor.replace(/\D/g,"");                   
	valor = valor.replace(/^(\d{2})(\d)/,"$1.$2");     
	valor = valor.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3"); 
	valor = valor.replace(/\.(\d{3})(\d)/,".$1/$2");
	valor = valor.replace(/(\d{4})(\d)/,"$1-$2");
	elemento.value = valor;
	
}

//Validador de datas ==============================================================================
function formatadata(campo, teclapres){
	var tecla = teclapres.keyCode;
	var vr = new String(campo.value);
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	tam = vr.length + 1;
	if (tecla != 8 && tecla != 8){
		if(tam > 0 && tam < 2){
			campo.value = vr.substr(0, 2)
		}
		if(tam > 2 && tam < 4){
			campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2);
		}
		if(tam > 4 && tam < 7){
			campo.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/' + vr.substr(4, 7);
		}
	}
}


/*Função que seleciona combo box ===============================================================*/
function select_combobox(id,valor_comparado){
	var combo_box = document.getElementById(id);
	if(combo_box){
		var opcoes = combo_box.getElementsByTagName('option');
		var nopcoes = opcoes.length;
		for(i=0; i<=nopcoes; i++){
			var valores = document.getElementById(id).options[i].innerHTML;
			var valores2 = document.getElementById(id).options[i].value;
			if(valores == valor_comparado){
				document.getElementById(id).options[i].selected = "selected";
				break;
			} else if(valores2 == valor_comparado){
				document.getElementById(id).options[i].selected = "selected";
				break;
			} else {
				
			}
		}
	}
}

//Gerar mapa google maps ============================================================================
var map = null;
function gerar_mapa(idi,latitude,longetude,zoom,t_zoom,t_type) {
	map = new GMap2(document.getElementById(idi));
	map.setCenter(new GLatLng(latitude,longetude), zoom);
	if(t_zoom == true){
	map.addControl(new GSmallMapControl());
	}
	if(t_type == true){
	map.addControl(new GMapTypeControl());
	}
	geocoder = new GClientGeocoder();
	var marker = new GMarker(new GLatLng(latitude,longetude));
	map.addOverlay(marker);
}
