﻿// JScript File

// Máscara de data
function MascaraData(event)
{
    //Verifica se caracter digitado é válido
    if (((event.keyCode>46)&&(event.keyCode<58))|(event.keyCode==13))
        {
            //Verifica se tem que colocar a barra e onde tem
            switch(event.srcElement.value.length)
        {
            case 2:
	        if (event.keyCode!=47)
	            {
	                event.srcElement.value=event.srcElement.value+'/'
	            }
	        break
            case 5:
	        if (event.keyCode!=47)
	            {
	                event.srcElement.value=event.srcElement.value+'/'
	            }
        	break
        }
        }
  else
    {
        event.keyCode = 0
    }
}


function Mascara(objeto, evt, mask) 
{
    var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
    var Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    var Numeros = '0123456789';
    var Fixos  = '().-:/ h,'; 
    var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

    evt = (evt) ? evt : (window.event) ? window.event : "";
    var value = objeto.value;
    if (evt)
    {
        var ntecla = (evt.which) ? evt.which : evt.keyCode;
        tecla = Charset.substr(ntecla - 32, 1);
        if (ntecla < 32) return true;

        var tamanho = value.length;
        if (tamanho >= mask.length) return false;

        var pos = mask.substr(tamanho,1); 
        while (Fixos.indexOf(pos) != -1)
        {
            value += pos;
            objeto.value = value; 
            tamanho = value.length;
            if (tamanho >= mask.length) return false;
            pos = mask.substr(tamanho,1);
        }
    }    
}


function CasasDecimais(controle)
    {
		var valor = controle.value;
		    valor = valor.replace('.',',');
		
		//alert(controle.value);
		
		var valorIndiceVirgula = valor.indexOf(',');
		// Existe o separador
		if(valorIndiceVirgula != -1)
		{
			if(valor.length - valorIndiceVirgula == 1)
			{
				controle.value = valor + '00';
			}
			else if(valor.length - valorIndiceVirgula == 2)
			{
				controle.value = valor + '0';
			}
			else if(valor.length - valorIndiceVirgula >= 3)
			{
				controle.value = valor.substring(0,valorIndiceVirgula + 3);
			}
			
		}
		else
		{
			controle.value = valor + ',00';
		}
	}
	
// Máscara de número
function MascaraNumero(event)
{
    var key = (window.event) ? event.keyCode : event.charCode;
    
    if(!(window.event) && key==13)
    {
        event.preventDefault();
    }
    
    //Verifica se caracter digitado é válido
	if (!(((key>47)&&(key<58))|(key==0)))
	{
        if(window.event)
            window.event.returnValue = false;
        else
            event.preventDefault();
    }
    else
    {
        return true;
    }
}

// Máscara de Número decimal
function MascaraNumeroDecimal(event)
{
    var key = (window.event) ? event.keyCode : event.charCode;
	//Verifica se caracter digitado é válido
	
	if(!(window.event) && key==13)
    {
        event.preventDefault();
    }
    
	if (!(((key>47)&&(key<58))|(key==44)|(key==0)))
	{
		if(window.event)
            window.event.returnValue = false;
        else
            event.preventDefault();
    }
}


///////////// Funções para contrução de menu horizontal ///////////////////

 function DOMgetElementsByClassName($node,$className)
 {
 /* Description: retorna um array com todos os elementos dentro
	de $node que possuam a classe indicada em $className
	Versão: 1.0 - 30/08/2006 Author: Micox - Náiron J.C.G - micoxjcg@yahoo.com.br Site:   http://elmicox.blogspot.com
 */
    var $node, $atual, $className, $retorno = new Array(), $novos = new Array();
    $retorno = new Array();
    for (var $i=0;$i<$node.childNodes.length;$i++)
    {
         $atual = $node.childNodes[$i];
        if($atual.nodeType==1)
        {// 1 = XML_ELEMENT_NODE
	        $classeAtual = $atual.className;							  
	        if(new RegExp("\\b"+$className+"\\b").test($classeAtual))
	        {
	            $retorno[$retorno.length] = $atual;
	        }
	        if($atual.childNodes.length>0)
	        {
	            $novos = DOMgetElementsByClassName($atual,$className);
	            if($novos.length>0)
	            {
	                $retorno = $retorno.concat($novos);
	            }
	        }
        }
    }
    return $retorno;
}

function addEvent(obj, evType, fn)
{
    //adiciona evento, versao crossbrowser
    //retirado de http://elcio.com.br/crossbrowser/#7
    if (obj.addEventListener)
    {
        obj.addEventListener(evType, fn, true)
    }
    if (obj.attachEvent)
    {
        obj.attachEvent("on"+evType, fn)
    }
}

function ativaHover(classe)
{
    //ativa o hover para elementos n?o links, por causa de bug do IE
    //retirado de http://www.maujor.com/tutorial/ddownmenu-a.php
    var pais = DOMgetElementsByClassName(document.body,classe);
    for (var j=0; j<pais.length; j++)
    {
        var sfEls = pais[j].getElementsByTagName("LI");
        for (var i=0; i<sfEls.length; i++)
        {
	        sfEls[i].onmouseover=function()
	        {
	            this.className+=" over";
	        }
	        sfEls[i].onmouseout=function()
	        {
	            this.className=this.className.replace(new RegExp(" over\\b"), "");
	        }
        }
    }
 }
