function validaCampo(elemento,tipo){
	argumentos = arguments
	tipos = {	'an8': validaAn8,
						'data': validaData,
						'nome': validaNome,
						'login': validaLogin,
						'senha': validaSenha,
						'email': validaEmail,
						'rg': validaRg,
						'cpf': validaCpf,
						'cep': validaCep,
						'telefone': validaTelefone,
						'inteiro': validaInteiro,
						'matricula': validaMatricula,
						'cnpj': validaCnpj,
						'moeda': validaMoeda,
						'hora': validaHora
					}
	function validaAn8(elemento){
		er = /^[0-9]{6}$/
		if(!er.test(elemento.value)){
			elemento.value = ''
			alert('Código AN8 inválido.')
		}		
	}
	function validaData(){
		function verificaData(strData){
			strData = strData.replace(/\-/g,'/')
			function eData(dia,mes,ano){
				var dia = parseFloat(dia)
				var mes = parseFloat(mes)-1
				var ano = parseFloat(ano)
				var data = new Date(ano, mes, dia, 12, 0, 0)
				var d = data.getDate()
				var m = data.getMonth()
				var a = data.getFullYear()
				return (d==dia && m==mes && a==ano)
			}
			padrao = /([0-9]|\/)+/		
			if(!padrao.test(strData)) return false
			hoje = new Date()
			mesHoje = hoje.getMonth() + 1
			anoHoje = hoje.getFullYear()
			strData = strData.split('/')
			switch(strData.length){
				case 1:
					switch(strData[0].length){
						case 1:
							dia = strData[0]
							mes = mesHoje
							ano = anoHoje
							break
						case 2:
							dia = strData[0]
							mes = mesHoje
							ano = anoHoje
							break
						case 4:
							dia = strData[0].substr(0,2)
							mes = strData[0].substr(2,2)
							ano = anoHoje
							break
						case 6:
							dia = strData[0].substr(0,2)
							mes = strData[0].substr(2,2)
							ano = strData[0].substr(4,2)
							break
						case 8:
							dia = strData[0].substr(0,2)
							mes = strData[0].substr(2,2)
							ano = strData[0].substr(4,4)	
							break
						default:
							return false						
					}
					break
				case 2:
					dia = strData[0]
					mes = strData[1]
					ano = anoHoje
					break
				case 3:
					dia = strData[0]
					mes = strData[1]
					ano = strData[2]
					break
				default:
					return false			
			}
			ano = parseFloat(ano)
			if(ano<=30) ano+=2000
			else if(ano>30 && ano<=99) ano+=1900		
			if(eData(dia,mes,ano)){
				mes+=''
				dia = ('00' + dia).substr(dia.length,2)
				mes = ('00' + mes).substr(mes.length,2)
				return dia + '/' + mes + '/' + ano
			}else return false		
		}
		if(d = verificaData(elemento.value)) elemento.value = d
		else{
			elemento.value = ''			
			alert('Data inválida.')
		}
	}
	function validaNome(elemento){
		mensagem = 'Nome inválido.'
		er = /.*/
		if(er.test(elemento.value)) elemento.value = elemento.value
		else{
			elemento.value = ''			
			alert(mensagem)
		}
	}
	function validaLogin(elemento){
		er = /^[a-zA-Z0-9]+$/
		if(!er.test(elemento.value)){
			elemento.value = ''			
			alert('Login inválido.')
		}
	}
	function validaSenha(elemento){
		er = /^[a-zA-Z0-9]+$/
		if(!er.test(elemento.value)){
			elemento.value = ''			
			alert('Senha inválida.')
		}
	}
	function validaEmail(elemento){
		er = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9_.]+$/
		if(er.test(elemento.value)) elemento.value = elemento.value.toLowerCase()
		else{
			elemento.value = ''
			alert('Formato de e-mail inválido.')
		}
	}
	function validaRg(elemento){
		er = /^[0-9]{2}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9a-zA-Z]$/
		if(er.test(elemento.value)){
			valor = elemento.value.replace(/\./g,'').replace(/\-/g,'')
			elemento.value = valor.substr(0,2)+'.'+valor.substr(2,3)+'.'+valor.substr(5,3)+'-'+valor.substr(8).toUpperCase()
		}else{
			elemento.value = ''
			alert('Formato de RG inválido.')
		}
	}
	function validaCpf(elemento){
		er = /^[0-9]{3}\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]{2}$/
		if(er.test(elemento.value)){
			valor = elemento.value.replace(/\./g,'').replace(/\-/g,'')
			elemento.value = valor.substr(0,3)+'.'+valor.substr(3,3)+'.'+valor.substr(6,3)+'-'+valor.substr(9)
		}else{
			elemento.value = ''
			alert('Formato de CPF inválido.')
		}
		
	}
	function validaCep(elemento){
		er = /^[0-9]{5}\-?[0-9]{3}$/;
		if(er.test(elemento.value)){
			valor = elemento.value.replace('-','');
			elemento.value = valor.substr(0,5)+'-'+valor.substr(5);
		}else{
			elemento.value = '';
			alert('Formato de CEP inválido.');
		}
	}
	function validaTelefone(elemento){
		dddPadrao = (elemento.value.indexOf('?')!=-1?'??':null) || argumentos[2]
		er = /^((\(([0-9]{2}|\?{2})\))|(([0-9]{2}|\?{2})))?(([0-9]{3,4}\-?[0-9]{4})|([0-9]{4}\-?[0-9]{3}))$/
		if(er.test(elemento.value)){
			valor = elemento.value.replace('(','').replace(')','').replace('-','').replace(/\?\?/g,'')			
			switch(valor.length){
				case 7:
					elemento.value = '('+(dddPadrao?dddPadrao:'??')+')'+valor.substr(0,3)+'-'+valor.substr(3)
					break
				case 8:
					elemento.value = '('+(dddPadrao?dddPadrao:'??')+')'+valor.substr(0,4)+'-'+valor.substr(4)
					break
				case 9:
					elemento.value = '('+valor.substr(0,2)+')'+valor.substr(2,3)+'-'+valor.substr(5)
					break
				case 10:
					elemento.value = '('+valor.substr(0,2)+')'+valor.substr(2,4)+'-'+valor.substr(6)
				case 12:
					elemento.value = '('+valor.substr(0,2)+')'+valor.substr(2,3)+'-'+valor.substr(6)
				case 13:
					elemento.value='('+valor.substr(0,2)+')'+valor.substr(2,4)+'-'+valor.substr(6)
			}
		}else{
			elemento.value = ''			
			alert('Formato de telefone inválido.')
		}
	}
	function validaInteiro(elemento){
		er = /^[0-9]+$/
		if(!er.test(elemento.value)){
			elemento.value = ''			
			alert('Número inteiro inválido.')
		}
	}
	function validaMatricula(elemento){
		er = /^[0-9]\.?[0-9]{3}\.?[0-9]{3}\-?[0-9]$/
		if(er.test(elemento.value)){
			valor = elemento.value.replace(/\./g,'').replace(/\-/g,'')
			elemento.value = valor.substr(0,1)+'.'+valor.substr(1,3)+'.'+valor.substr(4,3)+'-'+valor.substr(7)
		}else{
			elemento.value = ''
			alert('Formato de matrícula inválido.')
		}
	}
	function validaCnpj(elemento){
		er = /^[0-9]{2}\.?[0-9]{3}\.?[0-9]{3}\/?[0-9]{4}\-?[0-9]{2}$/
		if(er.test(elemento.value)){
			valor = elemento.value.replace(/\./g,'').replace(/\//g,'').replace(/\-/g,'')
			elemento.value = valor.substr(0,2)+'.'+valor.substr(2,3)+'.'+valor.substr(5,3)+'/'+valor.substr(8,4)+'-'+valor.substr(12)
		}else{
			elemento.value = ''
			alert('Formato de CNPJ inválido.')
		}
	}
	function validaMoeda(elemento){
		er = /^[0-9]+([,.][0-9]+)?$/
		if(er.test(elemento.value)) elemento.value = parseFloat(elemento.value.replace(',','.')).toFixed(2).replace('.',',')
		else{
			elemento.value = ''
			alert('Formato de moeda inválido.')
		}
	}
	function validaHora(elemento){
		partes = new Array()
		er = /^([0-9]{1,2}(:[0-9]{1,2})?)||([0-9]{4})$/
		if(er.test(elemento.value)){
			if(elemento.value.length==4 && elemento.value.indexOf(':')==-1){
				partes[0] = elemento.value.substr(0,2)
				partes[1] = elemento.value.substr(2,2)
			}else partes = elemento.value.split(':')
			if(partes.length==1){
				horas = parseFloat(partes[0])
				minutos = 0
			}else{
				horas = parseFloat(partes[0])
				minutos = parseFloat(partes[1])
			}
			if(horas>23 || minutos>59){
				elemento.value = ''
				alert('Formato de horário inválido.')
			}else{
				horas = horas>9?horas:'0'+horas
				minutos = minutos>9?minutos:'0'+minutos
				elemento.value = horas+':'+minutos
			}
		}else{
			elemento.value = ''
			alert('Formato de horário inválido.')
		}
	}
	if(tipos[tipo]){
		if(elemento.value) tipos[tipo](elemento)
	}else alert("Tipo '"+tipo+"' inexistente.")
}
function obrigatorios(){
	var aba
	if(arguments.length%2!=0) aba = arguments[arguments.length-1]
	var n = aba?arguments.length-1:arguments.length
	for(var i=0;i<n;i+=2){
		if(arguments[i].nome && arguments[i].nome=='InputCombo'){
			if(!arguments[i].hiden.value){
				if(aba) aba.foca(arguments[i].caixa)
				else arguments[i].caixa.focus()
				alert(arguments[i+1])
				return false				
			}
		}else{
			if(!arguments[i] || !arguments[i].value){
				if(aba && arguments[i]) aba.foca(arguments[i])
				else arguments[i].focus()				
				//if(arguments[i]) arguments[i].focus() //Acho que pode remover esta linha. Não testado.
				alert(arguments[i+1])
				return false
			}
		}
	}
	return true
}
function adicionaLinha(objeto,nome){
	tabela = objeto.parentNode.parentNode.parentNode.parentNode
	l = tabela.insertRow(-1)
	for(i=0;i<tabela.rows[0].cells.length-1;i++){
		c = l.insertCell(-1)
		c.innerHTML = tabela.rows[1].cells[i].innerHTML
		elemento =	tabela.rows[1].cells[i].getElementsByTagName('input')[0] || tabela.rows[1].cells[i].getElementsByTagName('select')[0]
		if(elemento.tagName=='INPUT'){
			if(elemento.type=='text'){
				c.getElementsByTagName('input')[0].value = elemento.value
				elemento.value = ''
			}else{
				c.getElementsByTagName('input')[0].checked = elemento.checked
				elemento.checked = false
			}
		}else{
			c.getElementsByTagName('select')[0].value = elemento.value
			elemento.selectedIndex = 0
		}
	}

	c = l.insertCell(-1)
	c.className = 'acao'
	c.innerHTML = '<a class="remover" title="Remover" onClick="removeLinha(this)"></a>'
	if(nome){
		hiden = document.createElement('input')
		hiden.type = 'hidden'
		hiden.name = nome
		hiden.value = ''
		c.appendChild(hiden)
	}	
	primeiroElemento = tabela.rows[1].cells[0].getElementsByTagName('input')[0] || tabela.rows[1].cells[0].getElementsByTagName('select')[0]
	primeiroElemento.focus()
}
function removeLinha(objeto,nome,codigo){
	tabela = objeto.parentNode.parentNode.parentNode.parentNode
	if(nome){
		p = objeto		
		while(p = p.parentNode){
			if(p.tagName=='FORM'){
				hiden = document.createElement('input')
				hiden.type = 'hidden'
				hiden.name = nome
				hiden.value = codigo
				p.appendChild(hiden)
				break
			}
		}
	}
	tabela.deleteRow(objeto.parentNode.parentNode.rowIndex)
}
function mostraLinha(linha){
	linha.style.display = navigator.appName=='Netscape'?'table-row':'block'
}
function filtro(acao,formulario){
	formulario = formulario || document.forms[0]
	if(acao=='fecha'){
		formulario.reset()
		formulario.style.display = 'none'
	}else formulario.style.display = 'block'
}

function txtBoxFormat(objeto, sMask, evtKeyPress,tipo) 
{
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

	if(document.all) 
	{ // Internet Explorer
	    nTecla = evtKeyPress.keyCode;
	}
	else 
		if(document.layers) 
		{ // Nestcape
			nTecla = evtKeyPress.which;
		}
		else 
		{
			nTecla = evtKeyPress.which;
			if (nTecla == 8) 
			{
				return true;
			}
			if(nTecla == 0)
			{
				validaCampo(objeto,tipo);
				objeto.blur();				
			}
		}

    sValue = objeto.value;

    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ",", "" );
    sValue = sValue.toString().replace( ",", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( "/", "" );
    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) == ",") || (sMask.charAt(i) == ":"))
    	bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " ") || (sMask.charAt(i) == "/"))
		if (bolMask) 
		{
			sCod += sMask.charAt(i);
			mskLen++; 
		}
		else 
		{
			sCod += sValue.charAt(nCount);
			nCount++;
		}
    	i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
    	if (sMask.charAt(i-1) == "9") { // apenas números...
    		return ((nTecla > 47) && (nTecla < 58)); } 
    	else { // qualquer caracter...
    		return true;
    	} 
    }
    else {
    	return true;
    }
}
