﻿function PulaPBotao(control, campo)
{
  
}

function TrocaTabControles(control)
{
	if((control.tagName == "INPUT" && (control.type == "text" || control.type == "pass"))
		|| control.tagName == "SELECT")
	{
       addHandler(control, "keydown", EnterOnKeyDown);
	}
	
	for(var i=0; i<control.childNodes.length; i++)
	{
		TrocaTabControles(control.childNodes[i]);
	}
}

function EnterOnKeyDown(evt)
{
	var oEvent = (window.event) ? window.event : evt;
	var nKeyCode =  oEvent.keyCode ? oEvent.keyCode :
			oEvent.which ? oEvent.which : 
			void 0;

	var bAltPressed = (oEvent.altKey) ? oEvent.altKey : oEvent.modifiers & 1 > 0;
	var bShiftPressed = (oEvent.shiftKey) ? oEvent.shiftKey : oEvent.modifiers & 4 > 0;
	var bCtrlPressed = (oEvent.ctrlKey) ? oEvent.ctrlKey : oEvent.modifiers & 2 > 0;
   
	var ret = false;
	
	//Enter
	if(nKeyCode == 13 && !bAltPressed && !bShiftPressed && !bCtrlPressed)
	{
		ret = false;
	}
	else
		ret = true;
	
	if(!ret)
	{
		var campo = (oEvent.srcElement)?oEvent.srcElement:oEvent.target;
		// localiza o próximo campo em relação ao pai
    		
	    var pai = campo.parentNode;
	    var processaFilho = false;
	    while(pai != null)
	    {
	        for(var i=0; i<pai.childNodes.length; i++)
	        {
	            if(processaFilho)
	            {
                    if(!pai.childNodes[i].disabled) // Não pode ser dado foco em campos desabilitados;
                    {
                        if(pai.childNodes[i].tagName == "INPUT" && (pai.childNodes[i].type == "text" || pai.childNodes[i].type == "pass") && !pai.childNodes[i].readOnly)
                        {
                            EnterOnKeyDownFoco(oEvent, pai.childNodes[i]);
                            return false;
                        }
                        
	                    if(pai.childNodes[i].tagName == "INPUT" && pai.childNodes[i].type == "image")
                        {
	                        if(!pai.childNodes[i].src.endsWith("imagens/icones/lupa.gif") && !pai.childNodes[i].src.endsWith("imagens/icones/calendario2.jpg") && !pai.childNodes[i].src.endsWith("imagens/ico-detalhar-medidas.gif"))
                            {
                                EnterOnKeyDownFoco(oEvent, pai.childNodes[i]);
                                return false;
                            }
                        }
                        
	                    if(pai.childNodes[i].tagName == "INPUT" && pai.childNodes[i].type == "button")
	                    {
                            EnterOnKeyDownFoco(oEvent, pai.childNodes[i]);
                            return false;
	                    }
                			    
	                    if(pai.childNodes[i].tagName == "INPUT" && pai.childNodes[i].type == "submit")
	                    {
                            EnterOnKeyDownFoco(oEvent, pai.childNodes[i]);
                            return false;
	                    }

	                    if(pai.childNodes[i].tagName == "A")
	                    {
	                        if(pai.childNodes[i].innerHTML.indexOf("imagens/icones/lupa.gif") == -1 && pai.childNodes[i].innerHTML.indexOf("imagens/icones/calendario2.gif") == -1)
	                        {
                                EnterOnKeyDownFoco(oEvent, pai.childNodes[i]);
                                return false;
                            }
	                    }
    	                
                        if(pai.childNodes[i].tagName == "TEXTAREA" && !pai.childNodes[i].readOnly)
                        {
                            EnterOnKeyDownFoco(oEvent, pai.childNodes[i]);
                            return false;
                        }

                        if(pai.childNodes[i].tagName == "SELECT")
                        {
                            EnterOnKeyDownFoco(oEvent, pai.childNodes[i]);
                            return false;
                        }

	                    if(pai.childNodes[i].childNodes.length > 0)
	                    {
	                        pai = pai.childNodes[i];
	                        break;
	                    }
	                }
	            }
    	        
	            if(pai.childNodes[i] == campo)
	            {
	                processaFilho = true;
	            }
    	        
	            // se chegou no último filho, sobe na árvore
	            if(pai.childNodes.length == i+1)
	            {
	                campo = pai;
	                processaFilho = false;
	                pai = pai.parentNode;
                    break;
	            }
	        }
		}
	}
	return true;
}

