﻿function _MaskAPI(){
	this.version = "0.4a";
	this.instances = 0;
	this.objects = {};
}
MaskAPI = new _MaskAPI();

function Mask(m, t){
	this.mask = m;
	this.type = (typeof t == "string") ? t : "string";
	this.error = [];
	this.errorCodes = [];
	this.value = "";
	this.strippedValue = "";
	this.allowPartial = false;
	this.id = MaskAPI.instances++;
	this.ref = "MaskAPI.objects['" + this.id + "']";
	MaskAPI.objects[this.id] = this;
}


Mask.prototype.attach = function (o){	
    $addEvent(o, "onkeyup", this.ref + ".DigitarDepois(event, this);", true);
    $addEvent(o, "onkeypress", this.ref + ".DigitarAntes(event, this);", true);
	$addEvent(o, "onblur", this.ref + ".SairdoCampo(this);", true);
}

Mask.prototype.SairdoCampo = function (obj)
{
    if (this.mask == "VALOR" && obj.value != "")
    {        
        var temVirgula = false;
        var c = 0;
        for (var t = 0; t <= obj.value.length; t++)
        {
            if (obj.value.substring(t,t+1) == ",")
            {
                temVirgula = true;
                c = t;                                             
            }
        }
        if (temVirgula == true)
        {
            obj.value = obj.value;
            if (obj.value.length == c+1)
            {
                obj.value = obj.value + "00";
            }
            if (obj.value.length == c + 2)
            {
                obj.value = obj.value + "0";
            }            
        }
        else
        {
            obj.value = obj.value + ",00";
        }

    }    
    
    return obj.value;
}

Mask.prototype.DigitarAntes = function (event, obj)
{
    var keypress =(window.event)?event.keyCode:evento.which;
	campo = eval(obj);		
    if (this.mask == "MAIUSCULAS")
    {        
        campo.value =  campo.value.toUpperCase();
        if (keypress >=97 && keypress <= 122)
        {   
            event.returnValue = true;
            return true;            
        }
        else        
        {
	        if ((keypress < 48 || keypress > 57) && (keypress != 32) && (keypress < 65 || keypress > 90))
	        {
                event.returnValue = false;
                return true;	    
	        }
	    }
	}
	else        
	{
        if (this.mask == "VALOR")
        {
	        if ((keypress < 48 || keypress > 57) && keypress != 44)
	        {
	            event.returnValue = false;
	            return true;
	        }
	    }
	    else 
	    {
	        if (keypress < 48 || keypress > 57)
	        {
	            event.returnValue = false;
	            return true;
	        }
	    }	
	}
	return true;
}


