/**************************************
 Valida Formulário
***************************************/
function validaForm(formName)
{
	var form = document.getElementById(formName);
	
	for( i=0; i<form.length; i++ )
	{
		if( form[i].attributes["validacao"] != undefined )
		{
			if( (form[i].type == 'text') || (form[i].type == 'textarea') || (form[i].type == 'password') )
			{
				if( form[i].attributes["validacao"].value.indexOf('requerido') != -1 )
				{
					if( form[i].value == "" )
					{
						alert( 'Preencha corretamente o campo '+ form[i].attributes["nomecampo"].value );
						form[i].focus();
						return false;
					}
				}
				
				// Campo de E-mail
				if( form[i].attributes["validacao"].value.indexOf('email') != -1 )
				{
					var reTipo = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
					
					if (!reTipo.test(form[i].value)) {
						alert( 'Preencha corretamente o campo '+ form[i].attributes["nomecampo"].value );
						form[i].focus();
						return false;
					}
				}	
			}
			
			else if(form[i].type == 'file')
			{
				// Preenchimento Obrigatório
				if( form[i].attributes["validacao"].value.indexOf('requerido') != -1 )
				{
					if( form[i].value == "" )
					{
						alert( 'Selecione o arquivo no campo '+ form[i].attributes["nomecampo"].value );
						form[i].focus();
						return false;
					}
				}
			}
			
			else if( (form[i].type == 'select-one') || (form[i].type == 'select-multiple') )
			{
				if(form[i].attributes["validacao"].value == 'requerido')
				{
					if(form[i].selectedIndex == 0)
					{
						alert( 'Selecione uma opção no campo ' + form[i].attributes["nomecampo"].value);
						form[i].focus();
						return false;
					}
				}
			}
			
			else if(form[i].type == 'checkbox')
			{
				if(form[i].attributes["validacao"].value == 'requerido')
				{
					sel = false;
					chk = document.forms[formName][form[i].name];
					
					for (x=0; x<chk.length; x++)
					{
						if( chk[x].checked )
						{
							sel = true;
							break;
						}
					}
					
					if( sel == false )
					{
						alert('Selecione uma opção no campo ' + form[i].attributes["nomecampo"].value);
						return false;
					}
				}
			}
			
			else if(form[i].type == 'radio')
			{
				if(form[i].attributes["validacao"].value == 'requerido')
				{
					sel = false;
					elRadio = document.forms[formName][form[i].name];
					
					for (x=0; x<elRadio.length; x++)
					{
						if( elRadio[x].checked )
						{
							sel = true;
							break;
						}
					}
					
					if( sel == false )
					{
						alert('Selecione a opção no campo ' + form[i].attributes["nomecampo"].value);
						return false;
					}
				}
			}
		}
	}
}

/***********************************************************
 Funções - Evento OnKeyPress
***********************************************************/
// Formata campo com a máscara passada
function mask(src, formato)
{
	var i = src.value.length; 
	var saida = formato.substring(0,1); 
	var texto = formato.substring(i);
	
	if (texto.substring(0,1) != saida){ 
		src.value += texto.substring(0,1); 
	} 
}

// Valida entrada de caracteres especificados
function checkChars(src, chars, e)
{
	var keyNum = (window.event) ? event.keyCode : e.which;
	caracter = String.fromCharCode(keyNum);

	if( chars.indexOf(caracter) < 0 )
	{
		return false;
	}
}

// Valida entrada apenas de números
function checkNumber(e)
{
	var tecla = (window.event) ? event.keyCode : e.which;
	if ((tecla > 47 && tecla < 58)) {
        return true;
    }
    else {
        if (tecla == 8 || tecla == 0) {
            return true;
        }
        else { return false; }
    }
}


/***********************************************************
 Funções - Evento onBlur
***********************************************************/
// Valida data
function checkDate(src)
{
	var data = src.value;
	if(data != '')
	{
		var reDate = /^((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}$/;
		
		if( !reDate.test(data) )
		{
			alert('Digite uma data válida.');
			src.value = "";
			src.focus();
			return false;
		}
	}
}

// Valida hora
function checkTime(src)
{
	var hora = src.value;
	
	if(hora != '')
	{
		arrayHora = hora.split(":");
		horas = arrayHora[0];
		minutos = arrayHora[1];
		
		if( (horas == 24) && (minutos > 0) )
		{
			alert("Hora inválida, verifique");
			src.focus();
			return false;
		}
		
		if(horas > 24)
		{
			alert("Hora inválida, verifique");
			src.focus();
			return false;
		}
		
		if(minutos > 59)
		{
			alert("Minutos inválidos, verifique");
			src.focus();
			return false;
		}
	}
}
