function valida(form) {
	var elementos = form.elements;
	for (var i=0; i<elementos.length; i++){
		if ((elementos[i].getAttribute("v") == 1) && (elementos[i].value == "")){
			exibemsg('msg','images/atencao.png','&nbsp;Ops! Preencha corretamente o campo: '+elementos[i].getAttribute("descricao")+'.','block');
			elementos[i].focus();
			return false;
		}else if((elementos[i].getAttribute("m") == 1)){
			if(!isMail(elementos[i].value)){
				exibemsg('msg','images/atencao.png','&nbsp;Ops! Informe um endereço de e-mail válido.','block');
				elementos[i].focus();
				return false;
			}
		}
	}return true;
}

function exibemsg(div,icone,msg,opcao){
	var local = document.getElementById(div);
	local.style.display = opcao;
	local.innerHTML = '<img src='+icone+' align=absmiddle>'+msg;
}

function abreVideoYouTube(url,largura,altura,obj)
{
	var div;
	div = document.getElementById(obj);
	div.innerHTML = "<object width='"+largura+"' height='"+altura+"'><param name='movie' value='http://www.youtube.com/v/"+url+"&hl=pt-br&fs=1&autoplay=1&rel=0&border=0'></param><param name='wmode' value='transparent'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/"+url+"&hl=pt-br&fs=1&autoplay=1&rel=0&border=0' type='application/x-shockwave-flash' wmode='transparent' allowscriptaccess='always' allowfullscreen='true' width='"+largura+"' height='"+altura+"'></embed></object>";
}

function zoom(div,tipo){
	alert(div);
	var sizeAtual = document.getElementById(div).style.fontSize;
	sizeAtual = sizeAtual.substring(0, sizeAtual.indexOf('p')); 
	if (tipo == "-"){
		document.getElementById(div).style.fontSize = (parseInt(sizeAtual)-2)+'px'; 
	}else if (tipo == "+"){
		document.getElementById(div).style.fontSize = (parseInt(sizeAtual)+2)+'px';
		} 
}


function MascaraCPF(Obj,Opcao)
{var KeyCod = window.event.keyCode;
 //PARA EVENTO ONBLUR / ONKEYUP
 if(Opcao == 0 && ((!(KeyCod == 16 || KeyCod == 8 || (KeyCod > 34 && KeyCod < 41)) && ((KeyCod > 47 && KeyCod < 58) || (KeyCod > 95 && KeyCod < 106))) || (KeyCod == 0)) || (RetornaNumeros(Obj.value).length == 11 && (!(KeyCod == 16 || KeyCod == 8 || (KeyCod > 34 && KeyCod < 41)))))
	{
	var NovoTexto = '';
	 var TextoAntigo = RetornaNumeros(Obj.value)
	 if(TextoAntigo.length > 11)
		{TextoAntigo = TextoAntigo.substr(0,11)}
	 for(Pos=0; Pos<TextoAntigo.length; Pos++)
		{NovoTexto+= TextoAntigo.substr(Pos,1)
		 if(NovoTexto.length == 3 || NovoTexto.length == 7)
			{NovoTexto+= '.'}
		 if(NovoTexto.length == 11) 
			{NovoTexto+= '-'}
		}
	 Obj.value = NovoTexto
	}
}

function MascaraCEP(Obj,Opcao)
{var KeyCod = window.event.keyCode;
 //PARA EVENTO ONBLUR / ONKEYUP	
 if(Opcao == 0 && ((!(KeyCod == 16 || KeyCod == 8 || (KeyCod > 34 && KeyCod < 41)) && ((KeyCod > 47 && KeyCod < 58) || (KeyCod > 95 && KeyCod < 106))) || (KeyCod == 0)) || (RetornaNumeros(Obj.value).length == 8 && (!(KeyCod == 16 || KeyCod == 8 || (KeyCod > 34 && KeyCod < 41)))))
	{var NovoTexto = '';
	 var TextoAntigo = RetornaNumeros(Obj.value)
	 if(TextoAntigo.length > 8)
		{TextoAntigo = TextoAntigo.substr(0,8)}
	 for(Pos=0; Pos<TextoAntigo.length; Pos++)
		{NovoTexto+= TextoAntigo.substr(Pos,1)
		 if(NovoTexto.length == 5)
			{NovoTexto+= '-'}
		}
	 Obj.value = NovoTexto
	}
}

