function enviarFicha() {
	if (document.forms[0].tx_tipo_cliente.selectedIndex == 0) {
		if (verificaCampoInvalido(document.forms[0].tx_cpf,  'N', 11,'CPF Inválido.')) return
		if (!validaCPF(document.forms[0].tx_cpf)) return
	} else {
		if (verificaCampoInvalido(document.forms[0].tx_cpf,  'N', 14,'CNPJ Inválido.')) return
		if (!validaCNPJ(document.forms[0].tx_cpf)) return
	}
    if (verificaCampoInvalido(document.forms[0].tx_nome, 'T', 5, 'Nome Inválido !')) return
    
    if (verificaCampoInvalido(document.forms[0].tx_end, 'E', 4, 'Endereço Inválido !')) return
    if (verificaCampoInvalido(document.forms[0].tx_bai, 'E', 4, 'Bairro Inválido !')) return
    if (verificaCampoInvalido(document.forms[0].tx_cid, 'E', 4, 'Cidade Inválida !')) return
    if (opcaoInvalida(document.forms[0].tx_estado, 'UF Inválida.')) return
    
    if (verificaCampoInvalido(document.forms[0].tx_cep,  'N', 5,'CEP Inválido.')) return
    if (!emailValido(document.forms[0].tx_email.value)) return
    if (verificaCampoInvalido(document.forms[0].tx_senha, 'E', 4, 'Senha inválida! Tamanho mínimo de 4 caracteres.')) return
    if (!senhaValida(document.forms[0].tx_senha, document.forms[0].tx_senhaconf, 'As senhas digitadas estão diferentes!')) return

	//Valida Entrega    
   	var opcSel=document.forms.frmDadosAssinante.tx_tipo_endentr;
	if (opcSel.options[opcSel.selectedIndex].value=="999") {
	    if (verificaCampoInvalido(document.forms[0].tx_entr_nome, 'T', 5, 'End. Entrega - Nome Inválido !')) return
	    if (verificaCampoInvalido(document.forms[0].tx_entr_end, 'E', 4, 'End. Entrega - Endereço Inválido !')) return
	    if (verificaCampoInvalido(document.forms[0].tx_entr_bai, 'E', 4, 'End. Entrega - Bairro Inválido !')) return
	    if (verificaCampoInvalido(document.forms[0].tx_entr_cid, 'E', 4, 'End. Entrega - Cidade Inválida !')) return
	    if (opcaoInvalida(document.forms[0].tx_entr_estado, 'End. Entrega - UF Inválida.')) return
	    if (verificaCampoInvalido(document.forms[0].tx_entr_cep,  'N', 5,'End. Entrega - CEP Inválido.')) return
		
	}
    
    if (!temItemSelecionado()) return
    
	document.forms[0].submit();
}

function enviar2ViaBoleto() {
	if (!temItemSelecionado2Via()) return
	document.forms[1].submit();
}
function enviarDatasLista(){
	document.forms[0].submit();
}
function enviarLoginAdmin() {
	if (verificaCampoInvalido(document.forms[0].tx_matric, 'N', 1,'Matrícula inválida.')) return
    if (verificaCampoInvalido(document.forms[0].tx_senha, 'E', 4, 'Senha inválida! Tamanho mínimo de 4 caracteres.')) return
	document.forms[0].submit();
}

function enviarNovaCompra() {
	document.forms[0].submit();
}

function enviarNovaCompra2() {
	//Valida Entrega
	
   	var opcSel=document.forms.frmNovaCompra.tx_tipo_endentr;

	if (opcSel.options[opcSel.selectedIndex].value=="999") {
	    if (verificaCampoInvalido(document.forms[0].tx_entr_nome, 'T', 5, 'End. Entrega - Nome Inválido !')) return
	    if (verificaCampoInvalido(document.forms[0].tx_entr_end, 'E', 4, 'End. Entrega - Endereço Inválido !')) return
	    if (verificaCampoInvalido(document.forms[0].tx_entr_bai, 'E', 4, 'End. Entrega - Bairro Inválido !')) return
	    if (verificaCampoInvalido(document.forms[0].tx_entr_cid, 'E', 4, 'End. Entrega - Cidade Inválida !')) return
	    if (opcaoInvalida(document.forms[0].tx_entr_estado, 'End. Entrega - UF Inválida.')) return
	    if (verificaCampoInvalido(document.forms[0].tx_entr_cep,  'N', 5,'End. Entrega - CEP Inválido.')) return
		
	}

	if (!temItemSelecionado()) return
	document.forms[0].submit();
}

