﻿// MASCARAS
function mascaras() {
    $(".data").mask("99/99/9999")
    $(".fone").mask("(999)9999-9999")
    $(".cep").mask("99999-999")
    $(".hora").mask("99:99")
}

$(function () {
    mascaras()
})

function FormatacaoCpfCnpj(e) {
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla >= 45 && tecla < 58) || (tecla == 44 || tecla == 46))
        return true;
    else {
        if (tecla != 8)
            return false;
        else
            return true;
    }
}


// FLASH
function ReescreveFlash(swf, width, height, flashvars, id) {
    var escreveFlash = new String();
    if (navigator.appName.indexOf("Microsoft") != -1) {
        escreveFlash += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
        escreveFlash += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=version=8,0,0,0" ';
        escreveFlash += 'width="' + width + '" height="' + height + '" name="' + id + '"id = ' + id + '">';
        escreveFlash += '<param name="movie" value="' + swf + '"/>';
        if (flashvars != null) { escreveFlash += '<param name="flashvars" value="' + flashvars + '"/>' };
        escreveFlash += '<param name="menu" value="false"/>';
        escreveFlash += '<param name="salign" value="LT"/>';
        escreveFlash += '<param name="scale" value="noscale"/>';
        escreveFlash += '<param name="wmode" value="transparent"/>';
        escreveFlash += '<param name="allowScriptAccess" value="sameDomain"/>';
        escreveFlash += '</object>';
    } else {
        escreveFlash += '<embed src="' + swf + '" ';
        escreveFlash += 'width="' + width + '" ';
        escreveFlash += 'height="' + height + '" ';
        escreveFlash += 'id="' + id + '" ';
        escreveFlash += 'name="' + id + '" ';
        escreveFlash += 'menu="false" ';
        escreveFlash += 'scale="noscale" ';
        escreveFlash += 'salign="LT" ';
        escreveFlash += 'wmode="transparent" ';
        escreveFlash += 'allowScriptAccess="sameDomain" ';
        if (flashvars != null) { escreveFlash += 'flashvars="' + flashvars + '" ' };
        escreveFlash += 'type="application/x-shockwave-flash" ';
        escreveFlash += 'pluginspage="http://www.macromedia.com/go/getflashplayer">';
        escreveFlash += '</embed>';
    }
    return escreveFlash;
}

function Flash(swf, width, height, flashvars, id) {
    var escreveFlash = new String();
    if (navigator.appName.indexOf("Microsoft") != -1) {
        escreveFlash += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
        escreveFlash += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=version=8,0,0,0" ';
        escreveFlash += 'width="' + width + '" height="' + height + '" name="' + id + '"id = ' + id + '">';
        escreveFlash += '<param name="movie" value="' + swf + '"/>';
        if (flashvars != null) { escreveFlash += '<param name="flashvars" value="' + flashvars + '"/>' };
        escreveFlash += '<param name="menu" value="false"/>';
        escreveFlash += '<param name="salign" value="LT"/>';
        escreveFlash += '<param name="scale" value="noscale"/>';
        escreveFlash += '<param name="wmode" value="transparent"/>';
        escreveFlash += '<param name="allowScriptAccess" value="sameDomain"/>';
        escreveFlash += '</object>';
    } else {
        escreveFlash += '<embed src="' + swf + '" ';
        escreveFlash += 'width="' + width + '" ';
        escreveFlash += 'height="' + height + '" ';
        escreveFlash += 'id="' + id + '" ';
        escreveFlash += 'name="' + id + '" ';
        escreveFlash += 'menu="false" ';
        escreveFlash += 'scale="noscale" ';
        escreveFlash += 'salign="LT" ';
        escreveFlash += 'wmode="transparent" ';
        escreveFlash += 'allowScriptAccess="sameDomain" ';
        if (flashvars != null) { escreveFlash += 'flashvars="' + flashvars + '" ' };
        escreveFlash += 'type="application/x-shockwave-flash" ';
        escreveFlash += 'pluginspage="http://www.macromedia.com/go/getflashplayer">';
        escreveFlash += '</embed>';
    }
    document.write(escreveFlash);
}


//MENSAGENS PARA USUARIO
function exibeerro(msg) {
    mensagem(msg);
    document.getElementById('erro').className = "errocampo campoerro erroMenor";
}

function errar(msg) {
    mensagem(msg);
    document.getElementById('erro').className = "errocampo campoerro erroMenor";
}