function MascaraData(Obj,Opcao)
{var KeyCod = window.event.keyCode;
 //PARA EVENTO ONBLUR / ONKEYUP
 if(Opcao == 0 && ((!(KeyCod == 16 || KeyCod == 8 || (KeyCod > 34 && KeyCod < 41)) && ((KeyCod > 47 && KeyCod < 58) || (KeyCod > 95 && KeyCod < 106))) || (KeyCod == 0)) || (RetornaNumeros(Obj.value).length == 8 && (!(KeyCod == 16 || KeyCod == 8 || (KeyCod > 34 && KeyCod < 41)))))
	{var NovoTexto = '';
	 var TextoAntigo = RetornaNumeros(Obj.value)
	 if(TextoAntigo.length > 8)
		{TextoAntigo = TextoAntigo.substr(0,8)}
	 for(Pos=0; Pos<TextoAntigo.length; Pos++)
		{NovoTexto+= TextoAntigo.substr(Pos,1)
		 if(NovoTexto.length == 2 || NovoTexto.length == 5)
			{NovoTexto+= '/'}
		}
	 Obj.value = NovoTexto
	}
}

function MascaraFone(Obj,Opcao)
{var KeyCod = window.event.keyCode;
 //PARA EVENTO ONBLUR / ONKEYUP
 if(Opcao == 0 && ((!(KeyCod == 16 || KeyCod == 8 || (KeyCod > 34 && KeyCod < 41)) && ((KeyCod > 47 && KeyCod < 58) || (KeyCod > 95 && KeyCod < 106))) || (KeyCod == 0)) || (RetornaNumeros(Obj.value).length == 10 && (!(KeyCod == 16 || KeyCod == 8 || (KeyCod > 34 && KeyCod < 41)))))
	{var NovoTexto = '(';
	 var TextoAntigo = RetornaNumeros(Obj.value)
	 if(TextoAntigo.length > 10)
		{TextoAntigo = TextoAntigo.substr(0,10)}
	 for(Pos=0; Pos<TextoAntigo.length; Pos++)
		{NovoTexto+= TextoAntigo.substr(Pos,1)
		 if(NovoTexto.length == 3)
			{NovoTexto+= ')'}
		 if(NovoTexto.length == 8)
			{NovoTexto+= '-'}
		}
	 Obj.value = NovoTexto
	}
}

function SomenteNumeros() // PARA EVENTO OnKeyDown
{var Tecla = window.event.keyCode;
if (!(	(Tecla > 95 && Tecla < 106) || 
	(Tecla > 44 && Tecla < 58)  || 
	(Tecla > 32 && Tecla < 41)  ||
	(	Tecla == 17 ||
		Tecla == 16 ||
		Tecla == 8  || 
		Tecla == 9  || 
		Tecla == 13 ||
		Tecla == 144  )))
{window.event.returnValue = false;}

}

function ApagarCaracterSomente()  // PARA EVENTO OnKeyDown
{var Tecla = window.event.keyCode;
 if (!(	(Tecla > 33 && Tecla < 40) ||
 	(Tecla > 16 && Tecla < 18) ||
 	(Tecla == 8) ||
 	(Tecla == 46)))
	{window.event.returnValue = false;}
	
}

function RemoveTexto(Obj)
{CharPego = new RegExp("[^0-9]","gi")
 Obj.value= Obj.value.replace(CharPego,'')
}

function RetornaNumeros(Texto)
{var CharPego = new RegExp("[^0-9]","gi")
 var NovoTexto= Texto.replace(CharPego,'')
 return NovoTexto
}

function RetornaReverse(Texto)
{var str = '';
 for(indx=Texto.length; indx > 0 ;indx--)
	{str+=Texto.substr(indx - 1,1);
	}
return str;
}

function RetornaDecimal(Valor)// PARA VALORES MONETÁRIOS, PORCENTAGENS, ETC.
{Valor = Valor.replace(/[^0-9^\.^\-^\,]/g,'')
 if(Valor.replace(/[^0-9]/g,'') == ''){Valor = '0';}
 var indxVirg = RetornaReverse(Valor.replace(/,/g,'.')).indexOf('.'); // encontra a posição do separador decimal ('.')
 var FormatVal = ''
 FormatVal = RetornaNumeros(Valor.substr(0,Valor.length - indxVirg)) + '.' + RetornaNumeros(Valor.substr((Valor.length - indxVirg),indxVirg))
 return parseFloat(FormatVal);
}

function isDecimal(Valor)
{var indxVirg = RetornaReverse(Valor.replace(/,/g,'.')).indexOf('.'); // encontra a posição do separador decimal ('.')
 var SimulaValor = '';
 SimulaValor = ((Valor.substr(0,Valor.length - indxVirg)).replace(/,/g,'').replace(/\./g,'') + '.' + (Valor.substr((Valor.length - indxVirg),indxVirg)).replace(/,/g,'').replace(/\./g,'')).replace(/ /g,'')
 return !isNaN(SimulaValor)
}