function enviarDadosLogin() {
    if (!emailValido(document.forms[0].tx_email.value)) return
    if (verificaCampoInvalido(document.forms[0].tx_senha, 'E', 4, 'Senha inválida! Tamanho mínimo de 4 caracteres.')) return
	document.forms[0].submit();
}

function enviarDadosLoginANT() {
	alert(document.getElementById('formAtual').value)
    if (!emailValido(document.forms[0].tx_email.value)) return
    if (verificaCampoInvalido(document.forms[0].tx_senha, 'E', 4, 'Senha inválida! Tamanho mínimo de 4 caracteres.')) return
    document.getElementById('formAtual').value='FormLoginCliente';
	document.forms[0].submit();
}

function setarFormAtual(nomeForm) {
	document.forms[0].formAtual.value = nomeForm;
}

function enviarSenha() {
    if (!emailValido(document.forms[0].tx_email.value)) return
    document.forms[0].submit();
}

function enviarSenhaANT() {
	alert(document.getElementById('formAtual').value)
    if (!emailValido(document.forms[0].tx_email.value)) return
    document.getElementById('formAtual').value='FormEsqueciSenha';
    document.forms[0].submit();
}


function enviarLogin() {
	if (verificaCampoInvalido(document.forms[0].tx_matric, 'N', 1,'Matrícula inválida.')) return
	document.forms[0].submit();
}

function verificaCampoInvalido(campo, tipodocampo, tamanhominimo, msg) {
         camposvalidos = ''
         msgcompl = '';
         if (tipodocampo== 'N') {
             camposvalidos = '0123456789'
             msgcompl = 'Só é permitido o uso de Números.'
         }
         if (tipodocampo== 'C') {
             camposvalidos = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
             msgcompl = 'Só é permitido o uso de letras sem acentuação.'
         }
         if (tipodocampo== 'A') {
            camposvalidos = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
            msgcompl = 'Só é permitido o uso de números e letras sem acentuação.'
         }
         if (tipodocampo== 'T') {
            camposvalidos = ' 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
            msgcompl = 'Só é permitido o uso de números e letras sem acentuação.'
         }
         if (tipodocampo== 'E') {
            camposvalidos = ' ,/.-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
            msgcompl = 'Só é permitido o uso de números e letras sem acentuação.'
         }
         if (tipodocampo== 'I') {
            camposvalidos = '- 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
            msgcompl = 'Só é permitido o uso de números, ponto(.), hífen(-) e letras sem acentuação.'
         }
         erro = false
         if (campo.value.length < tamanhominimo )
            erro = true
         else {
              if (tipodocampo == 'C' && campo.value.charAt(0)== ' ')
                  erro= true
              else {
                 for (i=0;i < campo.value.length;i++)
                 	
                   if (camposvalidos.indexOf(campo.value.charAt(i)) < 0) {
                       //msg = msg + ' ( ' + campo.value.charAt(i) + ' ) caracter inválido.'
                       erro = true
                   }
                   if (erro)  msg = msg + msgcompl
              }
         }
         if (erro) {
            alert(msg);
            campo.focus();
         }
         return erro
}

function emailValido(email){
      resp = false;
      if  (email.length < 3) {
           alert("Email inválido !");
           document.forms[0].tx_email.focus();
      }
      else {
           erro  = false;
           achou = 0;
           for (i=0;i < email.length; i++) {
                if (email.charAt(i) == " ") {
                    erro = true;
                    break;
                }
                if (email.charAt(i) == "'") {
                    erro = true;
                    break;
                }
                if (email.charAt(i) == ";") {
                    erro = true;
                    break;
                }
                if (email.charAt(i) == "@") {
                    if (achou == 0) {
                        achou = i;
                    }
                    else {
                        erro = true;
                        break;
                    }
                }
           }
           if (erro || achou == (email.length - 1) || achou == 1 || achou == 0) {
               alert("Email Inválido !");
               document.forms[0].tx_email.focus();
           }
           else {
               resp = true;
           }
      }
      return resp;
}

