function limpa_campo(){
  d=document.all.pesquisa.value;
  d="";
}
function verificacampo(numbers,letters,others,e)
{
	if(window.event) key = window.event.keyCode
	else if(e) key = e.which
	else return true
	
	S=(others)?others:''
	if(numbers)S+='0123456789'
	if(letters)S+='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZãáàéêíõóôú'
	if(key==null||key==0||key==8||key==9||key==13||key==27)return true
	else if(S.indexOf(String.fromCharCode(key))!=-1)return true
	else return false
}

/**
* Formata o Campo de acordo com a mascara informada.
* Ex de uso: onkeyup="AplicaMascara('00:00:00', this);".
* @author Igor Escobar (blog@igorescobar.com)
* @param Mascara String que possui a mascara de formatação do campo.
* @param elemento Campo que será formatado de acordo com a mascara, voce pode informar o id direto ou o próprio elemento usando o this.
* @returns {void}
*/
//Mascara para campos do tipo Hora:
//<input name="hora" maxlength="8" onkeyup="AplicaMascara('XX:XX:XX', this);" type="text" /> 

//Mascara para campos do tipo Data:
//<input name="data" maxlength="10" onkeyup="AplicaMascara('XX/XX/XXXX', this);" type="text" />

//Mascara para campos do tipo CEP:
//<input name="cep" onkeyup="AplicaMascara('XXXX-XXX', this);" type="text" />

//Mascara para campos do tipo TELEFONE:
//input name="tel" onkeyup="AplicaMascara('(XX)XXXX-XXXX', this);" type="text" />

//Mascara para campos do tipo CPF:
//<input name="cpf" onkeyup="AplicaMascara('XXX.XXX.XXX-XX', this);" type="text" />

function AplicaMascara(Mascara, elemento){
    
    // Seta o elemento
    var elemento = (elemento) ? elemento : document.getElementById(elemento); 
    if(!elemento) return false;
    
    // Método que busca um determinado caractere ou string dentro de uma Array
    function in_array( oque, onde ){
            for(var i = 0 ; i <onde.length; i++){
            if(oque == onde[i]){
                return true;
            }
        }
        return false;
    }
    // Informa o array com todos os caracteres que podem ser considerados caracteres de mascara
    var SpecialChars = [':', '-', '.', '(',')', '/', ',', '_'];
    var oValue = elemento.value;
    var novo_valor = '';
    for( i = 0 ; i <oValue.length; i++){
        //Recebe o caractere de mascara atual
        var nowMask = Mascara.charAt(i);
        //Recebe o caractere do campo atual
        var nowLetter = oValue.charAt(i);
        //Aplica a masca
        if(in_array(nowMask, SpecialChars) == true && nowLetter != nowMask){
            novo_valor +=  nowMask + '' + nowLetter;
        } else {
            novo_valor += nowLetter;
        }
        // Remove regras duplicadas
        var DuplicatedMasks = nowMask+''+nowMask;
        while (novo_valor.indexOf(DuplicatedMasks)>= 0) {
         novo_valor = novo_valor.replace(DuplicatedMasks, nowMask);
        }
    }
    // Retorna o valor do elemento com seu novo valor
    elemento.value = novo_valor;
    
} 

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 == "000.000.000-00" ||   
                            cpf.value == "111.111.111-11" ||   
                            cpf.value == "222.222.222-22" ||   
                            cpf.value == "333.333.333-33" ||   
                            cpf.value == "444.444.444-44" ||   
                            cpf.value == "555.555.555-55" ||   
                            cpf.value == "666.666.666-66" ||   
                            cpf.value == "777.777.777-77" ||   
                            cpf.value == "888.888.888-88" ||   
                            cpf.value == "999.999.999-99") {  
                              
                            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 verificacampo(numbers,letters,others,e)
{
	if(window.event) key = window.event.keyCode
	else if(e) key = e.which
	else return true
	
	S=(others)?others:''
	if(numbers)S+='0123456789'
	if(letters)S+='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZãáàéêíõóôúç'
	if(key==null||key==0||key==8||key==9||key==13||key==27)return true
	else if(S.indexOf(String.fromCharCode(key))!=-1)return true
	else return false
}
function campomoeda(campo)
{
        campo.value = campo.value.replace(/,/gi, ","); //Substitui Virgula Por Ponto
		campo.value = campo.value.replace(/\D/g,""); //Remove tudo o que não é dígito
		campo.value = campo.value.replace(/(\d)(\d{2})$/,"$1,$2"); //Coloca ponto antes dos 2 últimos digitos

	
	ttp = new Number(document.total.ttp.value);
	vmao = new Number(document.total.vmao.value);
	ttg = ttp + vmao;
	document.total.ttg.value = ttg;
}

function addrefcom(){
  var textarea=document.getElementById("txtrefcom");
  var ref_empresa=document.getElementById("ref_empresa");
  var ref_telefone=document.getElementById("ref_telefone");
  var ref_contato=document.getElementById("ref_contato");
  textarea.value+=ref_empresa.value+"\n"+ref_telefone.value + " - " + ref_contato.value+"\n\n";
  ref_empresa.value = "";
  ref_telefone.value = "";
  ref_contato.value = "";
}

function teladd(){
  var context=document.getElementById("con_txt");
  var con_tel=document.getElementById("con_tel");
  var con_tipo=document.getElementById("con_tipo");
  context.value+=con_tel.value + " - Tipo:" + con_tipo.value + "\n";
  con_tel.value = "";
  con_tipo.value = "Selecione";
}

function addrefban(){
  var bantext=document.getElementById("txtrefban");
  var refban_banco=document.getElementById("refban_banco");
  var refban_telefone=document.getElementById("refban_telefone");
  var refban_contato=document.getElementById("refban_contato");
  bantext.value+=refban_banco.value + "\n" + refban_telefone.value + " - " + refban_contato.value + "\n\n";
  refban_banco.value = "Selecione";
  refban_telefone.value = "";
  refban_contato.value = "";
}

function renadd(){
  var rentext=document.getElementById("ren_txt");
  var ren_valor=document.getElementById("ren_valor");
  var ren_tipo=document.getElementById("ren_tipo");
  rentext.value+="R$ " + ren_valor.value + " - " + ren_tipo.value + "\n";
  ren_valor.value = "";
  ren_tipo.value = "Selecione";
}
