﻿// Atualiza a página
function AtualizarPagina() {
    document.getElementById("ctl00_ContentPlaceHolder1_hdnAtualiza").value = "1";
    document.aspnetForm.action = document.location;
    document.aspnetForm.method = "post";
    document.aspnetForm.submit();
}

// FUNCAO INSERIR FLASH
// DETECÇÃO DO FLASH PLAYER E COLOCAR FLASH VÁLIDO
// www.ajrdesign.net

/* -- 1. DETECÇÃO DA VERSÃO DO PLAYER -- */

// SETAR VERSÃO DO PLAYER
var MM_contentVersion = 6;

// SETAR SUB-VERSÃO
var release = "6,0,67,0";


// CHECAR SE EXISTE O PLAYER
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if (plugin) {
    var words = navigator.plugins["Shockwave Flash"].description.split(" ");
    for (var i = 0; i < words.length; ++i) {
        if (isNaN(parseInt(words[i])))
            continue;
        var MM_PluginVersion = words[i];
    }
    var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0
&& (navigator.appVersion.indexOf("Win") != -1)) {
    document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag 
    document.write('on error resume next \n');
    document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
    document.write('</SCR' + 'IPT\> \n');
}

/* -- Usada na página de banner -- */

function InserirFlash(swf, flashVarString, width, height, bgcolor, menu, mode, qualidade, id) {
    if (MM_FlashCanPlay) {
        document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
		+ 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + release + '" '
		+ 'width="' + width + '" height="' + height + '" id="' + id + '" align="">'
		+ '<param name="movie" value="' + swf + '">'
		+ '<param name="menu" value="' + menu + '"> '
		+ '<param name="quality" value="' + qualidade + '"> '
		+ '<param name="wmode" value="' + mode + '"> '
		+ '<param name="bgcolor" value="' + bgcolor + '"> '
		+ '<param name="flashvars" value="' + flashVarString + '"> '
		+ '<embed src="' + swf + '" flashvars="' + flashVarString + '" menu="' + menu + '" quality="' + qualidade + '" wmode="' + mode + '" '
		+ ' bgcolor="' + bgcolor + '" width="' + width + '" height="' + height + '" name="' + swf + '" '
		+ ' align="" type="application/x-shockwave-flash" '
		+ ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object> ');
    } else {

        //behaviour for if there is no flash player 
        document.write('É necessário o Flash Player ' + MM_contentVersion + ' ou superior para visualizar a animação. ');
        document.write('<em><a href="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">');
        document.write('Clique aqui para instalá-lo agora..</a></em>');

        //this can be replaced by anything you want 
        //e.g. document.location.replace("text.html"); 	
    }
}


function FormatarMascara(controle, teclapres, mascara) {
    if (somenteNumeros(controle, teclapres)) {
        var campo = controle.value.length;
        var saida = mascara.substring(0, 1);
        var texto = mascara.substring(campo);
        var navegador = navigator.appName

        if (texto.substring(0, 1) != saida) {
            if (navegador == "Netscape") {
                if (teclapres.which == 8)
                    return true;
                else
                    controle.value += texto.substring(0, 1);
            }
            else {
                controle.value += texto.substring(0, 1);
            }
        }
        return true;
    }
    else
        return false;
}

function Processando(visivel) {
    if (visivel == 1) {
        document.getElementById('processando').style.display = 'block';
        document.getElementById('bloqueia').style.display = 'block';
        document.body.focus();
    }
    else {
        document.getElementById('processando').style.display = 'none';
        document.getElementById('bloqueia').style.display = 'none';
    }
    return true;
}

function FormataData(controle, teclapres) {

    if (somenteNumeros(controle, teclapres)) {
        navegador = navigator.appName;

        if (navegador == "Microsoft Internet Explorer") {
            var tecla = event.KeyCode;
        }
        else if (navegador == "Netscape") {
            var tecla = teclapres.which;
        }
        vr = controle.value;
        vr = vr.replace(".", "");
        vr = vr.replace("/", "");
        vr = vr.replace("/", "");
        tam = vr.length + 1;

        if (tam <= 6) {
            if (tecla != 9 && tecla != 8) {
                if (tam > 2 && tam < 5)
                    controle.value = vr.substr(0, tam - 2) + '/' + vr.substr(tam - 2, tam);
                if (tam >= 5 && tam <= 10)
                    controle.value = vr.substr(0, 2) + '/' + vr.substr(2, 2) + '/' + vr.substr(4, 4);
            }
        }
        return true;
    }
    else {
        return false;
    }
}

