//Mostrar / Esconder DIV
function hideShow(id) {
	if (document.getElementById(id).style.display == 'none'){ document.getElementById(id).style.display = 'block'; }
	else { document.getElementById(id).style.display = 'none'; }
}

// Esconder alvo
function hide(id) {
	document.getElementById(id).style.display = 'none';
}

// Mostrar alvo
function show(id) {
	document.getElementById(id).style.display = 'block';
}

//Retorna só numeros
function only_numbers (n) {
	return n.replace(/[^0-9]+/g, "");
}

// Realça as tags TR da tabela que possuem
// na tag pai (table, tbody, thead) a classe highlight
function highlight() {
	$(".highlight tr").hover(
		function () {
			$(this).addClass("highlight");
		}, 
		function () {
			$(this).removeClass("highlight");
		}
	);
}

// Para mostrar os tooltips nos inputs,
// basta colocar "tooltip" na class do input
// e no title do input o conteúdo do tooltip
function tooltip() {
	$(".tooltip").focus(
		function() {
			var html = '<div class="tooltip_box">';
			html += '<div class="msg">';
			html += $(this).attr("title");
			html += '</div>';
			html += '</div>';
			$(this).parent().prepend(html);
			$(this).blur(
				function() {
					$(this).parent().children(".tooltip_box").remove();
				}
			);
		}
	);
}

//------------------
// Valida CPF
//------------------
function validaCPF (cpf)
{
	var cpf = cpf.replace(/[^0-9]+/g, "");

	if (cpf.length < 11) { alert("CPF v\u00e1lido.\n- O CPF deve possuir 11 n\u00fameros."); return false; }
	if (cpf == "00000000000") { alert("CPF v\u00e1lido. Corrija o CPF."); return false; }

	var dig = [];
	for(var i=0; i < 11; i++){
		dig[i + 1] = cpf.charAt(i);
	}

	// Primeiro Dígito...
	var nSoma = (dig[1] * 10) + (dig[2] * 9) + (dig[3] * 8) + (dig[4] * 7) + (dig[5] * 6) + (dig[6] * 5) + (dig[7] * 4) + (dig[8] * 3) + (dig[9] * 2);
	var nResto = nSoma % 11;	
	var nDigV1 = 0;
	var nDigV2 = 0;
	if (nResto == 0 || nResto == 1 ){ nDigV1 = 0; }
	else { nDigV1 = 11 - nResto; }
	if ( dig[10] != nDigV1) { alert( "O CPF digitado n\u00e3o \u00e9 v\u00e1lido!" ); return false; }

	//Segundo Dígito...
	nSoma = (dig[1] * 11) + (dig[2] * 10) + (dig[3] * 9) + (dig[4] * 8) + (dig[5] * 7) + (dig[6] * 6) + (dig[7] * 5) + (dig[8] * 4) + (dig[9] * 3) + (dig[10] * 2);
	nResto = nSoma % 11;	
	if (nResto == 0 || nResto == 1){ nDigV2 = 0 }
	else { nDigV2 = 11 - nResto }
	if ( dig[11] != nDigV2 ) { alert( "O CPF digitado n\u00e3o \u00e9 v\u00e1lido!" ); return false; }

	return true;
}

//-----------
//Valida cnpj
//-----------
function validaCNPJ (cnpj)
{
	var cnpj = cnpj.replace(/[^0-9]+/g, "");

	if ( cnpj == "00000000000000") { alert("CNPJ inválido."); return false; }

	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];

	for(var i=0; i < 13; i++){
		a[i] = cnpj.charAt(i);
	}

	for (var i=0; i < 12; i++) { b += a[i] * c[i+1]; }
	if ((x = b % 11) < 2) { a[12] = 0; }
	else { a[12] = 11-x; }
	b = 0;
	for (var y=0; y < 13; y++) { b += (a[y] * c[y]); }
	if ((x = b % 11) < 2) { a[13] = 0; }
	else { a[13] = 11-x; }

	if ((cnpj.charAt(12) != a[12]) || (cnpj.charAt(13) != a[13]))
	{
	   alert ("CNPJ inválido."); return false;
	}

	return true;
}