/********************************************************************** 
Função de formatação de campos do tipo Data 

Objetivos : 
    - Mascarar a entrada de dados no formato : dd/mm/aaaa 

Parâmetros : 
    objeto        -> Nome do campo de formulário (Usar this) 
    teclapress    -> Tecla pressionada (Usar event) 

Exemplo : 
    OnKeyDown    FormataData(this,event); 

Requirido : 
    Função ValidaData 
/**********************************************************************/ 

function FormataData(objeto,teclapress) { 
    var tecla = teclapress.keyCode; 

    if(((window.event.keyCode == 13) || (window.event.keyCode == 9))&&objeto.value != "") 
    { 
        if(!(ValidaData(objeto))) 
            { 
                window.event.cancelBubble = true; 
                window.event.returnValue = false; 
                alert("Data Inválida"); 
                objeto.value = ""; 
                objeto.focus(); 
            } 
    } 

    if (( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )&& objeto.value.length < (10)) 
    { 
        vr = objeto.value; 
        vr = vr.replace( "/", "" ); 
        vr = vr.replace( "/", "" ); 
        tam = vr.length; 

        if (tam < 8) 
            { 
                if (tecla != 8) {tam = vr.length + 1 ;} 
            } 
        else 
            { 
                window.event.cancelBubble = true; 
                window.event.returnValue = false; 
            } 
         
        if ((tecla == 8) && (tam > 1)) 
            { 
                tam = tam - 1 ; 
                objeto.value = vr.substr(0,tam); 
                window.event.cancelBubble = true; 
                window.event.returnValue = false; 
            } 
                if ( tam <= 4 && tecla != 8){ 
                     objeto.value = vr ; } 

                if ( (tam >= 4) && (tam <= 6) ){ 
                     objeto.value = vr.substr(0, tam - 4) + '/' + vr.substr( tam - 4, 4 ); } 

                if ( (tam >= 6) && (tam <= 8) ){ 
                    objeto.value = vr.substr(0, tam - 6 ) + '/' + vr.substr( tam - 6, 2 ) + '/' + vr.substr( tam - 4, 4 ); } 

                if ((tam == (8)) && tecla != 8) 
                    { 
                        if(tecla >=96 && tecla <=105) 
                            { 
                                tecla = tecla - 48; 
                            } 

                        objeto.value = objeto.value + (String.fromCharCode(tecla)); 
                        window.event.cancelBubble = true; 
                        window.event.returnValue = false; 

                        if (!(ValidaData(objeto))) 
                            { 
                                alert("Data Inválida"); 
                                objeto.value = ""; 
                                objeto.focus(); 
                            } 
                    } 
    } 
    else if((window.event.keyCode != 8) && (window.event.keyCode != 9) && (window.event.keyCode != 13) && (window.event.keyCode != 35) && (window.event.keyCode != 36) && (window.event.keyCode != 46)) 
        { 
            event.returnValue = false; 
        } 
} 


// JavaScript Document
function Limpar(valor, validos) { 
	// retira caracteres invalidos da string 
	var result = ""; 
	var aux; 
	for (var i=0; i < valor.length; i++) { 
		aux = validos.indexOf(valor.substring(i, i+1)); 
		if (aux >= 0) { 
			result += aux;
		} 
	} 
	return result; 
}

// Formata número tipo moeda usando o evento onKeyDown 
// formatarMoeda(this, event, 2)
function formatarMoeda(campo, teclapres, decimal) { 
	var tecla = teclapres.keyCode;
	vr 	= Limpar(campo.value,"0123456789"); 
	tam = vr.length; 
	dec	= decimal 

	/* Testa por tab e shift */
	if ((tecla == 9) || (tecla == 16)) {
		return;
	}
	
	if (tecla != 8){ 
		tam = vr.length + 1 ; 
	} 

	if (tecla == 8) { 
		tam = tam - 1 ; 
	} 

	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) { 
		if ( tam > dec ) { 
			vr= vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec , tam ); 
		} 
	} 
	
	campo.value = vr;
} 

/**************************************
* Jonas Raoni Soares Silva
* http://www.joninhas.ath.cx
**************************************/

//========================================================
// REQUIRES http://www.jsfromhell.com/geral/event-listener
//========================================================

MaskInput = function(f, m){ //v1.0
	function mask(e){
		
		/* Testa por tab e shift - adicionado por Tiago */
		if ((e.keyCode == 9) || (e.keyCode == 16)) {
			return;
		}
		
		var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[À-ÿ]/i, "8": /./ },
		rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
	
		function accept(c, rule){
			for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1) {
				if (r & i && patterns[i].test(c)) {
					break;
				}
			}
				
			return i <= r || c == rule;
		}
		
		var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
		
		(!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
		r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
		: (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
		r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
	}
	
	for (var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1}) {
		addEvent(f, i, mask);
	}
	
};

/*
**************************************
* Event Listener Function v1.4       *
* Autor: Carlos R. L. Rodrigues      *
**************************************
*/
addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;


    r[r.length] = [f, s || o], o[e] = function(e){
        try {
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        } catch(f) {}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};