function remove(str, sub) {
    i = str.indexOf(sub);
    r = "";
    if (i == -1) return str;
    r += str.substring(0, i) + remove(str.substring(i + sub.length), sub);
    return r;
}

function ValidarCPF(Controle) {
    var char_valido = 0,
		char_invalido = 0,
		digito1,
		digito2, msgaux, texto, erro,
		soma_digito1 = 0,
		soma_digito2 = 0;

    var conteudo = Controle.value;
    conteudo = remove(conteudo, '.');
    conteudo = remove(conteudo, '-');

    erro = false;

    if (conteudo.length == 0)
        return true;
    while (conteudo.length < 11)
        conteudo = 0 + conteudo;

    if (conteudo.length > 11)
        erro = true;

    if (conteudo == '00000000000' || conteudo == '11111111111' ||
    	conteudo == '22222222222' || conteudo == '33333333333' ||
		conteudo == '44444444444' || conteudo == '55555555555' ||
		conteudo == '66666666666' || conteudo == '77777777777' ||
		conteudo == '99999999999')
        erro = true;

    if (conteudo.length == 11) {
        for (var i = 0; i <= conteudo.length - 1; i++)
            if (conteudo.charAt(i) >= '0' && conteudo.charAt(i) <= '9')
            char_valido = char_valido + 1;
        else
            char_invalido = char_invalido + 1;

        if (char_valido == conteudo.length) {
            for (i = 0; i < 9; i++)
                soma_digito1 += (10 - i) * (eval(conteudo.charAt(i)));

            digito1 = 11 - (soma_digito1 % 11);

            for (i = 0; i < 9; i++)
                soma_digito2 += (11 - i) * (eval(conteudo.charAt(i)));

            soma_digito2 += 2 * (eval(conteudo.charAt(9)));
            digito2 = 11 - (soma_digito2 % 11);

            if ((soma_digito1 % 11) < 2)
                digito1 = 0;

            if ((soma_digito2 % 11) < 2)
                digito2 = 0;

            if (eval(conteudo.charAt(9) != digito1) || eval(conteudo.charAt(10) != digito2))
                erro = true;
        }
        else
            erro = true;
    }

    if (erro)
        return false;

    return !(erro);
}

