/******************************************************************
Validación de formularios
Archivo: validacionFormulariosNok.js
Programado por: Nokrosis
Versión: 0.1
Fecha: 23/10/2007
*******************************************************************
USO:
<head>
<script type="text/javascript" src="validacionFormulariosNok.js"></script>
<script type="text/javascript">
function validar()
{
	if(!esMail('mail'))
		return false;
	if(estaVacio('textoNormal'))
		return false;
	
	return true;
}
</script>
</head>



<form method="" action="" onSubmit="javascript: return validar();">
Mail: <input type="text" id="mail">
Campo de texto normal: <input type="text" id="textoNormal">
<input type="submit">
</form>

*******************************************************************/
function getFormValue(tipo,id)
{
	var valor;
	switch(tipo)
	{
		case "input":
			valor=document.getElementById(id).value;
		break;
		case "select":
			valor=document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
		break
	}
	
	return valor;
}
function estaVacio(id)
{
	if(document.getElementById(id).value.length==0)
	{
		alert("El campo está vacío");
		document.getElementById(id).focus();
		return true;
	}
	else
		return false;
}
function estaSeleccionado(id)
{
	if(getFormValue('select',id).length==0)
	{
		alert("No se ha seleccionado una opcion");
		document.getElementById(id).focus();
		return false;
	}
	else
		return true;
}
function esMail(id)
{
	var mail = document.getElementById(id).value;
	var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (mail.length == 0 ) 
	{
		alert("El campo está vacío");
		document.getElementById(id).focus();
		return false;
	}
	if (filter.test(mail))
		return true;
		
	alert("El texto del campo no es un correo electrónico válido");
	document.getElementById(id).focus();
	return false;
}