function enviarConsultaporCurso() {
    document.forms[0].wRotina.value = 2;
    document.forms[0].submit();
}

function enviarConsultaOrientacao() {
    document.forms[0].wRotina.value = 3;
    document.forms[0].submit();
}

function enviarConsultaMinioficinas() {
    document.forms[0].wRotina.value = 4;
    document.forms[0].submit();
}

function enviarConsultaTodos() {
    document.forms[0].wRotina.value = 5;
    document.forms[0].submit();
}


function  calculaDigitoCPF(campo,tam) {
       wsoma = 0;
       for (ind=0; ind < tam; ind++)
            wsoma = wsoma + campo.charAt(ind) * (tam+1-ind);
       valor = wsoma / 11;
       vl    = Math.floor(wsoma / 11);
       resto = Math.round((valor-vl)*11);
       if (resto == 0 || resto == 1)
           resto = 0;
       else
           resto = 11 - resto;
       return resto;
}

function verificaCPF() {
    campo = document.forms[0].tx_cpf.value;
    cpfinvalido = false;
    if (campo.length == 0) return;
    if (campo.length != 11) {
        alert('CPF Inválido - Erro na quantidade de dígitos');
        document.forms[0].tx_cpf.focus();
    }
    else {
        if (calculaDigitoCPF(campo,9) != campo.charAt(9))
            cpfinvalido = true;
        else {
            if (calculaDigitoCPF(campo,10) != campo.charAt(10))
                cpfinvalido = true;
        }
        if (cpfinvalido) {
            alert('CPF Inválido');
            document.forms[0].tx_cpf.focus();
            cpfinvalido = true;
        }
    }
}

function digitoCPFInvalido(cpf){
       campo = cpf.value;
       cpfinvalido = false;
       if (calculaDigitoCPF(campo,9) != campo.charAt(9))
           cpfinvalido = true;
       else {
           if (calculaDigitoCPF(campo,10) != campo.charAt(10))
               cpfinvalido = true;
       }
       if (cpfinvalido) {
          alert('Digito do CPF Inválido');
          cpf.focus();
       }
       return cpfinvalido;
}

function validaCPF(cpf)   
{   
  erro = new String;   
  
    if (cpf.value.length == 11)   
    {     
            cpf.value = cpf.value.replace('.', '');   
            cpf.value = cpf.value.replace('.', '');   
            cpf.value = cpf.value.replace('-', '');   
  
            var nonNumbers = /\D/;   
       
            if (nonNumbers.test(cpf.value))   
            {   
                    erro = "A verificacao de CPF suporta apenas números!";   
            }   
            else   
            {   
                    if (cpf.value == "00000000000" ||   
                            cpf.value == "11111111111" ||   
                            cpf.value == "22222222222" ||   
                            cpf.value == "33333333333" ||   
                            cpf.value == "44444444444" ||   
                            cpf.value == "55555555555" ||   
                            cpf.value == "66666666666" ||   
                            cpf.value == "77777777777" ||   
                            cpf.value == "88888888888" ||   
                            cpf.value == "99999999999") {   
                               
                            erro = "Número de CPF inválido!"   
                    }   
       
                    var a = [];   
                    var b = new Number;   
                    var c = 11;   
  
                    for (i=0; i<11; i++){   
                            a[i] = cpf.value.charAt(i);   
                            if (i < 9) b += (a[i] * --c);   
                    }   
       
                    if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }   
                    b = 0;   
                    c = 11;   
       
                    for (y=0; y<10; y++) b += (a[y] * c--);   
       
                    if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }   
       
                    if ((cpf.value.charAt(9) != a[9]) || (cpf.value.charAt(10) != a[10])) {   
                        erro = "Número de CPF inválido.";   
                    }   
            }   
    }   
    else   
    {   
        if(cpf.value.length == 0)   
            return false   
        else   
            erro = "Número de CPF inválido.";   
    }   
    if (erro.length > 0) {   
            alert(erro);   
            cpf.focus();   
            return false;   
    }     
    return true;       
}   
  
