//Objetivo		: Validar a data digitada
//Parametro		: pDia = campo que contem o dia, pMes = Campo que contem o mes, pAno = campo que contem o ano
//Por			: Marcelo costa
function IsDate(pDia, pMes, pAno)
{
	var dia = '';
	var mes = '';
	var ano = '';

	//Recuperando partes da Data
	dia = pDia.value;
	mes = pMes.value;
	ano = pAno.value;
		
	if(dia == '00' || dia == '')
	{
		alert('Dia Invalido.');
		pDia.focus();
		return false;
	}
		
	if(mes > '12' || mes == '00' || mes == '')
	{
		alert('Mes Invalido.');
		pMes.focus();
		return false;
	}
		
	if (ano < '0200' || ano == '' || ano.length < 4)
	{
		alert('Ano Invalido.');
		pAno.focus();
		return false;
	}

	if (mes == '01' || mes == '03' || mes == '05' || mes == '07' || mes == '08' || mes == '10' || mes == '12')
	{
		if (dia > '31')
		{
			alert('Dia Invalido.');
			pDia.focus();
			return false;
		}
	}

	if(mes == '04' || mes == '06' || mes == '09' || mes == '11')
	{
		if (dia > '30')
		{
			alert('Dia Invalido.');
			pDia.focus();
			return false;
		}
	}

	if(mes == '02')
	{
		if((parseInt(ano) % 4) == 0)
		{
			if (dia > '29')
			{
				alert('Dia Invalido.');
				pDia.focus();
				return false;
			}
		}
		else
		{
			if (dia > '28')
			{
				alert('Dia Invalido.');
				pDia.focus();
				return false;
			}
		}
	}
		
	return true;
}

