﻿function Flash(w, h, wmode, movie) {
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + w + '" height="' + h + '">');
    document.write('<param name="scale" value="noscale">');
    document.write('<param name="wmode" value="' + wmode + '">');
    document.write('<param name="quality" value="high">');
    document.write('<param name="menu" value="false">');
    document.write('<param name="movie" value="' + movie + '">');
    document.write('<embed src="' + movie + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="' + wmode + '" scale="noscale" menu="false" type="application/x-shockwave-flash" width="' + w + '" height="' + h + '"></embed>');
    document.write('</object>');
}


function MenuDisplay(Menu, Page) {

    GerarCookie('OpenMenu', Menu, 0);

    if (document.getElementById("ctl00_SubMenu" + Menu) != null) {
        if (document.getElementById("ctl00_SubMenu" + Menu).style.display == "none") {
            document.getElementById("ctl00_SubMenu" + Menu).style.display = "";
        }
        else if (Page == null) {
            document.getElementById("ctl00_SubMenu" + Menu).style.display = "none";
            ExcluirCookie('OpenMenu');
        }
    }
    if (Page != null) {
        document.location = Page;
    }

}


// onkeypress="return SoNumero(event)"
function SoNumero(caracter) {
    var tecla = 0;
    if (caracter.keyCode != '') {
        tecla = event.keyCode;
    }
    else {
        tecla = caracter.which;
    }

    if (tecla > 47 && tecla < 58) { // numeros de 0 a 9
        return true;
    } else
        if ((tecla == 0) || (tecla == 8) || (tecla == 13)) { // backspace e Del
        return true;
    }
    else {
        return false;
    }

}


// onblur="VerificaData(this)"
function VerificaData(data) {
    if (data.value != "") {
        dia = (data.value.substring(0, 2));
        mes = (data.value.substring(3, 5)); 
        ano = (data.value.substring(6, 10));

        if (((dia < 01) || (dia < 01 || dia > 30) && (mes == 04 || mes == 06 || mes == 09 || mes == 11) || dia > 31) || (mes < 01 || mes > 12) || (mes == 2 && (dia < 01 || dia > 29 || (dia > 28 && (parseInt(ano / 4) != ano / 4))))) {
            alert("Data inválida.");
            aux = data;
            data.select();
            return false;
        }
    }
}

function ValidarData(sender, args) {
    data = args.Value;
    dia = (data.substring(0, 2));
    mes = (data.substring(3, 5));
    ano = (data.substring(6, 10));
    
    if (((dia < 01) || (dia < 01 || dia > 30) && (mes == 04 || mes == 06 || mes == 09 || mes == 11) || dia > 31) || (mes < 01 || mes > 12) || (mes == 2 && (dia < 01 || dia > 29 || (dia > 28 && (parseInt(ano / 4) != ano / 4))))) {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
}


// onkeypress="return MascaraData(this,event)" - Testado: Firefox 3.5.2, IE 8, Chrome 2.0
function MascaraData(input, evnt) {
    if (input.value.length == 2 || input.value.length == 5) {
        if (navigator.userAgent.indexOf('Firefox') != -1) {
            if (evnt.keyCode == 0) {
                input.value += "/";
            }
        }
        else {
            input.value += "/";
        }
    }
    return SoNumero(evnt);
}


function Mascara(o, f) {
    v_obj = o
    v_fun = f
    setTimeout("ExecMascara()", 1)
}


//máscaras gerais
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\d)(\d)/g, "($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
    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") //Esse é tão fácil que não merece explicações
    return v
}


function CNPJ(v) {
    v = v.replace(/\D/g, "")                           //Remove tudo o que não é dígito
    v = v.replace(/^(\d{2})(\d)/, "$1.$2")             //Coloca ponto entre o segundo e o terceiro dígitos
    v = v.replace(/^(\d{2})\.(\d{3})(\d)/, "$1.$2.$3") //Coloca ponto entre o quinto e o sexto dígitos
    v = v.replace(/\.(\d{3})(\d)/, ".$1/$2")           //Coloca uma barra entre o oitavo e o nono dígitos
    v = v.replace(/(\d{4})(\d)/, "$1-$2")              //Coloca um hífen depois do bloco de quatro dígitos
    return v
}