//envento onkeyup   
function maskCPF(CPF) {   
    var evt = window.event;   
    kcode=evt.keyCode;   
    if (kcode == 8) return;   
    if (CPF.value.length == 3) { CPF.value = CPF.value + '.'; }   
    if (CPF.value.length == 7) { CPF.value = CPF.value + '.'; }   
    if (CPF.value.length == 11) { CPF.value = CPF.value + '-'; }   
}   
  
// evento onBlur   
function formataCPF(CPF)   
{   
    with (CPF)   
    {   
        value = value.substr(0, 3) + '.' +   
                value.substr(3, 3) + '.' +   
                value.substr(6, 3) + '-' +   
                value.substr(9, 2);   
    }   
}   
function retiraFormatacao(CPF)   
{   
    with (CPF)   
    {   
        value = value.replace (".","");   
        value = value.replace (".","");   
        value = value.replace ("-","");   
        value = value.replace ("/","");   
    }   
}

function opcaoInvalida(campo,msg) {
         if (campo.selectedIndex == 0) {
            campo.focus()
            alert(msg)
            return true
         } 
         return false
}


function senhaValida(campo1, campo2 ,msg) {
         if (campo1.value == campo2.value) {
            campo1.focus()
            return true
         } else {
            alert(msg)
         }
         return false
}

function temItemSelecionado() {
	temSelecao = false;
    for (i=0; i<=document.forms[0].length-1; i++)
        if (document.forms[0].elements[i].name == 'chk_avulso') {
             if (document.forms[0].elements[i].checked) {
             	temSelecao = true;
             	document.forms[0].elements[i].focus();
             	break;
             }
		} else {
			if (document.forms[0].elements[i].name == 'chk_assinar') {
				if (document.forms[0].elements[i].checked) {
					temSelecao = true;
					document.forms[0].elements[i].focus();
					break;
				}
			}
		}
	if (!temSelecao) {
		alert('Selecione alguma Assinatura ou Exemplar Avulso!')
	}		  
	return temSelecao;
}


function temItemSelecionado2Via() {
	temSelecao = false;
    for (i=0; i<=document.forms[1].length-1; i++)
        if (document.forms[1].elements[i].name == 'radio_compras') {
             if (document.forms[1].elements[i].checked) {
             	temSelecao = true;
             	document.forms[1].elements[i].focus();
             	break;
             }
		} 
	if (!temSelecao) {
		alert('Selecione alguma compra para emitir a 2a via do boleto!')
	}		  
	return temSelecao;
}


         function validaCNPJ(pCNPJ) {
                 CNPJ = pCNPJ.value;
                 erro = new String;
               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);
                       return false;
               } 
               return true;
       }

function opcaoEndEntrega()
{
	var opcSel=document.forms.frmDadosAssinante.tx_tipo_endentr;
	alert(opcSel.options[opcSel.selectedIndex].index);	
}     

function endEntrega()
{
	var mudaHtml=document.getElementById("end_entrega");
	var opcSel=document.forms.frmDadosAssinante.tx_tipo_endentr;
	if (opcSel.options[opcSel.selectedIndex].value=="999") {
		//mudaHtml.innerHTML="TESTE<br/><br/>Teste";
		//mudaHtml.style.visibility = "visible";
		mudaHtml.style.display = "";
	} else {
		//mudaHtml.innerHTML="TESTE 2<br/><br/>Teste";
		//mudaHtml.style.visibility = "hidden";
		mudaHtml.style.display = "none";
	}
		
}  

function endEntregaNovaCompra()
{
	var mudaHtml=document.getElementById("end_entrega");
	var opcSel=document.forms.frmNovaCompra.tx_tipo_endentr;
	if (opcSel.options[opcSel.selectedIndex].value=="999") {
		//mudaHtml.innerHTML="TESTE<br/><br/>Teste";
		//mudaHtml.style.visibility = "visible";
		mudaHtml.style.display = "";
	} else {
		//mudaHtml.innerHTML="TESTE 2<br/><br/>Teste";
		//mudaHtml.style.visibility = "hidden";
		mudaHtml.style.display = "none";
	}
		
}  