Mask.prototype.DigitarDepois = function (event, obj)
{
    var Mascara = "";
    var NovoValor = "";
    
    var Valor = obj.value.replace(".", "").replace(".", "").replace(".", "").replace("/", "").replace("/", "").replace("-", "").replace("(", "").replace(")", "").replace(" ", "").replace(":", "");
    var Tecla = (window.event)?event.keyCode:evento.which;

    if (Tecla >= 37 && Tecla <= 40)
    {
        return true;
    }
    if (this.mask == "MAIUSCULAS")
    {
        obj.value = obj.value.toUpperCase();
        return true;
    }
    if (this.mask == "RG")
    {           
        var Mascara = "99.999.999-9";
    }
    if (this.mask == "CPF")
    {           
        var Mascara = "999.999.999-99";
    }
    if (this.mask == "CNPJ")
    {           
        var Mascara = "99.999.999/9999-99";
    }
    if (this.mask == "TELEFONE")
    {           
        var Mascara = "(99) 9999-9999";
    }
    if (this.mask == "CEP")
    {           
        var Mascara = "99999-999";
    }
    if (this.mask == "DATA")
    {           
        var Mascara = "99/99/9999";
    }
    if (this.mask == "HORA")
    {           
        var Mascara = "99:99";
    }
    if (this.mask == "DATAHORA")
    {           
        var Mascara = "99/99/9999 99:99";
    }
    //FALTAM
    if (this.mask == "INTEIRO")
    {           
        var Mascara = "9.999.999.999";
        if (Valor.length == 1)            
            var Mascara = "9";
        if (Valor.length == 2)            
            var Mascara = "99";
        if (Valor.length == 3)            
            var Mascara = "999";
        if (Valor.length == 4)            
            var Mascara = "9.999";
        if (Valor.length == 5)            
            var Mascara = "99.999";
        if (Valor.length == 6)            
            var Mascara = "999.999";
        if (Valor.length == 7)            
            var Mascara = "9.999.999";
        if (Valor.length == 8)            
            var Mascara = "99.999.999";
        if (Valor.length == 9)            
            var Mascara = "999.999.999";
        if (Valor.length == 10)            
            var Mascara = "9.999.999.999";                    
    }
    if (this.mask == "SONUMERO")
    {   
        var Mascara = "";
        for (var t = 1; t <= obj.maxLength; t++)
        {
            Mascara = Mascara + "9";
        }                
    }
    if (this.mask == "VALOR")
    {   
        //procurando por , (virgula)
        
        var temVirgula = false;
        var c = 0;
        for (var t = 0; t <= Valor.length; t++)
        {
            if (Valor.substring(t,t+1) == ",")
            {
                temVirgula = true;
                c = t;                                             
            }
        }
        if (temVirgula == false)
        {
            if (Valor.length == 1)            
                var Mascara = "9";
            if (Valor.length == 2)            
                var Mascara = "99";
            if (Valor.length == 3)            
                var Mascara = "999";
            if (Valor.length == 4)            
                var Mascara = "9.999";
            if (Valor.length == 5)            
                var Mascara = "99.999";
            if (Valor.length == 6)            
                var Mascara = "999.999";
            if (Valor.length == 7)            
                var Mascara = "9.999.999";
            if (Valor.length == 8)            
                var Mascara = "99.999.999";
            if (Valor.length == 9)            
                var Mascara = "999.999.999";
            if (Valor.length == 10)            
                var Mascara = "9.999.999.999"; 
        }
        else
        {   
            if (c == 1)            
                var Mascara = "9";
            if (c == 2)            
                var Mascara = "99";
            if (c == 3)            
                var Mascara = "999";
            if (c == 4)            
                var Mascara = "9.999";
            if (c == 5)            
                var Mascara = "99.999";
            if (c == 6)            
                var Mascara = "999.999";
            if (c == 7)            
                var Mascara = "9.999.999";
            if (c == 8)            
                var Mascara = "99.999.999";
            if (c == 9)            
                var Mascara = "999.999.999";
            if (c == 10)            
                var Mascara = "9.999.999.999"; 
                     
            Mascara = Mascara + "," + "99";
        }
        
    }
    var Valor = Valor.replace(",", "");
    
    var NovoValor = "";
    var i = 0;
    var m = 0;
        
    var x = Valor.length;
    
    while (i < x)    
    {        
        if (Mascara.length != NovoValor.length)
        {            
            if (Mascara.substring(i, i + 1) == "9")
            {                
                NovoValor = NovoValor + Valor.substring(i - m, i - m + 1);                
                if (Mascara.length != NovoValor.length)
                {                    
                    if (Mascara.substring(i + 1, i + 2) != "9")
                    {
                        NovoValor = NovoValor + Mascara.substring(i + 1, i + 2);
                        m = m + 1;
                        x = x + 1;
                        if (Mascara.substring(i + 2, i + 3) != "9")
                        {                            
                            NovoValor = NovoValor + Mascara.substring(i + 2, i + 3);                            
                            m = m + 1;
                            x = x + 1;                            
                        }
                    }
                }                
            }
            else
            {                
                if (NovoValor.length == 0)
                {
                    NovoValor = NovoValor + Mascara.substring(i - m, i - m + 1);
                    m = m + 1;                
                    NovoValor = NovoValor + Valor.substring(i - m, i - m + 1);                    
                    x = x + 1;                    
                }
            }
        }
        i = i + 1;
    }
    obj.value = NovoValor;
     
	return true;
}

function $addEvent(o, _e, c, _b){
	var e = _e.toLowerCase(), b = (typeof _b == "boolean") ? _b : true, x = (o[e]) ? o[e].toString() : "";
	
	x = x.substring(x.indexOf("{")+1, x.lastIndexOf("}"));
	x = ((b) ? (x + c) : (c + x)) + "\n";
	return o[e] = (!!window.Event) ? new Function("event", x) : new Function(x);
}

d = document;
f = d.forms[0];

function ObterControleAspNet(nomeParcial, tag)
{
    var inputs = document.getElementsByTagName(tag);
    for(var i=0; i<inputs.length; i++)
    {
	    if(inputs[i].id.search(nomeParcial) >= 0)
	    {
		    return inputs[i];
	    }
    }
}

function Mascarar(nome, mascara)
{
    var ctrl =  ObterControleAspNet(nome,'input');
    if(ctrl != null)
        mascara.attach(ctrl);
}

