// *****************************************************************************
// validaciones.js
// ---------------
//
// Contiene todas las funciones de validacion de campos modularizadas
// para chequeo de consistencia en los datos.
//
// J.SOSA, Certant S.A.
// *****************************************************************************

// validarRRHH: valida en el caso de que el area sea recursos humanos
// devuelve true o false

function validarRRHH(frm)
{
	// analizo los campos obligatorios
	if(frm.asuntoRRHH.value == "-1")
	{
		alert("Atención: Debe seleccionar un asunto\ndesde la lista desplegable.");
		return false;
	}
	if(frm.nombre.value == "")
	{
		alert("Atención: Debe ingresar su nombre y apellido.");
		return false;
	}
	else
	{
		// valido que el nombre sea valido
		str = String(frm.nombre.value);
		posibles = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		if(!composicion(str,posibles))
		{
			alert("Atención: El nombre / apellido contiene caracteres inválidos.");
			return false;
		}
	}
	if(frm.dia.value == "" || frm.mes.value == "" || frm.anio.value == "")
	{
		alert("Debe ingresar fecha de nacimiento");
		return false;
	}
	// analizo dia, mes y anio
	dd = Number(frm.dia.value);
	mm = Number(frm.mes.value);
	yy = Number(frm.anio.value);
	msg = "";
	if(yy < 1900 || yy > 2000)
		msg += "Año\n";
	if(mm < 1 || mm > 12)
		msg += "Mes\n";
	if(dd < 1 || ((mm == 2 && dd > 29)
		|| ((mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) && dd > 31)
		|| ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30)))
		msg += "Día\n";
	if(msg != "")
	{
		alert("Atención: Los siguientes valores son incorrectos:\n" + msg);
		return false;
	}

	// validaciones para los demas campos

	if(!validarTel(frm.tel.value))
	{
		alert("Atención: El dato ingresado para teléfono es incorrecto.");
		return false;
	}

	if(!validarMail(frm.mail.value))
	{
		alert("Atención: El dato ingresado para e-Mail es incorrecto.");
		return false;
	}

	if(!validarDire(frm.dire.value))
	{
		alert("Atención: El dato ingresado para dirección es incorrecto.");
		return false;
	}

	if(!validarAcad(frm.acad.value))
	{
		alert("Atención: El dato ingresado para Formación Académica es incorrecto.");
		return false;
	}

	if(!validarExper(frm.exper.value))
	{
		alert("Atención: El dato ingresado para Experiencia Laboral es incorrecto.");
		return false;
	}

	// los datos fueron bien validados
	return true;
}


function validarOtros(frm)
{
	if(!validarAsunto(frm.asuntoOtros.value))
	{
		alert("Atención: El dato ingresado para el Asunto es incorrecto.");
		return false;
	}
	if(frm.nombre.value == "")
	{
		alert("Atención: Debe ingresar su nombre y apellido.");
		return false;
	}
	else
	{
		// valido que el nombre sea valido
		str = String(frm.nombre.value);
		posibles = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
		if(!composicion(str,posibles))
		{
			alert("Atención: El nombre / apellido contiene caracteres inválidos.");
			return false;
		}
	}

	if(!validarTel(frm.tel.value))
	{
		alert("Atención: El dato ingresado para teléfono es incorrecto.");
		return false;
	}

	if(!validarMail(frm.mail.value))
	{
		alert("Atención: El dato ingresado para e-Mail es incorrecto.");
		return false;
	}

	if(!validarComentario(frm.comentarios.value))
	{
		alert("Atención: El dato ingresado para comentarios es incorrecto.");
		return false;
	}

	return true;

}

// funciones auxiliares para validar cada ingreso

function validarAsunto(asunt)
{
	if(String(asunt).length)
	{
		str = String(asunt);
		posibles = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-. ";
		if(composicion(str,posibles))
			return true;
		else
			return false;
	}
	else
		return true;
}

function validarTel(tel)
{
	if(String(tel).length != 0 && !esTel(tel))
		return false;
	else
		return true;
}

function validarMail(mail)
{
	str = String(mail);
	posibles = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@_.";
	alphaIng = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	// me fijo que tenga caracteres validos
	if(str.length == 0)
		return true;
	if(composicion(str,posibles))
	{
		// que la primer y ultima letra sean alphas
		if(pertenece(str.charAt(0),alphaIng) && pertenece(str.charAt(str.length - 1),alphaIng))
		{
			// una y solo una '@'
			var pos = 0;
			var hayMas = true;
			var cant = 0;
			while(hayMas)
			{
				pos = str.indexOf("@",pos + 1);
				if(pos == -1)
					hayMas = false;
				else
					cant++;
			}
			if(cant == 1)
			{
				// por lo menos un '.' al final de la arroba
				posArr = str.indexOf("@");
				posPto = str.indexOf(".",posArr);
				if(posPto != -1)
					return true;
			}
		}
	}
	return false;
}

function validarDire(dir)
{
	if(String(dir).length != 0 && !esProsa(dir))
		return false;
	else
		return true;
}

function validarAcad(ac)
{
	if(String(ac).length != 0 && !esProsa(ac))
		return false;
	else
		return true;
}

function validarExper(ex)
{
	if(String(ex).length != 0 && !esProsa(ex))
		return false;
	else
		return true;
}

function validarComentario(com)
{
	if(String(com).length != 0 && !esProsa(com))
		return false;
	else
		return true;
}






function pertenece(caracter,simbolos)
{
	i = 0;
	esta = false;
	while(!esta && i < String(simbolos).length)
	{
		if(caracter == simbolos.charAt(i))
			esta = true;
		else
			i++;
	}
	return esta;
}


function composicion(str,posibles)
{
	var i = 0;
	var esta = true;
	while(esta && i < str.length)
	{
		if(!pertenece(str.charAt(i),posibles))
			esta = false;
		else
			i++;
	}
	if(i == 0)
		esta = false;	// 0 length strings
	return esta;
}

function esAlpha(txt)
{
	str = String(txt);
	posibles = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz";
	
	return composicion(str,posibles);
}

function esAlphaNum(txt)
{
	str = String(txt);
	posibles = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz0123456789";
	
	return composicion(str,posibles);
}

function esProsa(txt)
{
	str = String(txt);
	posibles = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz0123456789 ,;.:-_'\"?¿¡!@";
	
	return composicion(str,posibles);
}

function esNum(txt)
{
	str = String(txt);
	posibles = "0123456789";
	
	return composicion(str,posibles);
}

function esTel(txt)
{
	str = String(txt);
	posibles = "0123456789+-()";
	
	return composicion(str,posibles);
}