function ValidarCNPJ(Controle) {
    var 
					char_valido = 0,
					char_invalido = 0,
					digito1,
					digito2,
					soma_digito1 = 0,
					soma_digito2 = 0,
					valororiginal = '';

    erro = false;

    valororiginal = Controle.value;

    Controle.value = Controle.value.replace(".", "");
    Controle.value = Controle.value.replace(".", "");
    Controle.value = Controle.value.replace(".", "");
    Controle.value = Controle.value.replace(".", "");
    Controle.value = Controle.value.replace(".", "");
    Controle.value = Controle.value.replace(",", "");
    Controle.value = Controle.value.replace(",", "");
    Controle.value = Controle.value.replace(",", "");
    Controle.value = Controle.value.replace(",", "");
    Controle.value = Controle.value.replace(",", "");
    Controle.value = Controle.value.replace("-", "");
    Controle.value = Controle.value.replace("-", "");
    Controle.value = Controle.value.replace("-", "");
    Controle.value = Controle.value.replace("-", "");
    Controle.value = Controle.value.replace("-", "");
    Controle.value = Controle.value.replace("/", "");
    Controle.value = Controle.value.replace("/", "");
    Controle.value = Controle.value.replace("/", "");
    Controle.value = Controle.value.replace("/", "");
    Controle.value = Controle.value.replace("/", "");

    if (Controle.value != "") {
        while (Controle.value.length < 14)
            Controle.value = 0 + Controle.value;

        //verifica quantidade de caracteres 
        if (Controle.value.length > 14) {            
            erro = true;
        }

        //verifica sequencia de caracteres repetidos							
        if (Controle.value.length == 14) {
            if (Controle.value == '00000000000000' || Controle.value == '11111111111111' ||
						    Controle.value == '22222222222222' || Controle.value == '33333333333333' ||
							Controle.value == '44444444444444' || Controle.value == '55555555555555' ||
							Controle.value == '66666666666666' || Controle.value == '77777777777777' ||
							Controle.value == '88888888888888' || Controle.value == '99999999999999') {              
                erro = true;
            }

            if (erro == false) {
                //verifica se digitou apenas números
                for (var i = 0; i <= Controle.value.length - 1; i++) {
                    if (Controle.value.charAt(i) >= '0' && Controle.value.charAt(i) <= '9') {
                        char_valido = char_valido + 1;
                    }
                    else {
                        char_invalido = char_invalido + 1;
                    }
                }

                if (char_valido == Controle.value.length) {
                    for (i = 0; i < 4; i++) {
                        soma_digito1 += (5 - i) * (eval(Controle.value.charAt(i)));
                    }
                    for (i = 0; i < 8; i++) {
                        soma_digito1 += (9 - i) * (eval(Controle.value.charAt(i + 4)));
                    }
                    digito1 = 11 - (soma_digito1 % 11);

                    for (i = 0; i < 5; i++) {
                        soma_digito2 += (6 - i) * (eval(Controle.value.charAt(i)));
                    }
                    for (i = 0; i < 7; i++) {
                        soma_digito2 += (9 - i) * (eval(Controle.value.charAt(i + 5)));
                    }
                    soma_digito2 += 2 * (eval(Controle.value.charAt(12)));
                    digito2 = 11 - (soma_digito2 % 11);

                    if ((soma_digito1 % 11) < 2) {
                        digito1 = 0;
                    }
                    if ((soma_digito2 % 11) < 2) {
                        digito2 = 0;
                    }
                    if (eval(Controle.value.charAt(12) != digito1) || eval(Controle.value.charAt(13) != digito2)) {                        
                        erro = true;
                    }
                }
                else {                    
                    erro = true;
                }
            }
        }

        if (erro == false) {
            Controle.value = valororiginal;
        }     

        return erro;
    }
}
function CampoNumerico(e) {
    if (document.all)
        var tecla = event.keyCode;
    else if (document.layers)
        var tecla = e.which;
    if (tecla > 47 && tecla < 58)
        return true;
    else {
        if ((tecla == 8) || (tecla == 13) || (tecla == 9))
            return true;
        else
            event.keyCode = 0;

    }
}


function somenteNumeros(controle, event) {


    navegador = navigator.appName;

    if (navegador == "Microsoft Internet Explorer") {
        var tecla = event.keyCode;
    }
    else if (navegador == "Netscape") {
        var tecla = event.which;
    }
    else {
        return true;
    }
    if ((tecla > 46 && tecla < 58) || (tecla > 95 && tecla < 106) || ((tecla == 8) || (tecla == 13) || (tecla == 9) || (tecla == 0))) {
        return true;
    }
    else {
        return false;
    }
}

function somenteValor(controle, event) {
    if ((!event.shiftKey) && (event.keyCode == 46 || event.keyCode == 48
|| event.keyCode == 49 ||
        event.keyCode == 50 || event.keyCode == 51 || event.keyCode ==
52 || event.keyCode == 53 ||
        event.keyCode == 54 || event.keyCode == 55 || event.keyCode ==
56 || event.keyCode == 57 ||
        event.keyCode == 96 || event.keyCode == 97 || event.keyCode ==
98 || event.keyCode == 99 ||
        event.keyCode == 100 || event.keyCode == 101 || event.keyCode
== 102 || event.keyCode == 103 ||
        event.keyCode == 104 || event.keyCode == 105 || event.keyCode ==
8 || event.keyCode == 46 ||
        event.keyCode == 9 || event.keyCode == 110 || event.keyCode == 188)) {
        return true;
    }
    else {
        event.setKeyCode = 0;
        return false;
    }
}