function EnterOnKeyDownFoco(oEvent, campo)
{
    SetFocoCampo(campo.id);
    CancelEvent(oEvent);
}

function SetFocoCampo(campo)
{
    try
    {
        campo = document.getElementById(campo);
        campo.focus();
        
        if(campo.type == "text")
        {
		    // IE Support
	        if (document.selection)
	        {
	            selecao = document.selection.createRange();
	            selecao.moveStart('character', -campo.value.length);
	            selecao.moveEnd('character', campo.value.length)
	            selecao.select();
	        }
	        // Firefox support
	        else
	        {
		        campo.selectionStart = 0;
		        campo.selectionEnd = campo.value.length;
	        }
	    }
    }
    catch (ex) {}
}

function formatCPF(t,teclapres) {
	var tecla = teclapres.keyCode;
	if ( tecla != 9 && tecla != 8 ) {
		var n = t;
		if (t.length==3) return n = n+".";
		if (t.length==7) return n = n+".";
		if (t.length==11) return n = n+"-";
	}
	return t
}

function formatCNPJ(t,teclapres) {
	var tecla = teclapres.keyCode;
	if ( tecla != 9 && tecla != 8 ) {
		var n = t;
		if (t.length==2) return n = n+".";
		if (t.length==6) return n = n+".";
		if (t.length==10) return n = n+"/";
		if (t.length==15) return n = n+"-";
	}
	return t
}

function formatCep(t,teclapres) {
	var tecla = teclapres.keyCode;
	if ( tecla != 9 && tecla != 8 ) {
		t = t.replace(/[^0-9]/g,"");
		var n = t;
		if (t.length>=4) n =t.substr(0,5)+"-"+t.substr(5,3);
		if (t.length>=7) return n.substr(0,8);
		{
		}
		return n;
	}
	return t
}

function formatTel(t,teclapres) {
	var tecla = teclapres.keyCode;
	if ( tecla != 9 && tecla != 8 ) {
		t = t.replace(/[^0-9]/g,"");
		var n = t;
		if (t.length>=2) n = "("+t.substr(0,2)+")"+t.substr(2,4);
		if (t.length>=6) n+= "-"+t.substr(6,4);
		if (t.length>=10) return n.substr(0,12);
		{
		}
		return n;
	}
	return t
}

function formatDuasCasasDecimais(campo)
{
    if(campo.value != "")
    {
	var n = campo.value.replace(/[^0-9]/g,"");
	while(n.indexOf("0")==0)
	    n = n.replace("0", "");
	
	for(var i = 0; n.length < 3; i++)
        n = "0" + n;  
	
	n = n.substr(0,n.length - 2)+","+n.substr(n.length - 2, 2);
	campo.value = n;
}
}	

function formatData(t,teclapres) {
	var tecla = teclapres.keyCode;
	if ( tecla != 9 && tecla != 8 ) {
		var n = t;
		if (t.length==2) return n = n+"/";
		if (t.length==5) return n = n+"/";
	}
	return t
}

//Essa funcao formata um campo qqr e retira qqr quantidade de caracteres especiais
//e formata o campo de acordo com a mascara passada em sMask

//Exemplo
//Para chamar a funcao no aspx e permitindo apenas numeros
//this.txtCodigoContaFinanceira.Attributes.Add("onkeypress", "return formatMask(this, '" + txtCodigoContaFinanceira.ClientID + "', '" + mascara + "', event); if(event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;");

function formatMask(id, sMask, evtKeyPress) {
     var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
     sValue = document.getElementById(id).value;
     // Limpa todos os caracteres de formatação que
     // já estiverem no campo.
     while(sValue.indexOf('-', 0)!=-1)
     {
        sValue = sValue.toString().replace( "-", "" );
     }
     while(sValue.indexOf('.', 0)!=-1)
     {
        sValue = sValue.toString().replace( ".", "" );
     }
     while(sValue.indexOf('/', 0)!=-1)
     {
        sValue = sValue.toString().replace( "/", "" );
     }
     while(sValue.indexOf('(', 0)!=-1)
     {
        sValue = sValue.toString().replace( "(", "" );
     }
     while(sValue.indexOf(')', 0)!=-1)
     {
        sValue = sValue.toString().replace( "(", "" );
     }
     while(sValue.indexOf(' ', 0)!=-1)
     {
        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) == "/"))
       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++;
     }

     document.getElementById(id).value = sCod;
}        


