﻿//exibe as umagens do menu ativadas
function coloreMenu (option, id)
{
id.src = "imagens/" + option + "_on.jpg";
}
//exibe as imagens do menu desativadas
function descoloreMenu (option, id)
{
id.src = "imagens/" + option + ".jpg";
}
//esconde div
function escondeDiv(modo,div){
	if(modo == 1){
		document.getElementById(div).style.display = 'block';
	}else{
		document.getElementById(div).style.display = 'none';
	}
}
//div balanco
function divBalanco(modo){
	if(modo == 1){
		document.getElementById("balance").style.display = 'block';
		document.getElementById("operacionais").style.display = 'none';
	}else{
		document.getElementById("balance").style.display = 'none';
		document.getElementById("operacionais").style.display = 'block';
	}
}
//exibe ou nao o formulario do conjuge
function parteConjuge(){
	if (document.frmFisico.ddlcivil.value == "Casado"){
	document.getElementById("regime").style.display = 'block';
	document.getElementById("conjuge").style.display = 'block';
	}else{
	document.getElementById("regime").style.display = 'none';
	document.getElementById("conjuge").style.display = 'none';
	}
}
//mascaras
function mascaras(objForm, strField, sMask, evtKeyPress) {
     var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

     if(document.all) { // Internet Explorer
       nTecla = evtKeyPress.keyCode; }
     else if(document.layers) { // Nestcape
       nTecla = evtKeyPress.which;
     }

     sValue = objForm[strField].value;

     // Limpa todos os caracteres de formatação que
     // já estiverem no campo.
     sValue = sValue.toString().replace( "-", "" );
     sValue = sValue.toString().replace( "-", "" );
     sValue = sValue.toString().replace( ".", "" );
     sValue = sValue.toString().replace( ".", "" );
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( "(", "" );
     sValue = sValue.toString().replace( "(", "" );
     sValue = sValue.toString().replace( ")", "" );
     sValue = sValue.toString().replace( ")", "" );
     sValue = sValue.toString().replace( " ", "" );
     sValue = sValue.toString().replace( " ", "" );
     fldLen = sValue.length;
     mskLen = sMask.length;

     i = 0;
     nCount = 0;
     sCod = "";
     mskLen = fldLen;

     while (i <= mskLen) {
       bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
       bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

       if (bolMask) {
         sCod += sMask.charAt(i);
         mskLen++; }
       else {
         sCod += sValue.charAt(nCount);
         nCount++;
       }

       i++;
     }

     objForm[strField].value = sCod;

     if (nTecla != 8) { // backspace
       if (sMask.charAt(i-1) == "9") { // apenas números...
         return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
       else { // qualquer caracter...
         return true;
       } }
     else {
       return true;
     }
}
//enter como tab
function mudaFoco (field, event) {
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (keyCode == 13) {
var i;
for (i = 0; i < field.form.elements.length; i++)
if (field == field.form.elements[i])
break;
i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus();
return false;
}
else
return true;
}
//valida geral fisico
function geral(){
	if(document.getElementById("nome").value == ""){
	alert("O campo Nome é de preenchimento obrigatório");
	document.getElementById("nome").focus();
	return false;
	}
	if(document.getElementById("cep").value == ""){
	alert("O campo CEP é de preenchimento obrigatório");
	document.getElementById("cep").focus();
	return false;
	}
	if(document.getElementById("bairro").value == ""){
	alert("O campo Bairro é de preenchimento obrigatório");
	document.getElementById("bairro").focus();
	return false;
	}
	if(document.getElementById("cidade").value == ""){
	alert("O campo Cidade é de preenchimento obrigatório");
	document.getElementById("cidade").focus();
	return false;
	}
	if(document.getElementById("estado").value == ""){
	alert("O campo Estado é de preenchimento obrigatório");
	document.getElementById("estado").focus();
	return false;
	}
	if(document.getElementById("endereco").value == ""){
	alert("O campo Endereço é de preenchimento obrigatório");
	document.getElementById("endereco").focus();
	return false;
	}
	if(document.getElementById("numero").value == ""){
	alert("O campo Numero é de preenchimento obrigatório");
	document.getElementById("numero").focus();
	return false;
	}
	if(document.getElementById("cpf").value == ""){
	alert("O campo CPF é de preenchimento obrigatório");
	document.getElementById("cpf").focus();
	return false;
	}
	if (checacpf(document.getElementById("cpf").value)){
	}else{
	errors="1";if (errors) alert('CPF invalido, por favor digite-o novamente.');
	document.retorno = (errors == '');
	document.getElementById("cpf").focus();
	return false;
	}
	if(document.getElementById("dtnasc").value == ""){
	alert("O campo Data de Nascimento é de preenchimento obrigatório");
	document.getElementById("cpf").focus();
	return false;
	}
	if(document.getElementById("ddlcivil").value == ""){
	alert("O campo Estado Civil é de seleção obrigatória");
	document.getElementById("cpf").focus();
	return false;
	}
	//regime de casamento
	if(document.getElementById("rg").value == ""){
	alert("O campo RG é de preenchimento obrigatório");
	document.getElementById("rg").focus();
	return false;
	}
	if(document.getElementById("orgao").value == ""){
	alert("O campo Órgão Emissor é de preenchimento obrigatório");
	document.getElementById("orgao").focus();
	return false;
	}
	return true;
}
function geral2(){
	if(document.getElementById("razao").value == ""){
	alert("O campo Razao é de preenchimento obrigatório");
	document.getElementById("razao").focus();
	return false;
	}
	CNPJ = document.frmJuridico.cnpj.value;
	erro = new String;
	if (CNPJ.length < 18) erro += "O campo CNPJ é de preenchimento obrigatório! \n\n";
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
	if (erro.length == 0) erro += "É necessario preencher corretamente o numero do CNPJ! \n\n";
	}
	//substituir os caracteres que nao sao numeros
	if(document.layers && parseInt(navigator.appVersion) == 4){
		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;	
	} else {
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace(".","");
		CNPJ = CNPJ.replace("-","");
		CNPJ = CNPJ.replace("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \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 +="Digite o CNPJ corretamente";
	}
	if (erro.length > 0){
		alert(erro);
		document.frmJuridico.cnpj.focus();
		return false;
	}
	if(document.getElementById("cep").value == ""){
	alert("O campo CEP é de preenchimento obrigatório");
	document.getElementById("cep").focus();
	return false;
	}
	if(document.getElementById("bairro").value == ""){
	alert("O campo Bairro é de preenchimento obrigatório");
	document.getElementById("bairro").focus();
	return false;
	}
	if(document.getElementById("cidade").value == ""){
	alert("O campo Cidade é de preenchimento obrigatório");
	document.getElementById("cidade").focus();
	return false;
	}
	if(document.getElementById("estado").value == ""){
	alert("O campo Estado é de preenchimento obrigatório");
	document.getElementById("estado").focus();
	return false;
	}
	if(document.getElementById("endereco").value == ""){
	alert("O campo Endereço é de preenchimento obrigatório");
	document.getElementById("endereco").focus();
	return false;
	}
	if(document.getElementById("numero").value == ""){
	alert("O campo Numero é de preenchimento obrigatório");
	document.getElementById("numero").focus();
	return false;
	}
	if(document.getElementById("dddtel").value == ""){
	alert("O campo DDD Telefone é de preenchimento obrigatório");
	document.getElementById("dddtel").focus();
	return false;
	}
	if(document.getElementById("telefone").value == ""){
	alert("O campo Telefone é de preenchimento obrigatório");
	document.getElementById("telefone").focus();
	return false;
	}
	if(document.getElementById("capital").value == ""){
	alert("O campo Capital Social é de preenchimento obrigatório");
	document.getElementById("capital").focus();
	return false;
	}
	if(document.getElementById("responsa").value == ""){
	alert("O campo Responsavel é de preenchimento obrigatório");
	document.getElementById("numero").focus();
	return false;
	}
	if(document.getElementById("rcargo").value == ""){
	alert("O campo Cargo do Responsavel é de preenchimento obrigatório");
	document.getElementById("rcargo").focus();
	return false;
	}
	return true;
}
//validacao cpf
function checacpf (form1){
	if (form1.length != 11 || form1 == "00000000000" || form1 == "11111111111" || form1 == "22222222222" || form1 == "33333333333" || form1 == "44444444444" || form1 == "55555555555" || form1 == "66666666666" || form1 == "77777777777" || form1 == "88888888888" || form1 == "99999999999")
	return false;
	soma = 0;
	for (i=0; i < 9; i ++)
	soma += parseInt(form1.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
	resto = 0;
	if (resto != parseInt(form1.charAt(9)))
	return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
	soma += parseInt(form1.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
	resto = 0;
	if (resto != parseInt(form1.charAt(10)))
	return false;
	return true;
}
function validaCNPJ (){
CNPJ = document.getElementById("cnpj").value;
                 erro = new String;
                 if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! "; 
                 if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
                 if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! ";
                 }
                 //substituir os caracteres que não são números
               if(document.layers && parseInt(navigator.appVersion) == 4){
                       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; 
               } else {
                       CNPJ = CNPJ. replace (".","");
                       CNPJ = CNPJ. replace (".","");
                       CNPJ = CNPJ. replace ("-","");
                       CNPJ = CNPJ. replace ("/","");
               }
               var nonNumbers = /\D/;
               if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! "; 
               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 +="Dígito verificador com problema!";
               }
               if (erro.length > 0){
                       alert(erro);
                       document.getElementById("txtcnpj").focus();
                       return false;
               } else {
                       //alert("CNPJ valido!");
               }
               return true;

}
//validacao email
function validaEmail (){
	var oRegEmail = /^[a-z0-9\._\-]+\@[a-z0-9\._\-]+\.[a-z]{2,3}$/i;
	if (!oRegEmail.test(document.getElementById("txtemail").value)){
	alert("E-mail invalido, por favor digite-o novamente.");
	document.getElementById("txtemail").focus();
	return false;
	}
}
//ajax cep
function pegaCEP() {
 cep = document.getElementById("cep").value;
 ajax = ajaxInit();
 if(ajax) {
   ajax.open("GET", "ajax_cep.asp?cep=" + cep, true);
   ajax.onreadystatechange = function() {
     if(ajax.readyState == 4) {
       if(ajax.status == 200) {
         if (ajax.responseText.length < 5){
         	alert("Digite um CEP válido");
         	document.getElementById("cep").value = '';
         	document.getElementById("cep").focus();
         }else{
         	tudo = ajax.responseText.length;
         	virgula = ajax.responseText.indexOf(",");
         	barra = ajax.responseText.indexOf("/");
         	traco = ajax.responseText.indexOf("-");
         	
         	document.getElementById("endereco").value = ajax.responseText.substring(0,virgula)
		  	document.getElementById("bairro").value = ajax.responseText.substring(virgula + 1, barra)
		  	document.getElementById("cidade").value = ajax.responseText.substring(barra + 1, traco)
         	document.getElementById("estado").value = ajax.responseText.substr(traco + 1, 2)
         }
       } else {
       }
     }
   }
   ajax.send(null);
 }
}
function pegaCEPmais() {
 cep = document.getElementById("cep2").value;
 ajax = ajaxInit();
 if(ajax) {
   ajax.open("GET", "ajax_cep.asp?cep=" + cep, true);
   ajax.onreadystatechange = function() {
     if(ajax.readyState == 4) {
       if(ajax.status == 200) {
         if (ajax.responseText.length < 5){
         	alert("Digite um CEP válido");
         	document.getElementById("cep2").value = '';
         	document.getElementById("cep2").focus();
         }else{
         	tudo = ajax.responseText.length;
         	virgula = ajax.responseText.indexOf(",");
         	barra = ajax.responseText.indexOf("/");
         	traco = ajax.responseText.indexOf("-");
         	
         	document.getElementById("endereco2").value = ajax.responseText.substring(0,virgula)
		  	document.getElementById("bairro2").value = ajax.responseText.substring(virgula + 1, barra)
		  	document.getElementById("cidade2").value = ajax.responseText.substring(barra + 1, traco)
         	document.getElementById("estado2").value = ajax.responseText.substr(traco + 1, 2)
         }
       } else {
       }
     }
   }
   ajax.send(null);
 }
}
//campo monetario
function moeda(fld, milSep, decSep, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13)
return true;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';

// Aqui tem que testar: se $Column->{decimals} for 3 coloca + '0' + '0' + senão, apenas + '0' +
if (len == 1) fld.value = '0' + decSep + '0' + aux; // Alterei coloquei + '0' , decSep = , (vírgula)

// Aqui tem que testar: se $Column->{decimals} for 3 coloca + '0' + senão, não coloca
if (len == 2) fld.value = '0' + decSep + aux; // Alterei coloquei + '0'

// Aqui tem que testar: se $Column->{decimals} for 3 coloca essa linha senão não coloca
//if (len == 3) fld.value = '0' + decSep + aux + aux2; // Adicionei esta linha

// Aqui tem que colocar a variável $decimal
if (len > 2) { // Alterei de 2 para 3
 aux2 = '';

 // A cada três caracteres adiciona um milSep (ponto ".")
 for (j = 0, i = len - 3; i >= 0; i--) {
  if (j == 3) {
   aux2 += milSep;
   j = 0;
  }
  aux2 += aux.charAt(i);
  j++;
 }

 fld.value = '';
 len2 = aux2.length;

 // Aqui tem que testar: se $Column->{decimals} for 3 coloca i > 0 senão coloca i >= 0
 for (i = len2 - 1; i >= 0; i--) // Alterei de i >= 0 para i > 0
 fld.value += aux2.charAt(i);

 // Aqui tem que coloca a variável mo lugar do 3
 fld.value += decSep + aux.substr(len - 2, len); // O número dois é o valor chave (número de casas que vem depois da vírgula)
}
return false;
}
//valida formulario de envio
function valContato(){
	if (document.getElementById("nome").value == ""){
	alert("O campo Nome é de preenchimento obrigatório");
	document.getElementById("nome").focus();
	return false;
	}
	if (document.getElementById("email").value == ""){
	alert("O campo Email é de preenchimento obrigatório");
	document.getElementById("email").focus();
	return false;
	}
	if (document.getElementById("ddd").value == ""){
	alert("O campo DDD é de preenchimento obrigatório");
	document.getElementById("ddd").focus();
	return false;
	}
	if (document.getElementById("telefone").value == ""){
	alert("O campo Telefone é de preenchimento obrigatório");
	document.getElementById("telefone").focus();
	return false;
	}
	if (document.getElementById("mensagem").value == ""){
	alert("O campo Mensagem é de preenchimento obrigatório");
	document.getElementById("mensagem").focus();
	return false;
	}
	return true;
}
function divs(layerID,mode){
	var currentRef = document.getElementById(layerID).style
	modes = new Array
	modes[0] = 'none'
	modes[1] = 'block'
	if(isNaN(mode)){      
		currentRef.display = (currentRef.display == 'none') ? 'block' : 'none'   
	}else{
		currentRef.display = modes[mode]
	}
}
function ajaxAgenda(){
 usuario = document.getElementById("usuario").value;
 cnpj = document.getElementById("cnpj").value;
 cliente = document.getElementById("cliente").value;
 data = document.getElementById("data").value;
 ajax = ajaxInit();
 if(ajax) {
   ajax.open("GET", "area_salva_agenda.asp?usuario=" + usuario +"&cnpj=" + cnpj + "&cliente=" + cliente + "&data=" + data, true);
   ajax.onreadystatechange = function() {
     if(ajax.readyState == 4) {
       if(ajax.status == 200) {
         if (ajax.responseText.length != 0){
         	document.getElementById("cnpj").value = ""
         	document.getElementById("cliente").value = ""
         	document.getElementById("data").value = ""
         	document.getElementById('compromissos').innerHTML = ""
         	document.getElementById('compromissos').innerHTML = ajax.responseText
         	alert("Compromisso agendado.");
         }
       } else {
       }
     }
   }
   ajax.send(null);
 }
}
function ajaxAgendaAdmin(){
 gerente = document.getElementById("gerente").value;
 if(gerente != ""){
 ajax = ajaxInit();
 if(ajax) {
   ajax.open("GET", "area_lista_compromissos.asp?gerente=" + gerente, true);
   ajax.onreadystatechange = function() {
     if(ajax.readyState == 4) {
       if(ajax.status == 200) {
         if (ajax.responseText.length != 0){
         	document.getElementById('compromissos').innerHTML = ""
         	document.getElementById('compromissos').innerHTML = ajax.responseText
         }else{
         	alert("Nenhum compromisso agendado por este gerente.");
         }
       } else {
       }
     }
   }
   ajax.send(null);
 }
 }
}