function Romanos(v) {
    v = v.toUpperCase()             //Maiúsculas
    v = v.replace(/[^IVXLCDM]/g, "") //Remove tudo o que não for I, V, X, L, C, D ou M
    //Essa é complicada! Copiei daqui: http://www.diveintopython.org/refactoring/refactoring.html
    while (v.replace(/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/, "") != "")
        v = v.replace(/.$/, "")
    return v
}


function Site(v) {
    v = v.replace(/^http:\/\/?/, "")
    dominio = v
    caminho = ""
    if (v.indexOf("/") > -1)
        dominio = v.split("/")[0]
    caminho = v.replace(/[^\/]*/, "")
    dominio = dominio.replace(/[^\w\.\+-:@]/g, "")
    caminho = caminho.replace(/[^\w\d\+-@:\?&=%\(\)\.]/g, "")
    caminho = caminho.replace(/([\?&])=/, "$1")
    if (caminho != "") dominio = dominio.replace(/\.+$/, "")
    v = "http://" + dominio + caminho
    return v
}




// Mascara moeda - onKeyPress="return MascaraMoeda(this,'.',',',event)" - Testado: Firefox 3.5.2, IE 8, Chrome 2.0
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) {
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode;
    if (navigator.appName.indexOf('Microsoft') != -1) {
        whichCode = e.keyCode;
    }
    else {
        whichCode = (window.Event) ? e.which : e.keyCode;
    }
    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc
    var tamanho = objTextBox.maxLength
    if (objTextBox.value.length >= tamanho && whichCode != 8)
        whichCode = 1;
    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
        return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave

    if (strCheck.indexOf(key) == -1)
        return false; // Chave inválida
    len = objTextBox.value.length;
    for (i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal))
        break;
    aux = '';
    for (; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1)
        aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0)
        objTextBox.value = '';
    if (len == 1)
        objTextBox.value = '0' + SeparadorDecimal + '0' + aux;
    if (len == 2)
        objTextBox.value = '0' + SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function MascaraHora(input, evnt) {
    if (input.value.length == 2) {
        if (navigator.userAgent.indexOf('Firefox') != -1) {
            if (evnt.keyCode == 0) {
                input.value += ":";
            }
        }
        else {
                input.value += ":";
        }
    }
    return SoNumero(evnt);
}

