// Global user functions

function mascara(o,f){
        v_obj=o;
        v_fun=f;
        setTimeout("execmascara()",1);
    }

    function execmascara(){
        v_obj.value=v_fun(v_obj.value);
    }
/*exemplo = onKeyPress="mascara(this,cnpj);"*/

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 numero(v){
        return v.replace(/\D/g,"");
    }

    function telefone(v){
        v=v.replace(/\D/g,"");
        v=v.replace(/^(\d\d)(\d)/g,"($1) $2");
        v=v.replace(/(\d{4})(\d)/,"$1-$2");
        return v
    }

    function codigopostal(v){
        v=v.replace(/\D/g,"");
        v=v.replace(/^(\d{5})(\d)/,"$1-$2");
        return v
    }

    function cpf(v){
        v=v.replace(/\D/g,"");
        v=v.replace(/(\d{3})(\d)/,"$1.$2");
        v=v.replace(/(\d{3})(\d)/,"$1.$2");
        v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2");
        return v
    }

    function cnpj(v){
        v=v.replace(/\D/g,"");
        v=v.replace(/^(\d{2})(\d)/,"$1.$2");
        v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3");
        v=v.replace(/\.(\d{3})(\d)/,".$1/$2");
        v=v.replace(/(\d{4})(\d)/,"$1-$2");
        return v
    }

    function rg(v){
        v=v.replace(/\D/g,"");
        v=v.replace(/(\d{2})(\d)/,"$1.$2");
        v=v.replace(/(\d{3})(\d)/,"$1.$2");
        v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2");
        return v
    }

    function data(v){
        v=v.replace(/\D/g,"");
        v=v.replace(/(\d{2})(\d)/,"$1/$2");
        v=v.replace(/(\d{2})(\d)/,"$1/$2");
        return v
    }

    function hora(v){
        v=v.replace(/\D/g,"");
        v=v.replace(/^(\d{2})(\d)/,"$1:$2");
        return v
    }

    function makenum(nro){
        var valid    = "0123456789";
        var numerook = "";
        var temp;
        for(var i = 0; i < nro.length; i++)
        {
            temp = nro.substr(i, 1);
            if (valid.indexOf(temp) != -1)
                numerook = numerook + temp;
        }
        return(numerook);
    }

    function FormatarValor(objeto,e,tammax,decimais){
        var tecla  = (window.Event) ? e.which : e.keyCode;
        var tamObj = objeto.value.length;
        if ((tecla == 8) && (tamObj == tammax))
            tamObj = tamObj - 1;
        vr = makenum(objeto.value);
        tam = vr.length;
        if (((tecla == 8) || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105)) && (parseInt(tamObj) + 1 <= parseInt(tammax)))
        {
            if ((tam < tammax) && (tecla != 8))
                tam = vr.length + 1;
            if ((tecla == 8) && (tam > 1))
                tam = tam - 1;
            if (tam <= decimais)
                objeto.value = ("0," + vr);
            if ((tam == (decimais + 1)) && (tecla == 8))
                objeto.value = vr.substr(0, (tam - decimais)) + "," + vr.substr((tam - decimais), tam);
            if ((tam > (decimais + 1)) && (tam <= (decimais + 3)) && (vr.substr(0,1) == "0"))
                objeto.value = vr.substr(1, (tam - (decimais+1))) + "," + vr.substr(tam - (decimais), tam);
            if ((tam > (decimais + 1)) && (tam <= (decimais + 3)) && (vr.substr(0,1) != "0"))
                objeto.value = vr.substr(0, (tam - decimais)) + "," + vr.substr(tam - decimais, tam);
            if ((tam >= (decimais + 4)) && (tam <= (decimais + 6)))
                objeto.value = vr.substr(0, tam - (decimais + 3)) + "." + vr.substr(tam - (decimais + 3), 3) + "," + vr.substr(tam - decimais, tam);
            if ((tam >= (decimais + 7)) && (tam <= (decimais + 9)))
                objeto.value = vr.substr(0, tam - (decimais + 6)) + "." + vr.substr(tam - (decimais + 6), 3) + "." + vr.substr(tam - (decimais + 3), 3) + "," + vr.substr(tam - decimais, tam);
            if ((tam >= (decimais + 10)) && (tam <= (decimais + 12)))
                objeto.value = vr.substr(0, tam - (decimais + 9)) + "." + vr.substr(tam - (decimais + 9), 3) + "." + vr.substr(tam - (decimais + 6), 3) + "." + vr.substr(tam - (decimais + 3), 3) + "," + vr.substr(tam - decimais, tam);
            if ((tam >= (decimais + 13)) && (tam <= (decimais + 15)))
                objeto.value = vr.substr(0, tam - (decimais + 12)) + "." + vr.substr(tam - (decimais + 12), 3) + "." + vr.substr(tam - (decimais + 9), 3) + "." + vr.substr(tam - (decimais + 6), 3) + "." + vr.substr(tam - (decimais + 3), 3) + "," + vr.substr(tam - decimais, tam);
        }
        else if((tecla != 8) && (tecla != 9) && (tecla != 13) && (tecla != 18) && (tecla != 35) && (tecla != 36) && (tecla != 37) && (tecla != 39))
        {
            return false;
        }
    }

    function flash(arquivo, largura, altura){
        document.writeln('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + largura + '" height="' + altura + '">');
        document.writeln('  <param name="movie" value="' + arquivo + '">');
        document.writeln('  <param name="quality" value="high">');
        document.writeln('  <param name="allowScriptAccess" value="sameDomain" />');
        document.writeln('  <param name="wmode" value="transparent" />');
        document.writeln('  <param name="menu" value="false" />');
        document.writeln('  <param name="bgcolor" value="#ffffff" />');
        document.writeln('  <embed src="' + arquivo + '" quality="high" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + largura + '" height="' + altura + '"></embed>');
        document.writeln('</object>');
    }

    function AbreJanela(linkjanela, x, y, s) {
        var t = (screen.height - y) / 2;
        var e = (screen.width - x) / 2;
        window.open(linkjanela,'','width=' + x + ',height=' + y + ',scrollbars=' + s + ',toolbar=0,location=0,status=0,menubar=0,resizable=0,left=' + e + ',top=' + t + '');
    }

    function validarContato(frm){
        if(frm.Nome.value==''){
            alert('Informe o Nome');
            frm.Nome.focus();
            return false;
        }
        frm.Email.value=frm.Email.value.replace(" ", "");
        if(frm.Email.value=='' || frm.Email.value.match(/(\w+)@(.+)\.(\w+)$/)==null){
            alert('Informe o E-mail');
            frm.Email.focus();
            return false;
        }
        if(frm.Telefone.value==''){
            alert('Informe o Telefone');
            frm.Telefone.focus();
            return false;
        }
        if(frm.Telefone.value.length!=14){
            alert('Informe o Telefone\n\nFormato: (11) 9999-99999');
            frm.Telefone.focus();
            return false;
        }
        if(frm.Assunto.value==''){
            alert('Informe o Assunto');
            frm.Assunto.focus();
            return false;
        }
    }