// ajuste do css para a resolução de tela:
function AjustarResolucaoCss() 
{
    if ((window.screen.width == 1440) & (window.screen.height == 900)) 
    {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1440x900.css" type="text/css" media="screen" />')
    }
    else if ((window.screen.width == 1280) & (window.screen.height == 1024)) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1280x1024.css" type="text/css" media="screen" />')
    }
    else if ((window.screen.width == 1280) & (window.screen.height == 960)) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1280x960.css" type="text/css" media="screen" />')
    }
    else if ((window.screen.width == 1280) & (window.screen.height == 800)) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1280x800.css" type="text/css" media="screen" />')
    }
    else if ((window.screen.width == 1280) & (window.screen.height == 768)) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1280x768.css" type="text/css" media="screen" />')
    }
    else if ((window.screen.width == 1280) & (window.screen.height == 720)) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1280x720.css" type="text/css" media="screen" />')
    }
    else if ((window.screen.width == 1280) & (window.screen.height == 600)) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1280x600.css" type="text/css" media="screen" />')
    }
    else if ((window.screen.width == 1152) & (window.screen.height == 864)) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1152x864.css" type="text/css" media="screen" />')
    }
    else if ((window.screen.width == 1024) & (window.screen.height == 768)) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico1024x768.css" type="text/css" media="screen" />')
    }
    else if (window.screen.width == 800) {
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico800x600.css" type="text/css" media="screen" />')
    }
    else
        document.write('<link rel="stylesheet" href="../CssLib/EstilosPublico.css" type="text/css" media="screen" />');  
}

function precarrega(img, id) {
    imgVar = new Image();
    imgVar.src = img;
    imagemVar = img;
    loadCheck(id);
}

//Usar no KeyPress
function maxLenght(txt, maxLen) {
    try {
        if (txt.value.length > (maxLen - 1))
            return false;
    }
    catch (e) {
    }
}

//Essa funciona no IE6
function TamMax(Controle, Max) {
    if (Controle.value.length >= Max) {
        Controle.value = Controle.value.substring(0, Max)
    }
}

//Essa função conta a quantidade de caracteres e limita o tamanho máximo
//Utilizar no KeyUp passando seu id depois de compilado
//Fazer um span ou uma div onde será contado a quantidade de caracteres e passar
//por parâmetro (Segundo parâmetro), todos os campos que serão passados deverão estar entre ''
function LimitarCaracter(idCampo, idContador, TamMax){  
    Caracteres = TamMax - document.getElementById(idCampo).value.length;  
    document.getElementById(idContador).innerHTML = Caracteres;  
    
    if(document.getElementById(idCampo).value.length >= TamMax){  
        document.getElementById(idCampo).value = document.getElementById(idCampo).value.substring(0, TamMax);  
        document.getElementById(idContador).innerHTML = "0";  
    }  
}

function mascara(o, f) {
    v_obj = o
    v_fun = f
    setTimeout("execmascara()", 1)
}

function execmascara() {
    v_obj.value = v_fun(v_obj.value)
}

function leech(v) {
    v = v.replace(/o/gi, "0")
    v = v.replace(/i/gi, "1")
    v = v.replace(/z/gi, "2")
    v = v.replace(/e/gi, "3")
    v = v.replace(/a/gi, "4")
    v = v.replace(/s/gi, "5")
    v = v.replace(/t/gi, "7")
    return v
}

function soNumeros(v) {
    return v.replace(/\D/g, "")
}

function telefone(v) {
    v = v.replace(/\D/g, "")                 //Remove tudo o que não é dígito
    v = v.replace(/(\d{4})(\d)/, "$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
    return v
}

function cpf(v) {
    v = v.replace(/\D/g, "")                    //Remove tudo o que não é dígito
    v = v.replace(/(\d{3})(\d)/, "$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    v = v.replace(/(\d{3})(\d)/, "$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
    //de novo (para o segundo bloco de números)
    v = v.replace(/(\d{3})(\d{1,2})$/, "$1-$2") //Coloca um hífen entre o terceiro e o quarto dígitos
    return v
}


function cep(v) {
    v = v.replace(/\D/g, "")                //Remove tudo o que não é dígito
    v = v.replace(/^(\d{5})(\d)/, "$1-$2") 
    return v
}