function RetornaMoney(Valor)
{
var decValor = "" + RetornaDecimal(Valor)
var PontoPos = decValor.search(/\./i)
if (PontoPos >= 0)
	{casaDec = "" + decValor.substr(PontoPos+1,decValor.length) + "00000000000000000"
	 Incremento = Math.round("0." + casaDec.substr(2,15))
	 VetorDec = casaDec.substr(0,2).split("");
	 indx = 1;
	 
	 while(Incremento == 1 && indx >= 0)
	 	{VetorDec[indx] = parseInt(VetorDec[indx]) + 1;
	 	 if(VetorDec[indx] > 9)
	 	 	{VetorDec[indx] = 0;}
	 	 else
	 	 	{Incremento = 0;}
	 	 indx-- ;
	 	}
	
	 casaDec = VetorDec.join("")
	 decValor = "" + (parseFloat(decValor) + Incremento);
	 
	 //casaDec = "" + casaDec.substr(0,1) + (parseInt(casaDec.substr(1,1)) + Math.round(parseInt(Arrendonda) / 10000))
	 ValTemp = decValor.substring(decValor.substr(0,PontoPos).length - parseInt(decValor.substr(0,PontoPos).length / 3) * 3,PontoPos)
	 
	 Val = ""
	 for(indx=0; indx < ValTemp.length; indx++)
	 	{Val+= (indx % 3 == 0) ? "." + ValTemp.substr(indx,1) : ValTemp.substr(indx,1);
	 	}
	 
	 valMonetario = decValor.substr(0,PontoPos - ValTemp.length) + Val + "," + casaDec;
	 
	 if ((decValor.substr(0,PontoPos).length) % 3 == 0){valMonetario = valMonetario.substring(1,valMonetario.length)}
	}
else
	{if (decValor.length > 0 && Valor.replace(/ /g,'') != '')
		{ValTemp = decValor.substring(decValor.length - parseInt(decValor.length / 3) * 3,decValor.length)
		 Val = ""
		 for(indx=0; indx < ValTemp.length; indx++)
		 	{Val+= (indx % 3 == 0) ? "." + ValTemp.substr(indx,1) : ValTemp.substr(indx,1);
		 	}
		 valMonetario = decValor.substr(0,decValor.length - ValTemp.length) + Val + ",00";
		 
		 if ((decValor.length) % 3 == 0){valMonetario = valMonetario.substring(1,valMonetario.length)}
		}
	 else
	 	{valMonetario = "0,00";}
		
	}
return valMonetario;
}


function ArredondaDec(Numero, NumCasaDec)
{	var NumDec = "" + RetornaDecimal(Numero);
	var PontoPos = NumDec.search(/\./i);
	var decValor = NumDec.substr(PontoPos+1,NumDec.length) + "00000000000000" ;
	var Arredonda = "";
	var valFinal = "";

	var Incremento = Math.round("0." + decValor.substr(NumCasaDec,decValor.length))
	var VetorDec = decValor.substr(0,NumCasaDec).split("");
	var indx = parseInt(NumCasaDec) - 1;
	
	while(Incremento == 1 && indx >= 0)
	{VetorDec[indx] = parseInt(VetorDec[indx]) + 1;
	 if(VetorDec[indx] > 9)
	 	{VetorDec[indx] = 0;}
	 else
	 	{Incremento = 0;}
	 indx-- ;
	}
	
	decValor = VetorDec.join("")
	NumDec = "" + (parseFloat(NumDec) + Incremento);
	
	
	if(parseInt(NumCasaDec) > 0)
		{Arredonda = "." + decValor.substr(0,NumCasaDec-1) + (parseInt(decValor.substr(NumCasaDec-1,1)) + parseInt(Math.round("0."+decValor.substr(NumCasaDec,decValor.length))));
		 valFinal = NumDec.substr(0,PontoPos) + Arredonda;
		}
	else
		{valFinal = parseInt(NumDec.substr(0,PontoPos)) + parseInt(Math.round("0." + decValor))
		}
	
	return valFinal;
}

function LimitarCaracteres(TextBox, LimiteCaracteres)
{	
	objLabel = arguments[2];
	
	if(objLabel != null)
	{
		objLabel.innerHTML = TextBox.value.length + ' caracteres.';
	}
	
	if(TextBox.value.length > LimiteCaracteres && LimiteCaracteres != null)
	{
		alert('Este campo é limitado a ' + LimiteCaracteres + ' caracteres.');
		TextBox.value = TextBox.value.substr(0, LimiteCaracteres);
		TextBox.focus();
		if(objLabel != null)
		{
			objLabel.innerHTML = TextBox.value.length + ' caracteres.';
		}
		return false;
	}
}