function sucesso(msg) {
    document.getElementById("imgConversaoGA").src = "http://www.googleadservices.com/pagead/conversion/1035864199/?label=_yeaCIXyxAEQh5H47QM&amp;guid=ON&amp;script=0";

    mensagem(msg);
    document.getElementById('erro').className = "sucesso";
}

function mensagem(msg) {
    document.getElementById('erro').innerHTML = msg;
}

function ocultar() {
    document.getElementById('erro').innerHTML = "";
    document.getElementById('erro').className = "invisivel";
}


//FUNCOES DIVERSAS
function trim(String) {
    return String.replace(/^\s+|\s+$/g, "");
}

function SomenteNumero(e) {
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 47 && tecla < 58))
        return true;
    else {
        if (tecla != 8)
            return false;
        else
            return true;
    }
}

function FormataValor(el, portugues) {
    if (portugues) {
        var val = el.value
        var answer_match = "$1,$2"                         // Delimitador casas decimal , (virgula)
        var aux;
        val = val.replace(/\D/g, "")                       // Remove tudo o que não é dígito
        val = val.replace(/(\d)(\d{2})$/, answer_match)    // Coloca a separação de decimal
        answer_match = "$1.$2"                             // Delimitador milhares
        aux = val.match(/(\,\d+)$/)                        // Salva o valor decimal na variavel auxiliar
        if (aux) {                                          // Verifica se existe decimais, senão não existirá milhar
            aux = aux[0]
            var compare = val.replace(aux, "")              // Pega apenas o valor a ser comparado
            while (compare.match(/(\d+)(\d{3})/)) {
                compare = compare.replace(/(\d+)(\d{3})/, answer_match)    // Coloca o delimitador a cada milhar
            }
            val = compare + aux
        }
        // Change this val
        el.value = val
    } else {
        var val = el.value
        var answer_match = "$1.$2"                         // Delimitador casas decimal , (virgula)
        var aux;
        val = val.replace(/\D/g, "")                       // Remove tudo o que não é dígito
        val = val.replace(/(\d)(\d{2})$/, answer_match)    // Coloca a separação de decimal
        answer_match = "$1,$2"                             // Delimitador milhares
        aux = val.match(/(\.\d+)$/)                        // Salva o valor decimal na variavel auxiliar
        if (aux) {                                          // Verifica se existe decimais, senão não existirá milhar
            aux = aux[0]
            var compare = val.replace(aux, "")              // Pega apenas o valor a ser comparado
            while (compare.match(/(\d+)(\d{3})/)) {
                compare = compare.replace(/(\d+)(\d{3})/, answer_match)    // Coloca o delimitador a cada milhar
            }
            val = compare + aux
        }
        // Change this val
        el.value = val
    }
}

function SomenteNumeroComPontuacao(e) {
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 47 && tecla < 58) || (tecla == 44 || tecla == 46))
        return true;
    else {
        if (tecla != 8)
            return false;
        else
            return true;
    }
}

function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen, bolMask, sCod, nTecla;

    if (document.all) { // Internet Explorer
        nTecla = evtKeyPress.keyCode;
    } else if (document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
    } else {
        nTecla = evtKeyPress.which;
        if (nTecla == 8) {
            return true;
        }
    }

    sValue = objeto.value;

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace("-", "");
    sValue = sValue.toString().replace("-", "");
    sValue = sValue.toString().replace(".", "");
    sValue = sValue.toString().replace(".", "");
    sValue = sValue.toString().replace("/", "");
    sValue = sValue.toString().replace("/", "");
    sValue = sValue.toString().replace(":", "");
    sValue = sValue.toString().replace(":", "");
    sValue = sValue.toString().replace("(", "");
    sValue = sValue.toString().replace("(", "");
    sValue = sValue.toString().replace(")", "");
    sValue = sValue.toString().replace(")", "");
    sValue = sValue.toString().replace(" ", "");
    sValue = sValue.toString().replace(" ", "");
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

        if (bolMask) {
            sCod += sMask.charAt(i);
            mskLen++;
        }
        else {
            sCod += sValue.charAt(nCount);
            nCount++;
        }

        i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
        if (sMask.charAt(i - 1) == "9") { // apenas números...
            return ((nTecla >= 47) && (nTecla <= 58));
        }
        else { // qualquer caracter...
            return true;
        }
    }
    else {
        return true;
    }
}