/* 
+---------------------------------------------+
| Bibblioteca de funções gerais em JavaScript |
| Funções coletadas por: Felipe Rebelo Lopes  |
| Empresa: BCJ Soluções Inteligentes          |
| Contato: suporte@bcj.com.br                 |
+---------------------------------------------+
*/

// Valida se o conteúdo da string email é um e-mail válido
function validaEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    if (email == "") {
        return false;
    }

    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}

function numbersonly(){
  if (event.keyCode==32) 
    return true
  if (event.keyCode<48||event.keyCode>57)
    return false
}	
	
// Funcação que valida o con
function validaCPF(sCPF) {
        cpf = sCPF;
        erro = new String;
        if (cpf.length < 11) erro += "Um CPF deve conter 11 digitos! ";
        var nonNumbers = /\D/;
        if (nonNumbers.test(cpf)) erro += "O CPF deve conter apenas números! ";
        if (cpf == "00000000000" || 
			cpf == "11111111111" || 
			cpf == "22222222222" || 
			cpf == "33333333333" || 
			cpf == "44444444444" || 
			cpf == "55555555555" || 
			cpf == "66666666666" || 
			cpf == "77777777777" || 
			cpf == "88888888888" || 
			cpf == "99999999999"){
                erro += "Numero de CPF invalido!"
       }
       var a = [];
       var b = new Number;
       var c = 11;
       for (i=0; i<11; i++){
               a[i] = cpf.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.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
               erro +="Digito verificador incorreto!";
       }
       if (erro.length > 0){
               alert(erro);
               return false;
       }
       return true;
}

/*
1 <script language="Javascript">
2         function validaCNPJ() {
3                 CNPJ = document.validacao.CNPJID.value;
4                 erro = new String;
5                 if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! ";
6                 if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
7                 if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! ";
8                 }
9                 //substituir os caracteres que não são números
10               if(document.layers && parseInt(navigator.appVersion) == 4){
11                       x = CNPJ.substring(0,2);
12                       x += CNPJ. substring (3,6);
13                       x += CNPJ. substring (7,10);
14                       x += CNPJ. substring (11,15);
15                       x += CNPJ. substring (16,18);
16                       CNPJ = x;
17               } else {
18                       CNPJ = CNPJ. replace (".","");
19                       CNPJ = CNPJ. replace (".","");
20                       CNPJ = CNPJ. replace ("-","");
21                       CNPJ = CNPJ. replace ("/","");
22               }
23               var nonNumbers = /\D/;
24               if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! ";
25               var a = [];
26               var b = new Number;
27               var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
28               for (i=0; i<12; i++){
29                       a[i] = CNPJ.charAt(i);
30                       b += a[i] * c[i+1];
31 }
32               if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
33               b = 0;
34               for (y=0; y<13; y++) {
35                       b += (a[y] * c[y]);
36               }
37               if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
38               if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
39                       erro +="Dígito verificador com problema!";
40               }
41               if (erro.length > 0){
42                       alert(erro);
43                       return false;
44               } else {
45                       alert("CNPJ valido!");
46               }
47               return true;
48       }
49 </script>


*/// Registra o Flash para o Update do Internet Explorer
function clearObject(obj){
	if(obj==null){
		var theObjects = document.getElementsByTagName("object");
		for (var i=0; i < theObjects.length; i++) {
			 theObjects[i].outerHTML=theObjects[i].outerHTML;
		}
	}
	else{
		obj.outerHTML = obj.outerHTML;
	}
}

function clearAllIeObjects(){
	if(document.all){
		for(i = 0; i < document.all.length; i++){
			if(document.all[i].tagName == "OBJECT"){
				clearObject(document.all[i]);
			}
		}
	}
}