function isMail(MailAddress)
{
	var R,S,T,U,V,X,Y,Z;
	var ErroM = 0; My = MailAddress;
	X = My.search('@');// Procura pela Primeira @
	Y = My.substr((X+1));// Guarda todo o texto após a primeira @
	Z = Y.search('@');// Procura por uma segunda @
	V = My.search('.');// Procura pelo primeiro ponto(.)  
	U = Y.length;// Guarda o numero de caracteres após a primeira @
	T = Y.charAt(U-1);// Pega o primeiro caracter de pois da primeira @
	S = My.slice(0,X);//
	for (L = 1; L < U; L++)// Pega o Numero de pontos(.) encontrados apos a primeira @
	{
		if (Y.charAt(L)=='.')
		{
			ErroM++;
		}
	}
	if (Y.charAt(0) == '.' || ErroM > 8 || ErroM == 0 || T == '.' || U == 0 || X == (-1) || Z != (-1) || S == '')
	{
		return false;
	}
	return true;
}

function isDate(day, month, year)
{
		Data=
			(!(
				isNaN(day) ||
				isNaN(month) ||
				isNaN(year) ||
				(day>31)||
				(month>12)||
				(day<0)||
				(month<0)||
				(day == '') ||
				(month == '') ||
				(year == '') ||
				(
					(day == 31) &&
					(
						(month == 2) ||
						(month == 4) ||
						(month == 6) ||
						(month == 9) ||
						(month == 11)
					)
				)
				||
				(
					(
						(year % 4) == 0
					)
					?
					(
						(month == 2) &&
						(day > 29)
					)
					:
					(
						(month == 2) &&
						(day > 28)
					)
				)
			))
		
		return Data;
}

//Bloqueia a tecla enter
function Enter(Event)
{
    var Key;
    Key = Event.keyCode;
    if(Key == 13) return false;
    return true;
}

//Limpa campo ao clicar
function limparCampo(campo) {
    if (campo.value == campo.defaultValue) {
        campo.value = "";
    }
}

//Preenche campo um valor
function escreverCampo(campo) {
    if (campo.value == "") {
        campo.value = campo.defaultValue;
    }
}

/* Show / Hide - Copyright (c) 2005-2007 Stu Nicholls. All rights reserved.*/
clickMenu = function(menu,element,cname) {
	var getEls = document.getElementById(menu).getElementsByTagName(element);
	for (var i=0; i<getEls.length; i++) {
			getEls[i].onclick=function() {
			if ((this.className.indexOf(cname))!=-1)
			{
			if ((this.className.indexOf('click'))!=-1) {
				this.className=this.className.replace("click", "");;
				}
				else {
				this.className+=" click";
				}
			}
		}
	}
}

// Formata Float centavos com ',' milhar com '.' 
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 
function Formata(campo,tammax,teclapres,decimal) { 
            var tecla = teclapres.keyCode; 
            vr = Limpar(campo.value,"0123456789"); 
            tam = vr.length; 
            dec=decimal 

            if (tam < tammax && 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 ){ 
                    campo.value = vr ; 
                } 
                if ( (tam > dec) && (tam <= 5) ){ 
                    campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; 
                } 
                if ( (tam >= 6) && (tam <= 8) ){ 
                    campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
                } 
                if ( (tam >= 9) && (tam <= 11) ){ 
                    campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
                } 
                if ( (tam >= 12) && (tam <= 14) ){ 
                    campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
                } 
                if ( (tam >= 15) && (tam <= 17) ){ 
                    campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ; 
                } 
            } 
        } 

function FormataValor(obj,tammax,teclapres) {
 var tecla = teclapres.keyCode;
 vr = obj.value;
 vr = vr.replace( "/", "" );
 vr = vr.replace( "/", "" );
 vr = vr.replace( ",", "" );
 vr = vr.replace( ",", "" );
 vr = vr.replace( ".", "" );
 vr = vr.replace( ".", "" );
 vr = vr.replace( ".", "" );
 vr = vr.replace( ".", "" );
 //Replaces adicionais
 //vr = vr.replace( "-", "" );
 //vr = vr.replace( "+", "" );
 //vr = vr.replace( "*", "" );
 tam = vr.length;
 
 if (tam < tammax && 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 <= 2 ){ 
   obj.value = vr ; }
  if ( (tam > 2) && (tam <= 5) ){
   obj.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
  if ( (tam >= 6) && (tam <= 8) ){
   obj.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
  if ( (tam >= 9) && (tam <= 11) ){
   obj.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
  if ( (tam >= 12) && (tam <= 14) ){
   obj.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
  if ( (tam >= 15) && (tam <= 17) ){
   obj.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
 }
}
