// JavaScript Document
function focar(obj,limpar)
{
	if (limpar==true)
		obj.value="";
	obj.focus();
}
function validaCPF(cpf){
        var i = soma = soma2 = 0;
		if (cpf.length<11) return false;
        for(i=0;i<10;i++) {
                if (i<9)
					soma += cpf.charAt(i) * (10 - i);
                soma2 += cpf.charAt(i) * (11 - i);
        }
        if (((soma % 11) < 2 ? 0 : 11 - (soma  % 11 )) != cpf.charAt(9)) return false;
        return (((soma2 % 11 ) < 2 ? 0 : 11 - (soma2 % 11 )) != cpf.charAt(10)) ? false : true;
}
function validaFormCheckCPF(){
	var id0 = "p4r4m3tRo";
	if (!validaCPF(document.getElementById(id0).value)) {
		alert("Informe o CPF corretamente!");
		document.getElementById(id0).focus();
		return false;
	}
	//document.getElementById("p4r4m3tRo").disabled="disabled";
	document.getElementById("form1").submit();
}
function callFormCadPF(){
	if (document.getElementById("form1").action!="") {
		document.getElementById("form1").submit();
	}
}
function voltar(){
	history.back();
	return;
}
function validaFormCadPF(tipo) {
	var a = document.getElementById("RG");
	var b = document.getElementById("ORGAO_RG");
	var c = document.getElementById("NOME");
	var d = document.getElementById("DTA_NASC");
	var e = document.getElementById("EST_CIVIL");
	var f = document.getElementById("idTIPO_ENDERECO");
	var g = document.getElementById("END_LOGRADOURO");
	var h = document.getElementById("END_NUM");
	var i = document.getElementById("END_BAIRRO");
	var j = document.getElementById("END_CIDADE");
	var k = document.getElementById("END_UF");
	var l = document.getElementById("END_CEP");
	var m = document.getElementById("EMAIL");
	var n = document.getElementById("FONE");
	var o = document.getElementById("SENHA");
	var p = document.getElementById("SENHA2");
	var q = document.getElementById("LEMBRETE_SENHA");
	if (a.value.length==0) { alert("Preencha o RG!"); a.focus(); } else
	if (b.value.length==0) { alert("Preencha o Órgão Expedidor!"); b.focus(); } else
	if (c.value.length==0) { alert("Preencha o nome completo!"); c.focus(); } else
	if (d.value.length==0) { alert("Preencha a data de nascimento!"); d.focus(); } else
	if (e.value.toString()=="") { alert("Escolha o estado civil!"); e.focus(); } else
	if (f.value.toString()=="") { alert("Escolha o tipo de endereço!"); f.focus(); } else
	if (g.value.length==0) { alert("Preencha o logradouro!"); g.focus(); } else
	if (h.value.length==0) { alert("Preencha o numero!"); h.focus(); } else
	if (i.value.length==0) { alert("Preencha o bairro!"); i.focus(); } else
	if (j.value.length==0) { alert("Preencha a cidade!"); j.focus(); } else
	if (k.value.length==0) { alert("Preencha a UF!"); k.focus(); } else
	if (l.value.length==0) { alert("Preencha o CEP!"); l.focus(); } else
	if (m.value.length==0) { alert("Preencha o e-mail!"); m.focus(); } else
	if (n.value.length==0) { alert("Preencha o telefone!"); n.focus(); } else
	if (tipo==1 ) {
		if (o.value.length==0) { alert("Preencha a senha!"); o.focus(); return true;} else
		if (p.value.length==0) { alert("Repita a senha!"); p.focus(); return true;} else
		if (o.value!=p.value)  { alert("A senha não confere!"); p.focus(); return true;}
	} else
	if (tipo==2 & (o.value.length!=0 || p.value.length!=0)) {
		if (o.value.length==0) { alert("Preencha a senha atual!"); o.focus(); return true;} else
		if (p.value.length==0) { alert("Preencha a nova senha!"); p.focus(); return true;}
	}
	if (q.value.length==0) { alert("Digite o lembrete da senha!"); q.focus(); } else
	{
		document.getElementById("VALIDADO").value=true;
		document.getElementById("form1").submit();
	}
	return true;
}
function mascaraCNPJ(obj) {
	// onClick="focar(this,true)" onKeyPress="return MascaraCNPJ(this)">
	i = obj.value.length;
	if (i==2 || i==6)
		obj.value += "."
	else if (i==10)
		obj.value += "/"
	else if (i==15)
		obj.value += "-";
	return true;
}

