﻿	// máscara de campos
    // uso: onkeydown="FormataCampo(this,event,'##/##/####')"
    function FormataCampo(Campo,teclapres,mascara, sai) {

        if (sai == "S" && consistente == "N") {
            if (obrigatorio == "N" && Campo.value.length > 0) {
                obrig_fixo = "S";
                Consist(Campo.maxLength, Campo);
                obrig_fixo = "N";
            }
            if (obrigatorio == "S" || Campo.value.length > 0) {
                if (sai == "S") {
                    if (Campo.value.length != mascara.length) {
                        alert('O campo precisa estar neste formato:\n\n       '+ mascara);
                        Campo.value = "";
                    }
                    erro = "S";
                    return false;
                }
            }
        }
        if (sai == "S" && obrigatorio == "N" && Campo.value.length > 0) {
            obrig_fixo = "S";
            Consist(Campo.maxLength, Campo);
            obrig_fixo = "N";
            if (consistente == "N") {
                alert('O campo precisa estar neste formato:\n\n       '+ mascara);
            }
        }

        strtext = Campo.value;
        tamtext = strtext.length;
        tammask = mascara.length;
        arrmask = new Array(tammask);
        for (var i = 0 ; i < tammask; i++) {
            arrmask[i] = mascara.slice(i,i+1)
        } 

		//alert(teclapres.keyCode );
        if (((((arrmask[tamtext] == "#") || (arrmask[tamtext] == "9"))) || (((arrmask[tamtext+1] != "#") || (arrmask[tamtext+1] != "9"))))) {
            if ((teclapres.keyCode >= 35 && teclapres.keyCode <= 40)||(teclapres.keyCode >= 48 && teclapres.keyCode <= 57)||(teclapres.keyCode >= 96 && teclapres.keyCode <= 105)||(teclapres.keyCode == 8)||(teclapres.keyCode == 9) ||(teclapres.keyCode == 46) ||(teclapres.keyCode == 13)||(teclapres.keyCode == 16)){
                Organiza_Casa(Campo,arrmask[tamtext],teclapres.keyCode,strtext)		
            } else {
                Detona_Event(Campo,strtext)
            }
        } else {
            if ((arrmask[tamtext] == "A")) {
                charupper = event.valueOf()
                Detona_Event(Campo,strtext)
                masktext = strtext + charupper 
                Campo.value = masktext
            }
        }
    }

	function Organiza_Casa(Campo,arrpos,teclapres_key,strtext){
        if (((arrpos == "/") || (arrpos == ".") || (arrpos == ",") || (arrpos == ":") || (arrpos == " ") || (arrpos == "-")) && !(teclapres_key == 8)){
            separador = arrpos
            masktext = strtext + separador
            Campo.value = masktext
        }
    }

	function Detona_Event(Campo,strtext){
        event.returnValue = false
        if (strtext != "") {
            Campo.value = strtext
        }
    }