function ValidarHora(sender, args) {
    horario = args.Value;
    dia = (data.substring(0, 2));
    mes = (data.substring(3, 5));
    ano = (data.substring(6, 10));

    if (((dia < 01) || (dia < 01 || dia > 30) && (mes == 04 || mes == 06 || mes == 09 || mes == 11) || dia > 31) || (mes < 01 || mes > 12) || (mes == 2 && (dia < 01 || dia > 29 || (dia > 28 && (parseInt(ano / 4) != ano / 4))))) {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
}
function VerificaHora(tempo) {
    horario = tempo.value.split(":");
    var horas = horario[0];
    var minutos = horario[1];
    var segundos = horario[2];
    if (horas > 24) { //para relógio de 12 horas altere o valor aqui
        alert("Hora inválida.");
        tempo.select();
        return false;
    }
    if (minutos > 59) {
        alert("Hora inválida.");
        tempo.select();
        return false;
    }
    if (segundos > 59) {
        alert("Hora inválida.");
        tempo.select();
        return false;
    }
}


function removeEspacos(valor) {
    var valorSemEspacos = "";

    var tamanho = valor.length;
    for (i = 0; i < 30; i++) {
        if (valor.substr(i, 1) == " ") {
        } else {
            valorSemEspacos = valorSemEspacos + valor.substr(i, 1);
        }
    }
    return valorSemEspacos;
}

function mascara_global(objTextBox, mascara) {

    valor = objTextBox.value;
    
    if (mascara == '###.###.###-##|##.###.###/####-##') {
        if (valor.length > 14) {
            return mascara_global('##.###.###/####-##', valor);
        } else {
            return mascara_global('###.###.###-##', valor);
        }
    }

    tvalor = "";
    ret = "";
    caracter = "#";
    separador = "|";
    mascara_utilizar = "";
    valor = removeEspacos(valor);
    if (valor == "") return valor;
    temp = mascara.split(separador);
    dif = 1000;

    valorm = valor;
    //tirando mascara do valor já existente
    for (i = 0; i < valor.length; i++) {
        if (!isNaN(valor.substr(i, 1))) {
            tvalor = tvalor + valor.substr(i, 1);
        }
    }
    valor = tvalor;

    //formatar mascara dinamica
    for (i = 0; i < temp.length; i++) {
        mult = "";
        validar = 0;
        for (j = 0; j < temp[i].length; j++) {
            if (temp[i].substr(j, 1) == "]") {
                temp[i] = temp[i].substr(j + 1);
                break;
            }
            if (validar == 1) mult = mult + temp[i].substr(j, 1);
            if (temp[i].substr(j, 1) == "[") validar = 1;
        }
        for (j = 0; j < valor.length; j++) {
            temp[i] = mult + temp[i];
        }
    }

    //verificar qual mascara utilizar
    if (temp.length == 1) {
        mascara_utilizar = temp[0];
        mascara_limpa = "";
        for (j = 0; j < mascara_utilizar.length; j++) {
            if (mascara_utilizar.substr(j, 1) == caracter) {
                mascara_limpa = mascara_limpa + caracter;
            }
        }
        tam = mascara_limpa.length;
    } else {
        //limpar caracteres diferente do caracter da máscara
        for (i = 0; i < temp.length; i++) {
            mascara_limpa = "";
            for (j = 0; j < temp[i].length; j++) {
                if (temp[i].substr(j, 1) == caracter) {
                    mascara_limpa = mascara_limpa + caracter;
                }
            }
            if (valor.length > mascara_limpa.length) {
                if (dif > (valor.length - mascara_limpa.length)) {
                    dif = valor.length - mascara_limpa.length;
                    mascara_utilizar = temp[i];
                    tam = mascara_limpa.length;
                }
            } else if (valor.length < mascara_limpa.length) {
                if (dif > (mascara_limpa.length - valor.length)) {
                    dif = mascara_limpa.length - valor.length;
                    mascara_utilizar = temp[i];
                    tam = mascara_limpa.length;
                }
            } else {
                mascara_utilizar = temp[i];
                tam = mascara_limpa.length;
                break;
            }
        }
    }

    //validar tamanho da mascara de acordo com o tamanho do valor
    if (valor.length > tam) {
        valor = valor.substr(0, tam);
    } else if (valor.length < tam) {
        masct = "";
        j = valor.length;
        for (i = mascara_utilizar.length - 1; i >= 0; i--) {
            if (j == 0) break;
            if (mascara_utilizar.substr(i, 1) == caracter) {
                j--;
            }
            masct = mascara_utilizar.substr(i, 1) + masct;
        }
        mascara_utilizar = masct;
    }

    //mascarar
    j = mascara_utilizar.length - 1;
    for (i = valor.length - 1; i >= 0; i--) {
        if (mascara_utilizar.substr(j, 1) != caracter) {
            ret = mascara_utilizar.substr(j, 1) + ret;
            j--;
        }
        ret = valor.substr(i, 1) + ret;
        j--;
    }
    objTextBox.value = ret;
    //return ret;
}


// Função para criar o cookie.
// Para que o cookie seja destruído quando o brawser for fechado, basta passar 0 no parametro lngDias.
function GerarCookie(strCookie, strValor, lngDias)
{
    var dtmData = new Date();

    if(lngDias)
    {
        dtmData.setTime(dtmData.getTime() + (lngDias * 24 * 60 * 60 * 1000));
        var strExpires = "; expires=" + dtmData.toGMTString();
    }
    else
    {
        var strExpires = "";
    }
    document.cookie = strCookie + "=" + strValor + strExpires + "; path=/";
}

// Função para ler o cookie.
function LerCookie(strCookie)
{
    var strNomeIgual = strCookie + "=";
    var arrCookies = document.cookie.split(';');

    for(var i = 0; i < arrCookies.length; i++)
    {
        var strValorCookie = arrCookies[i];
        while(strValorCookie.charAt(0) == ' ')
        {
            strValorCookie = strValorCookie.substring(1, strValorCookie.length);
        }
        if(strValorCookie.indexOf(strNomeIgual) == 0)
        {
            return strValorCookie.substring(strNomeIgual.length, strValorCookie.length);
        }
    }
    return null;
}

// Função para excluir o cookie desejado.
function ExcluirCookie(strCookie)
{
    GerarCookie(strCookie, '', -1);
}