function validaCNPJ(obj) {
	CNPJ = obj.value;
	x =  CNPJ.substring(0,2);
	x += CNPJ.substring(3,6);
	x += CNPJ.substring(7,10);
	x += CNPJ.substring(11,15);
	x += CNPJ.substring(16,18);
	CNPJ = x;
	msg = "É necessário preencher corretamente o número do CNPJ! \n\n";		 	
	erro = new String;
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "No campo CNPJ, digite apenas números! \n\n";
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++)
	{
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2)
		a[12] = 0
	else
		a[12] = 11-x;
	b = 0;
	for (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]))
		erro +="O CNPJ digitado é inválido!";
	if (erro.length > 0)
	{
		alert(erro);
		focar(obj,true);
		return false;
	} else
	return true;
}
function base64_encode(data) {
	var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
	if (typeof this.window['atob'] == 'function') {
	    return atob(data);
	}
	if (!data) {
		return data;
	}
	//data = utf8_encode(data+'');
	do {
		o1 = data.charCodeAt(i++);
		o2 = data.charCodeAt(i++);
		o3 = data.charCodeAt(i++);
		bits = o1<<16 | o2<<8 | o3;
		h1 = bits>>18 & 0x3f;
		h2 = bits>>12 & 0x3f;
		h3 = bits>>6 & 0x3f;
		h4 = bits & 0x3f;
		tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
	} while (i < data.length);
	enc = tmp_arr.join('');
	switch (data.length % 3) {
	case 1:	enc = enc.slice(0, -2) + '==';
			break;
	case 2: enc = enc.slice(0, -1) + '=';
			break;
	}
	return enc;
}
function Tecla(evento,campo) {
	var resp = false;
	if (document.all)
		var x = evento.keyCode;
	else 
		var x = evento.which;
		
	if (campo==0) //só numeros
	{
		if (x>47 && x<58 || x==8)
			resp=true;
	}
	else if (campo==1) //numeros e traço
	{
		if (x>47 && x<58 || x==8 || x==45)
			resp=true
	}
	else if (campo==2) //numeros e barra
	{
		if (x>47 && x<58 || x==8 || x==47)
			resp=true
	}
	else if (campo==3) //numeros e vírgula
	{
		if (x>47 && x<58 || x==8 || x==44)
			resp=true
	}
	else if (campo==4) //só numeros e retorna o char quando backspace
	{
		if (x>47 && x<58) resp=true;
		else
		if (x==8) resp=x;
	}
	if (resp)
		return x
	else
		return resp;
}
function validaFormIncluirPontos() {
	var a = document.getElementById("DTA_CONSUMO");
	var b = document.getElementById("idPARCEIRO");
	var c = document.getElementById("OBJ_CONSUMIDO");
	var d = document.getElementById("VAL_CONSUMO");
	if (a.value.length==0) { alert("Preencha a data da compra (Quando)!"); a.focus(); } else
	if (b.value.toString()=="") { alert("Escolha o local de compra (Onde)!"); b.focus(); } else
	if (c.value.length==0) { alert("Preencha a natureza da compra (O que)!"); c.focus(); } else
	if (d.value.length==0) { alert("Preencha o valor total da compra (Valor)!"); d.focus(); } else
	{
		document.getElementById("VALIDADO").value=true;
		document.getElementById("FORM1").submit();
	}
	return;
}
function maskData(campo,evento){
	var tecla = Tecla(evento,4);
	if (tecla==8) return true;
	else
	if (tecla!=false) {
      if (campo.value.length == 2) campo.value = campo.value + "/";
	  else
      if (campo.value.length == 5) campo.value = campo.value + "/";  
	  return true;
   } else return false;
}
function validaData(campo){
	if ((campo.value.length>0)&(campo.value.length!=10)) {
		alert("Digite a data no formato DD/MM/AAAA.");
		campo.focus();
	} else
	if (campo.value.length==10) {
		var dia = campo.value.substr(0,2);
		var mes = campo.value.substr(3,2);
		var ano = campo.value.substr(6,4);
		if ((Number(dia)<1)||(Number(dia)>31)) {
			alert("Digite um dia válido (01 a 31).");
			campo.focus();
		} else
		if ((Number(mes)<1)||(Number(mes)>12)) {
			alert("Digite um mês válido (01 a 12).");
			campo.focus();
		} else {
			var dta = new Date();
			if ((Number(ano)<1909)||(Number(ano)>dta.getFullYear())) {
				alert("Digite um ano válido (1909 a "+dta.getFullYear()+").");
				campo.focus();
			}
		}
	}
}
function maskDecimal(campo,evento){
	var tecla = Tecla(evento,3);
	if (tecla!=false) {
		if (tecla==44 & campo.value.indexOf(",")!=-1)
			return false;
	  	else
	  		return true;
   } else return false;
}
function maskFone(campo,evento){
	var tecla = Tecla(evento,4);
	if (tecla==8) return true;
	else
	if (tecla!=false) {
		switch (campo.value.length) {
			case 1: campo.value = (campo.value!="(")?"("+campo.value:campo.value;break;
			case 3: campo.value = campo.value+") ";break;
			case 4: campo.value = campo.value+" ";break;
			case 9: campo.value = campo.value+"-";break;
		}
     	return true;
   } else return false;
}
function validaFone(campo){
	if ((campo.value.length>0)&(campo.value.length!=14)) {
		alert('Digite o número com DDD no formato "(XX) XXXX-XXXX".');
		campo.focus();
	}
	return true;
}
function upper(campo){
	campo.value = campo.value.toUpperCase();
}
function redireciona(url){
	document.location = url;	
}
//Validar formulário de contato
function validarContato(){

	if(document.getElementById("nomeContato").value == ""){
		alert("Por favor, preencha o nome.");
		document.getElementById("nomeContato").focus();
	}else if(document.getElementById("emailContato").value == ""){
		alert("Por favor, preencha o e-mail corretamente.");
		document.getElementById("emailContato").focus();
	}else if(document.getElementById("assuntoContato").value == ""){
		alert("Por favor, preencha o assunto corretamente.");
		document.getElementById("assuntoContato").focus();
	}else if(document.getElementById("profissaoContato").value == ""){
		alert("Por favor, preencha a profissão.");
		document.getElementById("profissaoContato").focus();
	}else if(document.getElementById("profissaoContato").value == ""){
		alert("Por favor, preencha a profissão.");
		document.getElementById("profissaoContato").focus();
	}else if(document.getElementById("recadoContato").value == ""){
		alert("Por favor, preencha o recado.");
		document.getElementById("recadoContato").focus();
	}else{
		document.getElementById("frmContato").submit();
	}	

}

function validaFormLogin() {
	if(document.getElementById("cp1").value == ""){
		alert("Por favor, preencha o login.");
		document.getElementById("cp1").focus();
	}else if(document.getElementById("cp2").value == ""){
		alert("Por favor, preencha sua senha.");
		document.getElementById("cp2").focus();
	}else document.getElementById("frmLogin").submit();
	
	return true;	
}