function MascaraGenerica(evt, mascara)
{
    var oEvent = (window.event) ? window.event : evt;
    var nKeyCode =  oEvent.keyCode ? oEvent.keyCode : oEvent.which ? oEvent.which : void 0;
    var campo = (oEvent.srcElement)?oEvent.srcElement:oEvent.target;

    var bNumerico = (nKeyCode >= 48 && nKeyCode <=57) || (nKeyCode >= 96 && nKeyCode <= 105);
    var bCaractere = nKeyCode >= 65 && nKeyCode <=90;
    var bAlfaNumerico = bNumerico || bCaractere;
    
    var teclasIgnoradas = [17, 16, 13, 8, 46, 9, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 36, 35, 45, 37, 38, 39, 40];
    
    for(var j=0; j < teclasIgnoradas.length; j++)
        if(teclasIgnoradas[j] == nKeyCode)
            return;

    if(campo.value.length + 1 <= mascara.length)
    {
        var texto = campo.value;
        
        var caractere;
        
        caractere = mascara.substring(texto.length, texto.length+1);
        while(caractere != "A" && caractere != "0" && caractere != "C" && caractere != "")
        {
            texto = texto + caractere;
            caractere = mascara.substring(texto.length, texto.length+1);
        }

        if((caractere == "0" || caractere == "A") && bNumerico)
        {
            if(nKeyCode >= 96)
            {
                texto += (nKeyCode - 96);
            }
            else
            {
                texto += (nKeyCode - 48);
            }
        }
        
        if((caractere == "C" || caractere == "A") && bCaractere)
        {
            texto += String.fromCharCode(nKeyCode);
        }


        caractere = mascara.substring(texto.length, texto.length+1);
        while(caractere != "A" && caractere != "0" && caractere != "C" && caractere != "")
        {
            texto = texto + caractere;
            caractere = mascara.substring(texto.length, texto.length+1);
        }
        
        campo.value = texto;
    }
    
    CancelEvent(oEvent);
}

function Mascarar(evt, o, f)
{
	var oEvent = (window.event) ? window.event : evt;
	if(oEvent.type != "blur")
	{
		var nKeyCode =  oEvent.keyCode ? oEvent.keyCode : oEvent.which ? oEvent.which : void 0;
		
		var ret = false;
		
		if(nKeyCode >= 48 && nKeyCode <= 57)
			ret = true;
		if(nKeyCode == 8 || nKeyCode == 46 || nKeyCode == 13 || nKeyCode == 9)
			ret = true;

		if(!ret)
		{
			CancelEvent(oEvent);
		}
		else if(nKeyCode != 13 && nKeyCode != 9)
			setTimeout(function() { execmascara(o, f); }, 0);
   }
   else
       execmascara(o, f);
}

function CancelEvent(oEvent)
{
	try
	{
		oEvent.returnValue = false;
		oEvent.cancelBubble = true;

		if(document.all) //IE
		{
			oEvent.keyCode = 0;
		}
		else //NS
		{
			oEvent.preventDefault();
			oEvent.stopPropagation();
		}
	}
	catch(ex)
	{
		//alert(ex);
	}
}

function execmascara(v_obj, v_fun)
{       
	v_obj.value=v_fun(v_obj.value);	
}

function isDate(obj)
{
	var data = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
	var valor = obj.value;

	if (valor != null && valor != "") 
	{
		if (!data.test(valor))
		{
			obj.value= '';
			alert("Por favor entre com a data válida! (dd/mm/yyyy)");
			setTimeout(function () {obj.focus()},0);
			return false;
		} 
	}
	return true;
}
	
function MascararData(valor)
{
    if(valor.length > 10)
	    valor = valor.substring(0,10);

    valor=valor.replace(/\D/g,"");
    valor=valor.replace(/(\d{2})(\d)/,"$1/$2");
    valor=valor.replace(/(\d{2})(\d)/,"$1/$2");
    valor=valor.replace(/^((0[1-9]|[12]\d)\-(0[1-9]|1[0-2])|30\-(0[13-9]|1[0-2])|31\-(0[13578]|1[02]))\-\d{4}/);
    
    return valor;
}
	
		
	