//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Validar se foi digitado apenas numeros
//Parametro		: pCampo = campo a ser validado
//Por			: Marcelo Costa
function IsNumber(pCampo)
{
	var pExp = /[ÁÉÍÓÚÀÈÌÒÙÃÕÇABCDEFGHIJLMNOPQRSTUVXYWZK.,<>;:{'"}()!@#$%^&*~/`_=+|]/;
	var pDados = '';
	
	//Transformando em Maiuscula
	pDados = pCampo.value.toUpperCase();
	
	//Verificando se possui alguma letra dentro do campo
	if (pDados.search(pExp)!= -1)
	{
		alert('Valor invalido');
		//pCampo.value = '';
		pCampo.focus();
		return false;
	}
	
	return true;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Para a digitação e muda o foco é necessario um tamanho maximo serve tambem para limitar tamanho
//Parametros	: Ultilizar no onKeyUp do campo
//Por			: Marcelo Costa

function MudaFoco(quem, tammax) { VerifiqueTAB=true;
if ( (quem.value.length == tammax) && (VerifiqueTAB) ) { 
var i=0,j=0, indice=-1;
for (i=0; i<document.forms.length; i++) { 
for (j=0; j<document.forms[i].elements.length; j++) { 
if (document.forms[i].elements[j].name == quem.name) { 
indice=i;
break;
} 
} 
if (indice != -1) break; 
} 
for (i=0; i<=document.forms[indice].elements.length; i++) { 
if (document.forms[indice].elements[i].name == quem.name) { 
while ( (document.forms[indice].elements[(i+1)].type == "hidden") &&
(i < document.forms[indice].elements.length) ) { 
i++;
} 
document.forms[indice].elements[(i+1)].focus();
VerifiqueTAB=false;
break;
} 
} 
} 
//Limitar conteúdo para textarea usar no onkeydown
if (quem.value.length >= tammax + 1) 
alert('Conteúdo maximo de ' + tammax + ' caracteres atingido.');
quem.value = quem.value.substring(0,tammax);
return false;
}

function PararTAB(quem) { VerifiqueTAB=false; } 
//function ChecarTAB() { VerifiqueTAB=true; } canselei e adicionei ( VerifiqueTAB=true; ) em todas as funções onKeyPress
//EX: <input type="text" name="conta2" maxlength="6" size="6" onKeyPress="ChecarTAB();" onKeyUp="Mostra(this, 6)" onFocus="PararTAB(this);">
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Campo aceita somente numeros, ponto e virgula
//Parametros	: Ultilizar no Onkeypress
//Por			: Marcelo Costa
function Numeros(event){ VerifiqueTAB=true; 
var nav4 = window.Event ? true : false; 
if(window.event) { // Internet Explorer
nav4 = event.keyCode;
}
else { // Firefox
nav4 = event.which;
}
if(nav4 >= 48 && nav4 <= 57 || nav4 == 8 || nav4 == 46 || nav4 == 09 || nav4 == 44) return true;
return false;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Campo aceita somente numeros inteiros
//Parametros	: Ultilizar no Onkeypress
//Por			: Marcelo Costa
function Inteiros(event){ VerifiqueTAB=true; 
var nav4 = window.Event ? true : false; 
if(window.event) { // Internet Explorer
nav4 = event.keyCode;
}
else { // Firefox
nav4 = event.which;
}
if(nav4 >= 48 && nav4 <= 57 || nav4 == 09 || nav4 == 8) return true;
return false;
} 
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Campo aceita somente letras
//Parametros	: Ultilizar no Onkeypress
//Por			: Marcelo Costa
function Letras(event){
var nav4 = window.Event ? true : false; 
if(window.event) { // Internet Explorer
nav4 = event.keyCode;
}
else { // Firefox
nav4 = event.which;
}
if(nav4 >= 65 && nav4 <= 90 || nav4 >= 97 && nav4 <= 122 || nav4 == 8 || nav4 == 20 || nav4 == 0 || nav4 == 231 || nav4 == 199 || nav4 == 09 || nav4 == 32) return true;
return false;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Campo aceita somente letras e numeros
//Parametros	: Ultilizar no Onkeypress
//Por			: Marcelo Costa
function LetrasNumeros(event){ VerifiqueTAB=true; 
var nav4 = window.Event ? true : false; 
if(window.event) { // Internet Explorer
nav4 = event.keyCode;
}
else { // Firefox
nav4 = event.which;
}
if(nav4 >= 65 && nav4 <= 90 || nav4 >= 97 && nav4 <= 122 || nav4 >= 48 && nav4 <= 57 || nav4 == 8 || nav4 == 20 || nav4 == 0 || nav4 == 231 || nav4 == 199 || nav4 == 46 || nav4 == 44 || nav4 == 45 || nav4 == 95 || nav4 >= 48 || nav4 == 09 || nav4 == 32) return true;
return false;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Campo aceita somente letras minusculas
//Parametros	: Ultilizar no Onkeypress
//Por			: Marcelo Costa
function LetrasMinusculas(event){ VerifiqueTAB=true; 
var nav4 = window.Event ? true : false; 
if(window.event) { // Internet Explorer
nav4 = event.keyCode;
}
else { // Firefox
nav4 = event.which;
}
if(nav4 >= 97 && nav4 <= 122 || nav4 == 8 || nav4 == 20 || nav4 == 0 || nav4 == 231 || nav4 == 46 || nav4 == 44) return true;
return false;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Campo aceita somente letras maiusculas
//Parametros	: Ultilizar no Onkeypress
//Por			: Marcelo Costa
function LetrasMaiusculas(event){ VerifiqueTAB=true; 
var nav4 = window.Event ? true : false; 
if(window.event) { // Internet Explorer
nav4 = event.keyCode;
}
else { // Firefox
nav4 = event.which;
}
if(nav4 >= 65 && nav4 <= 90 || nav4 == 8 || nav4 == 20 || nav4 == 0 || nav4 == 231 || nav4 == 46 || nav4 == 44 || nav4 == 199) return true;
return false;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Campo só não aceita Enter
//Parametros	: Ultilizar no Onkeypress
//Por			: Marcelo Costa
function BlockEnter(event){ VerifiqueTAB=true; 
var nav4 = window.Event ? true : false; 
if(window.event) { // Internet Explorer
nav4 = event.keyCode;
}
else { // Firefox
nav4 = event.which;
}
if(nav4 == 13)return false;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Limitar conteúdo para textarea usar no onkeydown ( não uso mais coloquei essa função na função Muda Foco
//Parametros	: Ultilizar no Onkeypress
//Por			: Marcelo Costa
function LimitaConteudo(pcampo, tamax) {
if (pcampo.value.length >= tamax) 
alert('Conteúdo maximo de ' + tamax + ' caracteres atingido.');
pcampo.value = pcampo.value.substring(0,tamax - 1);
return false;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Formatar numeros em moeda
//Parametros	: -
//Por			: Marcelo Costa
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;
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Formatar numeros em moeda usando o evento onKeyDown
//Parametros	: -
//Por			: Marcelo Costa
//onKeydown="FormataValor(this,10,event)"
function FormataValor(campo,tammax,teclapres) {
var tecla = teclapres.keyCode;
vr = Limpar(campo.value,"0123456789");
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 ){ 
campo.value = vr ; }
if ( (tam > 2) && (tam <= 5) ){
campo.value = "R$" + vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - 2, tam ) ; }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = "R$" + vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ; }
if ( (tam >= 9) && (tam <= 11) ){
campo.value = "R$" + 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) ){
campo.value = "R$" + 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) ){
campo.value = "R$" + 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 ) ;}
} 
}
//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Validar se foi digitado apenas conteúdo permetido
//Parametro		: pCampo = campo a ser validado
//Por			: Marcelo Costa
function Aspas1(pCampo)
{
	var pExp = /['"]/; //conteúdo não permetido
	var pDados = '';
	
	//Transformando em Maiuscula
	pDados = pCampo.value.toUpperCase();
	
	//Verificando se possui alguma letra dentro do campo
	if (pDados.search(pExp)!= -1)
	{
		alert('Este campo não aceita "Aspas".');
		pCampo.value = '';
		pCampo.focus();
		return false;
	}
	
	return true;
}

//-----------------------------------------------------------------------------------------------------------------
//Objetivo		: Validar se foi digitado apenas conteúdo permetido caso o usuario cole o conteudo proibido no campo só valida no submit
//Parametro		: pCampo = campo a ser validado
//Por			: Marcelo Costa
function Aspas(pCampo)
{
	var pExp = /['"]/; //conteúdo não permetido
	var pDados = '';
	
	//Transformando em Maiuscula
	pDados = pCampo.value.toUpperCase();
	
	//Verificando se possui alguma letra dentro do campo
	if (pDados.search(pExp)!= -1)
	{
		alert('Este campo não aceita "Aspas".');
		//pCampo.value = '';
		pCampo.value = pCampo.value.substr(1,(pCampo.value.length - 1));
		//pCampo = pCampo.replace('"','')
		pCampo.focus();
		return false;
	}
	
	return true;
}

function retirarAcento(objResp) {
var varString = new String(objResp.value);
var stringAcentos = new String('àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ');
var stringSemAcento = new String('aaeouaoaeioucuAAEOUAOAEIOUCU');

var i = new Number();
var j = new Number();
var cString = new String();
var varRes = '';

for (i = 0; i < varString.length; i++) {
cString = varString.substring(i, i + 1);
for (j = 0; j < stringAcentos.length; j++) {
if (stringAcentos.substring(j, j + 1) == cString){
cString = stringSemAcento.substring(j, j + 1);
}
}
varRes += cString;
}
objResp.value = varRes;
}

function retirarAspas(objResp) { // ultilizar no onchance ou melhor no onkeyup
var varString = new String(objResp.value);
var stringAcentos = new String(/'"/);
var stringSemAcento = new String('');

var i = new Number();
var j = new Number();
var cString = new String();
var varRes = '';

for (i = 0; i < varString.length; i++) {
cString = varString.substring(i, i + 1);
for (j = 0; j < stringAcentos.length; j++) {
if (stringAcentos.substring(j, j + 1) == cString){
cString = stringSemAcento.substring(j, j + 1);
}
}
varRes += cString;
}
objResp.value = varRes;
}